Skip to content

Commit

Permalink
Avoid converting proto <-> golang field names at runtime
Browse files Browse the repository at this point in the history
Use the metadata proto.GetProperty returns instead.

Since the conversion rule is getting complicated, it is hard to emulate
the rule. c.f. #85
  • Loading branch information
yugui committed Jan 28, 2016
1 parent 8b010c0 commit d70bef7
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions runtime/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ".")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -83,6 +83,18 @@ func populateFieldValueFromPath(msg proto.Message, fieldPath []string, values []
return populateField(m, values[0])
}

// fieldByProtoName looks up a field whose 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()]
Expand Down

0 comments on commit d70bef7

Please sign in to comment.