|
|
@@ -1892,6 +1892,238 @@ func TestMarshal(t *testing.T) {
|
|
|
"value": {}
|
|
|
},
|
|
|
"optFieldmask": "fooBar,barFoo"
|
|
|
+}`,
|
|
|
+ }, {
|
|
|
+ desc: "EmitUnpopulated: proto2 optional scalars",
|
|
|
+ mo: protojson.MarshalOptions{EmitUnpopulated: true},
|
|
|
+ input: &pb2.Scalars{},
|
|
|
+ want: `{
|
|
|
+ "optBool": null,
|
|
|
+ "optInt32": null,
|
|
|
+ "optInt64": null,
|
|
|
+ "optUint32": null,
|
|
|
+ "optUint64": null,
|
|
|
+ "optSint32": null,
|
|
|
+ "optSint64": null,
|
|
|
+ "optFixed32": null,
|
|
|
+ "optFixed64": null,
|
|
|
+ "optSfixed32": null,
|
|
|
+ "optSfixed64": null,
|
|
|
+ "optFloat": null,
|
|
|
+ "optDouble": null,
|
|
|
+ "optBytes": null,
|
|
|
+ "optString": null
|
|
|
+}`,
|
|
|
+ }, {
|
|
|
+ desc: "EmitUnpopulated: proto3 scalars",
|
|
|
+ mo: protojson.MarshalOptions{EmitUnpopulated: true},
|
|
|
+ input: &pb3.Scalars{},
|
|
|
+ want: `{
|
|
|
+ "sBool": false,
|
|
|
+ "sInt32": 0,
|
|
|
+ "sInt64": "0",
|
|
|
+ "sUint32": 0,
|
|
|
+ "sUint64": "0",
|
|
|
+ "sSint32": 0,
|
|
|
+ "sSint64": "0",
|
|
|
+ "sFixed32": 0,
|
|
|
+ "sFixed64": "0",
|
|
|
+ "sSfixed32": 0,
|
|
|
+ "sSfixed64": "0",
|
|
|
+ "sFloat": 0,
|
|
|
+ "sDouble": 0,
|
|
|
+ "sBytes": "",
|
|
|
+ "sString": ""
|
|
|
+}`,
|
|
|
+ }, {
|
|
|
+ desc: "EmitUnpopulated: proto2 enum",
|
|
|
+ mo: protojson.MarshalOptions{EmitUnpopulated: true},
|
|
|
+ input: &pb2.Enums{},
|
|
|
+ want: `{
|
|
|
+ "optEnum": null,
|
|
|
+ "rptEnum": [],
|
|
|
+ "optNestedEnum": null,
|
|
|
+ "rptNestedEnum": []
|
|
|
+}`,
|
|
|
+ }, {
|
|
|
+ desc: "EmitUnpopulated: proto3 enum",
|
|
|
+ mo: protojson.MarshalOptions{EmitUnpopulated: true},
|
|
|
+ input: &pb3.Enums{},
|
|
|
+ want: `{
|
|
|
+ "sEnum": "ZERO",
|
|
|
+ "sNestedEnum": "CERO"
|
|
|
+}`,
|
|
|
+ }, {
|
|
|
+ desc: "EmitUnpopulated: proto2 message and group fields",
|
|
|
+ mo: protojson.MarshalOptions{EmitUnpopulated: true},
|
|
|
+ input: &pb2.Nests{},
|
|
|
+ want: `{
|
|
|
+ "optNested": null,
|
|
|
+ "optgroup": null,
|
|
|
+ "rptNested": [],
|
|
|
+ "rptgroup": []
|
|
|
+}`,
|
|
|
+ }, {
|
|
|
+ desc: "EmitUnpopulated: proto3 message field",
|
|
|
+ mo: protojson.MarshalOptions{EmitUnpopulated: true},
|
|
|
+ input: &pb3.Nests{},
|
|
|
+ want: `{
|
|
|
+ "sNested": null
|
|
|
+}`,
|
|
|
+ }, {
|
|
|
+ desc: "EmitUnpopulated: proto2 empty message and group fields",
|
|
|
+ mo: protojson.MarshalOptions{EmitUnpopulated: true},
|
|
|
+ input: &pb2.Nests{
|
|
|
+ OptNested: &pb2.Nested{},
|
|
|
+ Optgroup: &pb2.Nests_OptGroup{},
|
|
|
+ },
|
|
|
+ want: `{
|
|
|
+ "optNested": {
|
|
|
+ "optString": null,
|
|
|
+ "optNested": null
|
|
|
+ },
|
|
|
+ "optgroup": {
|
|
|
+ "optString": null,
|
|
|
+ "optNested": null,
|
|
|
+ "optnestedgroup": null
|
|
|
+ },
|
|
|
+ "rptNested": [],
|
|
|
+ "rptgroup": []
|
|
|
+}`,
|
|
|
+ }, {
|
|
|
+ desc: "EmitUnpopulated: proto3 empty message field",
|
|
|
+ mo: protojson.MarshalOptions{EmitUnpopulated: true},
|
|
|
+ input: &pb3.Nests{
|
|
|
+ SNested: &pb3.Nested{},
|
|
|
+ },
|
|
|
+ want: `{
|
|
|
+ "sNested": {
|
|
|
+ "sString": "",
|
|
|
+ "sNested": null
|
|
|
+ }
|
|
|
+}`,
|
|
|
+ }, {
|
|
|
+ desc: "EmitUnpopulated: proto2 required fields",
|
|
|
+ mo: protojson.MarshalOptions{
|
|
|
+ AllowPartial: true,
|
|
|
+ EmitUnpopulated: true,
|
|
|
+ },
|
|
|
+ input: &pb2.Requireds{},
|
|
|
+ want: `{
|
|
|
+ "reqBool": null,
|
|
|
+ "reqSfixed64": null,
|
|
|
+ "reqDouble": null,
|
|
|
+ "reqString": null,
|
|
|
+ "reqEnum": null,
|
|
|
+ "reqNested": null
|
|
|
+}`,
|
|
|
+ }, {
|
|
|
+ desc: "EmitUnpopulated: repeated fields",
|
|
|
+ mo: protojson.MarshalOptions{EmitUnpopulated: true},
|
|
|
+ input: &pb2.Repeats{},
|
|
|
+ want: `{
|
|
|
+ "rptBool": [],
|
|
|
+ "rptInt32": [],
|
|
|
+ "rptInt64": [],
|
|
|
+ "rptUint32": [],
|
|
|
+ "rptUint64": [],
|
|
|
+ "rptFloat": [],
|
|
|
+ "rptDouble": [],
|
|
|
+ "rptString": [],
|
|
|
+ "rptBytes": []
|
|
|
+}`,
|
|
|
+ }, {
|
|
|
+ desc: "EmitUnpopulated: repeated containing empty message",
|
|
|
+ mo: protojson.MarshalOptions{EmitUnpopulated: true},
|
|
|
+ input: &pb2.Nests{
|
|
|
+ RptNested: []*pb2.Nested{nil, {}},
|
|
|
+ },
|
|
|
+ want: `{
|
|
|
+ "optNested": null,
|
|
|
+ "optgroup": null,
|
|
|
+ "rptNested": [
|
|
|
+ {
|
|
|
+ "optString": null,
|
|
|
+ "optNested": null
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "optString": null,
|
|
|
+ "optNested": null
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "rptgroup": []
|
|
|
+}`,
|
|
|
+ }, {
|
|
|
+ desc: "EmitUnpopulated: map fields",
|
|
|
+ mo: protojson.MarshalOptions{EmitUnpopulated: true},
|
|
|
+ input: &pb3.Maps{},
|
|
|
+ want: `{
|
|
|
+ "int32ToStr": {},
|
|
|
+ "boolToUint32": {},
|
|
|
+ "uint64ToEnum": {},
|
|
|
+ "strToNested": {},
|
|
|
+ "strToOneofs": {}
|
|
|
+}`,
|
|
|
+ }, {
|
|
|
+ desc: "EmitUnpopulated: map containing empty message",
|
|
|
+ mo: protojson.MarshalOptions{EmitUnpopulated: true},
|
|
|
+ input: &pb3.Maps{
|
|
|
+ StrToNested: map[string]*pb3.Nested{
|
|
|
+ "nested": &pb3.Nested{},
|
|
|
+ },
|
|
|
+ StrToOneofs: map[string]*pb3.Oneofs{
|
|
|
+ "nested": &pb3.Oneofs{},
|
|
|
+ },
|
|
|
+ },
|
|
|
+ want: `{
|
|
|
+ "int32ToStr": {},
|
|
|
+ "boolToUint32": {},
|
|
|
+ "uint64ToEnum": {},
|
|
|
+ "strToNested": {
|
|
|
+ "nested": {
|
|
|
+ "sString": "",
|
|
|
+ "sNested": null
|
|
|
+ }
|
|
|
+ },
|
|
|
+ "strToOneofs": {
|
|
|
+ "nested": {}
|
|
|
+ }
|
|
|
+}`,
|
|
|
+ }, {
|
|
|
+ desc: "EmitUnpopulated: oneof fields",
|
|
|
+ mo: protojson.MarshalOptions{EmitUnpopulated: true},
|
|
|
+ input: &pb3.Oneofs{},
|
|
|
+ want: `{}`,
|
|
|
+ }, {
|
|
|
+ desc: "EmitUnpopulated: extensions",
|
|
|
+ mo: protojson.MarshalOptions{EmitUnpopulated: true},
|
|
|
+ input: func() proto.Message {
|
|
|
+ m := &pb2.Extensions{}
|
|
|
+ proto.SetExtension(m, pb2.E_OptExtNested, &pb2.Nested{})
|
|
|
+ proto.SetExtension(m, pb2.E_RptExtNested, []*pb2.Nested{
|
|
|
+ nil,
|
|
|
+ {},
|
|
|
+ })
|
|
|
+ return m
|
|
|
+ }(),
|
|
|
+ want: `{
|
|
|
+ "optString": null,
|
|
|
+ "optBool": null,
|
|
|
+ "optInt32": null,
|
|
|
+ "[pb2.opt_ext_nested]": {
|
|
|
+ "optString": null,
|
|
|
+ "optNested": null
|
|
|
+ },
|
|
|
+ "[pb2.rpt_ext_nested]": [
|
|
|
+ {
|
|
|
+ "optString": null,
|
|
|
+ "optNested": null
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "optString": null,
|
|
|
+ "optNested": null
|
|
|
+ }
|
|
|
+ ]
|
|
|
}`,
|
|
|
}}
|
|
|
|