Skip to content

Commit 7d1b268

Browse files
authored
jsonpb: avoid unexported fields in hand-crafted message (#671)
The purego implementation is unable to handle unexported fields since it is impossible for purego code to mutate such fields without using unsafe. Also, modify the Makefile to test the purego code paths. Fixes #670
1 parent f5983d5 commit 7d1b268

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ install:
3636

3737
test:
3838
go test ./... ./protoc-gen-go/testdata
39+
go test -tags purego ./... ./protoc-gen-go/testdata
3940
go build ./protoc-gen-go/testdata/grpc/grpc.pb.go
4041
make -C conformance test
4142

jsonpb/jsonpb_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ func TestMarshalIllegalTime(t *testing.T) {
572572

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

585585
func TestMarshalAnyJSONPBMarshaler(t *testing.T) {
586-
msg := dynamicMessage{rawJson: `{ "foo": "bar", "baz": [0, 1, 2, 3] }`}
586+
msg := dynamicMessage{RawJson: `{ "foo": "bar", "baz": [0, 1, 2, 3] }`}
587587
a, err := ptypes.MarshalAny(&msg)
588588
if err != nil {
589589
t.Errorf("an unexpected error occurred when marshalling to Any: %v", err)
@@ -601,7 +601,7 @@ func TestMarshalAnyJSONPBMarshaler(t *testing.T) {
601601
}
602602

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

606606
js, err := new(Marshaler).MarshalToString(&msg)
607607
if err != nil {
@@ -1011,8 +1011,8 @@ func TestUnmarshalJSONPBUnmarshaler(t *testing.T) {
10111011
if err := Unmarshal(strings.NewReader(rawJson), &msg); err != nil {
10121012
t.Errorf("an unexpected error occurred when parsing into JSONPBUnmarshaler: %v", err)
10131013
}
1014-
if msg.rawJson != rawJson {
1015-
t.Errorf("message contents not set correctly after unmarshalling JSON: got %s, wanted %s", msg.rawJson, rawJson)
1014+
if msg.RawJson != rawJson {
1015+
t.Errorf("message contents not set correctly after unmarshalling JSON: got %s, wanted %s", msg.RawJson, rawJson)
10161016
}
10171017
}
10181018

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

1039-
dm := &dynamicMessage{rawJson: `{"baz":[0,1,2,3],"foo":"bar"}`}
1039+
dm := &dynamicMessage{RawJson: `{"baz":[0,1,2,3],"foo":"bar"}`}
10401040
var want anypb.Any
10411041
if b, err := proto.Marshal(dm); err != nil {
10421042
t.Errorf("an unexpected error occurred when marshaling message: %v", err)
@@ -1097,30 +1097,30 @@ func (s *stringField) UnmarshalJSONPB(jum *Unmarshaler, js []byte) error {
10971097
// dynamicMessage implements protobuf.Message but is not a normal generated message type.
10981098
// It provides implementations of JSONPBMarshaler and JSONPBUnmarshaler for JSON support.
10991099
type dynamicMessage struct {
1100-
rawJson string `protobuf:"bytes,1,opt,name=rawJson"`
1100+
RawJson string `protobuf:"bytes,1,opt,name=rawJson"`
11011101

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

11071107
func (m *dynamicMessage) Reset() {
1108-
m.rawJson = "{}"
1108+
m.RawJson = "{}"
11091109
}
11101110

11111111
func (m *dynamicMessage) String() string {
1112-
return m.rawJson
1112+
return m.RawJson
11131113
}
11141114

11151115
func (m *dynamicMessage) ProtoMessage() {
11161116
}
11171117

11181118
func (m *dynamicMessage) MarshalJSONPB(jm *Marshaler) ([]byte, error) {
1119-
return []byte(m.rawJson), nil
1119+
return []byte(m.RawJson), nil
11201120
}
11211121

11221122
func (m *dynamicMessage) UnmarshalJSONPB(jum *Unmarshaler, js []byte) error {
1123-
m.rawJson = string(js)
1123+
m.RawJson = string(js)
11241124
return nil
11251125
}
11261126

0 commit comments

Comments
 (0)