Skip to content

Commit 479142a

Browse files
committed
runtime: Use protobuf enum name instead of (reflect.Type).String
This adds pass-through of the *proto.Properties for fields so that the fields being populated can be addressed by their enum name when the type is an enum (i.e., (*proto.Properties).Enum is set). As a result of this, the tests now pass, using the protobuf enum name as registered (i.e., fully qualified package name) instead of the Go package name derived by calling (reflect.Type).String. This should have no real impact on existing use, but it may be justified to re-add a fallback case for that reflect.Type name for hand-written protobuf types. Even then, however, it seems unlikely that it should be expected for enums-by-name to work when registering the enum map under a name other than the one it's referred to in protobuf.
1 parent e98d5db commit 479142a

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

runtime/query.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@ func populateFieldValueFromPath(msg proto.Message, fieldPath []string, values []
4040
if m.Kind() != reflect.Ptr {
4141
return fmt.Errorf("unexpected type %T: %v", msg, msg)
4242
}
43+
var props *proto.Properties
4344
m = m.Elem()
4445
for i, fieldName := range fieldPath {
4546
isLast := i == len(fieldPath)-1
4647
if !isLast && m.Kind() != reflect.Struct {
4748
return fmt.Errorf("non-aggregate type in the mid of path: %s", strings.Join(fieldPath, "."))
4849
}
49-
f := fieldByProtoName(m, fieldName)
50+
var f reflect.Value
51+
f, props = fieldByProtoName(m, fieldName)
5052
if !f.IsValid() {
5153
grpclog.Printf("field not found in %T: %s", msg, strings.Join(fieldPath, "."))
5254
return nil
@@ -60,7 +62,7 @@ func populateFieldValueFromPath(msg proto.Message, fieldPath []string, values []
6062
if !isLast {
6163
return fmt.Errorf("unexpected repeated field in %s", strings.Join(fieldPath, "."))
6264
}
63-
return populateRepeatedField(f, values)
65+
return populateRepeatedField(f, values, props)
6466
case reflect.Ptr:
6567
if f.IsNil() {
6668
m = reflect.New(f.Type().Elem())
@@ -82,26 +84,26 @@ func populateFieldValueFromPath(msg proto.Message, fieldPath []string, values []
8284
default:
8385
grpclog.Printf("too many field values: %s", strings.Join(fieldPath, "."))
8486
}
85-
return populateField(m, values[0])
87+
return populateField(m, values[0], props)
8688
}
8789

8890
// fieldByProtoName looks up a field whose corresponding protobuf field name is "name".
8991
// "m" must be a struct value. It returns zero reflect.Value if no such field found.
90-
func fieldByProtoName(m reflect.Value, name string) reflect.Value {
92+
func fieldByProtoName(m reflect.Value, name string) (reflect.Value, *proto.Properties) {
9193
props := proto.GetProperties(m.Type())
9294
for _, p := range props.Prop {
9395
if p.OrigName == name {
94-
return m.FieldByName(p.Name)
96+
return m.FieldByName(p.Name), p
9597
}
9698
}
97-
return reflect.Value{}
99+
return reflect.Value{}, nil
98100
}
99101

100-
func populateRepeatedField(f reflect.Value, values []string) error {
102+
func populateRepeatedField(f reflect.Value, values []string, props *proto.Properties) error {
101103
elemType := f.Type().Elem()
102104

103105
// is the destination field a slice of an enumeration type?
104-
if enumValMap := proto.EnumValueMap(elemType.String()); enumValMap != nil {
106+
if enumValMap := proto.EnumValueMap(props.Enum); enumValMap != nil {
105107
return populateFieldEnumRepeated(f, values, enumValMap)
106108
}
107109

@@ -120,7 +122,7 @@ func populateRepeatedField(f reflect.Value, values []string) error {
120122
return nil
121123
}
122124

123-
func populateField(f reflect.Value, value string) error {
125+
func populateField(f reflect.Value, value string, props *proto.Properties) error {
124126
// Handle well known type
125127
type wkt interface {
126128
XXX_WellKnownType() string
@@ -145,7 +147,7 @@ func populateField(f reflect.Value, value string) error {
145147
}
146148

147149
// is the destination field an enumeration type?
148-
if enumValMap := proto.EnumValueMap(f.Type().String()); enumValMap != nil {
150+
if enumValMap := proto.EnumValueMap(props.Enum); enumValMap != nil {
149151
return populateFieldEnum(f, value, enumValMap)
150152
}
151153

0 commit comments

Comments
 (0)