Skip to content

Commit 53cc3d3

Browse files
authored
gapic: add release-level option (#219)
1 parent 55de6f4 commit 53cc3d3

File tree

6 files changed

+249
-2
lines changed

6 files changed

+249
-2
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ The configuration supported by the plugin option includes:
7979
* `grpc-service-config`: the path to a gRPC ServiceConfig JSON file.
8080
* This is used for client-side retry configuration in accordance with [AIP-4221](http://aip.dev/4221)
8181

82+
* `release-level`: the client library release level.
83+
* Defaults to empty, which is essentially the GA release level.
84+
* Acceptable values are `alpha` and `beta`.
85+
8286
Docker Wrapper
8387
--------------
8488
The generator can also be executed via a Docker container. The image containes `protoc`, the microgenerator

internal/gengapic/doc_file.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ func (g *generator) genDocFile(pkgPath, pkgName string, year int, scopes []strin
5353
}
5454
}
5555

56+
switch g.relLvl {
57+
case alpha:
58+
p("//")
59+
p("// NOTE: This package is in alpha. It is not stable, and is likely to change.")
60+
case beta:
61+
p("//")
62+
p("// NOTE: This package is in beta. It is not stable, and may be subject to changes.")
63+
}
64+
5665
p("//")
5766
p("// Use of Context")
5867
p("//")

internal/gengapic/doc_file_test.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@ func TestDocFile(t *testing.T) {
3030
},
3131
}
3232

33-
g.genDocFile("path/to/awesome", "awesome", 42, []string{"https://foo.bar.com/auth", "https://zip.zap.com/auth"})
34-
txtdiff.Diff(t, "doc_file", g.pt.String(), filepath.Join("testdata", "doc_file.want"))
33+
for _, tst := range []struct {
34+
relLvl, want string
35+
}{
36+
{
37+
want: filepath.Join("testdata", "doc_file.want"),
38+
},
39+
{
40+
relLvl: alpha,
41+
want: filepath.Join("testdata", "doc_file_alpha.want"),
42+
},
43+
{
44+
relLvl: beta,
45+
want: filepath.Join("testdata", "doc_file_beta.want"),
46+
},
47+
} {
48+
g.relLvl = tst.relLvl
49+
g.genDocFile("path/to/awesome", "awesome", 42, []string{"https://foo.bar.com/auth", "https://zip.zap.com/auth"})
50+
txtdiff.Diff(t, "doc_file", g.pt.String(), tst.want)
51+
g.reset()
52+
}
3553
}

internal/gengapic/gengapic.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ const (
4545
emptyType = ".google.protobuf.Empty"
4646
lroType = ".google.longrunning.Operation"
4747
paramError = "need parameter in format: go-gapic-package=client/import/path;packageName"
48+
alpha = "alpha"
49+
beta = "beta"
4850
)
4951

5052
var headerParamRegexp = regexp.MustCompile(`{([_a-z]+)=`)
@@ -92,6 +94,8 @@ func Gen(genReq *plugin.CodeGeneratorRequest) (*plugin.CodeGeneratorResponse, er
9294
if err != nil {
9395
return &g.resp, errors.E(nil, "error unmarshaling gPRC service config: %v", err)
9496
}
97+
case "release-level":
98+
g.relLvl = strings.ToLower(s[e+1:])
9599
}
96100
}
97101
}
@@ -180,10 +184,14 @@ type generator struct {
180184
// Parsed service config from plugin option
181185
serviceConfig *serviceConfig
182186

187+
// gRPC ServiceConfig
183188
grpcConf *conf.ServiceConfig
184189

185190
// Auxiliary types to be generated in the package
186191
aux *auxTypes
192+
193+
// Release level that defaults to GA/nothing
194+
relLvl string
187195
}
188196

189197
func (g *generator) init(files []*descriptor.FileDescriptorProto) {
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright 42 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Code generated by protoc-gen-go_gapic. DO NOT EDIT.
16+
17+
// Package awesome is an auto-generated package for the
18+
// Awesome Foo API.
19+
//
20+
// The Awesome Foo API is really really awesome. It enables the use of Foo
21+
// with Buz and Baz to acclerate bar.
22+
//
23+
// NOTE: This package is in alpha. It is not stable, and is likely to change.
24+
//
25+
// Use of Context
26+
//
27+
// The ctx passed to NewClient is used for authentication requests and
28+
// for creating the underlying connection, but is not used for subsequent calls.
29+
// Individual methods on the client use the ctx given to them.
30+
//
31+
// To close the open connection, use the Close() method.
32+
//
33+
// For information about setting deadlines, reusing contexts, and more
34+
// please visit godoc.org/cloud.google.com/go.
35+
36+
package awesome // import "path/to/awesome"
37+
38+
import (
39+
"context"
40+
"runtime"
41+
"strings"
42+
"unicode"
43+
44+
"google.golang.org/grpc/metadata"
45+
)
46+
47+
const versionClient = "UNKNOWN"
48+
49+
func insertMetadata(ctx context.Context, mds ...metadata.MD) context.Context {
50+
out, _ := metadata.FromOutgoingContext(ctx)
51+
out = out.Copy()
52+
for _, md := range mds {
53+
for k, v := range md {
54+
out[k] = append(out[k], v...)
55+
}
56+
}
57+
return metadata.NewOutgoingContext(ctx, out)
58+
}
59+
60+
// DefaultAuthScopes reports the default set of authentication scopes to use with this package.
61+
func DefaultAuthScopes() []string {
62+
return []string{
63+
"https://foo.bar.com/auth",
64+
"https://zip.zap.com/auth",
65+
}
66+
}
67+
68+
// versionGo returns the Go runtime version. The returned string
69+
// has no whitespace, suitable for reporting in header.
70+
func versionGo() string {
71+
const develPrefix = "devel +"
72+
73+
s := runtime.Version()
74+
if strings.HasPrefix(s, develPrefix) {
75+
s = s[len(develPrefix):]
76+
if p := strings.IndexFunc(s, unicode.IsSpace); p >= 0 {
77+
s = s[:p]
78+
}
79+
return s
80+
}
81+
82+
notSemverRune := func(r rune) bool {
83+
return !strings.ContainsRune("0123456789.", r)
84+
}
85+
86+
if strings.HasPrefix(s, "go1") {
87+
s = s[2:]
88+
var prerelease string
89+
if p := strings.IndexFunc(s, notSemverRune); p >= 0 {
90+
s, prerelease = s[:p], s[p:]
91+
}
92+
if strings.HasSuffix(s, ".") {
93+
s += "0"
94+
} else if strings.Count(s, ".") < 2 {
95+
s += ".0"
96+
}
97+
if prerelease != "" {
98+
s += "-" + prerelease
99+
}
100+
return s
101+
}
102+
return "UNKNOWN"
103+
}
104+
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright 42 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Code generated by protoc-gen-go_gapic. DO NOT EDIT.
16+
17+
// Package awesome is an auto-generated package for the
18+
// Awesome Foo API.
19+
//
20+
// The Awesome Foo API is really really awesome. It enables the use of Foo
21+
// with Buz and Baz to acclerate bar.
22+
//
23+
// NOTE: This package is in beta. It is not stable, and may be subject to changes.
24+
//
25+
// Use of Context
26+
//
27+
// The ctx passed to NewClient is used for authentication requests and
28+
// for creating the underlying connection, but is not used for subsequent calls.
29+
// Individual methods on the client use the ctx given to them.
30+
//
31+
// To close the open connection, use the Close() method.
32+
//
33+
// For information about setting deadlines, reusing contexts, and more
34+
// please visit godoc.org/cloud.google.com/go.
35+
36+
package awesome // import "path/to/awesome"
37+
38+
import (
39+
"context"
40+
"runtime"
41+
"strings"
42+
"unicode"
43+
44+
"google.golang.org/grpc/metadata"
45+
)
46+
47+
const versionClient = "UNKNOWN"
48+
49+
func insertMetadata(ctx context.Context, mds ...metadata.MD) context.Context {
50+
out, _ := metadata.FromOutgoingContext(ctx)
51+
out = out.Copy()
52+
for _, md := range mds {
53+
for k, v := range md {
54+
out[k] = append(out[k], v...)
55+
}
56+
}
57+
return metadata.NewOutgoingContext(ctx, out)
58+
}
59+
60+
// DefaultAuthScopes reports the default set of authentication scopes to use with this package.
61+
func DefaultAuthScopes() []string {
62+
return []string{
63+
"https://foo.bar.com/auth",
64+
"https://zip.zap.com/auth",
65+
}
66+
}
67+
68+
// versionGo returns the Go runtime version. The returned string
69+
// has no whitespace, suitable for reporting in header.
70+
func versionGo() string {
71+
const develPrefix = "devel +"
72+
73+
s := runtime.Version()
74+
if strings.HasPrefix(s, develPrefix) {
75+
s = s[len(develPrefix):]
76+
if p := strings.IndexFunc(s, unicode.IsSpace); p >= 0 {
77+
s = s[:p]
78+
}
79+
return s
80+
}
81+
82+
notSemverRune := func(r rune) bool {
83+
return !strings.ContainsRune("0123456789.", r)
84+
}
85+
86+
if strings.HasPrefix(s, "go1") {
87+
s = s[2:]
88+
var prerelease string
89+
if p := strings.IndexFunc(s, notSemverRune); p >= 0 {
90+
s, prerelease = s[:p], s[p:]
91+
}
92+
if strings.HasSuffix(s, ".") {
93+
s += "0"
94+
} else if strings.Count(s, ".") < 2 {
95+
s += ".0"
96+
}
97+
if prerelease != "" {
98+
s += "-" + prerelease
99+
}
100+
return s
101+
}
102+
return "UNKNOWN"
103+
}
104+

0 commit comments

Comments
 (0)