Skip to content

Commit

Permalink
merged in golang/protobuf commit 7d1b268556d691919f2262240737157830ea…
Browse files Browse the repository at this point in the history
…b632 - jsonpb: avoid unexported fields in hand-crafted message (#526)

* merged in golang/protobuf commit 560bdb64431cc123098c2db67f16053a923a0688 - jsonpb: strictly document JSONPBMarshaler and JSONPBUnmarshaler behavior

* merged in golang/protobuf commit f5983d50c82d70eaa88c17080245cc871558081f - proto: make invalid UTF-8 errors non-fatal

* merged in golang/protobuf commit 7d1b268556d691919f2262240737157830eab632 - jsonpb: avoid unexported fields in hand-crafted message
  • Loading branch information
jmarais committed Dec 11, 2018
1 parent f2db49f commit 2f3f4c2
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions jsonpb/jsonpb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ func TestMarshalIllegalTime(t *testing.T) {

func TestMarshalJSONPBMarshaler(t *testing.T) {
rawJson := `{ "foo": "bar", "baz": [0, 1, 2, 3] }`
msg := dynamicMessage{rawJson: rawJson}
msg := dynamicMessage{RawJson: rawJson}
str, err := new(Marshaler).MarshalToString(&msg)
if err != nil {
t.Errorf("an unexpected error occurred when marshalling JSONPBMarshaler: %v", err)
Expand All @@ -580,7 +580,7 @@ func TestMarshalJSONPBMarshaler(t *testing.T) {
}

func TestMarshalAnyJSONPBMarshaler(t *testing.T) {
msg := dynamicMessage{rawJson: `{ "foo": "bar", "baz": [0, 1, 2, 3] }`}
msg := dynamicMessage{RawJson: `{ "foo": "bar", "baz": [0, 1, 2, 3] }`}
a, err := types.MarshalAny(&msg)
if err != nil {
t.Errorf("an unexpected error occurred when marshalling to Any: %v", err)
Expand All @@ -598,7 +598,7 @@ func TestMarshalAnyJSONPBMarshaler(t *testing.T) {
}

func TestMarshalWithCustomValidation(t *testing.T) {
msg := dynamicMessage{rawJson: `{ "foo": "bar", "baz": [0, 1, 2, 3] }`, dummy: &dynamicMessage{}}
msg := dynamicMessage{RawJson: `{ "foo": "bar", "baz": [0, 1, 2, 3] }`, Dummy: &dynamicMessage{}}

js, err := new(Marshaler).MarshalToString(&msg)
if err != nil {
Expand Down Expand Up @@ -1005,8 +1005,8 @@ func TestUnmarshalJSONPBUnmarshaler(t *testing.T) {
if err := Unmarshal(strings.NewReader(rawJson), &msg); err != nil {
t.Errorf("an unexpected error occurred when parsing into JSONPBUnmarshaler: %v", err)
}
if msg.rawJson != rawJson {
t.Errorf("message contents not set correctly after unmarshalling JSON: got %s, wanted %s", msg.rawJson, rawJson)
if msg.RawJson != rawJson {
t.Errorf("message contents not set correctly after unmarshalling JSON: got %s, wanted %s", msg.RawJson, rawJson)
}
}

Expand All @@ -1030,7 +1030,7 @@ func TestUnmarshalAnyJSONPBUnmarshaler(t *testing.T) {
t.Errorf("an unexpected error occurred when parsing into JSONPBUnmarshaler: %v", err)
}

dm := &dynamicMessage{rawJson: `{"baz":[0,1,2,3],"foo":"bar"}`}
dm := &dynamicMessage{RawJson: `{"baz":[0,1,2,3],"foo":"bar"}`}
var want types.Any
if b, err := proto.Marshal(dm); err != nil {
t.Errorf("an unexpected error occurred when marshaling message: %v", err)
Expand Down Expand Up @@ -1091,30 +1091,30 @@ func (s *stringField) UnmarshalJSONPB(jum *Unmarshaler, js []byte) error {
// 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 {
rawJson string `protobuf:"bytes,1,opt,name=rawJson"`
RawJson string `protobuf:"bytes,1,opt,name=rawJson"`

// an unexported nested message is present just to ensure that it
// won't result in a panic (see issue #509)
dummy *dynamicMessage `protobuf:"bytes,2,opt,name=dummy"`
Dummy *dynamicMessage `protobuf:"bytes,2,opt,name=dummy"`
}

func (m *dynamicMessage) Reset() {
m.rawJson = "{}"
m.RawJson = "{}"
}

func (m *dynamicMessage) String() string {
return m.rawJson
return m.RawJson
}

func (m *dynamicMessage) ProtoMessage() {
}

func (m *dynamicMessage) MarshalJSONPB(jm *Marshaler) ([]byte, error) {
return []byte(m.rawJson), nil
return []byte(m.RawJson), nil
}

func (m *dynamicMessage) UnmarshalJSONPB(jum *Unmarshaler, js []byte) error {
m.rawJson = string(js)
m.RawJson = string(js)
return nil
}

Expand Down

0 comments on commit 2f3f4c2

Please sign in to comment.