Skip to content

Commit

Permalink
Merge branch 'main' into p1
Browse files Browse the repository at this point in the history
  • Loading branch information
julieqiu authored Aug 28, 2024
2 parents 17a1c14 + 74b6c39 commit 60aa0f3
Show file tree
Hide file tree
Showing 25 changed files with 761 additions and 71 deletions.
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
# Changelog

## [0.46.2](https://github.com/googleapis/gapic-generator-go/compare/v0.46.1...v0.46.2) (2024-08-19)


### Bug Fixes

* Use correct function name ([#1559](https://github.com/googleapis/gapic-generator-go/issues/1559)) ([de2babb](https://github.com/googleapis/gapic-generator-go/commit/de2babb5d6ef50f1325710654e58d519811066f1))

## [0.46.1](https://github.com/googleapis/gapic-generator-go/compare/v0.46.0...v0.46.1) (2024-08-16)


### Bug Fixes

* Add missing cast for proto optionals ([#1557](https://github.com/googleapis/gapic-generator-go/issues/1557)) ([eb6fb9f](https://github.com/googleapis/gapic-generator-go/commit/eb6fb9f7091825f34555fb65ef79035180387540))

## [0.46.0](https://github.com/googleapis/gapic-generator-go/compare/v0.45.0...v0.46.0) (2024-08-16)


### Features

* **gengapic:** Generate helpers for Go 1.23 iterators ([#1542](https://github.com/googleapis/gapic-generator-go/issues/1542)) ([d7ed683](https://github.com/googleapis/gapic-generator-go/commit/d7ed68339f7a542ec55d7cd8120b7eb8fc63fbc8))
* Support wrapper types for autopagination ([#1541](https://github.com/googleapis/gapic-generator-go/issues/1541)) ([d10df59](https://github.com/googleapis/gapic-generator-go/commit/d10df5961ec6e8a79aed1ad2ed60a30d58ec748c))

## [0.45.0](https://github.com/googleapis/gapic-generator-go/compare/v0.44.1...v0.45.0) (2024-07-24)


Expand Down
2 changes: 1 addition & 1 deletion internal/gencli/gencli.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (g *gcli) genCommands() {
})

putImport(cmd.Imports, &pbinfo.ImportSpec{
Path: "github.com/golang/protobuf/jsonpb",
Path: "google.golang.org/protobuf/jsonpb",
})

if !cmd.ClientStreaming {
Expand Down
4 changes: 2 additions & 2 deletions internal/gencli/root_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ import (
"fmt"
"os"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/spf13/cobra"
"google.golang.org/protobuf/jsonpb"
"google.golang.org/protobuf/proto"
)
var Verbose, OutputJSON bool
Expand Down
3 changes: 1 addition & 2 deletions internal/gencli/service_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"testing"

"github.com/googleapis/gapic-generator-go/internal/pbinfo"

"github.com/googleapis/gapic-generator-go/internal/txtdiff"
)

Expand All @@ -37,7 +36,7 @@ func TestServiceFile(t *testing.T) {
root: "Root",
format: true,
imports: map[string]*pbinfo.ImportSpec{
"test": &pbinfo.ImportSpec{Name: "proto", Path: "github.com/golang/protobuf/proto"},
"test": &pbinfo.ImportSpec{Name: "proto", Path: "google.golang.org/protobuf/proto"},
},
subcommands: map[string][]*Command{
name: []*Command{
Expand Down
4 changes: 2 additions & 2 deletions internal/gencli/testdata/root_file.want
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"fmt"
"os"

"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/spf13/cobra"
"google.golang.org/protobuf/jsonpb"
"google.golang.org/protobuf/proto"
)

var Verbose, OutputJSON bool
Expand Down
2 changes: 1 addition & 1 deletion internal/gencli/testdata/service_file.want
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

proto "github.com/golang/protobuf/proto"
proto "google.golang.org/protobuf/proto"
)

var TodoConfig *viper.Viper
Expand Down
15 changes: 15 additions & 0 deletions internal/gengapic/auxiliary.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ func (g *generator) genAuxFile() error {
g.commit(filepath.Join(g.opts.outDir, "auxiliary.go"), g.opts.pkgName)
g.reset()

g.genIteratorsGo123()
g.commitWithBuildTag(filepath.Join(g.opts.outDir, "auxiliary_go123.go"), g.opts.pkgName, "go1.23")
g.reset()

return nil
}

Expand Down Expand Up @@ -123,6 +127,17 @@ func (g *generator) genIterators() error {
return nil
}

// genIteratorsGo123 generates adapters for Go iterators for Go versions 1.23+.
func (g *generator) genIteratorsGo123() {
// Sort iterators to generate by type name to
// avoid spurious regenerations created by
// non-deterministic map traversal order.
iters := sortIteratorMap(g.aux.iters)
for _, iter := range iters {
g.pagingIterGo123(iter)
}
}

// sortIteratorMap sorts the map of iterator types by iterTypeName.
func sortIteratorMap(m map[string]*iterType) []*iterType {
var iters []*iterType
Expand Down
16 changes: 16 additions & 0 deletions internal/gengapic/auxiliary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,22 @@ func TestGenIterators(t *testing.T) {
}

txtdiff.Diff(t, g.pt.String(), filepath.Join("testdata", "gen_iterators.want"))

g.reset()

wantImports = map[pbinfo.ImportSpec]bool{
{Path: "iter"}: true,
{Path: "github.com/googleapis/gax-go/v2/iterator"}: true,
{Name: "examplepb", Path: "cloud.google.com/go/example/apiv1/examplepb"}: true,
}

g.genIteratorsGo123()

if diff := cmp.Diff(g.imports, wantImports); diff != "" {
t.Errorf("imports got(-),want(+):\n%s", diff)
}

txtdiff.Diff(t, g.pt.String(), filepath.Join("testdata", "gen_iterators_go123.want"))
}

func TestSortOperationWrapperMap(t *testing.T) {
Expand Down
80 changes: 80 additions & 0 deletions internal/gengapic/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,62 @@ func (g *generator) genExampleFile(serv *descriptorpb.ServiceDescriptorProto) er
return nil
}

func (g *generator) genExampleIteratorFile(serv *descriptorpb.ServiceDescriptorProto) error {
pkgName := g.opts.pkgName
servName := pbinfo.ReduceServName(serv.GetName(), pkgName)
methods := append(serv.GetMethod(), g.getMixinMethods()...)
for _, m := range methods {
// Don't need streaming RPCs
if m.GetClientStreaming() || m.GetServerStreaming() {
continue
}
pf, _, err := g.getPagingFields(m)
if err != nil {
return err
}
// Don't generate for non-list RPCs
if pf == nil {
continue
}

p := g.printf

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

inSpec, err := g.descInfo.ImportSpec(inType)
if err != nil {
return err
}

g.imports[inSpec] = true
// Pick the first transport for simplicity. We don't need examples
// of each method for both transports when they have the same surface.
t := g.opts.transports[0]
s := servName
if t == rest {
s += "REST"
}
p("func Example%sClient_%s_all() {", servName, m.GetName())
g.exampleInitClient(pkgName, s)

p("")
p("req := &%s.%s{", inSpec.Name, inType.GetName())
p(" // TODO: Fill request struct fields.")
p(" // See https://pkg.go.dev/%s#%s.", inSpec.Path, inType.GetName())
p("}")

if err := g.examplePagingAllCall(m); err != nil {
return err
}
p("}")
p("")
}
return nil
}

func (g *generator) exampleClientFactory(pkgName, servName string) {
p := g.printf
for _, t := range g.opts.transports {
Expand Down Expand Up @@ -250,6 +306,30 @@ func (g *generator) examplePagingCall(m *descriptorpb.MethodDescriptorProto) err
return nil
}

func (g *generator) examplePagingAllCall(m *descriptorpb.MethodDescriptorProto) error {
outType := g.descInfo.Type[m.GetOutputType()]
if outType == nil {
return fmt.Errorf("cannot find type %q, malformed descriptor?", m.GetOutputType())
}

outSpec, err := g.descInfo.ImportSpec(outType)
if err != nil {
return err
}

p := g.printf

p("for resp, err := range c.%s(ctx, req).All() {", m.GetName())
p(" if err != nil {")
p(" // TODO: Handle error.")
p(" }")
p(" // TODO: Use resp.")
p(" _ = resp")
p("}")
g.imports[outSpec] = true
return nil
}

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

Expand Down
18 changes: 18 additions & 0 deletions internal/gengapic/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,24 @@ func TestExample(t *testing.T) {
t.Errorf("TestExample(%s): imports got(-),want(+):\n%s", tst.tstName, diff)
}
txtdiff.Diff(t, g.pt.String(), filepath.Join("testdata", tst.tstName+".want"))

g.reset()
// remove imports not used in iter example
delete(tst.imports, pbinfo.ImportSpec{Path: "google.golang.org/api/iterator"})
delete(tst.imports, pbinfo.ImportSpec{Path: "io"})
delete(tst.imports, pbinfo.ImportSpec{Name: "iampb", Path: "cloud.google.com/go/iam/apiv1/iampb"})

g.opts = &tst.options
g.mixins = mix
if tst.options.diregapic {
g.mixins = nil
}
g.aux.customOp = tst.op
g.genExampleIteratorFile(serv)
if diff := cmp.Diff(g.imports, tst.imports); diff != "" {
t.Errorf("TestExample(%s): imports got(-),want(+):\n%s", tst.tstName, diff)
}
txtdiff.Diff(t, g.pt.String(), filepath.Join("testdata", tst.tstName+"_all.want"))
})
}
}
Expand Down
27 changes: 11 additions & 16 deletions internal/gengapic/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,9 @@ import (
"google.golang.org/protobuf/types/pluginpb"
)

var enableNewAuthLibraryBlocklist = map[string]bool{
"generativelanguage.googleapis.com": true,
"aiplatform.googleapis.com": true,
"analyticshub.googleapis.com": true,
"biglake.googleapis.com": true,
"bigtableadmin.googleapis.com": true,
"bigtable.googleapis.com": true,
"datastore.googleapis.com": true,
"clouderrorreporting.googleapis.com": true,
"firestore.googleapis.com": true,
"logging.googleapis.com": true,
"cloudprofiler.googleapis.com": true,
"pubsub.googleapis.com": true,
"pubsublite.googleapis.com": true,
"spanner.googleapis.com": true,
// keyed by proto package name, e.g. "google.cloud.foo.v1".
var enableWrapperTypesForPageSize = map[string]bool{
"google.cloud.bigquery.v2": true,
}

type generator struct {
Expand Down Expand Up @@ -214,12 +202,19 @@ func (g *generator) printf(s string, a ...interface{}) {

// TODO(chrisdsmith): Add generator_test.go with TestCommit

func (g *generator) commit(fileName, pkgName string) int {
return g.commitWithBuildTag(fileName, pkgName, "")
}

// commit adds header, etc to current pt and returns the line length of the
// final file output.
func (g *generator) commit(fileName, pkgName string) int {
func (g *generator) commitWithBuildTag(fileName, pkgName, buildTag string) int {
var header strings.Builder
fmt.Fprintf(&header, license.Apache, time.Now().Year())
header.WriteString(g.headerComments.String() + "\n")
if buildTag != "" {
fmt.Fprintf(&header, "//go:build %s\n\n", buildTag)
}
fmt.Fprintf(&header, "package %s\n\n", pkgName)

var imps []pbinfo.ImportSpec
Expand Down
7 changes: 7 additions & 0 deletions internal/gengapic/gengapic.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ func gen(genReq *pluginpb.CodeGeneratorRequest) (*pluginpb.CodeGeneratorResponse
g.imports[pbinfo.ImportSpec{Name: g.opts.pkgName, Path: g.opts.pkgPath}] = true
g.commit(outFile+"_client_example_test.go", g.opts.pkgName+"_test")

g.reset()
if err := g.genExampleIteratorFile(s); err != nil {
return &g.resp, fmt.Errorf("error generating iter example for %q; %v", s.GetName(), err)
}
g.imports[pbinfo.ImportSpec{Name: g.opts.pkgName, Path: g.opts.pkgPath}] = true
g.commitWithBuildTag(outFile+"_client_example_go123_test.go", g.opts.pkgName+"_test", "go1.23")

// Replace original set of transports for the next service that may have
// REST-able RPCs.
if !hasREST {
Expand Down
4 changes: 1 addition & 3 deletions internal/gengapic/gengrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,7 @@ func (g *generator) grpcClientOptions(serv *descriptorpb.ServiceDescriptorProto,
p(" internaloption.WithDefaultAudience(%q),", generateDefaultAudience(host))
p(" internaloption.WithDefaultScopes(DefaultAuthScopes()...),")
p(" internaloption.EnableJwtWithScope(),")
if _, ok := enableNewAuthLibraryBlocklist[g.serviceConfig.GetName()]; !ok {
p("internaloption.EnableNewAuthLibrary(),")
}
p(" internaloption.EnableNewAuthLibrary(),")
p(" option.WithGRPCDialOption(grpc.WithDefaultCallOptions(")
p(" grpc.MaxCallRecvMsgSize(math.MaxInt32))),")
p(" }")
Expand Down
16 changes: 3 additions & 13 deletions internal/gengapic/genrest.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,7 @@ func (g *generator) restClientOptions(serv *descriptorpb.ServiceDescriptorProto,
p(" internaloption.WithDefaultUniverseDomain(%q),", googleDefaultUniverse)
p(" internaloption.WithDefaultAudience(%q),", generateDefaultAudience(host))
p(" internaloption.WithDefaultScopes(DefaultAuthScopes()...),")
if _, ok := enableNewAuthLibraryBlocklist[g.serviceConfig.GetName()]; !ok {
p("internaloption.EnableNewAuthLibrary(),")
}
p(" internaloption.EnableNewAuthLibrary(),")
p(" }")
p("}")
}
Expand Down Expand Up @@ -791,20 +789,12 @@ func (g *generator) pagingRESTCall(servName string, m *descriptorpb.MethodDescri

verb := strings.ToUpper(info.verb)

max := "math.MaxInt32"
g.imports[pbinfo.ImportSpec{Path: "math"}] = true
psTyp := pbinfo.GoTypeForPrim[pageSize.GetType()]
ps := fmt.Sprintf("%s(pageSize)", psTyp)
if isOptional(inType, pageSize.GetName()) {
max = fmt.Sprintf("proto.%s(%s)", upperFirst(psTyp), max)
ps = fmt.Sprintf("proto.%s(%s)", upperFirst(psTyp), ps)
}
tok := "pageToken"
if isOptional(inType, "page_token") {
tok = fmt.Sprintf("proto.String(%s)", tok)
}

pageSizeFieldName := snakeToCamel(pageSize.GetName())
p("func (c *%s) %s(ctx context.Context, req *%s.%s, opts ...gax.CallOption) *%s {",
lowcaseServName, m.GetName(), inSpec.Name, inType.GetName(), pt.iterTypeName)
p("it := &%s{}", pt.iterTypeName)
Expand All @@ -819,7 +809,7 @@ func (g *generator) pagingRESTCall(servName string, m *descriptorpb.MethodDescri

p("unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}")
p("it.InternalFetch = func(pageSize int, pageToken string) ([]%s, string, error) {", pt.elemTypeName)
g.internalFetchSetup(outType, outSpec, tok, pageSizeFieldName, max, ps)
g.internalFetchSetup(outType, outSpec, pageSize, tok)

if info.body != "" {
p(" jsonReq, err := m.Marshal(req)")
Expand Down Expand Up @@ -874,7 +864,7 @@ func (g *generator) pagingRESTCall(servName string, m *descriptorpb.MethodDescri
p(" return %s, resp.GetNextPageToken(), nil", elems)
p("}")
p("")
g.makeFetchAndIterUpdate(pageSizeFieldName)
g.makeFetchAndIterUpdate(pageSize)
p("}")

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

0 comments on commit 60aa0f3

Please sign in to comment.