From fe4e86261dd2e6f874c47a77fe285edba5221dd8 Mon Sep 17 00:00:00 2001 From: Yuki Yugui Sonoda Date: Thu, 28 Jan 2016 12:31:22 +0900 Subject: [PATCH] Avoid converting proto <-> golang field names at runtime Use the metadata proto.GetProperty returns instead. Since the conversion rule is getting complicated, it is hard to emulate the rule. c.f. https://github.com/gengo/grpc-gateway/issues/84 --- runtime/query.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/runtime/query.go b/runtime/query.go index 82b07886ca3..8b81679cdf8 100644 --- a/runtime/query.go +++ b/runtime/query.go @@ -12,7 +12,7 @@ import ( ) // PopulateQueryParameters populates "values" into "msg". -// A value is ignored if its key starts with one of the elements in "filters". +// A value is ignored if its key starts with one of the elements in "filter". func PopulateQueryParameters(msg proto.Message, values url.Values, filter *utilities.DoubleArray) error { for key, values := range values { fieldPath := strings.Split(key, ".") @@ -44,7 +44,7 @@ func populateFieldValueFromPath(msg proto.Message, fieldPath []string, values [] if !isLast && m.Kind() != reflect.Struct { return fmt.Errorf("non-aggregate type in the mid of path: %s", strings.Join(fieldPath, ".")) } - f := m.FieldByName(utilities.PascalFromSnake(fieldName)) + f := fieldByProtoName(m, fieldName) if !f.IsValid() { glog.Warningf("field not found in %T: %s", msg, strings.Join(fieldPath, ".")) return nil @@ -83,6 +83,18 @@ func populateFieldValueFromPath(msg proto.Message, fieldPath []string, values [] return populateField(m, values[0]) } +// fieldByProtoName looks up a field whose of "m" corresponding protobuf field name is "name". +// "m" must be a struct value. It returns zero reflect.Value if no such field found. +func fieldByProtoName(m reflect.Value, name string) reflect.Value { + props := proto.GetProperties(m.Type()) + for _, p := range props.Prop { + if p.OrigName == name { + return m.FieldByName(p.Name) + } + } + return reflect.Value{} +} + func populateRepeatedField(f reflect.Value, values []string) error { elemType := f.Type().Elem() conv, ok := convFromType[elemType.Kind()]