Example #1
0
// 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 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 Errorf("unexpected JSON response: %+v", m)
		}
		callback(v.Interface().(proto.Message))
	}
	return nil
}
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)
		}
	}
}
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)
		}
	}
}