Skip to content

Commit 0d0642d

Browse files
authored
refactor(sidekick): rust codec initialization (#2411)
Use the source specification name to initialize the Rust codec. We will need slightly different serialization of discovery doc bytes, and I suspect this is not the last difference we will need to deal with.
1 parent 516f29e commit 0d0642d

File tree

5 files changed

+46
-46
lines changed

5 files changed

+46
-46
lines changed

internal/sidekick/internal/rust/annotate_test.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestPackageNames(t *testing.T) {
3333
}
3434
// Override the default name for test APIs ("Test").
3535
model.Name = "workflows-v1"
36-
codec, err := newCodec(true, map[string]string{
36+
codec, err := newCodec("protobuf", map[string]string{
3737
"per-service-features": "true",
3838
"copyright-year": "2035",
3939
})
@@ -158,7 +158,7 @@ func TestServiceAnnotations(t *testing.T) {
158158
if !ok {
159159
t.Fatal("cannot find .test.v1.ResourceService.DeleteResource")
160160
}
161-
codec, err := newCodec(true, map[string]string{})
161+
codec, err := newCodec("protobuf", map[string]string{})
162162
if err != nil {
163163
t.Fatal(err)
164164
}
@@ -222,7 +222,7 @@ func TestServiceAnnotationsPerServiceFeatures(t *testing.T) {
222222
if !ok {
223223
t.Fatal("cannot find .test.v1.ResourceService")
224224
}
225-
codec, err := newCodec(true, map[string]string{
225+
codec, err := newCodec("protobuf", map[string]string{
226226
"per-service-features": "true",
227227
})
228228
if err != nil {
@@ -304,7 +304,7 @@ func TestServiceAnnotationsLROTypes(t *testing.T) {
304304
t.Fatal(err)
305305
}
306306

307-
codec, err := newCodec(true, map[string]string{
307+
codec, err := newCodec("protobuf", map[string]string{
308308
"include-grpc-only-methods": "true",
309309
})
310310
if err != nil {
@@ -341,7 +341,7 @@ func TestServiceAnnotationsNameOverrides(t *testing.T) {
341341
t.Fatal("cannot find .test.v1.ResourceService.GetResource")
342342
}
343343

344-
codec, err := newCodec(true, map[string]string{
344+
codec, err := newCodec("protobuf", map[string]string{
345345
"name-overrides": ".test.v1.ResourceService=Renamed",
346346
})
347347
if err != nil {
@@ -566,7 +566,7 @@ func TestOneOfConflictAnnotations(t *testing.T) {
566566
}
567567
model := api.NewTestAPI([]*api.Message{message}, []*api.Enum{}, []*api.Service{})
568568
api.CrossReference(model)
569-
codec, err := newCodec(true, map[string]string{
569+
codec, err := newCodec("protobuf", map[string]string{
570570
"name-overrides": ".test.Message.nested_thing=NestedThingOneOf",
571571
})
572572
if err != nil {
@@ -632,7 +632,7 @@ func TestEnumAnnotations(t *testing.T) {
632632

633633
model := api.NewTestAPI(
634634
[]*api.Message{}, []*api.Enum{enum}, []*api.Service{})
635-
codec, err := newCodec(true, map[string]string{})
635+
codec, err := newCodec("protobuf", map[string]string{})
636636
if err != nil {
637637
t.Fatal(err)
638638
}
@@ -725,7 +725,7 @@ func TestDuplicateEnumValueAnnotations(t *testing.T) {
725725

726726
model := api.NewTestAPI(
727727
[]*api.Message{}, []*api.Enum{enum}, []*api.Service{})
728-
codec, err := newCodec(true, map[string]string{})
728+
codec, err := newCodec("protobuf", map[string]string{})
729729
if err != nil {
730730
t.Fatal(err)
731731
}
@@ -787,7 +787,7 @@ func TestJsonNameAnnotations(t *testing.T) {
787787
}
788788
model := api.NewTestAPI([]*api.Message{message}, []*api.Enum{}, []*api.Service{})
789789
api.CrossReference(model)
790-
codec, err := newCodec(true, map[string]string{})
790+
codec, err := newCodec("protobuf", map[string]string{})
791791
if err != nil {
792792
t.Fatal(err)
793793
}
@@ -887,7 +887,7 @@ func TestMessageAnnotations(t *testing.T) {
887887

888888
model := api.NewTestAPI([]*api.Message{message}, []*api.Enum{}, []*api.Service{})
889889
api.CrossReference(model)
890-
codec, err := newCodec(true, map[string]string{})
890+
codec, err := newCodec("protobuf", map[string]string{})
891891
if err != nil {
892892
t.Fatal(err)
893893
}
@@ -976,7 +976,7 @@ func TestFieldAnnotations(t *testing.T) {
976976
model.State.MessageByID[map_message.ID] = map_message
977977
api.CrossReference(model)
978978
api.LabelRecursiveFields(model)
979-
codec, err := newCodec(true, map[string]string{})
979+
codec, err := newCodec("protobuf", map[string]string{})
980980
if err != nil {
981981
t.Fatal(err)
982982
}
@@ -1095,7 +1095,7 @@ func TestPrimitiveFieldAnnotations(t *testing.T) {
10951095
model := api.NewTestAPI([]*api.Message{message}, []*api.Enum{}, []*api.Service{})
10961096
api.CrossReference(model)
10971097
api.LabelRecursiveFields(model)
1098-
codec, err := newCodec(true, map[string]string{})
1098+
codec, err := newCodec("protobuf", map[string]string{})
10991099
if err != nil {
11001100
t.Fatal(err)
11011101
}
@@ -1248,7 +1248,7 @@ func TestEnumFieldAnnotations(t *testing.T) {
12481248
model.State.MessageByID[map_message.ID] = map_message
12491249
api.CrossReference(model)
12501250
api.LabelRecursiveFields(model)
1251-
codec, err := newCodec(true, map[string]string{
1251+
codec, err := newCodec("protobuf", map[string]string{
12521252
"package:wkt": "force-used=true,package=google-cloud-wkt,source=google.protobuf",
12531253
})
12541254
if err != nil {
@@ -1407,7 +1407,7 @@ func TestPathInfoAnnotations(t *testing.T) {
14071407
[]*api.Enum{},
14081408
[]*api.Service{service})
14091409
api.CrossReference(model)
1410-
codec, err := newCodec(true, map[string]string{
1410+
codec, err := newCodec("protobuf", map[string]string{
14111411
"include-grpc-only-methods": "true",
14121412
})
14131413
if err != nil {
@@ -1617,7 +1617,7 @@ func TestPathBindingAnnotations(t *testing.T) {
16171617
[]*api.Enum{},
16181618
[]*api.Service{service})
16191619
api.CrossReference(model)
1620-
codec, err := newCodec(true, map[string]string{})
1620+
codec, err := newCodec("protobuf", map[string]string{})
16211621
if err != nil {
16221622
t.Fatal(err)
16231623
}
@@ -1707,7 +1707,7 @@ func TestPathBindingAnnotationsStyle(t *testing.T) {
17071707
[]*api.Enum{},
17081708
[]*api.Service{service})
17091709
api.CrossReference(model)
1710-
codec, err := newCodec(true, map[string]string{})
1710+
codec, err := newCodec("protobuf", map[string]string{})
17111711
if err != nil {
17121712
t.Fatal(err)
17131713
}
@@ -1879,7 +1879,7 @@ func TestInternalMessageOverrides(t *testing.T) {
18791879
model := api.NewTestAPI([]*api.Message{public, private1, private2},
18801880
[]*api.Enum{},
18811881
[]*api.Service{})
1882-
codec, err := newCodec(true, map[string]string{
1882+
codec, err := newCodec("protobuf", map[string]string{
18831883
"internal-types": ".test.Private1,.test.Private2",
18841884
})
18851885
if err != nil {
@@ -1922,7 +1922,7 @@ func TestRoutingRequired(t *testing.T) {
19221922
if err := api.CrossReference(model); err != nil {
19231923
t.Fatal(err)
19241924
}
1925-
codec, err := newCodec(true, map[string]string{
1925+
codec, err := newCodec("protobuf", map[string]string{
19261926
"include-grpc-only-methods": "true",
19271927
"routing-required": "true",
19281928
})
@@ -1938,7 +1938,7 @@ func TestRoutingRequired(t *testing.T) {
19381938

19391939
func TestGenerateSetterSamples(t *testing.T) {
19401940
model := serviceAnnotationsModel()
1941-
codec, err := newCodec(true, map[string]string{
1941+
codec, err := newCodec("protobuf", map[string]string{
19421942
"generate-setter-samples": "true",
19431943
})
19441944
if err != nil {
@@ -1978,7 +1978,7 @@ func TestSetterSampleAnnotations(t *testing.T) {
19781978

19791979
model := api.NewTestAPI([]*api.Message{message}, []*api.Enum{enum}, []*api.Service{})
19801980
api.CrossReference(model)
1981-
codec, err := newCodec(true, map[string]string{
1981+
codec, err := newCodec("protobuf", map[string]string{
19821982
"generate-setter-samples": "true",
19831983
})
19841984
if err != nil {

internal/sidekick/internal/rust/codec.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ var commentUrlRegex = regexp.MustCompile(
5151
`[a-zA-Z]{2,63}` + // The root domain is far more strict
5252
`(/[-a-zA-Z0-9@:%_\+.~#?&/={}\$]*)?`) // Accept just about anything on the query and URL fragments
5353

54-
func newCodec(protobufSource bool, options map[string]string) (*codec, error) {
54+
func newCodec(specificationFormat string, options map[string]string) (*codec, error) {
5555
var sysParams []systemParameter
56-
if protobufSource {
56+
if specificationFormat == "protobuf" {
5757
sysParams = append(sysParams, systemParameter{
5858
Name: "$alt", Value: "json;enum-encoding=int",
5959
})
@@ -72,7 +72,7 @@ func newCodec(protobufSource bool, options map[string]string) (*codec, error) {
7272
version: "0.0.0",
7373
releaseLevel: "preview",
7474
systemParameters: sysParams,
75-
serializeEnumsAsStrings: !protobufSource,
75+
serializeEnumsAsStrings: specificationFormat != "protobuf",
7676
}
7777

7878
for key, definition := range options {

internal/sidekick/internal/rust/codec_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func TestParseOptionsProtobuf(t *testing.T) {
5454
"include-grpc-only-methods": "true",
5555
"per-service-features": "true",
5656
}
57-
got, err := newCodec(true, options)
57+
got, err := newCodec("protobuf", options)
5858
if err != nil {
5959
t.Fatal(err)
6060
}
@@ -113,7 +113,7 @@ func TestParseOptionsOpenAPI(t *testing.T) {
113113
"package-name-override": "test-only",
114114
"copyright-year": "2035",
115115
}
116-
got, err := newCodec(false, options)
116+
got, err := newCodec("openapi", options)
117117
if err != nil {
118118
t.Fatal(err)
119119
}
@@ -152,7 +152,7 @@ func TestParseOptionsTemplateOverride(t *testing.T) {
152152
"copyright-year": "2038",
153153
"template-override": "templates/fancy-templates",
154154
}
155-
got, err := newCodec(false, options)
155+
got, err := newCodec("disco", options)
156156
if err != nil {
157157
t.Fatal(err)
158158
}
@@ -189,7 +189,7 @@ func TestParseOptionsDetailedTracingAttributes(t *testing.T) {
189189
options := map[string]string{
190190
"detailed-tracing-attributes": "true",
191191
}
192-
got, err := newCodec(false, options)
192+
got, err := newCodec("", options)
193193
if err != nil {
194194
t.Fatal(err)
195195
}
@@ -200,7 +200,7 @@ func TestParseOptionsDetailedTracingAttributes(t *testing.T) {
200200
options = map[string]string{
201201
"detailed-tracing-attributes": "--invalid--",
202202
}
203-
if got, err := newCodec(false, options); err == nil {
203+
if got, err := newCodec("", options); err == nil {
204204
t.Errorf("expected an error with an invalid detailed-tracing-attributes flag, got=%v", got)
205205
}
206206
}
@@ -224,7 +224,7 @@ func TestPackageName(t *testing.T) {
224224

225225
func rustPackageNameImpl(t *testing.T, want string, opts map[string]string, api *api.API) {
226226
t.Helper()
227-
c, err := newCodec(true, opts)
227+
c, err := newCodec("protobuf", opts)
228228
if err != nil {
229229
t.Fatal(err)
230230
}
@@ -235,7 +235,7 @@ func rustPackageNameImpl(t *testing.T, want string, opts map[string]string, api
235235
}
236236

237237
func TestServiceName(t *testing.T) {
238-
c, err := newCodec(true, map[string]string{
238+
c, err := newCodec("protobuf", map[string]string{
239239
"name-overrides": ".google.testing.BadName=GoodName,.google.testing.Old=New",
240240
})
241241
if err != nil {
@@ -245,7 +245,7 @@ func TestServiceName(t *testing.T) {
245245
testServiceNameImpl(t, c, "Old", "New")
246246
testServiceNameImpl(t, c, "Unchanged", "Unchanged")
247247

248-
c2, err := newCodec(true, map[string]string{})
248+
c2, err := newCodec("protobuf", map[string]string{})
249249
if err != nil {
250250
t.Fatal(err)
251251
}
@@ -266,7 +266,7 @@ func testServiceNameImpl(t *testing.T, c *codec, serviceName string, want string
266266
}
267267

268268
func TestOneOfEnumName(t *testing.T) {
269-
c, err := newCodec(true, map[string]string{
269+
c, err := newCodec("protobuf", map[string]string{
270270
"name-overrides": ".google.testing.Message.conflict=ConflictOneOf",
271271
})
272272
if err != nil {
@@ -275,7 +275,7 @@ func TestOneOfEnumName(t *testing.T) {
275275
testOneOfEnumNameImpl(t, c, "conflict", "ConflictOneOf")
276276
testOneOfEnumNameImpl(t, c, "basic_case", "BasicCase")
277277

278-
c2, err := newCodec(true, map[string]string{})
278+
c2, err := newCodec("protobuf", map[string]string{})
279279
if err != nil {
280280
t.Fatal(err)
281281
}
@@ -1875,7 +1875,7 @@ func TestParseOptionsGenerateSetterSamples(t *testing.T) {
18751875
options := map[string]string{
18761876
"generate-setter-samples": "true",
18771877
}
1878-
got, err := newCodec(false, options)
1878+
got, err := newCodec("", options)
18791879
if err != nil {
18801880
t.Fatal(err)
18811881
}

internal/sidekick/internal/rust/generate.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var templates embed.FS
2828

2929
// Generate generates Rust code from the model.
3030
func Generate(model *api.API, outdir string, cfg *config.Config) error {
31-
codec, err := newCodec(cfg.General.SpecificationFormat == "protobuf", cfg.Codec)
31+
codec, err := newCodec(cfg.General.SpecificationFormat, cfg.Codec)
3232
if err != nil {
3333
return err
3434
}
@@ -40,12 +40,12 @@ func Generate(model *api.API, outdir string, cfg *config.Config) error {
4040

4141
// GenerateStorage generates Rust code for the storage service.
4242
func GenerateStorage(outdir string, storageModel *api.API, storageConfig *config.Config, controlModel *api.API, controlConfig *config.Config) error {
43-
storageCodec, err := newCodec(storageConfig.General.SpecificationFormat == "protobuf", storageConfig.Codec)
43+
storageCodec, err := newCodec(storageConfig.General.SpecificationFormat, storageConfig.Codec)
4444
if err != nil {
4545
return err
4646
}
4747
annotateModel(storageModel, storageCodec)
48-
controlCodec, err := newCodec(controlConfig.General.SpecificationFormat == "protobuf", controlConfig.Codec)
48+
controlCodec, err := newCodec(controlConfig.General.SpecificationFormat, controlConfig.Codec)
4949
if err != nil {
5050
return err
5151
}

0 commit comments

Comments
 (0)