Skip to content

Commit 176c0cb

Browse files
authored
cleanup(sidekick/rust): prefer error over panic() (#3875)
Propagate errors from the functions that compute fully qualified symbol names.
1 parent c1848b5 commit 176c0cb

8 files changed

Lines changed: 448 additions & 178 deletions

File tree

internal/sidekick/rust/annotate.go

Lines changed: 152 additions & 50 deletions
Large diffs are not rendered by default.

internal/sidekick/rust/annotate_map_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func TestMapAnnotationsSameSame(t *testing.T) {
197197
JSONName: "field",
198198
ID: ".test.Message.field",
199199
Typez: api.MESSAGE_TYPE,
200-
TypezID: "$map<unused, unused>",
200+
TypezID: "$map<string, string>",
201201
}
202202
message := &api.Message{
203203
Name: "Message",
@@ -210,7 +210,10 @@ func TestMapAnnotationsSameSame(t *testing.T) {
210210
api.CrossReference(model)
211211
api.LabelRecursiveFields(model)
212212
codec := newTestCodec(t, "protobuf", "test", map[string]string{})
213-
annotateModel(model, codec)
213+
_, err := annotateModel(model, codec)
214+
if err != nil {
215+
t.Fatal(err)
216+
}
214217

215218
got := field.Codec.(*fieldAnnotations).SerdeAs
216219
if got != "" {

internal/sidekick/rust/annotate_method_test.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ func TestAnnotateMethodNames(t *testing.T) {
3131
codec := newTestCodec(t, "protobuf", "", map[string]string{
3232
"include-grpc-only-methods": "true",
3333
})
34-
_ = annotateModel(model, codec)
34+
_, err = annotateModel(model, codec)
35+
if err != nil {
36+
t.Fatal(err)
37+
}
3538

3639
for _, test := range []struct {
3740
MethodID string
@@ -101,7 +104,10 @@ func TestAnnotateDiscoveryAnnotations(t *testing.T) {
101104
if err != nil {
102105
t.Fatal(err)
103106
}
104-
_ = annotateModel(model, codec)
107+
_, err = annotateModel(model, codec)
108+
if err != nil {
109+
t.Fatal(err)
110+
}
105111

106112
methodID := ".test.v1.ResourceService.Delete"
107113
gotMethod, ok := model.State.MethodByID[methodID]
@@ -139,7 +145,10 @@ func TestAnnotateMethodAPIVersion(t *testing.T) {
139145
gotMethod.APIVersion = "v1_20260205"
140146

141147
codec := newTestCodec(t, "disco", "", map[string]string{})
142-
_ = annotateModel(model, codec)
148+
_, err = annotateModel(model, codec)
149+
if err != nil {
150+
t.Fatal(err)
151+
}
143152

144153
got := gotMethod.Codec.(*methodAnnotation)
145154
want := []systemParameter{
@@ -161,7 +170,10 @@ func TestAnnotateMethodInternalBuilders(t *testing.T) {
161170
codec := newTestCodec(t, "protobuf", "", map[string]string{
162171
"internal-builders": "true",
163172
})
164-
_ = annotateModel(model, codec)
173+
_, err = annotateModel(model, codec)
174+
if err != nil {
175+
t.Fatal(err)
176+
}
165177

166178
methodID := ".test.v1.ResourceService.Delete"
167179
gotMethod, ok := model.State.MethodByID[methodID]

internal/sidekick/rust/annotate_model_test.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ func TestDefaultFeatures(t *testing.T) {
5555
} {
5656
model := newTestAnnotateModelAPI()
5757
codec := newTestCodec(t, "protobuf", "", test.Options)
58-
got := annotateModel(model, codec)
58+
got, err := annotateModel(model, codec)
59+
if err != nil {
60+
t.Fatal(err)
61+
}
5962
t.Logf("Options=%v", test.Options)
6063
if diff := cmp.Diff(test.Want, got.DefaultFeatures); diff != "" {
6164
t.Errorf("mismatch (-want, +got):\n%s", diff)
@@ -87,7 +90,10 @@ func TestRustdocWarnings(t *testing.T) {
8790
} {
8891
model := newTestAnnotateModelAPI()
8992
codec := newTestCodec(t, "protobuf", "", test.Options)
90-
got := annotateModel(model, codec)
93+
got, err := annotateModel(model, codec)
94+
if err != nil {
95+
t.Fatal(err)
96+
}
9197
t.Logf("Options=%v", test.Options)
9298
if diff := cmp.Diff(test.Want, got.DisabledRustdocWarnings); diff != "" {
9399
t.Errorf("mismatch (-want, +got):\n%s", diff)
@@ -119,7 +125,10 @@ func TestClippyWarnings(t *testing.T) {
119125
} {
120126
model := newTestAnnotateModelAPI()
121127
codec := newTestCodec(t, "protobuf", "", test.Options)
122-
got := annotateModel(model, codec)
128+
got, err := annotateModel(model, codec)
129+
if err != nil {
130+
t.Fatal(err)
131+
}
123132
t.Logf("Options=%v", test.Options)
124133
if diff := cmp.Diff(test.Want, got.DisabledClippyWarnings); diff != "" {
125134
t.Errorf("mismatch (-want, +got):\n%s", diff)
@@ -155,7 +164,10 @@ func TestInternalBuildersAnnotation(t *testing.T) {
155164
} {
156165
model := newTestAnnotateModelAPI()
157166
codec := newTestCodec(t, "protobuf", "", test.Options)
158-
got := annotateModel(model, codec)
167+
got, err := annotateModel(model, codec)
168+
if err != nil {
169+
t.Fatal(err)
170+
}
159171
if got.InternalBuilders != test.Want {
160172
t.Errorf("mismatch in InternalBuilders, want=%v, got=%v", test.Want, got.InternalBuilders)
161173
}

internal/sidekick/rust/annotate_test.go

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ func TestPackageNames(t *testing.T) {
5151
codec.packageMapping = map[string]*packagez{
5252
"google.protobuf": {name: "wkt"},
5353
}
54-
got := annotateModel(model, codec)
54+
got, err := annotateModel(model, codec)
55+
if err != nil {
56+
t.Fatal(err)
57+
}
5558
want := &modelAnnotations{
5659
PackageName: "google-cloud-workflows-v1",
5760
PackageVersion: "1.2.3",
@@ -377,7 +380,10 @@ func TestServiceAnnotationsAPIVersions(t *testing.T) {
377380
t.Fatalf("cannot find service %s", id)
378381
}
379382
codec := newTestCodec(t, "protobuf", "", map[string]string{})
380-
_ = annotateModel(model, codec)
383+
_, err := annotateModel(model, codec)
384+
if err != nil {
385+
t.Fatal(err)
386+
}
381387
got := service.Codec.(*serviceAnnotations)
382388
if got == nil {
383389
t.Fatalf("no annotations for service %s", service.ID)
@@ -1817,7 +1823,10 @@ func TestAnnotateModelWithDetailedTracing(t *testing.T) {
18171823
t.Run(test.name, func(t *testing.T) {
18181824
model := api.NewTestAPI([]*api.Message{}, []*api.Enum{}, []*api.Service{})
18191825
codec := newTestCodec(t, "protobuf", "", test.options)
1820-
got := annotateModel(model, codec)
1826+
got, err := annotateModel(model, codec)
1827+
if err != nil {
1828+
t.Fatal(err)
1829+
}
18211830
if got.DetailedTracingAttributes != test.want {
18221831
t.Errorf("annotateModel() DetailedTracingAttributes = %v, want %v", got.DetailedTracingAttributes, test.want)
18231832
}
@@ -1966,11 +1975,20 @@ func TestOneOfExampleFieldSelection(t *testing.T) {
19661975
IsOneOf: true,
19671976
Deprecated: true,
19681977
}
1969-
map_field := &api.Field{
1978+
mapMessage := &api.Message{
1979+
Name: "$map<string, string>",
1980+
ID: "$map<string, string>",
1981+
IsMap: true,
1982+
Fields: []*api.Field{
1983+
{Name: "key", ID: "$map<string, string>.key", Typez: api.STRING_TYPE},
1984+
{Name: "value", ID: "$map<string, string>.value", Typez: api.STRING_TYPE},
1985+
},
1986+
}
1987+
mapField := &api.Field{
19701988
Name: "map_field",
19711989
ID: ".test.Message.map_field",
19721990
Typez: api.MESSAGE_TYPE,
1973-
TypezID: ".test.$Map",
1991+
TypezID: "$map<string, string>",
19741992
IsOneOf: true,
19751993
Map: true,
19761994
}
@@ -1987,14 +2005,14 @@ func TestOneOfExampleFieldSelection(t *testing.T) {
19872005
Typez: api.INT32_TYPE,
19882006
IsOneOf: true,
19892007
}
1990-
message_field := &api.Field{
2008+
messageField := &api.Field{
19912009
Name: "message_field",
19922010
ID: ".test.Message.message_field",
19932011
Typez: api.MESSAGE_TYPE,
19942012
TypezID: ".test.OneMessage",
19952013
IsOneOf: true,
19962014
}
1997-
another_message_field := &api.Field{
2015+
anotherMessageField := &api.Field{
19982016
Name: "another_message_field",
19992017
ID: ".test.Message.another_message_field",
20002018
Typez: api.MESSAGE_TYPE,
@@ -2009,28 +2027,28 @@ func TestOneOfExampleFieldSelection(t *testing.T) {
20092027
}{
20102028
{
20112029
name: "all types",
2012-
fields: []*api.Field{deprecated, map_field, repeated, scalar, message_field},
2030+
fields: []*api.Field{deprecated, mapField, repeated, scalar, messageField},
20132031
want: scalar,
20142032
},
20152033
{
20162034
name: "no primitives",
2017-
fields: []*api.Field{deprecated, map_field, repeated, message_field},
2018-
want: message_field,
2035+
fields: []*api.Field{deprecated, mapField, repeated, messageField},
2036+
want: messageField,
20192037
},
20202038
{
20212039
name: "only scalars and messages",
2022-
fields: []*api.Field{message_field, scalar, another_message_field},
2040+
fields: []*api.Field{messageField, scalar, anotherMessageField},
20232041
want: scalar,
20242042
},
20252043
{
20262044
name: "no scalars",
2027-
fields: []*api.Field{deprecated, map_field, repeated},
2045+
fields: []*api.Field{deprecated, mapField, repeated},
20282046
want: repeated,
20292047
},
20302048
{
20312049
name: "only map and deprecated",
2032-
fields: []*api.Field{deprecated, map_field},
2033-
want: map_field,
2050+
fields: []*api.Field{deprecated, mapField},
2051+
want: mapField,
20342052
},
20352053
{
20362054
name: "only deprecated",
@@ -2063,10 +2081,12 @@ func TestOneOfExampleFieldSelection(t *testing.T) {
20632081
ID: ".test.AnotherMessage",
20642082
Package: "test",
20652083
}
2066-
model := api.NewTestAPI([]*api.Message{message, oneMesage, anotherMessage}, []*api.Enum{}, []*api.Service{})
2084+
model := api.NewTestAPI([]*api.Message{message, oneMesage, anotherMessage, mapMessage}, []*api.Enum{}, []*api.Service{})
20672085
api.CrossReference(model)
2068-
codec := createRustCodec()
2069-
annotateModel(model, codec)
2086+
codec := newTestCodec(t, "protobuf", "test", map[string]string{})
2087+
if _, err := annotateModel(model, codec); err != nil {
2088+
t.Fatal(err)
2089+
}
20702090

20712091
got := group.Codec.(*oneOfAnnotation).ExampleField
20722092
if diff := cmp.Diff(tc.want, got); diff != "" {

0 commit comments

Comments
 (0)