本文整理汇总了Golang中github.com/grpc-ecosystem/grpc-gateway/runtime.JSONPb.NewDecoder方法的典型用法代码示例。如果您正苦于以下问题:Golang JSONPb.NewDecoder方法的具体用法?Golang JSONPb.NewDecoder怎么用?Golang JSONPb.NewDecoder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/grpc-ecosystem/grpc-gateway/runtime.JSONPb
的用法示例。
在下文中一共展示了JSONPb.NewDecoder方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: StreamJSON
// StreamJSON uses the supplied client to POST the given proto request as JSON
// to the supplied streaming grpc-gw endpoint; the response type serves only to
// create the values passed to the callback (which is invoked for every message
// in the stream with a value of the supplied response type masqueraded as an
// interface).
func StreamJSON(
httpClient http.Client,
path string,
request proto.Message,
dest proto.Message,
callback func(proto.Message),
) error {
str, err := (&jsonpb.Marshaler{}).MarshalToString(request)
if err != nil {
return err
}
resp, err := httpClient.Post(path, JSONContentType, strings.NewReader(str))
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
b, err := ioutil.ReadAll(resp.Body)
return errors.Errorf("status: %s, body: %s, error: %s", resp.Status, b, err)
}
// grpc-gw/runtime's JSONpb {en,de}coder is pretty half-baked. Essentially
// we must pass a *map[string]*concreteType or it won't work (in
// particular, using a struct or replacing `*concreteType` with either
// `concreteType` or `proto.Message` leads to errors). This method should do
// a decent enough job at encapsulating this away; should this change, we
// should consider cribbing and fixing up the marshaling code.
m := reflect.New(reflect.MapOf(reflect.TypeOf(""), reflect.TypeOf(dest)))
// TODO(tschottdorf,tamird): We have cribbed parts of this object to deal
// with varying proto imports, and should technically use them here. We can
// get away with not cribbing more here for now though.
marshaler := runtime.JSONPb{}
decoder := marshaler.NewDecoder(resp.Body)
for {
if err := decoder.Decode(m.Interface()); err == io.EOF {
break
} else if err != nil {
return err
}
v := m.Elem().MapIndex(reflect.ValueOf("result"))
if !v.IsValid() {
// TODO(tschottdorf): recover actual JSON.
return errors.Errorf("unexpected JSON response: %+v", m)
}
callback(v.Interface().(proto.Message))
}
return nil
}
示例2: TestJSONPbDecoderFields
func TestJSONPbDecoderFields(t *testing.T) {
var m runtime.JSONPb
for _, fixt := range fieldFixtures {
if fixt.skipUnmarshal {
continue
}
dest := reflect.New(reflect.TypeOf(fixt.data))
dec := m.NewDecoder(strings.NewReader(fixt.json))
if err := dec.Decode(dest.Interface()); err != nil {
t.Errorf("dec.Decode(%T) failed with %v; want success; input = %q", dest.Interface(), err, fixt.json)
}
if got, want := dest.Elem().Interface(), fixt.data; !reflect.DeepEqual(got, want) {
t.Errorf("dest = %#v; want %#v; input = %v", got, want, fixt.json)
}
}
}
示例3: TestJSONPbDecoder
func TestJSONPbDecoder(t *testing.T) {
var (
m runtime.JSONPb
got examplepb.ABitOfEverything
)
for _, data := range []string{
`{
"uuid": "6EC2446F-7E89-4127-B3E6-5C05E6BECBA7",
"nested": [
{"name": "foo", "amount": 12345}
],
"uint64Value": 18446744073709551615,
"enumValue": "ONE",
"oneofString": "bar",
"mapValue": {
"a": 1,
"b": 0
}
}`,
`{
"uuid": "6EC2446F-7E89-4127-B3E6-5C05E6BECBA7",
"nested": [
{"name": "foo", "amount": 12345}
],
"uint64Value": "18446744073709551615",
"enumValue": "ONE",
"oneofString": "bar",
"mapValue": {
"a": 1,
"b": 0
}
}`,
`{
"uuid": "6EC2446F-7E89-4127-B3E6-5C05E6BECBA7",
"nested": [
{"name": "foo", "amount": 12345}
],
"uint64Value": 18446744073709551615,
"enumValue": 1,
"oneofString": "bar",
"mapValue": {
"a": 1,
"b": 0
}
}`,
} {
r := strings.NewReader(data)
dec := m.NewDecoder(r)
if err := dec.Decode(&got); err != nil {
t.Errorf("m.Unmarshal(&got) failed with %v; want success; data=%q", err, data)
}
want := examplepb.ABitOfEverything{
Uuid: "6EC2446F-7E89-4127-B3E6-5C05E6BECBA7",
Nested: []*examplepb.ABitOfEverything_Nested{
{
Name: "foo",
Amount: 12345,
},
},
Uint64Value: 0xFFFFFFFFFFFFFFFF,
EnumValue: examplepb.NumericEnum_ONE,
OneofValue: &examplepb.ABitOfEverything_OneofString{
OneofString: "bar",
},
MapValue: map[string]examplepb.NumericEnum{
"a": examplepb.NumericEnum_ONE,
"b": examplepb.NumericEnum_ZERO,
},
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got = %v; want = %v; data = %v", &got, &want, data)
}
}
}