Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 15 additions & 21 deletions gengapic/client_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/googleapis/gapic-generator-go/internal/errors"
"github.com/googleapis/gapic-generator-go/internal/pbinfo"
"google.golang.org/genproto/googleapis/api/annotations"
)

Expand All @@ -28,22 +29,15 @@ func (g *generator) clientOptions(serv *descriptor.ServiceDescriptorProto, servN

// CallOptions struct
{
var maxNameLen int
for _, m := range serv.Method {
if l := len(*m.Name); maxNameLen < l {
maxNameLen = l
}
}

p("// %[1]sCallOptions contains the retry settings for each method of %[1]sClient.", servName)
p("type %sCallOptions struct {", servName)
for _, m := range serv.Method {
p("%s%s[]gax.CallOption", *m.Name, spaces(maxNameLen-len(*m.Name)+1))
p("%s []gax.CallOption", *m.Name)
}
p("}")
p("")

g.imports[importSpec{"gax", "github.com/googleapis/gax-go"}] = true
g.imports[pbinfo.ImportSpec{"gax", "github.com/googleapis/gax-go"}] = true
}

// defaultClientOptions
Expand All @@ -61,7 +55,7 @@ func (g *generator) clientOptions(serv *descriptor.ServiceDescriptorProto, servN
p("}")
p("")

g.imports[importSpec{path: "google.golang.org/api/option"}] = true
g.imports[pbinfo.ImportSpec{Path: "google.golang.org/api/option"}] = true
}

// defaultCallOptions
Expand Down Expand Up @@ -98,8 +92,8 @@ func (g *generator) clientOptions(serv *descriptor.ServiceDescriptorProto, servN
p("}")
p("")

g.imports[importSpec{path: "time"}] = true
g.imports[importSpec{path: "google.golang.org/grpc/codes"}] = true
g.imports[pbinfo.ImportSpec{Path: "time"}] = true
g.imports[pbinfo.ImportSpec{Path: "google.golang.org/grpc/codes"}] = true
}

p(" return &%sCallOptions{", servName)
Expand All @@ -125,7 +119,7 @@ func (g *generator) clientInit(serv *descriptor.ServiceDescriptorProto, servName
}
}

imp, err := g.importSpec(serv)
imp, err := g.descInfo.ImportSpec(serv)
if err != nil {
return err
}
Expand All @@ -142,7 +136,7 @@ func (g *generator) clientInit(serv *descriptor.ServiceDescriptorProto, servName
p("")

p("// The gRPC API client.")
p("%s %s.%sClient", grpcClientField(servName), imp.name, serv.GetName())
p("%s %s.%sClient", grpcClientField(servName), imp.Name, serv.GetName())
p("")

if hasLRO {
Expand All @@ -152,7 +146,7 @@ func (g *generator) clientInit(serv *descriptor.ServiceDescriptorProto, servName
p("LROClient *lroauto.OperationsClient")
p("")

g.imports[importSpec{name: "lroauto", path: "cloud.google.com/go/longrunning/autogen"}] = true
g.imports[pbinfo.ImportSpec{Name: "lroauto", Path: "cloud.google.com/go/longrunning/autogen"}] = true
}

p("// The call options for this service.")
Expand All @@ -164,8 +158,8 @@ func (g *generator) clientInit(serv *descriptor.ServiceDescriptorProto, servName
p("}")
p("")

g.imports[importSpec{path: "google.golang.org/grpc"}] = true
g.imports[importSpec{path: "google.golang.org/grpc/metadata"}] = true
g.imports[pbinfo.ImportSpec{Path: "google.golang.org/grpc"}] = true
g.imports[pbinfo.ImportSpec{Path: "google.golang.org/grpc/metadata"}] = true
}

// Client constructor
Expand All @@ -185,7 +179,7 @@ func (g *generator) clientInit(serv *descriptor.ServiceDescriptorProto, servName
p(" conn: conn,")
p(" CallOptions: default%sCallOptions(),", servName)
p("")
p(" %s: %s.New%sClient(conn),", grpcClientField(servName), imp.name, serv.GetName())
p(" %s: %s.New%sClient(conn),", grpcClientField(servName), imp.Name, serv.GetName())
p(" }")
p(" c.setGoogleClientInfo()")
p("")
Expand All @@ -207,8 +201,8 @@ func (g *generator) clientInit(serv *descriptor.ServiceDescriptorProto, servName
p("}")
p("")

g.imports[importSpec{path: "google.golang.org/api/transport"}] = true
g.imports[importSpec{path: "golang.org/x/net/context"}] = true
g.imports[pbinfo.ImportSpec{Path: "google.golang.org/api/transport"}] = true
g.imports[pbinfo.ImportSpec{Path: "golang.org/x/net/context"}] = true
}

// Connection()
Expand Down Expand Up @@ -242,7 +236,7 @@ func (g *generator) clientInit(serv *descriptor.ServiceDescriptorProto, servName
p("}")
p("")

g.imports[importSpec{path: "cloud.google.com/go/internal/version"}] = true
g.imports[pbinfo.ImportSpec{Path: "cloud.google.com/go/internal/version"}] = true
}
return nil
}
11 changes: 6 additions & 5 deletions gengapic/client_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/google/go-cmp/cmp"
"github.com/googleapis/gapic-generator-go/internal/pbinfo"
"google.golang.org/genproto/googleapis/api/annotations"
)

Expand All @@ -47,7 +48,7 @@ func diff(t *testing.T, name, got, goldenFile string) {

func TestClientOpt(t *testing.T) {
var g generator
g.imports = map[importSpec]bool{}
g.imports = map[pbinfo.ImportSpec]bool{}

serv := &descriptor.ServiceDescriptorProto{
Method: []*descriptor.MethodDescriptorProto{
Expand Down Expand Up @@ -78,14 +79,14 @@ func TestClientOpt(t *testing.T) {
t.Error(err)
continue
}
diff(t, tst.tstName, g.sb.String(), filepath.Join("testdata", tst.tstName+".want"))
diff(t, tst.tstName, g.pt.String(), filepath.Join("testdata", tst.tstName+".want"))
}
}

func TestClientInit(t *testing.T) {
var g generator
g.apiName = "Awesome Foo"
g.imports = map[importSpec]bool{}
g.imports = map[pbinfo.ImportSpec]bool{}

servPlain := &descriptor.ServiceDescriptorProto{
Name: proto.String("Foo"),
Expand All @@ -110,7 +111,7 @@ func TestClientInit(t *testing.T) {
{tstName: "empty_client_init", servName: "", serv: servPlain},
{tstName: "lro_client_init", servName: "Foo", serv: servLRO},
} {
g.parentFile = map[proto.Message]*descriptor.FileDescriptorProto{
g.descInfo.ParentFile = map[proto.Message]*descriptor.FileDescriptorProto{
tst.serv: &descriptor.FileDescriptorProto{
Options: &descriptor.FileOptions{
GoPackage: proto.String("mypackage"),
Expand All @@ -123,6 +124,6 @@ func TestClientInit(t *testing.T) {

g.reset()
g.clientInit(tst.serv, tst.servName)
diff(t, tst.tstName, g.sb.String(), filepath.Join("testdata", tst.tstName+".want"))
diff(t, tst.tstName, g.pt.String(), filepath.Join("testdata", tst.tstName+".want"))
}
}
2 changes: 1 addition & 1 deletion gengapic/doc_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ func TestDocFile(t *testing.T) {
var g generator
g.apiName = "Awesome Foo"
g.genDocFile("path/to/awesome", "awesome", 42, []string{"https://foo.bar.com/auth", "https://zip.zap.com/auth"})
diff(t, "doc_file", g.sb.String(), filepath.Join("testdata", "doc_file.want"))
diff(t, "doc_file", g.pt.String(), filepath.Join("testdata", "doc_file.want"))
}
17 changes: 9 additions & 8 deletions gengapic/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package gengapic
import (
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/googleapis/gapic-generator-go/internal/errors"
"github.com/googleapis/gapic-generator-go/internal/pbinfo"
)

func (g *generator) genExampleFile(serv *descriptor.ServiceDescriptorProto, pkgName string) error {
Expand All @@ -29,7 +30,7 @@ func (g *generator) genExampleFile(serv *descriptor.ServiceDescriptorProto, pkgN
p(" _ = c")
p("}")
p("")
g.imports[importSpec{path: "golang.org/x/net/context"}] = true
g.imports[pbinfo.ImportSpec{Path: "golang.org/x/net/context"}] = true

for _, m := range serv.Method {
if err := g.exampleMethod(pkgName, servName, m); err != nil {
Expand All @@ -52,12 +53,12 @@ func (g *generator) exampleInitClient(pkgName, servName string) {
func (g *generator) exampleMethod(pkgName, servName string, m *descriptor.MethodDescriptorProto) error {
p := g.printf

inType := g.types[m.GetInputType()]
inType := g.descInfo.Type[m.GetInputType()]
if inType == nil {
return errors.E(nil, "cannot find type %q, malformed descriptor?", m.GetInputType())
}

inSpec, err := g.importSpec(inType)
inSpec, err := g.descInfo.ImportSpec(inType)
if err != nil {
return err
}
Expand All @@ -69,7 +70,7 @@ func (g *generator) exampleMethod(pkgName, servName string, m *descriptor.Method

if !m.GetClientStreaming() && !m.GetServerStreaming() {
p("")
p("req := &%s.%s{", inSpec.name, *inType.Name)
p("req := &%s.%s{", inSpec.Name, *inType.Name)
p(" // TODO: Fill request struct fields.")
p("}")
}
Expand Down Expand Up @@ -146,10 +147,10 @@ func (g *generator) examplePagingCall(m *descriptor.MethodDescriptorProto) {
p(" _ = resp")
p("}")

g.imports[importSpec{path: "google.golang.org/api/iterator"}] = true
g.imports[pbinfo.ImportSpec{Path: "google.golang.org/api/iterator"}] = true
}

func (g *generator) exampleBidiCall(m *descriptor.MethodDescriptorProto, inType *descriptor.DescriptorProto, inSpec importSpec) {
func (g *generator) exampleBidiCall(m *descriptor.MethodDescriptorProto, inType *descriptor.DescriptorProto, inSpec pbinfo.ImportSpec) {
p := g.printf

p("stream, err := c.%s(ctx)", m.GetName())
Expand All @@ -158,7 +159,7 @@ func (g *generator) exampleBidiCall(m *descriptor.MethodDescriptorProto, inType
p("}")

p("go func() {")
p(" reqs := []*%s.%s{", inSpec.name, inType.GetName())
p(" reqs := []*%s.%s{", inSpec.Name, inType.GetName())
p(" // TODO: Create requests.")
p(" }")
p(" for _, req := range reqs {")
Expand All @@ -181,5 +182,5 @@ func (g *generator) exampleBidiCall(m *descriptor.MethodDescriptorProto, inType
p(" _ = resp")
p("}")

g.imports[importSpec{path: "io"}] = true
g.imports[pbinfo.ImportSpec{Path: "io"}] = true
}
13 changes: 7 additions & 6 deletions gengapic/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ import (

"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/googleapis/gapic-generator-go/internal/pbinfo"
)

func TestExample(t *testing.T) {
var g generator
g.imports = map[importSpec]bool{}
g.imports = map[pbinfo.ImportSpec]bool{}

inputType := &descriptor.DescriptorProto{
Name: proto.String("InputType"),
Expand Down Expand Up @@ -81,8 +82,8 @@ func TestExample(t *testing.T) {
for _, typ := range []*descriptor.DescriptorProto{
inputType, outputType, pageInputType, pageOutputType,
} {
g.types[".my.pkg."+*typ.Name] = typ
g.parentFile[typ] = file
g.descInfo.Type[".my.pkg."+*typ.Name] = typ
g.descInfo.ParentFile[typ] = file
}

serv := &descriptor.ServiceDescriptorProto{
Expand Down Expand Up @@ -125,7 +126,7 @@ func TestExample(t *testing.T) {
} {
g.reset()
g.genExampleFile(serv, tst.pkgName)
diff(t, tst.tstName, g.sb.String(), filepath.Join("testdata", tst.tstName+".want"))
diff(t, tst.tstName, g.pt.String(), filepath.Join("testdata", tst.tstName+".want"))
}
}

Expand All @@ -137,11 +138,11 @@ func commonTypes(g *generator) {
Name: proto.String("Operation"),
}

g.types = map[string]*descriptor.DescriptorProto{
g.descInfo.Type = map[string]*descriptor.DescriptorProto{
emptyType: empty,
lroType: lro,
}
g.parentFile = map[proto.Message]*descriptor.FileDescriptorProto{
g.descInfo.ParentFile = map[proto.Message]*descriptor.FileDescriptorProto{
empty: &descriptor.FileDescriptorProto{
Options: &descriptor.FileOptions{
GoPackage: proto.String("github.com/golang/protobuf/ptypes/empty"),
Expand Down
Loading