Skip to content

Commit

Permalink
Make grpc-gateway support enum fields in path parameter; repeated enu…
Browse files Browse the repository at this point in the history
…m fields are also supported.
  • Loading branch information
linuxerwang committed Nov 30, 2016
1 parent 199c40a commit b53e05a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,15 @@ To use the same port for custom HTTP handlers (e.g. serving `swagger.json`), gRP
* Method parameters in request body
* Method parameters in request path
* Method parameters in query string
* Enum fields in path parameter (including repeated enum fields).
* Mapping streaming APIs to newline-delimited JSON streams
* Mapping HTTP headers with `Grpc-Metadata-` prefix to gRPC metadata
* Optionally emitting API definition for [Swagger](http://swagger.io).
* Setting [gRPC timeouts](http://www.grpc.io/docs/guides/wire.html) through inbound HTTP `Grpc-Timeout` header.

### Want to support
But not yet.
* bytes and enum fields in path parameter. #5
* bytes fields in path parameter. #5
* Optionally generating the entrypoint. #8
* `import_path` parameter

Expand Down
8 changes: 4 additions & 4 deletions runtime/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func populateFieldValueFromPath(msg proto.Message, fieldPath []string, values []
case reflect.Ptr:
if f.IsNil() {
m = reflect.New(f.Type().Elem())
f.Set(m)
f.Set(m.Convert(f.Type()))
}
m = f.Elem()
continue
Expand Down Expand Up @@ -101,13 +101,13 @@ func populateRepeatedField(f reflect.Value, values []string) error {
if !ok {
return fmt.Errorf("unsupported field type %s", elemType)
}
f.Set(reflect.MakeSlice(f.Type(), len(values), len(values)))
f.Set(reflect.MakeSlice(f.Type(), len(values), len(values)).Convert(f.Type()))
for i, v := range values {
result := conv.Call([]reflect.Value{reflect.ValueOf(v)})
if err := result[1].Interface(); err != nil {
return err.(error)
}
f.Index(i).Set(result[0])
f.Index(i).Set(result[0].Convert(f.Index(i).Type()))
}
return nil
}
Expand All @@ -121,7 +121,7 @@ func populateField(f reflect.Value, value string) error {
if err := result[1].Interface(); err != nil {
return err.(error)
}
f.Set(result[0])
f.Set(result[0].Convert(f.Type()))
return nil
}

Expand Down
20 changes: 20 additions & 0 deletions runtime/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func TestPopulateParameters(t *testing.T) {
"bool_value": {"true"},
"string_value": {"str"},
"repeated_value": {"a", "b", "c"},
"enum_value": {"1"},
"repeated_enum": {"1", "2", "0"},
},
filter: utilities.NewDoubleArray(nil),
want: &proto3Message{
Expand All @@ -38,6 +40,8 @@ func TestPopulateParameters(t *testing.T) {
BoolValue: true,
StringValue: "str",
RepeatedValue: []string{"a", "b", "c"},
EnumValue: EnumValue_Y,
RepeatedEnum: []EnumValue{EnumValue_Y, EnumValue_Z, EnumValue_X},
},
},
{
Expand All @@ -51,6 +55,8 @@ func TestPopulateParameters(t *testing.T) {
"bool_value": {"true"},
"string_value": {"str"},
"repeated_value": {"a", "b", "c"},
"enum_value": {"1"},
"repeated_enum": {"1", "2", "0"},
},
filter: utilities.NewDoubleArray(nil),
want: &proto2Message{
Expand All @@ -63,6 +69,8 @@ func TestPopulateParameters(t *testing.T) {
BoolValue: proto.Bool(true),
StringValue: proto.String("str"),
RepeatedValue: []string{"a", "b", "c"},
EnumValue: EnumValue_Y,
RepeatedEnum: []EnumValue{EnumValue_Y, EnumValue_Z, EnumValue_X},
},
},
{
Expand Down Expand Up @@ -209,6 +217,8 @@ type proto3Message struct {
BoolValue bool `protobuf:"varint,8,opt,name=bool_value" json:"bool_value,omitempty"`
StringValue string `protobuf:"bytes,9,opt,name=string_value" json:"string_value,omitempty"`
RepeatedValue []string `protobuf:"bytes,10,rep,name=repeated_value" json:"repeated_value,omitempty"`
EnumValue EnumValue `protobuf:"varint,11,opt,name=enum_value,json=enumValue,enum=api.EnumValue" json:"enum_value,omitempty"`
RepeatedEnum []EnumValue `protobuf:"varint,12,rep,packed,name=repeated_enum,json=repeated_enum,enum=api.EnumValue" json:"repeated_enum,omitempty"`
}

func (m *proto3Message) Reset() { *m = proto3Message{} }
Expand All @@ -233,6 +243,8 @@ type proto2Message struct {
BoolValue *bool `protobuf:"varint,8,opt,name=bool_value" json:"bool_value,omitempty"`
StringValue *string `protobuf:"bytes,9,opt,name=string_value" json:"string_value,omitempty"`
RepeatedValue []string `protobuf:"bytes,10,rep,name=repeated_value" json:"repeated_value,omitempty"`
EnumValue EnumValue `protobuf:"varint,11,opt,name=enum_value,json=enumValue,enum=api.EnumValue" json:"enum_value,omitempty"`
RepeatedEnum []EnumValue `protobuf:"varint,12,rep,packed,name=repeated_enum,json=repeated_enum,enum=api.EnumValue" json:"repeated_enum,omitempty"`
XXX_unrecognized []byte `json:"-"`
}

Expand Down Expand Up @@ -309,3 +321,11 @@ func (m *proto2Message) GetRepeatedValue() []string {
}
return nil
}

type EnumValue int32

const (
EnumValue_X EnumValue = 0
EnumValue_Y EnumValue = 1
EnumValue_Z EnumValue = 2
)

0 comments on commit b53e05a

Please sign in to comment.