vendor: update all dependencies to latest versions
This commit is contained in:
parent
911d121bb9
commit
b017fcfe9a
3048 changed files with 537057 additions and 189681 deletions
170
vendor/github.com/golang/protobuf/jsonpb/jsonpb.go
generated
vendored
170
vendor/github.com/golang/protobuf/jsonpb/jsonpb.go
generated
vendored
|
@ -73,6 +73,31 @@ type Marshaler struct {
|
|||
|
||||
// Whether to use the original (.proto) name for fields.
|
||||
OrigName bool
|
||||
|
||||
// A custom URL resolver to use when marshaling Any messages to JSON.
|
||||
// If unset, the default resolution strategy is to extract the
|
||||
// fully-qualified type name from the type URL and pass that to
|
||||
// proto.MessageType(string).
|
||||
AnyResolver AnyResolver
|
||||
}
|
||||
|
||||
// AnyResolver takes a type URL, present in an Any message, and resolves it into
|
||||
// an instance of the associated message.
|
||||
type AnyResolver interface {
|
||||
Resolve(typeUrl string) (proto.Message, error)
|
||||
}
|
||||
|
||||
func defaultResolveAny(typeUrl string) (proto.Message, error) {
|
||||
// Only the part of typeUrl after the last slash is relevant.
|
||||
mname := typeUrl
|
||||
if slash := strings.LastIndex(mname, "/"); slash >= 0 {
|
||||
mname = mname[slash+1:]
|
||||
}
|
||||
mt := proto.MessageType(mname)
|
||||
if mt == nil {
|
||||
return nil, fmt.Errorf("unknown message type %q", mname)
|
||||
}
|
||||
return reflect.New(mt.Elem()).Interface().(proto.Message), nil
|
||||
}
|
||||
|
||||
// JSONPBMarshaler is implemented by protobuf messages that customize the
|
||||
|
@ -344,16 +369,17 @@ func (m *Marshaler) marshalAny(out *errWriter, any proto.Message, indent string)
|
|||
turl := v.Field(0).String()
|
||||
val := v.Field(1).Bytes()
|
||||
|
||||
// Only the part of type_url after the last slash is relevant.
|
||||
mname := turl
|
||||
if slash := strings.LastIndex(mname, "/"); slash >= 0 {
|
||||
mname = mname[slash+1:]
|
||||
var msg proto.Message
|
||||
var err error
|
||||
if m.AnyResolver != nil {
|
||||
msg, err = m.AnyResolver.Resolve(turl)
|
||||
} else {
|
||||
msg, err = defaultResolveAny(turl)
|
||||
}
|
||||
mt := proto.MessageType(mname)
|
||||
if mt == nil {
|
||||
return fmt.Errorf("unknown message type %q", mname)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
msg := reflect.New(mt.Elem()).Interface().(proto.Message)
|
||||
|
||||
if err := proto.Unmarshal(val, msg); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -590,6 +616,12 @@ type Unmarshaler struct {
|
|||
// Whether to allow messages to contain unknown fields, as opposed to
|
||||
// failing to unmarshal.
|
||||
AllowUnknownFields bool
|
||||
|
||||
// A custom URL resolver to use when unmarshaling Any messages from JSON.
|
||||
// If unset, the default resolution strategy is to extract the
|
||||
// fully-qualified type name from the type URL and pass that to
|
||||
// proto.MessageType(string).
|
||||
AnyResolver AnyResolver
|
||||
}
|
||||
|
||||
// UnmarshalNext unmarshals the next protocol buffer from a JSON object stream.
|
||||
|
@ -639,7 +671,14 @@ func (u *Unmarshaler) unmarshalValue(target reflect.Value, inputValue json.RawMe
|
|||
|
||||
// Allocate memory for pointer fields.
|
||||
if targetType.Kind() == reflect.Ptr {
|
||||
// If input value is "null" and target is a pointer type, then the field should be treated as not set
|
||||
// UNLESS the target is structpb.Value, in which case it should be set to structpb.NullValue.
|
||||
_, isJSONPBUnmarshaler := target.Interface().(JSONPBUnmarshaler)
|
||||
if string(inputValue) == "null" && targetType != reflect.TypeOf(&stpb.Value{}) && !isJSONPBUnmarshaler {
|
||||
return nil
|
||||
}
|
||||
target.Set(reflect.New(targetType.Elem()))
|
||||
|
||||
return u.unmarshalValue(target.Elem(), inputValue, prop)
|
||||
}
|
||||
|
||||
|
@ -647,15 +686,11 @@ func (u *Unmarshaler) unmarshalValue(target reflect.Value, inputValue json.RawMe
|
|||
return jsu.UnmarshalJSONPB(u, []byte(inputValue))
|
||||
}
|
||||
|
||||
// Handle well-known types.
|
||||
// Handle well-known types that are not pointers.
|
||||
if w, ok := target.Addr().Interface().(wkt); ok {
|
||||
switch w.XXX_WellKnownType() {
|
||||
case "DoubleValue", "FloatValue", "Int64Value", "UInt64Value",
|
||||
"Int32Value", "UInt32Value", "BoolValue", "StringValue", "BytesValue":
|
||||
// "Wrappers use the same representation in JSON
|
||||
// as the wrapped primitive type, except that null is allowed."
|
||||
// encoding/json will turn JSON `null` into Go `nil`,
|
||||
// so we don't have to do any extra work.
|
||||
return u.unmarshalValue(target.Field(0), inputValue, prop)
|
||||
case "Any":
|
||||
// Use json.RawMessage pointer type instead of value to support pre-1.8 version.
|
||||
|
@ -677,16 +712,17 @@ func (u *Unmarshaler) unmarshalValue(target reflect.Value, inputValue json.RawMe
|
|||
}
|
||||
target.Field(0).SetString(turl)
|
||||
|
||||
mname := turl
|
||||
if slash := strings.LastIndex(mname, "/"); slash >= 0 {
|
||||
mname = mname[slash+1:]
|
||||
var m proto.Message
|
||||
var err error
|
||||
if u.AnyResolver != nil {
|
||||
m, err = u.AnyResolver.Resolve(turl)
|
||||
} else {
|
||||
m, err = defaultResolveAny(turl)
|
||||
}
|
||||
mt := proto.MessageType(mname)
|
||||
if mt == nil {
|
||||
return fmt.Errorf("unknown message type %q", mname)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m := reflect.New(mt.Elem()).Interface().(proto.Message)
|
||||
if _, ok := m.(wkt); ok {
|
||||
val, ok := jsonFields["value"]
|
||||
if !ok {
|
||||
|
@ -716,21 +752,16 @@ func (u *Unmarshaler) unmarshalValue(target reflect.Value, inputValue json.RawMe
|
|||
|
||||
return nil
|
||||
case "Duration":
|
||||
ivStr := string(inputValue)
|
||||
if ivStr == "null" {
|
||||
target.Field(0).SetInt(0)
|
||||
target.Field(1).SetInt(0)
|
||||
return nil
|
||||
}
|
||||
|
||||
unq, err := strconv.Unquote(ivStr)
|
||||
unq, err := strconv.Unquote(string(inputValue))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d, err := time.ParseDuration(unq)
|
||||
if err != nil {
|
||||
return fmt.Errorf("bad Duration: %v", err)
|
||||
}
|
||||
|
||||
ns := d.Nanoseconds()
|
||||
s := ns / 1e9
|
||||
ns %= 1e9
|
||||
|
@ -738,33 +769,25 @@ func (u *Unmarshaler) unmarshalValue(target reflect.Value, inputValue json.RawMe
|
|||
target.Field(1).SetInt(ns)
|
||||
return nil
|
||||
case "Timestamp":
|
||||
ivStr := string(inputValue)
|
||||
if ivStr == "null" {
|
||||
target.Field(0).SetInt(0)
|
||||
target.Field(1).SetInt(0)
|
||||
return nil
|
||||
}
|
||||
|
||||
unq, err := strconv.Unquote(ivStr)
|
||||
unq, err := strconv.Unquote(string(inputValue))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
t, err := time.Parse(time.RFC3339Nano, unq)
|
||||
if err != nil {
|
||||
return fmt.Errorf("bad Timestamp: %v", err)
|
||||
}
|
||||
|
||||
target.Field(0).SetInt(t.Unix())
|
||||
target.Field(1).SetInt(int64(t.Nanosecond()))
|
||||
return nil
|
||||
case "Struct":
|
||||
if string(inputValue) == "null" {
|
||||
// Interpret a null struct as empty.
|
||||
return nil
|
||||
}
|
||||
var m map[string]json.RawMessage
|
||||
if err := json.Unmarshal(inputValue, &m); err != nil {
|
||||
return fmt.Errorf("bad StructValue: %v", err)
|
||||
}
|
||||
|
||||
target.Field(0).Set(reflect.ValueOf(map[string]*stpb.Value{}))
|
||||
for k, jv := range m {
|
||||
pv := &stpb.Value{}
|
||||
|
@ -775,14 +798,11 @@ func (u *Unmarshaler) unmarshalValue(target reflect.Value, inputValue json.RawMe
|
|||
}
|
||||
return nil
|
||||
case "ListValue":
|
||||
if string(inputValue) == "null" {
|
||||
// Interpret a null ListValue as empty.
|
||||
return nil
|
||||
}
|
||||
var s []json.RawMessage
|
||||
if err := json.Unmarshal(inputValue, &s); err != nil {
|
||||
return fmt.Errorf("bad ListValue: %v", err)
|
||||
}
|
||||
|
||||
target.Field(0).Set(reflect.ValueOf(make([]*stpb.Value, len(s), len(s))))
|
||||
for i, sv := range s {
|
||||
if err := u.unmarshalValue(target.Field(0).Index(i), sv, prop); err != nil {
|
||||
|
@ -933,11 +953,13 @@ func (u *Unmarshaler) unmarshalValue(target reflect.Value, inputValue json.RawMe
|
|||
if err := json.Unmarshal(inputValue, &slc); err != nil {
|
||||
return err
|
||||
}
|
||||
len := len(slc)
|
||||
target.Set(reflect.MakeSlice(targetType, len, len))
|
||||
for i := 0; i < len; i++ {
|
||||
if err := u.unmarshalValue(target.Index(i), slc[i], prop); err != nil {
|
||||
return err
|
||||
if slc != nil {
|
||||
l := len(slc)
|
||||
target.Set(reflect.MakeSlice(targetType, l, l))
|
||||
for i := 0; i < l; i++ {
|
||||
if err := u.unmarshalValue(target.Index(i), slc[i], prop); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
@ -949,33 +971,35 @@ func (u *Unmarshaler) unmarshalValue(target reflect.Value, inputValue json.RawMe
|
|||
if err := json.Unmarshal(inputValue, &mp); err != nil {
|
||||
return err
|
||||
}
|
||||
target.Set(reflect.MakeMap(targetType))
|
||||
var keyprop, valprop *proto.Properties
|
||||
if prop != nil {
|
||||
// These could still be nil if the protobuf metadata is broken somehow.
|
||||
// TODO: This won't work because the fields are unexported.
|
||||
// We should probably just reparse them.
|
||||
//keyprop, valprop = prop.mkeyprop, prop.mvalprop
|
||||
}
|
||||
for ks, raw := range mp {
|
||||
// Unmarshal map key. The core json library already decoded the key into a
|
||||
// string, so we handle that specially. Other types were quoted post-serialization.
|
||||
var k reflect.Value
|
||||
if targetType.Key().Kind() == reflect.String {
|
||||
k = reflect.ValueOf(ks)
|
||||
} else {
|
||||
k = reflect.New(targetType.Key()).Elem()
|
||||
if err := u.unmarshalValue(k, json.RawMessage(ks), keyprop); err != nil {
|
||||
if mp != nil {
|
||||
target.Set(reflect.MakeMap(targetType))
|
||||
var keyprop, valprop *proto.Properties
|
||||
if prop != nil {
|
||||
// These could still be nil if the protobuf metadata is broken somehow.
|
||||
// TODO: This won't work because the fields are unexported.
|
||||
// We should probably just reparse them.
|
||||
//keyprop, valprop = prop.mkeyprop, prop.mvalprop
|
||||
}
|
||||
for ks, raw := range mp {
|
||||
// Unmarshal map key. The core json library already decoded the key into a
|
||||
// string, so we handle that specially. Other types were quoted post-serialization.
|
||||
var k reflect.Value
|
||||
if targetType.Key().Kind() == reflect.String {
|
||||
k = reflect.ValueOf(ks)
|
||||
} else {
|
||||
k = reflect.New(targetType.Key()).Elem()
|
||||
if err := u.unmarshalValue(k, json.RawMessage(ks), keyprop); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Unmarshal map value.
|
||||
v := reflect.New(targetType.Elem()).Elem()
|
||||
if err := u.unmarshalValue(v, raw, valprop); err != nil {
|
||||
return err
|
||||
}
|
||||
target.SetMapIndex(k, v)
|
||||
}
|
||||
|
||||
// Unmarshal map value.
|
||||
v := reflect.New(targetType.Elem()).Elem()
|
||||
if err := u.unmarshalValue(v, raw, valprop); err != nil {
|
||||
return err
|
||||
}
|
||||
target.SetMapIndex(k, v)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
161
vendor/github.com/golang/protobuf/jsonpb/jsonpb_test.go
generated
vendored
161
vendor/github.com/golang/protobuf/jsonpb/jsonpb_test.go
generated
vendored
|
@ -379,9 +379,9 @@ var marshalingTests = []struct {
|
|||
&pb.Mappy{Strry: map[string]string{`"one"`: "two", "three": "four"}},
|
||||
`{"strry":{"\"one\"":"two","three":"four"}}`},
|
||||
{"map<int32, Object>", marshaler,
|
||||
&pb.Mappy{Objjy: map[int32]*pb.Simple3{1: &pb.Simple3{Dub: 1}}}, `{"objjy":{"1":{"dub":1}}}`},
|
||||
&pb.Mappy{Objjy: map[int32]*pb.Simple3{1: {Dub: 1}}}, `{"objjy":{"1":{"dub":1}}}`},
|
||||
{"map<int32, Object>", marshalerAllOptions,
|
||||
&pb.Mappy{Objjy: map[int32]*pb.Simple3{1: &pb.Simple3{Dub: 1}}}, objjyPrettyJSON},
|
||||
&pb.Mappy{Objjy: map[int32]*pb.Simple3{1: {Dub: 1}}}, objjyPrettyJSON},
|
||||
{"map<int64, string>", marshaler, &pb.Mappy{Buggy: map[int64]string{1234: "yup"}},
|
||||
`{"buggy":{"1234":"yup"}}`},
|
||||
{"map<bool, bool>", marshaler, &pb.Mappy{Booly: map[bool]bool{false: true}}, `{"booly":{"false":true}}`},
|
||||
|
@ -395,7 +395,7 @@ var marshalingTests = []struct {
|
|||
{"proto2 map<int64, string>", marshaler, &pb.Maps{MInt64Str: map[int64]string{213: "cat"}},
|
||||
`{"mInt64Str":{"213":"cat"}}`},
|
||||
{"proto2 map<bool, Object>", marshaler,
|
||||
&pb.Maps{MBoolSimple: map[bool]*pb.Simple{true: &pb.Simple{OInt32: proto.Int32(1)}}},
|
||||
&pb.Maps{MBoolSimple: map[bool]*pb.Simple{true: {OInt32: proto.Int32(1)}}},
|
||||
`{"mBoolSimple":{"true":{"oInt32":1}}}`},
|
||||
{"oneof, not set", marshaler, &pb.MsgWithOneof{}, `{}`},
|
||||
{"oneof, set", marshaler, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_Title{"Grand Poobah"}}, `{"title":"Grand Poobah"}`},
|
||||
|
@ -486,7 +486,7 @@ func TestMarshalAnyJSONPBMarshaler(t *testing.T) {
|
|||
}
|
||||
// after custom marshaling, it's round-tripped through JSON decoding/encoding already,
|
||||
// so the keys are sorted, whitespace is compacted, and "@type" key has been added
|
||||
expected := `{"@type":"type.googleapis.com/` + dynamicMessageName +`","baz":[0,1,2,3],"foo":"bar"}`
|
||||
expected := `{"@type":"type.googleapis.com/` + dynamicMessageName + `","baz":[0,1,2,3],"foo":"bar"}`
|
||||
if str != expected {
|
||||
t.Errorf("marshalling JSON produced incorrect output: got %s, wanted %s", str, expected)
|
||||
}
|
||||
|
@ -535,7 +535,7 @@ var unmarshalingTests = []struct {
|
|||
{"-Inf", Unmarshaler{}, `{"oDouble":"-Infinity"}`, &pb.Simple{ODouble: proto.Float64(math.Inf(-1))}},
|
||||
{"map<int64, int32>", Unmarshaler{}, `{"nummy":{"1":2,"3":4}}`, &pb.Mappy{Nummy: map[int64]int32{1: 2, 3: 4}}},
|
||||
{"map<string, string>", Unmarshaler{}, `{"strry":{"\"one\"":"two","three":"four"}}`, &pb.Mappy{Strry: map[string]string{`"one"`: "two", "three": "four"}}},
|
||||
{"map<int32, Object>", Unmarshaler{}, `{"objjy":{"1":{"dub":1}}}`, &pb.Mappy{Objjy: map[int32]*pb.Simple3{1: &pb.Simple3{Dub: 1}}}},
|
||||
{"map<int32, Object>", Unmarshaler{}, `{"objjy":{"1":{"dub":1}}}`, &pb.Mappy{Objjy: map[int32]*pb.Simple3{1: {Dub: 1}}}},
|
||||
{"proto2 extension", Unmarshaler{}, realNumberJSON, realNumber},
|
||||
{"Any with message", Unmarshaler{}, anySimpleJSON, anySimple},
|
||||
{"Any with message and indent", Unmarshaler{}, anySimplePrettyJSON, anySimple},
|
||||
|
@ -553,12 +553,12 @@ var unmarshalingTests = []struct {
|
|||
{"camelName input", Unmarshaler{}, `{"oBool":true}`, &pb.Simple{OBool: proto.Bool(true)}},
|
||||
|
||||
{"Duration", Unmarshaler{}, `{"dur":"3.000s"}`, &pb.KnownTypes{Dur: &durpb.Duration{Seconds: 3}}},
|
||||
{"null Duration", Unmarshaler{}, `{"dur":null}`, &pb.KnownTypes{Dur: &durpb.Duration{Seconds: 0}}},
|
||||
{"null Duration", Unmarshaler{}, `{"dur":null}`, &pb.KnownTypes{Dur: nil}},
|
||||
{"Timestamp", Unmarshaler{}, `{"ts":"2014-05-13T16:53:20.021Z"}`, &pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: 14e8, Nanos: 21e6}}},
|
||||
{"PreEpochTimestamp", Unmarshaler{}, `{"ts":"1969-12-31T23:59:58.999999995Z"}`, &pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: -2, Nanos: 999999995}}},
|
||||
{"ZeroTimeTimestamp", Unmarshaler{}, `{"ts":"0001-01-01T00:00:00Z"}`, &pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: -62135596800, Nanos: 0}}},
|
||||
{"null Timestamp", Unmarshaler{}, `{"ts":null}`, &pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: 0, Nanos: 0}}},
|
||||
{"null Struct", Unmarshaler{}, `{"st": null}`, &pb.KnownTypes{St: &stpb.Struct{}}},
|
||||
{"null Timestamp", Unmarshaler{}, `{"ts":null}`, &pb.KnownTypes{Ts: nil}},
|
||||
{"null Struct", Unmarshaler{}, `{"st": null}`, &pb.KnownTypes{St: nil}},
|
||||
{"empty Struct", Unmarshaler{}, `{"st": {}}`, &pb.KnownTypes{St: &stpb.Struct{}}},
|
||||
{"basic Struct", Unmarshaler{}, `{"st": {"a": "x", "b": null, "c": 3, "d": true}}`, &pb.KnownTypes{St: &stpb.Struct{Fields: map[string]*stpb.Value{
|
||||
"a": {Kind: &stpb.Value_StringValue{"x"}},
|
||||
|
@ -575,7 +575,7 @@ var unmarshalingTests = []struct {
|
|||
}}}},
|
||||
}}}},
|
||||
}}}},
|
||||
{"null ListValue", Unmarshaler{}, `{"lv": null}`, &pb.KnownTypes{Lv: &stpb.ListValue{}}},
|
||||
{"null ListValue", Unmarshaler{}, `{"lv": null}`, &pb.KnownTypes{Lv: nil}},
|
||||
{"empty ListValue", Unmarshaler{}, `{"lv": []}`, &pb.KnownTypes{Lv: &stpb.ListValue{}}},
|
||||
{"basic ListValue", Unmarshaler{}, `{"lv": ["x", null, 3, true]}`, &pb.KnownTypes{Lv: &stpb.ListValue{Values: []*stpb.Value{
|
||||
{Kind: &stpb.Value_StringValue{"x"}},
|
||||
|
@ -612,8 +612,17 @@ var unmarshalingTests = []struct {
|
|||
{"BoolValue", Unmarshaler{}, `{"bool":true}`, &pb.KnownTypes{Bool: &wpb.BoolValue{Value: true}}},
|
||||
{"StringValue", Unmarshaler{}, `{"str":"plush"}`, &pb.KnownTypes{Str: &wpb.StringValue{Value: "plush"}}},
|
||||
{"BytesValue", Unmarshaler{}, `{"bytes":"d293"}`, &pb.KnownTypes{Bytes: &wpb.BytesValue{Value: []byte("wow")}}},
|
||||
// `null` is also a permissible value. Let's just test one.
|
||||
{"null DoubleValue", Unmarshaler{}, `{"dbl":null}`, &pb.KnownTypes{Dbl: &wpb.DoubleValue{}}},
|
||||
|
||||
// Ensure that `null` as a value ends up with a nil pointer instead of a [type]Value struct.
|
||||
{"null DoubleValue", Unmarshaler{}, `{"dbl":null}`, &pb.KnownTypes{Dbl: nil}},
|
||||
{"null FloatValue", Unmarshaler{}, `{"flt":null}`, &pb.KnownTypes{Flt: nil}},
|
||||
{"null Int64Value", Unmarshaler{}, `{"i64":null}`, &pb.KnownTypes{I64: nil}},
|
||||
{"null UInt64Value", Unmarshaler{}, `{"u64":null}`, &pb.KnownTypes{U64: nil}},
|
||||
{"null Int32Value", Unmarshaler{}, `{"i32":null}`, &pb.KnownTypes{I32: nil}},
|
||||
{"null UInt32Value", Unmarshaler{}, `{"u32":null}`, &pb.KnownTypes{U32: nil}},
|
||||
{"null BoolValue", Unmarshaler{}, `{"bool":null}`, &pb.KnownTypes{Bool: nil}},
|
||||
{"null StringValue", Unmarshaler{}, `{"str":null}`, &pb.KnownTypes{Str: nil}},
|
||||
{"null BytesValue", Unmarshaler{}, `{"bytes":null}`, &pb.KnownTypes{Bytes: nil}},
|
||||
}
|
||||
|
||||
func TestUnmarshaling(t *testing.T) {
|
||||
|
@ -636,6 +645,26 @@ func TestUnmarshaling(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalNullArray(t *testing.T) {
|
||||
var repeats pb.Repeats
|
||||
if err := UnmarshalString(`{"rBool":null}`, &repeats); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(repeats, pb.Repeats{}) {
|
||||
t.Errorf("got non-nil fields in [%#v]", repeats)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalNullObject(t *testing.T) {
|
||||
var maps pb.Maps
|
||||
if err := UnmarshalString(`{"mInt64Str":null}`, &maps); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(maps, pb.Maps{}) {
|
||||
t.Errorf("got non-nil fields in [%#v]", maps)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalNext(t *testing.T) {
|
||||
// We only need to check against a few, not all of them.
|
||||
tests := unmarshalingTests[:5]
|
||||
|
@ -692,6 +721,65 @@ func TestUnmarshalingBadInput(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
type funcResolver func(turl string) (proto.Message, error)
|
||||
|
||||
func (fn funcResolver) Resolve(turl string) (proto.Message, error) {
|
||||
return fn(turl)
|
||||
}
|
||||
|
||||
func TestAnyWithCustomResolver(t *testing.T) {
|
||||
var resolvedTypeUrls []string
|
||||
resolver := funcResolver(func(turl string) (proto.Message, error) {
|
||||
resolvedTypeUrls = append(resolvedTypeUrls, turl)
|
||||
return new(pb.Simple), nil
|
||||
})
|
||||
msg := &pb.Simple{
|
||||
OBytes: []byte{1, 2, 3, 4},
|
||||
OBool: proto.Bool(true),
|
||||
OString: proto.String("foobar"),
|
||||
OInt64: proto.Int64(1020304),
|
||||
}
|
||||
msgBytes, err := proto.Marshal(msg)
|
||||
if err != nil {
|
||||
t.Errorf("an unexpected error occurred when marshaling message: %v", err)
|
||||
}
|
||||
// make an Any with a type URL that won't resolve w/out custom resolver
|
||||
any := &anypb.Any{
|
||||
TypeUrl: "https://foobar.com/some.random.MessageKind",
|
||||
Value: msgBytes,
|
||||
}
|
||||
|
||||
m := Marshaler{AnyResolver: resolver}
|
||||
js, err := m.MarshalToString(any)
|
||||
if err != nil {
|
||||
t.Errorf("an unexpected error occurred when marshaling any to JSON: %v", err)
|
||||
}
|
||||
if len(resolvedTypeUrls) != 1 {
|
||||
t.Errorf("custom resolver was not invoked during marshaling")
|
||||
} else if resolvedTypeUrls[0] != "https://foobar.com/some.random.MessageKind" {
|
||||
t.Errorf("custom resolver was invoked with wrong URL: got %q, wanted %q", resolvedTypeUrls[0], "https://foobar.com/some.random.MessageKind")
|
||||
}
|
||||
wanted := `{"@type":"https://foobar.com/some.random.MessageKind","oBool":true,"oInt64":"1020304","oString":"foobar","oBytes":"AQIDBA=="}`
|
||||
if js != wanted {
|
||||
t.Errorf("marshalling JSON produced incorrect output: got %s, wanted %s", js, wanted)
|
||||
}
|
||||
|
||||
u := Unmarshaler{AnyResolver: resolver}
|
||||
roundTrip := &anypb.Any{}
|
||||
err = u.Unmarshal(bytes.NewReader([]byte(js)), roundTrip)
|
||||
if err != nil {
|
||||
t.Errorf("an unexpected error occurred when unmarshaling any from JSON: %v", err)
|
||||
}
|
||||
if len(resolvedTypeUrls) != 2 {
|
||||
t.Errorf("custom resolver was not invoked during marshaling")
|
||||
} else if resolvedTypeUrls[1] != "https://foobar.com/some.random.MessageKind" {
|
||||
t.Errorf("custom resolver was invoked with wrong URL: got %q, wanted %q", resolvedTypeUrls[1], "https://foobar.com/some.random.MessageKind")
|
||||
}
|
||||
if !proto.Equal(any, roundTrip) {
|
||||
t.Errorf("message contents not set correctly after unmarshalling JSON: got %s, wanted %s", roundTrip, any)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalJSONPBUnmarshaler(t *testing.T) {
|
||||
rawJson := `{ "foo": "bar", "baz": [0, 1, 2, 3] }`
|
||||
var msg dynamicMessage
|
||||
|
@ -703,6 +791,19 @@ func TestUnmarshalJSONPBUnmarshaler(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalNullWithJSONPBUnmarshaler(t *testing.T) {
|
||||
rawJson := `{"stringField":null}`
|
||||
var ptrFieldMsg ptrFieldMessage
|
||||
if err := Unmarshal(strings.NewReader(rawJson), &ptrFieldMsg); err != nil {
|
||||
t.Errorf("unmarshal error: %v", err)
|
||||
}
|
||||
|
||||
want := ptrFieldMessage{StringField: &stringField{IsSet: true, StringValue: "null"}}
|
||||
if !proto.Equal(&ptrFieldMsg, &want) {
|
||||
t.Errorf("unmarshal result StringField: got %v, want %v", ptrFieldMsg, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalAnyJSONPBUnmarshaler(t *testing.T) {
|
||||
rawJson := `{ "@type": "blah.com/` + dynamicMessageName + `", "foo": "bar", "baz": [0, 1, 2, 3] }`
|
||||
var got anypb.Any
|
||||
|
@ -727,11 +828,47 @@ func TestUnmarshalAnyJSONPBUnmarshaler(t *testing.T) {
|
|||
const (
|
||||
dynamicMessageName = "google.protobuf.jsonpb.testing.dynamicMessage"
|
||||
)
|
||||
|
||||
func init() {
|
||||
// we register the custom type below so that we can use it in Any types
|
||||
proto.RegisterType((*dynamicMessage)(nil), dynamicMessageName)
|
||||
}
|
||||
|
||||
type ptrFieldMessage struct {
|
||||
StringField *stringField `protobuf:"bytes,1,opt,name=stringField"`
|
||||
}
|
||||
|
||||
func (m *ptrFieldMessage) Reset() {
|
||||
}
|
||||
|
||||
func (m *ptrFieldMessage) String() string {
|
||||
return m.StringField.StringValue
|
||||
}
|
||||
|
||||
func (m *ptrFieldMessage) ProtoMessage() {
|
||||
}
|
||||
|
||||
type stringField struct {
|
||||
IsSet bool `protobuf:"varint,1,opt,name=isSet"`
|
||||
StringValue string `protobuf:"bytes,2,opt,name=stringValue"`
|
||||
}
|
||||
|
||||
func (s *stringField) Reset() {
|
||||
}
|
||||
|
||||
func (s *stringField) String() string {
|
||||
return s.StringValue
|
||||
}
|
||||
|
||||
func (s *stringField) ProtoMessage() {
|
||||
}
|
||||
|
||||
func (s *stringField) UnmarshalJSONPB(jum *Unmarshaler, js []byte) error {
|
||||
s.IsSet = true
|
||||
s.StringValue = string(js)
|
||||
return nil
|
||||
}
|
||||
|
||||
// dynamicMessage implements protobuf.Message but is not a normal generated message type.
|
||||
// It provides implementations of JSONPBMarshaler and JSONPBUnmarshaler for JSON support.
|
||||
type dynamicMessage struct {
|
||||
|
@ -756,4 +893,4 @@ func (m *dynamicMessage) MarshalJSONPB(jm *Marshaler) ([]byte, error) {
|
|||
func (m *dynamicMessage) UnmarshalJSONPB(jum *Unmarshaler, js []byte) error {
|
||||
m.rawJson = string(js)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue