Skip to content

Commit e4958d0

Browse files
fix(sidekick): Fixes several issues with oneof main setter samples (#2589)
- Uses the right import for the oneof group - Takes boxing of the oneof message variants into account. - Messages variants are now deprioritized for selection as a oneof example variant. - Makes the values shown match the ones used in other samples This also removes the sample generation for when the example one of varinat that's use on the main setter is repeated or map, as that needs bigger improvements that will be done in a subsequent PR. Note that the samples for each specific variant, including repeated and map, continue to be generated.
1 parent 16df43c commit e4958d0

File tree

3 files changed

+55
-18
lines changed

3 files changed

+55
-18
lines changed

internal/sidekick/internal/rust/annotate.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,11 +396,16 @@ type oneOfAnnotation struct {
396396
QualifiedName string
397397
// The fully qualified name, relative to `codec.modulePath`. Typically this
398398
// is the `QualifiedName` with the `crate::model::` prefix removed.
399+
// This is always relative to `ScopeInExamples`.
399400
RelativeName string
400401
// The Rust `struct` that contains this oneof, fully qualified
401402
StructQualifiedName string
402-
FieldType string
403-
DocLines []string
403+
// The scope to show in examples. For messages in external packages
404+
// this is basically `QualifiedName`. For messages in the current package
405+
// this includes `modelAnnotations.PackageName`.
406+
ScopeInExamples string
407+
FieldType string
408+
DocLines []string
404409
// The best field to show in a oneof related samples.
405410
// Non deprecated fields are preferred, then scalar, repeated, map fields
406411
// in that order.
@@ -991,12 +996,22 @@ func (c *codec) annotateOneOf(oneof *api.OneOf, message *api.Message, model *api
991996
qualifiedName := fmt.Sprintf("%s::%s", scope, enumName)
992997
relativeEnumName := strings.TrimPrefix(qualifiedName, c.modulePath+"::")
993998
structQualifiedName := fullyQualifiedMessageName(message, c.modulePath, model.PackageName, c.packageMapping)
999+
scopeInExamples := scope
1000+
if strings.HasPrefix(scope, c.modulePath+"::") {
1001+
scopeInExamples = strings.Replace(scope, c.modulePath, fmt.Sprintf("%s::model", c.packageNamespace(model)), 1)
1002+
}
9941003

9951004
bestField := slices.MaxFunc(oneof.Fields, func(f1 *api.Field, f2 *api.Field) int {
9961005
if f1.Deprecated == f2.Deprecated {
9971006
if f1.Map == f2.Map {
9981007
if f1.Repeated == f2.Repeated {
999-
return 0
1008+
if f1.MessageType != nil && f2.MessageType == nil {
1009+
return -1
1010+
} else if f1.MessageType == nil && f2.MessageType != nil {
1011+
return 1
1012+
} else {
1013+
return 0
1014+
}
10001015
} else if f1.Repeated {
10011016
return -1
10021017
} else {
@@ -1021,6 +1036,7 @@ func (c *codec) annotateOneOf(oneof *api.OneOf, message *api.Message, model *api
10211036
QualifiedName: qualifiedName,
10221037
RelativeName: relativeEnumName,
10231038
StructQualifiedName: structQualifiedName,
1039+
ScopeInExamples: scopeInExamples,
10241040
FieldType: fmt.Sprintf("%s::%s", scope, enumName),
10251041
DocLines: c.formatDocComments(oneof.Documentation, oneof.ID, model.State, message.Scopes()),
10261042
ExampleField: bestField,

internal/sidekick/internal/rust/annotate_test.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ func TestOneOfAnnotations(t *testing.T) {
495495
QualifiedName: "crate::model::message::Type",
496496
RelativeName: "message::Type",
497497
StructQualifiedName: "crate::model::Message",
498+
ScopeInExamples: "google_cloud_test::model::message",
498499
FieldType: "crate::model::message::Type",
499500
DocLines: []string{"/// Say something clever about this oneof."},
500501
ExampleField: singular,
@@ -644,6 +645,7 @@ func TestOneOfConflictAnnotations(t *testing.T) {
644645
QualifiedName: "crate::model::message::NestedThingOneOf",
645646
RelativeName: "message::NestedThingOneOf",
646647
StructQualifiedName: "crate::model::Message",
648+
ScopeInExamples: "google_cloud_test::model::message",
647649
FieldType: "crate::model::message::NestedThingOneOf",
648650
DocLines: []string{"/// Say something clever about this oneof."},
649651
ExampleField: singular,
@@ -1877,6 +1879,13 @@ func TestOneOfExampleFieldSelection(t *testing.T) {
18771879
Name: "message_field",
18781880
ID: ".test.Message.message_field",
18791881
Typez: api.MESSAGE_TYPE,
1882+
TypezID: ".test.OneMessage",
1883+
IsOneOf: true,
1884+
}
1885+
another_message_field := &api.Field{
1886+
Name: "another_message_field",
1887+
ID: ".test.Message.another_message_field",
1888+
Typez: api.MESSAGE_TYPE,
18801889
TypezID: ".test.AnotherMessage",
18811890
IsOneOf: true,
18821891
}
@@ -1891,6 +1900,16 @@ func TestOneOfExampleFieldSelection(t *testing.T) {
18911900
fields: []*api.Field{deprecated, map_field, repeated, scalar, message_field},
18921901
want: scalar,
18931902
},
1903+
{
1904+
name: "no primitives",
1905+
fields: []*api.Field{deprecated, map_field, repeated, message_field},
1906+
want: message_field,
1907+
},
1908+
{
1909+
name: "only scalars and messages",
1910+
fields: []*api.Field{message_field, scalar, another_message_field},
1911+
want: scalar,
1912+
},
18941913
{
18951914
name: "no scalars",
18961915
fields: []*api.Field{deprecated, map_field, repeated},
@@ -1922,7 +1941,17 @@ func TestOneOfExampleFieldSelection(t *testing.T) {
19221941
Fields: tc.fields,
19231942
OneOfs: []*api.OneOf{group},
19241943
}
1925-
model := api.NewTestAPI([]*api.Message{message}, []*api.Enum{}, []*api.Service{})
1944+
oneMesage := &api.Message{
1945+
Name: "OneMessage",
1946+
ID: ".test.OneMessage",
1947+
Package: "test",
1948+
}
1949+
anotherMessage := &api.Message{
1950+
Name: "AnotherMessage",
1951+
ID: ".test.AnotherMessage",
1952+
Package: "test",
1953+
}
1954+
model := api.NewTestAPI([]*api.Message{message, oneMesage, anotherMessage}, []*api.Enum{}, []*api.Service{})
19261955
api.CrossReference(model)
19271956
codec := createRustCodec()
19281957
annotateModel(model, codec)

internal/sidekick/internal/rust/templates/common/setter_preamble/oneof.mustache

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,20 @@ limitations under the License.
2424
{{#Codec.ExampleField}}
2525
{{#IsString}}
2626
/// # use {{Parent.Codec.NameInExamples}};
27-
/// let x = {{Parent.Codec.Name}}::new().set_{{Group.Codec.SetterName}}(Some({{{Group.Codec.RelativeName}}}::{{Codec.BranchName}}("value".to_string())));
27+
/// use {{Group.Codec.ScopeInExamples}};
28+
/// let x = {{Parent.Codec.Name}}::new().set_{{Group.Codec.SetterName}}(Some({{{Group.Codec.RelativeName}}}::{{Codec.BranchName}}("example".to_string())));
2829
{{/IsString}}
2930
{{#IsLikeInt}}
3031
/// # use {{Parent.Codec.NameInExamples}};
31-
/// let x = {{Parent.Codec.Name}}::new().set_{{Group.Codec.SetterName}}(Some({{{Group.Codec.RelativeName}}}::{{Codec.BranchName}}(123)));
32+
/// use {{Group.Codec.ScopeInExamples}};
33+
/// let x = {{Parent.Codec.Name}}::new().set_{{Group.Codec.SetterName}}(Some({{{Group.Codec.RelativeName}}}::{{Codec.BranchName}}(42)));
3234
{{/IsLikeInt}}
3335
{{#IsObject}}
3436
/// # use {{Parent.Codec.NameInExamples}};
37+
/// use {{Group.Codec.ScopeInExamples}};
3538
/// use {{MessageType.Codec.NameInExamples}};
36-
/// let x = {{Parent.Codec.Name}}::new().set_{{Group.Codec.SetterName}}(Some({{{Group.Codec.RelativeName}}}::{{Codec.BranchName}}({{MessageType.Codec.Name}}::default()/* use setters */)));
39+
/// let x = {{Parent.Codec.Name}}::new().set_{{Group.Codec.SetterName}}(Some({{{Group.Codec.RelativeName}}}::{{Codec.BranchName}}({{MessageType.Codec.Name}}::default().into())));
3740
{{/IsObject}}
38-
{{#Repeated}}
39-
/// # use {{Parent.Codec.NameInExamples}};
40-
/// use {{MessageType.Codec.NameInExamples}};
41-
/// let x = {{Parent.Codec.Name}}::new().set_{{Group.Codec.SetterName}}(Some({{{Group.Codec.RelativeName}}}::{{Codec.BranchName}}(vec![{{MessageType.Codec.Name}}::default()])));
42-
{{/Repeated}}
43-
{{#Map}}
44-
/// # use {{Parent.Codec.NameInExamples}};
45-
/// use {{KeyField.MessageType.Codec.NameInExamples}};
46-
/// use {{ValueField.MessageType.Codec.NameInExamples}};
47-
/// let x = {{Parent.Codec.Name}}::new().set_{{Group.Codec.SetterName}}(Some({{{Group.Codec.RelativeName}}}::{{Codec.BranchName}}(std::collections::HashMap::from([({{KeyField.MessageType.Codec.Name}}::default(), {{ValueField.MessageType.Codec.Name}}::default())]))));
48-
{{/Map}}
4941
{{/Codec.ExampleField}}
5042
/// ```
5143
{{/ModelCodec.GenerateSetterSamples}}

0 commit comments

Comments
 (0)