本文整理汇总了Golang中github.com/grpc-ecosystem/grpc-gateway/runtime.JSONPb.NewEncoder方法的典型用法代码示例。如果您正苦于以下问题:Golang JSONPb.NewEncoder方法的具体用法?Golang JSONPb.NewEncoder怎么用?Golang JSONPb.NewEncoder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/grpc-ecosystem/grpc-gateway/runtime.JSONPb
的用法示例。
在下文中一共展示了JSONPb.NewEncoder方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestJSONPbEncoderFields
func TestJSONPbEncoderFields(t *testing.T) {
var m runtime.JSONPb
for _, fixt := range fieldFixtures {
var buf bytes.Buffer
enc := m.NewEncoder(&buf)
if err := enc.Encode(fixt.data); err != nil {
t.Errorf("enc.Encode(%#v) failed with %v; want success", fixt.data, err)
}
if got, want := buf.String(), fixt.json; got != want {
t.Errorf("enc.Encode(%#v) = %q; want %q", fixt.data, got, want)
}
}
m.EnumsAsInts = true
buf, err := m.Marshal(examplepb.NumericEnum_ONE)
if err != nil {
t.Errorf("m.Marshal(%#v) failed with %v; want success", examplepb.NumericEnum_ONE, err)
}
if got, want := string(buf), "1"; got != want {
t.Errorf("m.Marshal(%#v) = %q; want %q", examplepb.NumericEnum_ONE, got, want)
}
}
示例2: TestJSONPbEncoder
func TestJSONPbEncoder(t *testing.T) {
msg := examplepb.ABitOfEverything{
Uuid: "6EC2446F-7E89-4127-B3E6-5C05E6BECBA7",
Nested: []*examplepb.ABitOfEverything_Nested{
{
Name: "foo",
Amount: 12345,
},
},
Uint64Value: 0xFFFFFFFFFFFFFFFF,
OneofValue: &examplepb.ABitOfEverything_OneofString{
OneofString: "bar",
},
MapValue: map[string]examplepb.NumericEnum{
"a": examplepb.NumericEnum_ONE,
"b": examplepb.NumericEnum_ZERO,
},
}
for _, spec := range []struct {
enumsAsInts, emitDefaults bool
indent string
origName bool
verifier func(json string)
}{
{
verifier: func(json string) {
if strings.ContainsAny(json, " \t\r\n") {
t.Errorf("strings.ContainsAny(%q, %q) = true; want false", json, " \t\r\n")
}
if strings.Contains(json, "ONE") {
t.Errorf(`strings.Contains(%q, "ONE") = true; want false`, json)
}
if want := "uint64Value"; !strings.Contains(json, want) {
t.Errorf(`strings.Contains(%q, %q) = false; want true`, json, want)
}
},
},
{
enumsAsInts: true,
verifier: func(json string) {
if strings.Contains(json, "ONE") {
t.Errorf(`strings.Contains(%q, "ONE") = true; want false`, json)
}
},
},
{
emitDefaults: true,
verifier: func(json string) {
if want := `"sfixed32Value"`; !strings.Contains(json, want) {
t.Errorf(`strings.Contains(%q, %q) = false; want true`, json, want)
}
},
},
{
indent: "\t\t",
verifier: func(json string) {
if want := "\t\t\"amount\":"; !strings.Contains(json, want) {
t.Errorf(`strings.Contains(%q, %q) = false; want true`, json, want)
}
},
},
{
origName: true,
verifier: func(json string) {
if want := "uint64_value"; !strings.Contains(json, want) {
t.Errorf(`strings.Contains(%q, %q) = false; want true`, json, want)
}
},
},
} {
m := runtime.JSONPb{
EnumsAsInts: spec.enumsAsInts,
EmitDefaults: spec.emitDefaults,
Indent: spec.indent,
OrigName: spec.origName,
}
var buf bytes.Buffer
enc := m.NewEncoder(&buf)
if err := enc.Encode(&msg); err != nil {
t.Errorf("enc.Encode(%v) failed with %v; want success; spec=%v", &msg, err, spec)
}
var got examplepb.ABitOfEverything
if err := jsonpb.UnmarshalString(buf.String(), &got); err != nil {
t.Errorf("jsonpb.UnmarshalString(%q, &got) failed with %v; want success; spec=%v", buf.String(), err, spec)
}
if want := msg; !reflect.DeepEqual(got, want) {
t.Errorf("got = %v; want %v; spec=%v", &got, &want, spec)
}
if spec.verifier != nil {
spec.verifier(buf.String())
}
}
}