Skip to content

Commit f3aa758

Browse files
authored
Merge pull request #315 from nilium/fix-query-param-enums
Look up enum value maps by their proto name
2 parents b855144 + 479142a commit f3aa758

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
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

runtime/query_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func TestPopulateParameters(t *testing.T) {
136136
msg.Reset()
137137
err := runtime.PopulateQueryParameters(msg, spec.values, spec.filter)
138138
if err != nil {
139-
t.Errorf("runtime.PoplateQueryParameters(msg, %v, %v) failed with %v; want success", spec.values, spec.filter, err)
139+
t.Errorf("runtime.PopulateQueryParameters(msg, %v, %v) failed with %v; want success", spec.values, spec.filter, err)
140140
continue
141141
}
142142
if got, want := msg, spec.want; !proto.Equal(got, want) {
@@ -241,8 +241,8 @@ type proto3Message struct {
241241
BoolValue bool `protobuf:"varint,8,opt,name=bool_value" json:"bool_value,omitempty"`
242242
StringValue string `protobuf:"bytes,9,opt,name=string_value" json:"string_value,omitempty"`
243243
RepeatedValue []string `protobuf:"bytes,10,rep,name=repeated_value" json:"repeated_value,omitempty"`
244-
EnumValue EnumValue `protobuf:"varint,11,opt,name=enum_value,json=enumValue,enum=api.EnumValue" json:"enum_value,omitempty"`
245-
RepeatedEnum []EnumValue `protobuf:"varint,12,rep,packed,name=repeated_enum,json=repeated_enum,enum=api.EnumValue" json:"repeated_enum,omitempty"`
244+
EnumValue EnumValue `protobuf:"varint,11,opt,name=enum_value,json=enumValue,enum=runtime_test_api.EnumValue" json:"enum_value,omitempty"`
245+
RepeatedEnum []EnumValue `protobuf:"varint,12,rep,packed,name=repeated_enum,json=repeated_enum,enum=runtime_test_api.EnumValue" json:"repeated_enum,omitempty"`
246246
TimestampValue *timestamp.Timestamp `protobuf:"bytes,11,opt,name=timestamp_value" json:"timestamp_value,omitempty"`
247247
}
248248

@@ -268,8 +268,8 @@ type proto2Message struct {
268268
BoolValue *bool `protobuf:"varint,8,opt,name=bool_value" json:"bool_value,omitempty"`
269269
StringValue *string `protobuf:"bytes,9,opt,name=string_value" json:"string_value,omitempty"`
270270
RepeatedValue []string `protobuf:"bytes,10,rep,name=repeated_value" json:"repeated_value,omitempty"`
271-
EnumValue EnumValue `protobuf:"varint,11,opt,name=enum_value,json=enumValue,enum=api.EnumValue" json:"enum_value,omitempty"`
272-
RepeatedEnum []EnumValue `protobuf:"varint,12,rep,packed,name=repeated_enum,json=repeated_enum,enum=api.EnumValue" json:"repeated_enum,omitempty"`
271+
EnumValue EnumValue `protobuf:"varint,11,opt,name=enum_value,json=enumValue,enum=runtime_test_api.EnumValue" json:"enum_value,omitempty"`
272+
RepeatedEnum []EnumValue `protobuf:"varint,12,rep,packed,name=repeated_enum,json=repeated_enum,enum=runtime_test_api.EnumValue" json:"repeated_enum,omitempty"`
273273
XXX_unrecognized []byte `json:"-"`
274274
}
275275

@@ -367,5 +367,5 @@ var EnumValue_value = map[string]int32{
367367
}
368368

369369
func init() {
370-
proto.RegisterEnum("runtime_test.EnumValue", EnumValue_name, EnumValue_value)
370+
proto.RegisterEnum("runtime_test_api.EnumValue", EnumValue_name, EnumValue_value)
371371
}

0 commit comments

Comments
 (0)