encode_test.go 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981
  1. // Copyright 2019 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package protojson_test
  5. import (
  6. "bytes"
  7. "encoding/hex"
  8. "math"
  9. "strings"
  10. "testing"
  11. "github.com/google/go-cmp/cmp"
  12. "github.com/google/go-cmp/cmp/cmpopts"
  13. "google.golang.org/protobuf/encoding/protojson"
  14. "google.golang.org/protobuf/internal/encoding/pack"
  15. "google.golang.org/protobuf/internal/encoding/wire"
  16. pimpl "google.golang.org/protobuf/internal/impl"
  17. "google.golang.org/protobuf/internal/scalar"
  18. "google.golang.org/protobuf/proto"
  19. preg "google.golang.org/protobuf/reflect/protoregistry"
  20. "google.golang.org/protobuf/runtime/protoiface"
  21. "google.golang.org/protobuf/encoding/testprotos/pb2"
  22. "google.golang.org/protobuf/encoding/testprotos/pb3"
  23. "google.golang.org/protobuf/types/known/anypb"
  24. "google.golang.org/protobuf/types/known/durationpb"
  25. "google.golang.org/protobuf/types/known/emptypb"
  26. "google.golang.org/protobuf/types/known/fieldmaskpb"
  27. "google.golang.org/protobuf/types/known/structpb"
  28. "google.golang.org/protobuf/types/known/timestamppb"
  29. "google.golang.org/protobuf/types/known/wrapperspb"
  30. )
  31. // splitLines is a cmpopts.Option for comparing strings with line breaks.
  32. var splitLines = cmpopts.AcyclicTransformer("SplitLines", func(s string) []string {
  33. return strings.Split(s, "\n")
  34. })
  35. func pb2Enum(i int32) *pb2.Enum {
  36. p := new(pb2.Enum)
  37. *p = pb2.Enum(i)
  38. return p
  39. }
  40. func pb2Enums_NestedEnum(i int32) *pb2.Enums_NestedEnum {
  41. p := new(pb2.Enums_NestedEnum)
  42. *p = pb2.Enums_NestedEnum(i)
  43. return p
  44. }
  45. func setExtension(m proto.Message, xd *protoiface.ExtensionDescV1, val interface{}) {
  46. knownFields := m.ProtoReflect().KnownFields()
  47. extTypes := knownFields.ExtensionTypes()
  48. extTypes.Register(xd.Type)
  49. if val == nil {
  50. return
  51. }
  52. pval := xd.Type.ValueOf(val)
  53. knownFields.Set(wire.Number(xd.Field), pval)
  54. }
  55. // dhex decodes a hex-string and returns the bytes and panics if s is invalid.
  56. func dhex(s string) []byte {
  57. b, err := hex.DecodeString(s)
  58. if err != nil {
  59. panic(err)
  60. }
  61. return b
  62. }
  63. func TestMarshal(t *testing.T) {
  64. tests := []struct {
  65. desc string
  66. mo protojson.MarshalOptions
  67. input proto.Message
  68. want string
  69. wantErr bool // TODO: Verify error message substring.
  70. }{{
  71. desc: "proto2 optional scalars not set",
  72. input: &pb2.Scalars{},
  73. want: "{}",
  74. }, {
  75. desc: "proto3 scalars not set",
  76. input: &pb3.Scalars{},
  77. want: "{}",
  78. }, {
  79. desc: "proto2 optional scalars set to zero values",
  80. input: &pb2.Scalars{
  81. OptBool: scalar.Bool(false),
  82. OptInt32: scalar.Int32(0),
  83. OptInt64: scalar.Int64(0),
  84. OptUint32: scalar.Uint32(0),
  85. OptUint64: scalar.Uint64(0),
  86. OptSint32: scalar.Int32(0),
  87. OptSint64: scalar.Int64(0),
  88. OptFixed32: scalar.Uint32(0),
  89. OptFixed64: scalar.Uint64(0),
  90. OptSfixed32: scalar.Int32(0),
  91. OptSfixed64: scalar.Int64(0),
  92. OptFloat: scalar.Float32(0),
  93. OptDouble: scalar.Float64(0),
  94. OptBytes: []byte{},
  95. OptString: scalar.String(""),
  96. },
  97. want: `{
  98. "optBool": false,
  99. "optInt32": 0,
  100. "optInt64": "0",
  101. "optUint32": 0,
  102. "optUint64": "0",
  103. "optSint32": 0,
  104. "optSint64": "0",
  105. "optFixed32": 0,
  106. "optFixed64": "0",
  107. "optSfixed32": 0,
  108. "optSfixed64": "0",
  109. "optFloat": 0,
  110. "optDouble": 0,
  111. "optBytes": "",
  112. "optString": ""
  113. }`,
  114. }, {
  115. desc: "proto2 optional scalars set to some values",
  116. input: &pb2.Scalars{
  117. OptBool: scalar.Bool(true),
  118. OptInt32: scalar.Int32(0xff),
  119. OptInt64: scalar.Int64(0xdeadbeef),
  120. OptUint32: scalar.Uint32(47),
  121. OptUint64: scalar.Uint64(0xdeadbeef),
  122. OptSint32: scalar.Int32(-1001),
  123. OptSint64: scalar.Int64(-0xffff),
  124. OptFixed64: scalar.Uint64(64),
  125. OptSfixed32: scalar.Int32(-32),
  126. OptFloat: scalar.Float32(1.02),
  127. OptDouble: scalar.Float64(1.234),
  128. OptBytes: []byte("谷歌"),
  129. OptString: scalar.String("谷歌"),
  130. },
  131. want: `{
  132. "optBool": true,
  133. "optInt32": 255,
  134. "optInt64": "3735928559",
  135. "optUint32": 47,
  136. "optUint64": "3735928559",
  137. "optSint32": -1001,
  138. "optSint64": "-65535",
  139. "optFixed64": "64",
  140. "optSfixed32": -32,
  141. "optFloat": 1.02,
  142. "optDouble": 1.234,
  143. "optBytes": "6LC35q2M",
  144. "optString": "谷歌"
  145. }`,
  146. }, {
  147. desc: "string",
  148. input: &pb3.Scalars{
  149. SString: "谷歌",
  150. },
  151. want: `{
  152. "sString": "谷歌"
  153. }`,
  154. }, {
  155. desc: "string with invalid UTF8",
  156. input: &pb3.Scalars{
  157. SString: "abc\xff",
  158. },
  159. want: "{\n \"sString\": \"abc\xff\"\n}",
  160. wantErr: true,
  161. }, {
  162. desc: "float nan",
  163. input: &pb3.Scalars{
  164. SFloat: float32(math.NaN()),
  165. },
  166. want: `{
  167. "sFloat": "NaN"
  168. }`,
  169. }, {
  170. desc: "float positive infinity",
  171. input: &pb3.Scalars{
  172. SFloat: float32(math.Inf(1)),
  173. },
  174. want: `{
  175. "sFloat": "Infinity"
  176. }`,
  177. }, {
  178. desc: "float negative infinity",
  179. input: &pb3.Scalars{
  180. SFloat: float32(math.Inf(-1)),
  181. },
  182. want: `{
  183. "sFloat": "-Infinity"
  184. }`,
  185. }, {
  186. desc: "double nan",
  187. input: &pb3.Scalars{
  188. SDouble: math.NaN(),
  189. },
  190. want: `{
  191. "sDouble": "NaN"
  192. }`,
  193. }, {
  194. desc: "double positive infinity",
  195. input: &pb3.Scalars{
  196. SDouble: math.Inf(1),
  197. },
  198. want: `{
  199. "sDouble": "Infinity"
  200. }`,
  201. }, {
  202. desc: "double negative infinity",
  203. input: &pb3.Scalars{
  204. SDouble: math.Inf(-1),
  205. },
  206. want: `{
  207. "sDouble": "-Infinity"
  208. }`,
  209. }, {
  210. desc: "proto2 enum not set",
  211. input: &pb2.Enums{},
  212. want: "{}",
  213. }, {
  214. desc: "proto2 enum set to zero value",
  215. input: &pb2.Enums{
  216. OptEnum: pb2Enum(0),
  217. OptNestedEnum: pb2Enums_NestedEnum(0),
  218. },
  219. want: `{
  220. "optEnum": 0,
  221. "optNestedEnum": 0
  222. }`,
  223. }, {
  224. desc: "proto2 enum",
  225. input: &pb2.Enums{
  226. OptEnum: pb2.Enum_ONE.Enum(),
  227. OptNestedEnum: pb2.Enums_UNO.Enum(),
  228. },
  229. want: `{
  230. "optEnum": "ONE",
  231. "optNestedEnum": "UNO"
  232. }`,
  233. }, {
  234. desc: "proto2 enum set to numeric values",
  235. input: &pb2.Enums{
  236. OptEnum: pb2Enum(2),
  237. OptNestedEnum: pb2Enums_NestedEnum(2),
  238. },
  239. want: `{
  240. "optEnum": "TWO",
  241. "optNestedEnum": "DOS"
  242. }`,
  243. }, {
  244. desc: "proto2 enum set to unnamed numeric values",
  245. input: &pb2.Enums{
  246. OptEnum: pb2Enum(101),
  247. OptNestedEnum: pb2Enums_NestedEnum(-101),
  248. },
  249. want: `{
  250. "optEnum": 101,
  251. "optNestedEnum": -101
  252. }`,
  253. }, {
  254. desc: "proto3 enum not set",
  255. input: &pb3.Enums{},
  256. want: "{}",
  257. }, {
  258. desc: "proto3 enum set to zero value",
  259. input: &pb3.Enums{
  260. SEnum: pb3.Enum_ZERO,
  261. SNestedEnum: pb3.Enums_CERO,
  262. },
  263. want: "{}",
  264. }, {
  265. desc: "proto3 enum",
  266. input: &pb3.Enums{
  267. SEnum: pb3.Enum_ONE,
  268. SNestedEnum: pb3.Enums_UNO,
  269. },
  270. want: `{
  271. "sEnum": "ONE",
  272. "sNestedEnum": "UNO"
  273. }`,
  274. }, {
  275. desc: "proto3 enum set to numeric values",
  276. input: &pb3.Enums{
  277. SEnum: 2,
  278. SNestedEnum: 2,
  279. },
  280. want: `{
  281. "sEnum": "TWO",
  282. "sNestedEnum": "DOS"
  283. }`,
  284. }, {
  285. desc: "proto3 enum set to unnamed numeric values",
  286. input: &pb3.Enums{
  287. SEnum: -47,
  288. SNestedEnum: 47,
  289. },
  290. want: `{
  291. "sEnum": -47,
  292. "sNestedEnum": 47
  293. }`,
  294. }, {
  295. desc: "proto2 nested message not set",
  296. input: &pb2.Nests{},
  297. want: "{}",
  298. }, {
  299. desc: "proto2 nested message set to empty",
  300. input: &pb2.Nests{
  301. OptNested: &pb2.Nested{},
  302. Optgroup: &pb2.Nests_OptGroup{},
  303. },
  304. want: `{
  305. "optNested": {},
  306. "optgroup": {}
  307. }`,
  308. }, {
  309. desc: "proto2 nested messages",
  310. input: &pb2.Nests{
  311. OptNested: &pb2.Nested{
  312. OptString: scalar.String("nested message"),
  313. OptNested: &pb2.Nested{
  314. OptString: scalar.String("another nested message"),
  315. },
  316. },
  317. },
  318. want: `{
  319. "optNested": {
  320. "optString": "nested message",
  321. "optNested": {
  322. "optString": "another nested message"
  323. }
  324. }
  325. }`,
  326. }, {
  327. desc: "proto2 groups",
  328. input: &pb2.Nests{
  329. Optgroup: &pb2.Nests_OptGroup{
  330. OptString: scalar.String("inside a group"),
  331. OptNested: &pb2.Nested{
  332. OptString: scalar.String("nested message inside a group"),
  333. },
  334. Optnestedgroup: &pb2.Nests_OptGroup_OptNestedGroup{
  335. OptFixed32: scalar.Uint32(47),
  336. },
  337. },
  338. },
  339. want: `{
  340. "optgroup": {
  341. "optString": "inside a group",
  342. "optNested": {
  343. "optString": "nested message inside a group"
  344. },
  345. "optnestedgroup": {
  346. "optFixed32": 47
  347. }
  348. }
  349. }`,
  350. }, {
  351. desc: "proto3 nested message not set",
  352. input: &pb3.Nests{},
  353. want: "{}",
  354. }, {
  355. desc: "proto3 nested message set to empty",
  356. input: &pb3.Nests{
  357. SNested: &pb3.Nested{},
  358. },
  359. want: `{
  360. "sNested": {}
  361. }`,
  362. }, {
  363. desc: "proto3 nested message",
  364. input: &pb3.Nests{
  365. SNested: &pb3.Nested{
  366. SString: "nested message",
  367. SNested: &pb3.Nested{
  368. SString: "another nested message",
  369. },
  370. },
  371. },
  372. want: `{
  373. "sNested": {
  374. "sString": "nested message",
  375. "sNested": {
  376. "sString": "another nested message"
  377. }
  378. }
  379. }`,
  380. }, {
  381. desc: "oneof not set",
  382. input: &pb3.Oneofs{},
  383. want: "{}",
  384. }, {
  385. desc: "oneof set to empty string",
  386. input: &pb3.Oneofs{
  387. Union: &pb3.Oneofs_OneofString{},
  388. },
  389. want: `{
  390. "oneofString": ""
  391. }`,
  392. }, {
  393. desc: "oneof set to string",
  394. input: &pb3.Oneofs{
  395. Union: &pb3.Oneofs_OneofString{
  396. OneofString: "hello",
  397. },
  398. },
  399. want: `{
  400. "oneofString": "hello"
  401. }`,
  402. }, {
  403. desc: "oneof set to enum",
  404. input: &pb3.Oneofs{
  405. Union: &pb3.Oneofs_OneofEnum{
  406. OneofEnum: pb3.Enum_ZERO,
  407. },
  408. },
  409. want: `{
  410. "oneofEnum": "ZERO"
  411. }`,
  412. }, {
  413. desc: "oneof set to empty message",
  414. input: &pb3.Oneofs{
  415. Union: &pb3.Oneofs_OneofNested{
  416. OneofNested: &pb3.Nested{},
  417. },
  418. },
  419. want: `{
  420. "oneofNested": {}
  421. }`,
  422. }, {
  423. desc: "oneof set to message",
  424. input: &pb3.Oneofs{
  425. Union: &pb3.Oneofs_OneofNested{
  426. OneofNested: &pb3.Nested{
  427. SString: "nested message",
  428. },
  429. },
  430. },
  431. want: `{
  432. "oneofNested": {
  433. "sString": "nested message"
  434. }
  435. }`,
  436. }, {
  437. desc: "repeated fields not set",
  438. input: &pb2.Repeats{},
  439. want: "{}",
  440. }, {
  441. desc: "repeated fields set to empty slices",
  442. input: &pb2.Repeats{
  443. RptBool: []bool{},
  444. RptInt32: []int32{},
  445. RptInt64: []int64{},
  446. RptUint32: []uint32{},
  447. RptUint64: []uint64{},
  448. RptFloat: []float32{},
  449. RptDouble: []float64{},
  450. RptBytes: [][]byte{},
  451. },
  452. want: "{}",
  453. }, {
  454. desc: "repeated fields set to some values",
  455. input: &pb2.Repeats{
  456. RptBool: []bool{true, false, true, true},
  457. RptInt32: []int32{1, 6, 0, 0},
  458. RptInt64: []int64{-64, 47},
  459. RptUint32: []uint32{0xff, 0xffff},
  460. RptUint64: []uint64{0xdeadbeef},
  461. RptFloat: []float32{float32(math.NaN()), float32(math.Inf(1)), float32(math.Inf(-1)), 1.034},
  462. RptDouble: []float64{math.NaN(), math.Inf(1), math.Inf(-1), 1.23e-308},
  463. RptString: []string{"hello", "世界"},
  464. RptBytes: [][]byte{
  465. []byte("hello"),
  466. []byte("\xe4\xb8\x96\xe7\x95\x8c"),
  467. },
  468. },
  469. want: `{
  470. "rptBool": [
  471. true,
  472. false,
  473. true,
  474. true
  475. ],
  476. "rptInt32": [
  477. 1,
  478. 6,
  479. 0,
  480. 0
  481. ],
  482. "rptInt64": [
  483. "-64",
  484. "47"
  485. ],
  486. "rptUint32": [
  487. 255,
  488. 65535
  489. ],
  490. "rptUint64": [
  491. "3735928559"
  492. ],
  493. "rptFloat": [
  494. "NaN",
  495. "Infinity",
  496. "-Infinity",
  497. 1.034
  498. ],
  499. "rptDouble": [
  500. "NaN",
  501. "Infinity",
  502. "-Infinity",
  503. 1.23e-308
  504. ],
  505. "rptString": [
  506. "hello",
  507. "世界"
  508. ],
  509. "rptBytes": [
  510. "aGVsbG8=",
  511. "5LiW55WM"
  512. ]
  513. }`,
  514. }, {
  515. desc: "repeated enums",
  516. input: &pb2.Enums{
  517. RptEnum: []pb2.Enum{pb2.Enum_ONE, 2, pb2.Enum_TEN, 42},
  518. RptNestedEnum: []pb2.Enums_NestedEnum{2, 47, 10},
  519. },
  520. want: `{
  521. "rptEnum": [
  522. "ONE",
  523. "TWO",
  524. "TEN",
  525. 42
  526. ],
  527. "rptNestedEnum": [
  528. "DOS",
  529. 47,
  530. "DIEZ"
  531. ]
  532. }`,
  533. }, {
  534. desc: "repeated messages set to empty",
  535. input: &pb2.Nests{
  536. RptNested: []*pb2.Nested{},
  537. Rptgroup: []*pb2.Nests_RptGroup{},
  538. },
  539. want: "{}",
  540. }, {
  541. desc: "repeated messages",
  542. input: &pb2.Nests{
  543. RptNested: []*pb2.Nested{
  544. {
  545. OptString: scalar.String("repeat nested one"),
  546. },
  547. {
  548. OptString: scalar.String("repeat nested two"),
  549. OptNested: &pb2.Nested{
  550. OptString: scalar.String("inside repeat nested two"),
  551. },
  552. },
  553. {},
  554. },
  555. },
  556. want: `{
  557. "rptNested": [
  558. {
  559. "optString": "repeat nested one"
  560. },
  561. {
  562. "optString": "repeat nested two",
  563. "optNested": {
  564. "optString": "inside repeat nested two"
  565. }
  566. },
  567. {}
  568. ]
  569. }`,
  570. }, {
  571. desc: "repeated messages contains nil value",
  572. input: &pb2.Nests{
  573. RptNested: []*pb2.Nested{nil, {}},
  574. },
  575. want: `{
  576. "rptNested": [
  577. {},
  578. {}
  579. ]
  580. }`,
  581. }, {
  582. desc: "repeated groups",
  583. input: &pb2.Nests{
  584. Rptgroup: []*pb2.Nests_RptGroup{
  585. {
  586. RptString: []string{"hello", "world"},
  587. },
  588. {},
  589. nil,
  590. },
  591. },
  592. want: `{
  593. "rptgroup": [
  594. {
  595. "rptString": [
  596. "hello",
  597. "world"
  598. ]
  599. },
  600. {},
  601. {}
  602. ]
  603. }`,
  604. }, {
  605. desc: "map fields not set",
  606. input: &pb3.Maps{},
  607. want: "{}",
  608. }, {
  609. desc: "map fields set to empty",
  610. input: &pb3.Maps{
  611. Int32ToStr: map[int32]string{},
  612. BoolToUint32: map[bool]uint32{},
  613. Uint64ToEnum: map[uint64]pb3.Enum{},
  614. StrToNested: map[string]*pb3.Nested{},
  615. StrToOneofs: map[string]*pb3.Oneofs{},
  616. },
  617. want: "{}",
  618. }, {
  619. desc: "map fields 1",
  620. input: &pb3.Maps{
  621. BoolToUint32: map[bool]uint32{
  622. true: 42,
  623. false: 101,
  624. },
  625. },
  626. want: `{
  627. "boolToUint32": {
  628. "false": 101,
  629. "true": 42
  630. }
  631. }`,
  632. }, {
  633. desc: "map fields 2",
  634. input: &pb3.Maps{
  635. Int32ToStr: map[int32]string{
  636. -101: "-101",
  637. 0xff: "0xff",
  638. 0: "zero",
  639. },
  640. },
  641. want: `{
  642. "int32ToStr": {
  643. "-101": "-101",
  644. "0": "zero",
  645. "255": "0xff"
  646. }
  647. }`,
  648. }, {
  649. desc: "map fields 3",
  650. input: &pb3.Maps{
  651. Uint64ToEnum: map[uint64]pb3.Enum{
  652. 1: pb3.Enum_ONE,
  653. 2: pb3.Enum_TWO,
  654. 10: pb3.Enum_TEN,
  655. 47: 47,
  656. },
  657. },
  658. want: `{
  659. "uint64ToEnum": {
  660. "1": "ONE",
  661. "2": "TWO",
  662. "10": "TEN",
  663. "47": 47
  664. }
  665. }`,
  666. }, {
  667. desc: "map fields 4",
  668. input: &pb3.Maps{
  669. StrToNested: map[string]*pb3.Nested{
  670. "nested": &pb3.Nested{
  671. SString: "nested in a map",
  672. },
  673. },
  674. },
  675. want: `{
  676. "strToNested": {
  677. "nested": {
  678. "sString": "nested in a map"
  679. }
  680. }
  681. }`,
  682. }, {
  683. desc: "map fields 5",
  684. input: &pb3.Maps{
  685. StrToOneofs: map[string]*pb3.Oneofs{
  686. "string": &pb3.Oneofs{
  687. Union: &pb3.Oneofs_OneofString{
  688. OneofString: "hello",
  689. },
  690. },
  691. "nested": &pb3.Oneofs{
  692. Union: &pb3.Oneofs_OneofNested{
  693. OneofNested: &pb3.Nested{
  694. SString: "nested oneof in map field value",
  695. },
  696. },
  697. },
  698. },
  699. },
  700. want: `{
  701. "strToOneofs": {
  702. "nested": {
  703. "oneofNested": {
  704. "sString": "nested oneof in map field value"
  705. }
  706. },
  707. "string": {
  708. "oneofString": "hello"
  709. }
  710. }
  711. }`,
  712. }, {
  713. desc: "map field contains nil value",
  714. input: &pb3.Maps{
  715. StrToNested: map[string]*pb3.Nested{
  716. "nil": nil,
  717. },
  718. },
  719. want: `{
  720. "strToNested": {
  721. "nil": {}
  722. }
  723. }`,
  724. }, {
  725. desc: "required fields not set",
  726. input: &pb2.Requireds{},
  727. want: `{}`,
  728. wantErr: true,
  729. }, {
  730. desc: "required fields partially set",
  731. input: &pb2.Requireds{
  732. ReqBool: scalar.Bool(false),
  733. ReqSfixed64: scalar.Int64(0),
  734. ReqDouble: scalar.Float64(1.23),
  735. ReqString: scalar.String("hello"),
  736. ReqEnum: pb2.Enum_ONE.Enum(),
  737. },
  738. want: `{
  739. "reqBool": false,
  740. "reqSfixed64": "0",
  741. "reqDouble": 1.23,
  742. "reqString": "hello",
  743. "reqEnum": "ONE"
  744. }`,
  745. wantErr: true,
  746. }, {
  747. desc: "required fields not set with AllowPartial",
  748. mo: protojson.MarshalOptions{AllowPartial: true},
  749. input: &pb2.Requireds{
  750. ReqBool: scalar.Bool(false),
  751. ReqSfixed64: scalar.Int64(0),
  752. ReqDouble: scalar.Float64(1.23),
  753. ReqString: scalar.String("hello"),
  754. ReqEnum: pb2.Enum_ONE.Enum(),
  755. },
  756. want: `{
  757. "reqBool": false,
  758. "reqSfixed64": "0",
  759. "reqDouble": 1.23,
  760. "reqString": "hello",
  761. "reqEnum": "ONE"
  762. }`,
  763. }, {
  764. desc: "required fields all set",
  765. input: &pb2.Requireds{
  766. ReqBool: scalar.Bool(false),
  767. ReqSfixed64: scalar.Int64(0),
  768. ReqDouble: scalar.Float64(1.23),
  769. ReqString: scalar.String("hello"),
  770. ReqEnum: pb2.Enum_ONE.Enum(),
  771. ReqNested: &pb2.Nested{},
  772. },
  773. want: `{
  774. "reqBool": false,
  775. "reqSfixed64": "0",
  776. "reqDouble": 1.23,
  777. "reqString": "hello",
  778. "reqEnum": "ONE",
  779. "reqNested": {}
  780. }`,
  781. }, {
  782. desc: "indirect required field",
  783. input: &pb2.IndirectRequired{
  784. OptNested: &pb2.NestedWithRequired{},
  785. },
  786. want: `{
  787. "optNested": {}
  788. }`,
  789. wantErr: true,
  790. }, {
  791. desc: "indirect required field with AllowPartial",
  792. mo: protojson.MarshalOptions{AllowPartial: true},
  793. input: &pb2.IndirectRequired{
  794. OptNested: &pb2.NestedWithRequired{},
  795. },
  796. want: `{
  797. "optNested": {}
  798. }`,
  799. }, {
  800. desc: "indirect required field in empty repeated",
  801. input: &pb2.IndirectRequired{
  802. RptNested: []*pb2.NestedWithRequired{},
  803. },
  804. want: `{}`,
  805. }, {
  806. desc: "indirect required field in repeated",
  807. input: &pb2.IndirectRequired{
  808. RptNested: []*pb2.NestedWithRequired{
  809. &pb2.NestedWithRequired{},
  810. },
  811. },
  812. want: `{
  813. "rptNested": [
  814. {}
  815. ]
  816. }`,
  817. wantErr: true,
  818. }, {
  819. desc: "indirect required field in repeated with AllowPartial",
  820. mo: protojson.MarshalOptions{AllowPartial: true},
  821. input: &pb2.IndirectRequired{
  822. RptNested: []*pb2.NestedWithRequired{
  823. &pb2.NestedWithRequired{},
  824. },
  825. },
  826. want: `{
  827. "rptNested": [
  828. {}
  829. ]
  830. }`,
  831. }, {
  832. desc: "indirect required field in empty map",
  833. input: &pb2.IndirectRequired{
  834. StrToNested: map[string]*pb2.NestedWithRequired{},
  835. },
  836. want: "{}",
  837. }, {
  838. desc: "indirect required field in map",
  839. input: &pb2.IndirectRequired{
  840. StrToNested: map[string]*pb2.NestedWithRequired{
  841. "fail": &pb2.NestedWithRequired{},
  842. },
  843. },
  844. want: `{
  845. "strToNested": {
  846. "fail": {}
  847. }
  848. }`,
  849. wantErr: true,
  850. }, {
  851. desc: "indirect required field in map with AllowPartial",
  852. mo: protojson.MarshalOptions{AllowPartial: true},
  853. input: &pb2.IndirectRequired{
  854. StrToNested: map[string]*pb2.NestedWithRequired{
  855. "fail": &pb2.NestedWithRequired{},
  856. },
  857. },
  858. want: `{
  859. "strToNested": {
  860. "fail": {}
  861. }
  862. }`,
  863. }, {
  864. desc: "indirect required field in oneof",
  865. input: &pb2.IndirectRequired{
  866. Union: &pb2.IndirectRequired_OneofNested{
  867. OneofNested: &pb2.NestedWithRequired{},
  868. },
  869. },
  870. want: `{
  871. "oneofNested": {}
  872. }`,
  873. wantErr: true,
  874. }, {
  875. desc: "indirect required field in oneof with AllowPartial",
  876. mo: protojson.MarshalOptions{AllowPartial: true},
  877. input: &pb2.IndirectRequired{
  878. Union: &pb2.IndirectRequired_OneofNested{
  879. OneofNested: &pb2.NestedWithRequired{},
  880. },
  881. },
  882. want: `{
  883. "oneofNested": {}
  884. }`,
  885. }, {
  886. desc: "unknown fields are ignored",
  887. input: &pb2.Scalars{
  888. OptString: scalar.String("no unknowns"),
  889. XXX_unrecognized: pack.Message{
  890. pack.Tag{101, pack.BytesType}, pack.String("hello world"),
  891. }.Marshal(),
  892. },
  893. want: `{
  894. "optString": "no unknowns"
  895. }`,
  896. }, {
  897. desc: "json_name",
  898. input: &pb3.JSONNames{
  899. SString: "json_name",
  900. },
  901. want: `{
  902. "foo_bar": "json_name"
  903. }`,
  904. }, {
  905. desc: "extensions of non-repeated fields",
  906. input: func() proto.Message {
  907. m := &pb2.Extensions{
  908. OptString: scalar.String("non-extension field"),
  909. OptBool: scalar.Bool(true),
  910. OptInt32: scalar.Int32(42),
  911. }
  912. setExtension(m, pb2.E_OptExtBool, true)
  913. setExtension(m, pb2.E_OptExtString, "extension field")
  914. setExtension(m, pb2.E_OptExtEnum, pb2.Enum_TEN)
  915. setExtension(m, pb2.E_OptExtNested, &pb2.Nested{
  916. OptString: scalar.String("nested in an extension"),
  917. OptNested: &pb2.Nested{
  918. OptString: scalar.String("another nested in an extension"),
  919. },
  920. })
  921. return m
  922. }(),
  923. want: `{
  924. "optString": "non-extension field",
  925. "optBool": true,
  926. "optInt32": 42,
  927. "[pb2.opt_ext_bool]": true,
  928. "[pb2.opt_ext_enum]": "TEN",
  929. "[pb2.opt_ext_nested]": {
  930. "optString": "nested in an extension",
  931. "optNested": {
  932. "optString": "another nested in an extension"
  933. }
  934. },
  935. "[pb2.opt_ext_string]": "extension field"
  936. }`,
  937. }, {
  938. desc: "extension message field set to nil",
  939. input: func() proto.Message {
  940. m := &pb2.Extensions{}
  941. setExtension(m, pb2.E_OptExtNested, nil)
  942. return m
  943. }(),
  944. want: "{}",
  945. }, {
  946. desc: "extensions of repeated fields",
  947. input: func() proto.Message {
  948. m := &pb2.Extensions{}
  949. setExtension(m, pb2.E_RptExtEnum, &[]pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE})
  950. setExtension(m, pb2.E_RptExtFixed32, &[]uint32{42, 47})
  951. setExtension(m, pb2.E_RptExtNested, &[]*pb2.Nested{
  952. &pb2.Nested{OptString: scalar.String("one")},
  953. &pb2.Nested{OptString: scalar.String("two")},
  954. &pb2.Nested{OptString: scalar.String("three")},
  955. })
  956. return m
  957. }(),
  958. want: `{
  959. "[pb2.rpt_ext_enum]": [
  960. "TEN",
  961. 101,
  962. "ONE"
  963. ],
  964. "[pb2.rpt_ext_fixed32]": [
  965. 42,
  966. 47
  967. ],
  968. "[pb2.rpt_ext_nested]": [
  969. {
  970. "optString": "one"
  971. },
  972. {
  973. "optString": "two"
  974. },
  975. {
  976. "optString": "three"
  977. }
  978. ]
  979. }`,
  980. }, {
  981. desc: "extensions of non-repeated fields in another message",
  982. input: func() proto.Message {
  983. m := &pb2.Extensions{}
  984. setExtension(m, pb2.E_ExtensionsContainer_OptExtBool, true)
  985. setExtension(m, pb2.E_ExtensionsContainer_OptExtString, "extension field")
  986. setExtension(m, pb2.E_ExtensionsContainer_OptExtEnum, pb2.Enum_TEN)
  987. setExtension(m, pb2.E_ExtensionsContainer_OptExtNested, &pb2.Nested{
  988. OptString: scalar.String("nested in an extension"),
  989. OptNested: &pb2.Nested{
  990. OptString: scalar.String("another nested in an extension"),
  991. },
  992. })
  993. return m
  994. }(),
  995. want: `{
  996. "[pb2.ExtensionsContainer.opt_ext_bool]": true,
  997. "[pb2.ExtensionsContainer.opt_ext_enum]": "TEN",
  998. "[pb2.ExtensionsContainer.opt_ext_nested]": {
  999. "optString": "nested in an extension",
  1000. "optNested": {
  1001. "optString": "another nested in an extension"
  1002. }
  1003. },
  1004. "[pb2.ExtensionsContainer.opt_ext_string]": "extension field"
  1005. }`,
  1006. }, {
  1007. desc: "extensions of repeated fields in another message",
  1008. input: func() proto.Message {
  1009. m := &pb2.Extensions{
  1010. OptString: scalar.String("non-extension field"),
  1011. OptBool: scalar.Bool(true),
  1012. OptInt32: scalar.Int32(42),
  1013. }
  1014. setExtension(m, pb2.E_ExtensionsContainer_RptExtEnum, &[]pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE})
  1015. setExtension(m, pb2.E_ExtensionsContainer_RptExtString, &[]string{"hello", "world"})
  1016. setExtension(m, pb2.E_ExtensionsContainer_RptExtNested, &[]*pb2.Nested{
  1017. &pb2.Nested{OptString: scalar.String("one")},
  1018. &pb2.Nested{OptString: scalar.String("two")},
  1019. &pb2.Nested{OptString: scalar.String("three")},
  1020. })
  1021. return m
  1022. }(),
  1023. want: `{
  1024. "optString": "non-extension field",
  1025. "optBool": true,
  1026. "optInt32": 42,
  1027. "[pb2.ExtensionsContainer.rpt_ext_enum]": [
  1028. "TEN",
  1029. 101,
  1030. "ONE"
  1031. ],
  1032. "[pb2.ExtensionsContainer.rpt_ext_nested]": [
  1033. {
  1034. "optString": "one"
  1035. },
  1036. {
  1037. "optString": "two"
  1038. },
  1039. {
  1040. "optString": "three"
  1041. }
  1042. ],
  1043. "[pb2.ExtensionsContainer.rpt_ext_string]": [
  1044. "hello",
  1045. "world"
  1046. ]
  1047. }`,
  1048. }, {
  1049. desc: "MessageSet",
  1050. input: func() proto.Message {
  1051. m := &pb2.MessageSet{}
  1052. setExtension(m, pb2.E_MessageSetExtension_MessageSetExtension, &pb2.MessageSetExtension{
  1053. OptString: scalar.String("a messageset extension"),
  1054. })
  1055. setExtension(m, pb2.E_MessageSetExtension_NotMessageSetExtension, &pb2.MessageSetExtension{
  1056. OptString: scalar.String("not a messageset extension"),
  1057. })
  1058. setExtension(m, pb2.E_MessageSetExtension_ExtNested, &pb2.Nested{
  1059. OptString: scalar.String("just a regular extension"),
  1060. })
  1061. return m
  1062. }(),
  1063. want: `{
  1064. "[pb2.MessageSetExtension]": {
  1065. "optString": "a messageset extension"
  1066. },
  1067. "[pb2.MessageSetExtension.ext_nested]": {
  1068. "optString": "just a regular extension"
  1069. },
  1070. "[pb2.MessageSetExtension.not_message_set_extension]": {
  1071. "optString": "not a messageset extension"
  1072. }
  1073. }`,
  1074. }, {
  1075. desc: "not real MessageSet 1",
  1076. input: func() proto.Message {
  1077. m := &pb2.FakeMessageSet{}
  1078. setExtension(m, pb2.E_FakeMessageSetExtension_MessageSetExtension, &pb2.FakeMessageSetExtension{
  1079. OptString: scalar.String("not a messageset extension"),
  1080. })
  1081. return m
  1082. }(),
  1083. want: `{
  1084. "[pb2.FakeMessageSetExtension.message_set_extension]": {
  1085. "optString": "not a messageset extension"
  1086. }
  1087. }`,
  1088. }, {
  1089. desc: "not real MessageSet 2",
  1090. input: func() proto.Message {
  1091. m := &pb2.MessageSet{}
  1092. setExtension(m, pb2.E_MessageSetExtension, &pb2.FakeMessageSetExtension{
  1093. OptString: scalar.String("another not a messageset extension"),
  1094. })
  1095. return m
  1096. }(),
  1097. want: `{
  1098. "[pb2.message_set_extension]": {
  1099. "optString": "another not a messageset extension"
  1100. }
  1101. }`,
  1102. }, {
  1103. desc: "BoolValue empty",
  1104. input: &wrapperspb.BoolValue{},
  1105. want: `false`,
  1106. }, {
  1107. desc: "BoolValue",
  1108. input: &wrapperspb.BoolValue{Value: true},
  1109. want: `true`,
  1110. }, {
  1111. desc: "Int32Value empty",
  1112. input: &wrapperspb.Int32Value{},
  1113. want: `0`,
  1114. }, {
  1115. desc: "Int32Value",
  1116. input: &wrapperspb.Int32Value{Value: 42},
  1117. want: `42`,
  1118. }, {
  1119. desc: "Int64Value",
  1120. input: &wrapperspb.Int64Value{Value: 42},
  1121. want: `"42"`,
  1122. }, {
  1123. desc: "UInt32Value",
  1124. input: &wrapperspb.UInt32Value{Value: 42},
  1125. want: `42`,
  1126. }, {
  1127. desc: "UInt64Value",
  1128. input: &wrapperspb.UInt64Value{Value: 42},
  1129. want: `"42"`,
  1130. }, {
  1131. desc: "FloatValue",
  1132. input: &wrapperspb.FloatValue{Value: 1.02},
  1133. want: `1.02`,
  1134. }, {
  1135. desc: "FloatValue Infinity",
  1136. input: &wrapperspb.FloatValue{Value: float32(math.Inf(-1))},
  1137. want: `"-Infinity"`,
  1138. }, {
  1139. desc: "DoubleValue",
  1140. input: &wrapperspb.DoubleValue{Value: 1.02},
  1141. want: `1.02`,
  1142. }, {
  1143. desc: "DoubleValue NaN",
  1144. input: &wrapperspb.DoubleValue{Value: math.NaN()},
  1145. want: `"NaN"`,
  1146. }, {
  1147. desc: "StringValue empty",
  1148. input: &wrapperspb.StringValue{},
  1149. want: `""`,
  1150. }, {
  1151. desc: "StringValue",
  1152. input: &wrapperspb.StringValue{Value: "谷歌"},
  1153. want: `"谷歌"`,
  1154. }, {
  1155. desc: "StringValue with invalid UTF8 error",
  1156. input: &wrapperspb.StringValue{Value: "abc\xff"},
  1157. want: "\"abc\xff\"",
  1158. wantErr: true,
  1159. }, {
  1160. desc: "StringValue field with invalid UTF8 error",
  1161. input: &pb2.KnownTypes{
  1162. OptString: &wrapperspb.StringValue{Value: "abc\xff"},
  1163. },
  1164. want: "{\n \"optString\": \"abc\xff\"\n}",
  1165. wantErr: true,
  1166. }, {
  1167. desc: "BytesValue",
  1168. input: &wrapperspb.BytesValue{Value: []byte("hello")},
  1169. want: `"aGVsbG8="`,
  1170. }, {
  1171. desc: "Empty",
  1172. input: &emptypb.Empty{},
  1173. want: `{}`,
  1174. }, {
  1175. desc: "NullValue field",
  1176. input: &pb2.KnownTypes{OptNull: new(structpb.NullValue)},
  1177. want: `{
  1178. "optNull": null
  1179. }`,
  1180. }, {
  1181. desc: "Value empty",
  1182. input: &structpb.Value{},
  1183. wantErr: true,
  1184. }, {
  1185. desc: "Value empty field",
  1186. input: &pb2.KnownTypes{
  1187. OptValue: &structpb.Value{},
  1188. },
  1189. wantErr: true,
  1190. }, {
  1191. desc: "Value contains NullValue",
  1192. input: &structpb.Value{Kind: &structpb.Value_NullValue{}},
  1193. want: `null`,
  1194. }, {
  1195. desc: "Value contains BoolValue",
  1196. input: &structpb.Value{Kind: &structpb.Value_BoolValue{}},
  1197. want: `false`,
  1198. }, {
  1199. desc: "Value contains NumberValue",
  1200. input: &structpb.Value{Kind: &structpb.Value_NumberValue{1.02}},
  1201. want: `1.02`,
  1202. }, {
  1203. desc: "Value contains StringValue",
  1204. input: &structpb.Value{Kind: &structpb.Value_StringValue{"hello"}},
  1205. want: `"hello"`,
  1206. }, {
  1207. desc: "Value contains StringValue with invalid UTF8",
  1208. input: &structpb.Value{Kind: &structpb.Value_StringValue{"\xff"}},
  1209. want: "\"\xff\"",
  1210. wantErr: true,
  1211. }, {
  1212. desc: "Value contains Struct",
  1213. input: &structpb.Value{
  1214. Kind: &structpb.Value_StructValue{
  1215. &structpb.Struct{
  1216. Fields: map[string]*structpb.Value{
  1217. "null": {Kind: &structpb.Value_NullValue{}},
  1218. "number": {Kind: &structpb.Value_NumberValue{}},
  1219. "string": {Kind: &structpb.Value_StringValue{}},
  1220. "struct": {Kind: &structpb.Value_StructValue{}},
  1221. "list": {Kind: &structpb.Value_ListValue{}},
  1222. "bool": {Kind: &structpb.Value_BoolValue{}},
  1223. },
  1224. },
  1225. },
  1226. },
  1227. want: `{
  1228. "bool": false,
  1229. "list": [],
  1230. "null": null,
  1231. "number": 0,
  1232. "string": "",
  1233. "struct": {}
  1234. }`,
  1235. }, {
  1236. desc: "Value contains ListValue",
  1237. input: &structpb.Value{
  1238. Kind: &structpb.Value_ListValue{
  1239. &structpb.ListValue{
  1240. Values: []*structpb.Value{
  1241. {Kind: &structpb.Value_BoolValue{}},
  1242. {Kind: &structpb.Value_NullValue{}},
  1243. {Kind: &structpb.Value_NumberValue{}},
  1244. {Kind: &structpb.Value_StringValue{}},
  1245. {Kind: &structpb.Value_StructValue{}},
  1246. {Kind: &structpb.Value_ListValue{}},
  1247. },
  1248. },
  1249. },
  1250. },
  1251. want: `[
  1252. false,
  1253. null,
  1254. 0,
  1255. "",
  1256. {},
  1257. []
  1258. ]`,
  1259. }, {
  1260. desc: "Struct with nil map",
  1261. input: &structpb.Struct{},
  1262. want: `{}`,
  1263. }, {
  1264. desc: "Struct with empty map",
  1265. input: &structpb.Struct{
  1266. Fields: map[string]*structpb.Value{},
  1267. },
  1268. want: `{}`,
  1269. }, {
  1270. desc: "Struct",
  1271. input: &structpb.Struct{
  1272. Fields: map[string]*structpb.Value{
  1273. "bool": {Kind: &structpb.Value_BoolValue{true}},
  1274. "null": {Kind: &structpb.Value_NullValue{}},
  1275. "number": {Kind: &structpb.Value_NumberValue{3.1415}},
  1276. "string": {Kind: &structpb.Value_StringValue{"hello"}},
  1277. "struct": {
  1278. Kind: &structpb.Value_StructValue{
  1279. &structpb.Struct{
  1280. Fields: map[string]*structpb.Value{
  1281. "string": {Kind: &structpb.Value_StringValue{"world"}},
  1282. },
  1283. },
  1284. },
  1285. },
  1286. "list": {
  1287. Kind: &structpb.Value_ListValue{
  1288. &structpb.ListValue{
  1289. Values: []*structpb.Value{
  1290. {Kind: &structpb.Value_BoolValue{}},
  1291. {Kind: &structpb.Value_NullValue{}},
  1292. {Kind: &structpb.Value_NumberValue{}},
  1293. },
  1294. },
  1295. },
  1296. },
  1297. },
  1298. },
  1299. want: `{
  1300. "bool": true,
  1301. "list": [
  1302. false,
  1303. null,
  1304. 0
  1305. ],
  1306. "null": null,
  1307. "number": 3.1415,
  1308. "string": "hello",
  1309. "struct": {
  1310. "string": "world"
  1311. }
  1312. }`,
  1313. }, {
  1314. desc: "Struct message with invalid UTF8 string",
  1315. input: &structpb.Struct{
  1316. Fields: map[string]*structpb.Value{
  1317. "string": {Kind: &structpb.Value_StringValue{"\xff"}},
  1318. },
  1319. },
  1320. want: "{\n \"string\": \"\xff\"\n}",
  1321. wantErr: true,
  1322. }, {
  1323. desc: "ListValue with nil values",
  1324. input: &structpb.ListValue{},
  1325. want: `[]`,
  1326. }, {
  1327. desc: "ListValue with empty values",
  1328. input: &structpb.ListValue{
  1329. Values: []*structpb.Value{},
  1330. },
  1331. want: `[]`,
  1332. }, {
  1333. desc: "ListValue",
  1334. input: &structpb.ListValue{
  1335. Values: []*structpb.Value{
  1336. {Kind: &structpb.Value_BoolValue{true}},
  1337. {Kind: &structpb.Value_NullValue{}},
  1338. {Kind: &structpb.Value_NumberValue{3.1415}},
  1339. {Kind: &structpb.Value_StringValue{"hello"}},
  1340. {
  1341. Kind: &structpb.Value_ListValue{
  1342. &structpb.ListValue{
  1343. Values: []*structpb.Value{
  1344. {Kind: &structpb.Value_BoolValue{}},
  1345. {Kind: &structpb.Value_NullValue{}},
  1346. {Kind: &structpb.Value_NumberValue{}},
  1347. },
  1348. },
  1349. },
  1350. },
  1351. {
  1352. Kind: &structpb.Value_StructValue{
  1353. &structpb.Struct{
  1354. Fields: map[string]*structpb.Value{
  1355. "string": {Kind: &structpb.Value_StringValue{"world"}},
  1356. },
  1357. },
  1358. },
  1359. },
  1360. },
  1361. },
  1362. want: `[
  1363. true,
  1364. null,
  1365. 3.1415,
  1366. "hello",
  1367. [
  1368. false,
  1369. null,
  1370. 0
  1371. ],
  1372. {
  1373. "string": "world"
  1374. }
  1375. ]`,
  1376. }, {
  1377. desc: "ListValue with invalid UTF8 string",
  1378. input: &structpb.ListValue{
  1379. Values: []*structpb.Value{
  1380. {Kind: &structpb.Value_StringValue{"\xff"}},
  1381. },
  1382. },
  1383. want: "[\n \"\xff\"\n]",
  1384. wantErr: true,
  1385. }, {
  1386. desc: "Duration empty",
  1387. input: &durationpb.Duration{},
  1388. want: `"0s"`,
  1389. }, {
  1390. desc: "Duration with secs",
  1391. input: &durationpb.Duration{Seconds: 3},
  1392. want: `"3s"`,
  1393. }, {
  1394. desc: "Duration with -secs",
  1395. input: &durationpb.Duration{Seconds: -3},
  1396. want: `"-3s"`,
  1397. }, {
  1398. desc: "Duration with nanos",
  1399. input: &durationpb.Duration{Nanos: 1e6},
  1400. want: `"0.001s"`,
  1401. }, {
  1402. desc: "Duration with -nanos",
  1403. input: &durationpb.Duration{Nanos: -1e6},
  1404. want: `"-0.001s"`,
  1405. }, {
  1406. desc: "Duration with large secs",
  1407. input: &durationpb.Duration{Seconds: 1e10, Nanos: 1},
  1408. want: `"10000000000.000000001s"`,
  1409. }, {
  1410. desc: "Duration with 6-digit nanos",
  1411. input: &durationpb.Duration{Nanos: 1e4},
  1412. want: `"0.000010s"`,
  1413. }, {
  1414. desc: "Duration with 3-digit nanos",
  1415. input: &durationpb.Duration{Nanos: 1e6},
  1416. want: `"0.001s"`,
  1417. }, {
  1418. desc: "Duration with -secs -nanos",
  1419. input: &durationpb.Duration{Seconds: -123, Nanos: -450},
  1420. want: `"-123.000000450s"`,
  1421. }, {
  1422. desc: "Duration max value",
  1423. input: &durationpb.Duration{Seconds: 315576000000, Nanos: 999999999},
  1424. want: `"315576000000.999999999s"`,
  1425. }, {
  1426. desc: "Duration min value",
  1427. input: &durationpb.Duration{Seconds: -315576000000, Nanos: -999999999},
  1428. want: `"-315576000000.999999999s"`,
  1429. }, {
  1430. desc: "Duration with +secs -nanos",
  1431. input: &durationpb.Duration{Seconds: 1, Nanos: -1},
  1432. wantErr: true,
  1433. }, {
  1434. desc: "Duration with -secs +nanos",
  1435. input: &durationpb.Duration{Seconds: -1, Nanos: 1},
  1436. wantErr: true,
  1437. }, {
  1438. desc: "Duration with +secs out of range",
  1439. input: &durationpb.Duration{Seconds: 315576000001},
  1440. wantErr: true,
  1441. }, {
  1442. desc: "Duration with -secs out of range",
  1443. input: &durationpb.Duration{Seconds: -315576000001},
  1444. wantErr: true,
  1445. }, {
  1446. desc: "Duration with +nanos out of range",
  1447. input: &durationpb.Duration{Seconds: 0, Nanos: 1e9},
  1448. wantErr: true,
  1449. }, {
  1450. desc: "Duration with -nanos out of range",
  1451. input: &durationpb.Duration{Seconds: 0, Nanos: -1e9},
  1452. wantErr: true,
  1453. }, {
  1454. desc: "Timestamp zero",
  1455. input: &timestamppb.Timestamp{},
  1456. want: `"1970-01-01T00:00:00Z"`,
  1457. }, {
  1458. desc: "Timestamp",
  1459. input: &timestamppb.Timestamp{Seconds: 1553036601},
  1460. want: `"2019-03-19T23:03:21Z"`,
  1461. }, {
  1462. desc: "Timestamp with nanos",
  1463. input: &timestamppb.Timestamp{Seconds: 1553036601, Nanos: 1},
  1464. want: `"2019-03-19T23:03:21.000000001Z"`,
  1465. }, {
  1466. desc: "Timestamp with 6-digit nanos",
  1467. input: &timestamppb.Timestamp{Nanos: 1e3},
  1468. want: `"1970-01-01T00:00:00.000001Z"`,
  1469. }, {
  1470. desc: "Timestamp with 3-digit nanos",
  1471. input: &timestamppb.Timestamp{Nanos: 1e7},
  1472. want: `"1970-01-01T00:00:00.010Z"`,
  1473. }, {
  1474. desc: "Timestamp max value",
  1475. input: &timestamppb.Timestamp{Seconds: 253402300799, Nanos: 999999999},
  1476. want: `"9999-12-31T23:59:59.999999999Z"`,
  1477. }, {
  1478. desc: "Timestamp min value",
  1479. input: &timestamppb.Timestamp{Seconds: -62135596800},
  1480. want: `"0001-01-01T00:00:00Z"`,
  1481. }, {
  1482. desc: "Timestamp with +secs out of range",
  1483. input: &timestamppb.Timestamp{Seconds: 253402300800},
  1484. wantErr: true,
  1485. }, {
  1486. desc: "Timestamp with -secs out of range",
  1487. input: &timestamppb.Timestamp{Seconds: -62135596801},
  1488. wantErr: true,
  1489. }, {
  1490. desc: "Timestamp with -nanos",
  1491. input: &timestamppb.Timestamp{Nanos: -1},
  1492. wantErr: true,
  1493. }, {
  1494. desc: "Timestamp with +nanos out of range",
  1495. input: &timestamppb.Timestamp{Nanos: 1e9},
  1496. wantErr: true,
  1497. }, {
  1498. desc: "FieldMask empty",
  1499. input: &fieldmaskpb.FieldMask{},
  1500. want: `""`,
  1501. }, {
  1502. desc: "FieldMask",
  1503. input: &fieldmaskpb.FieldMask{
  1504. Paths: []string{
  1505. "foo",
  1506. "foo_bar",
  1507. "foo.bar_qux",
  1508. "_foo",
  1509. },
  1510. },
  1511. want: `"foo,fooBar,foo.barQux,Foo"`,
  1512. }, {
  1513. desc: "FieldMask error 1",
  1514. input: &fieldmaskpb.FieldMask{
  1515. Paths: []string{"foo_"},
  1516. },
  1517. wantErr: true,
  1518. }, {
  1519. desc: "FieldMask error 2",
  1520. input: &fieldmaskpb.FieldMask{
  1521. Paths: []string{"foo__bar"},
  1522. },
  1523. wantErr: true,
  1524. }, {
  1525. desc: "Any empty",
  1526. input: &anypb.Any{},
  1527. want: `{}`,
  1528. }, {
  1529. desc: "Any with non-custom message",
  1530. mo: protojson.MarshalOptions{
  1531. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
  1532. },
  1533. input: func() proto.Message {
  1534. m := &pb2.Nested{
  1535. OptString: scalar.String("embedded inside Any"),
  1536. OptNested: &pb2.Nested{
  1537. OptString: scalar.String("inception"),
  1538. },
  1539. }
  1540. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1541. if err != nil {
  1542. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1543. }
  1544. return &anypb.Any{
  1545. TypeUrl: "foo/pb2.Nested",
  1546. Value: b,
  1547. }
  1548. }(),
  1549. want: `{
  1550. "@type": "foo/pb2.Nested",
  1551. "optString": "embedded inside Any",
  1552. "optNested": {
  1553. "optString": "inception"
  1554. }
  1555. }`,
  1556. }, {
  1557. desc: "Any with empty embedded message",
  1558. mo: protojson.MarshalOptions{
  1559. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
  1560. },
  1561. input: &anypb.Any{TypeUrl: "foo/pb2.Nested"},
  1562. want: `{
  1563. "@type": "foo/pb2.Nested"
  1564. }`,
  1565. }, {
  1566. desc: "Any without registered type",
  1567. mo: protojson.MarshalOptions{Resolver: preg.NewTypes()},
  1568. input: &anypb.Any{TypeUrl: "foo/pb2.Nested"},
  1569. wantErr: true,
  1570. }, {
  1571. desc: "Any with missing required error",
  1572. mo: protojson.MarshalOptions{
  1573. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.PartialRequired{})),
  1574. },
  1575. input: func() proto.Message {
  1576. m := &pb2.PartialRequired{
  1577. OptString: scalar.String("embedded inside Any"),
  1578. }
  1579. b, err := proto.MarshalOptions{
  1580. AllowPartial: true,
  1581. Deterministic: true,
  1582. }.Marshal(m)
  1583. if err != nil {
  1584. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1585. }
  1586. return &anypb.Any{
  1587. TypeUrl: string(m.ProtoReflect().Descriptor().FullName()),
  1588. Value: b,
  1589. }
  1590. }(),
  1591. want: `{
  1592. "@type": "pb2.PartialRequired",
  1593. "optString": "embedded inside Any"
  1594. }`,
  1595. wantErr: true,
  1596. }, {
  1597. desc: "Any with partial required and AllowPartial",
  1598. mo: protojson.MarshalOptions{
  1599. AllowPartial: true,
  1600. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.PartialRequired{})),
  1601. },
  1602. input: func() proto.Message {
  1603. m := &pb2.PartialRequired{
  1604. OptString: scalar.String("embedded inside Any"),
  1605. }
  1606. b, err := proto.MarshalOptions{
  1607. AllowPartial: true,
  1608. Deterministic: true,
  1609. }.Marshal(m)
  1610. if err != nil {
  1611. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1612. }
  1613. return &anypb.Any{
  1614. TypeUrl: string(m.ProtoReflect().Descriptor().FullName()),
  1615. Value: b,
  1616. }
  1617. }(),
  1618. want: `{
  1619. "@type": "pb2.PartialRequired",
  1620. "optString": "embedded inside Any"
  1621. }`,
  1622. }, {
  1623. desc: "Any with invalid UTF8",
  1624. mo: protojson.MarshalOptions{
  1625. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
  1626. },
  1627. input: func() proto.Message {
  1628. m := &pb2.Nested{
  1629. OptString: scalar.String("abc\xff"),
  1630. }
  1631. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1632. if err != nil {
  1633. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1634. }
  1635. return &anypb.Any{
  1636. TypeUrl: "foo/pb2.Nested",
  1637. Value: b,
  1638. }
  1639. }(),
  1640. want: `{
  1641. "@type": "foo/pb2.Nested",
  1642. "optString": "` + "abc\xff" + `"
  1643. }`,
  1644. wantErr: true,
  1645. }, {
  1646. desc: "Any with invalid value",
  1647. mo: protojson.MarshalOptions{
  1648. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
  1649. },
  1650. input: &anypb.Any{
  1651. TypeUrl: "foo/pb2.Nested",
  1652. Value: dhex("80"),
  1653. },
  1654. wantErr: true,
  1655. }, {
  1656. desc: "Any with BoolValue",
  1657. mo: protojson.MarshalOptions{
  1658. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&wrapperspb.BoolValue{})),
  1659. },
  1660. input: func() proto.Message {
  1661. m := &wrapperspb.BoolValue{Value: true}
  1662. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1663. if err != nil {
  1664. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1665. }
  1666. return &anypb.Any{
  1667. TypeUrl: "type.googleapis.com/google.protobuf.BoolValue",
  1668. Value: b,
  1669. }
  1670. }(),
  1671. want: `{
  1672. "@type": "type.googleapis.com/google.protobuf.BoolValue",
  1673. "value": true
  1674. }`,
  1675. }, {
  1676. desc: "Any with Empty",
  1677. mo: protojson.MarshalOptions{
  1678. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&emptypb.Empty{})),
  1679. },
  1680. input: func() proto.Message {
  1681. m := &emptypb.Empty{}
  1682. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1683. if err != nil {
  1684. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1685. }
  1686. return &anypb.Any{
  1687. TypeUrl: "type.googleapis.com/google.protobuf.Empty",
  1688. Value: b,
  1689. }
  1690. }(),
  1691. want: `{
  1692. "@type": "type.googleapis.com/google.protobuf.Empty",
  1693. "value": {}
  1694. }`,
  1695. }, {
  1696. desc: "Any with StringValue containing invalid UTF8",
  1697. mo: protojson.MarshalOptions{
  1698. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&wrapperspb.StringValue{})),
  1699. },
  1700. input: func() proto.Message {
  1701. m := &wrapperspb.StringValue{Value: "abcd"}
  1702. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1703. if err != nil {
  1704. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1705. }
  1706. return &anypb.Any{
  1707. TypeUrl: "google.protobuf.StringValue",
  1708. Value: bytes.Replace(b, []byte("abcd"), []byte("abc\xff"), -1),
  1709. }
  1710. }(),
  1711. want: `{
  1712. "@type": "google.protobuf.StringValue",
  1713. "value": "` + "abc\xff" + `"
  1714. }`,
  1715. wantErr: true,
  1716. }, {
  1717. desc: "Any with Int64Value",
  1718. mo: protojson.MarshalOptions{
  1719. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&wrapperspb.Int64Value{})),
  1720. },
  1721. input: func() proto.Message {
  1722. m := &wrapperspb.Int64Value{Value: 42}
  1723. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1724. if err != nil {
  1725. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1726. }
  1727. return &anypb.Any{
  1728. TypeUrl: "google.protobuf.Int64Value",
  1729. Value: b,
  1730. }
  1731. }(),
  1732. want: `{
  1733. "@type": "google.protobuf.Int64Value",
  1734. "value": "42"
  1735. }`,
  1736. }, {
  1737. desc: "Any with Duration",
  1738. mo: protojson.MarshalOptions{
  1739. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&durationpb.Duration{})),
  1740. },
  1741. input: func() proto.Message {
  1742. m := &durationpb.Duration{}
  1743. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1744. if err != nil {
  1745. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1746. }
  1747. return &anypb.Any{
  1748. TypeUrl: "type.googleapis.com/google.protobuf.Duration",
  1749. Value: b,
  1750. }
  1751. }(),
  1752. want: `{
  1753. "@type": "type.googleapis.com/google.protobuf.Duration",
  1754. "value": "0s"
  1755. }`,
  1756. }, {
  1757. desc: "Any with empty Value",
  1758. mo: protojson.MarshalOptions{
  1759. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&structpb.Value{})),
  1760. },
  1761. input: func() proto.Message {
  1762. m := &structpb.Value{}
  1763. b, err := proto.Marshal(m)
  1764. if err != nil {
  1765. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1766. }
  1767. return &anypb.Any{
  1768. TypeUrl: "type.googleapis.com/google.protobuf.Value",
  1769. Value: b,
  1770. }
  1771. }(),
  1772. wantErr: true,
  1773. }, {
  1774. desc: "Any with Value of StringValue",
  1775. mo: protojson.MarshalOptions{
  1776. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&structpb.Value{})),
  1777. },
  1778. input: func() proto.Message {
  1779. m := &structpb.Value{Kind: &structpb.Value_StringValue{"abcd"}}
  1780. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1781. if err != nil {
  1782. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1783. }
  1784. return &anypb.Any{
  1785. TypeUrl: "type.googleapis.com/google.protobuf.Value",
  1786. Value: bytes.Replace(b, []byte("abcd"), []byte("abc\xff"), -1),
  1787. }
  1788. }(),
  1789. want: `{
  1790. "@type": "type.googleapis.com/google.protobuf.Value",
  1791. "value": "` + "abc\xff" + `"
  1792. }`,
  1793. wantErr: true,
  1794. }, {
  1795. desc: "Any with Value of NullValue",
  1796. mo: protojson.MarshalOptions{
  1797. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&structpb.Value{})),
  1798. },
  1799. input: func() proto.Message {
  1800. m := &structpb.Value{Kind: &structpb.Value_NullValue{}}
  1801. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1802. if err != nil {
  1803. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1804. }
  1805. return &anypb.Any{
  1806. TypeUrl: "type.googleapis.com/google.protobuf.Value",
  1807. Value: b,
  1808. }
  1809. }(),
  1810. want: `{
  1811. "@type": "type.googleapis.com/google.protobuf.Value",
  1812. "value": null
  1813. }`,
  1814. }, {
  1815. desc: "Any with Struct",
  1816. mo: protojson.MarshalOptions{
  1817. Resolver: preg.NewTypes(
  1818. pimpl.Export{}.MessageTypeOf(&structpb.Struct{}),
  1819. pimpl.Export{}.MessageTypeOf(&structpb.Value{}),
  1820. pimpl.Export{}.MessageTypeOf(&wrapperspb.BoolValue{}),
  1821. pimpl.Export{}.EnumTypeOf(structpb.NullValue_NULL_VALUE),
  1822. pimpl.Export{}.MessageTypeOf(&wrapperspb.StringValue{}),
  1823. ),
  1824. },
  1825. input: func() proto.Message {
  1826. m := &structpb.Struct{
  1827. Fields: map[string]*structpb.Value{
  1828. "bool": {Kind: &structpb.Value_BoolValue{true}},
  1829. "null": {Kind: &structpb.Value_NullValue{}},
  1830. "string": {Kind: &structpb.Value_StringValue{"hello"}},
  1831. "struct": {
  1832. Kind: &structpb.Value_StructValue{
  1833. &structpb.Struct{
  1834. Fields: map[string]*structpb.Value{
  1835. "string": {Kind: &structpb.Value_StringValue{"world"}},
  1836. },
  1837. },
  1838. },
  1839. },
  1840. },
  1841. }
  1842. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1843. if err != nil {
  1844. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1845. }
  1846. return &anypb.Any{
  1847. TypeUrl: "google.protobuf.Struct",
  1848. Value: b,
  1849. }
  1850. }(),
  1851. want: `{
  1852. "@type": "google.protobuf.Struct",
  1853. "value": {
  1854. "bool": true,
  1855. "null": null,
  1856. "string": "hello",
  1857. "struct": {
  1858. "string": "world"
  1859. }
  1860. }
  1861. }`,
  1862. }, {
  1863. desc: "Any with missing type_url",
  1864. mo: protojson.MarshalOptions{
  1865. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&wrapperspb.BoolValue{})),
  1866. },
  1867. input: func() proto.Message {
  1868. m := &wrapperspb.BoolValue{Value: true}
  1869. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1870. if err != nil {
  1871. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1872. }
  1873. return &anypb.Any{
  1874. Value: b,
  1875. }
  1876. }(),
  1877. wantErr: true,
  1878. }, {
  1879. desc: "well known types as field values",
  1880. mo: protojson.MarshalOptions{
  1881. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&emptypb.Empty{})),
  1882. },
  1883. input: &pb2.KnownTypes{
  1884. OptBool: &wrapperspb.BoolValue{Value: false},
  1885. OptInt32: &wrapperspb.Int32Value{Value: 42},
  1886. OptInt64: &wrapperspb.Int64Value{Value: 42},
  1887. OptUint32: &wrapperspb.UInt32Value{Value: 42},
  1888. OptUint64: &wrapperspb.UInt64Value{Value: 42},
  1889. OptFloat: &wrapperspb.FloatValue{Value: 1.23},
  1890. OptDouble: &wrapperspb.DoubleValue{Value: 3.1415},
  1891. OptString: &wrapperspb.StringValue{Value: "hello"},
  1892. OptBytes: &wrapperspb.BytesValue{Value: []byte("hello")},
  1893. OptDuration: &durationpb.Duration{Seconds: 123},
  1894. OptTimestamp: &timestamppb.Timestamp{Seconds: 1553036601},
  1895. OptStruct: &structpb.Struct{
  1896. Fields: map[string]*structpb.Value{
  1897. "string": {Kind: &structpb.Value_StringValue{"hello"}},
  1898. },
  1899. },
  1900. OptList: &structpb.ListValue{
  1901. Values: []*structpb.Value{
  1902. {Kind: &structpb.Value_NullValue{}},
  1903. {Kind: &structpb.Value_StringValue{}},
  1904. {Kind: &structpb.Value_StructValue{}},
  1905. {Kind: &structpb.Value_ListValue{}},
  1906. },
  1907. },
  1908. OptValue: &structpb.Value{
  1909. Kind: &structpb.Value_StringValue{"world"},
  1910. },
  1911. OptEmpty: &emptypb.Empty{},
  1912. OptAny: &anypb.Any{
  1913. TypeUrl: "google.protobuf.Empty",
  1914. },
  1915. OptFieldmask: &fieldmaskpb.FieldMask{
  1916. Paths: []string{"foo_bar", "bar_foo"},
  1917. },
  1918. },
  1919. want: `{
  1920. "optBool": false,
  1921. "optInt32": 42,
  1922. "optInt64": "42",
  1923. "optUint32": 42,
  1924. "optUint64": "42",
  1925. "optFloat": 1.23,
  1926. "optDouble": 3.1415,
  1927. "optString": "hello",
  1928. "optBytes": "aGVsbG8=",
  1929. "optDuration": "123s",
  1930. "optTimestamp": "2019-03-19T23:03:21Z",
  1931. "optStruct": {
  1932. "string": "hello"
  1933. },
  1934. "optList": [
  1935. null,
  1936. "",
  1937. {},
  1938. []
  1939. ],
  1940. "optValue": "world",
  1941. "optEmpty": {},
  1942. "optAny": {
  1943. "@type": "google.protobuf.Empty",
  1944. "value": {}
  1945. },
  1946. "optFieldmask": "fooBar,barFoo"
  1947. }`,
  1948. }}
  1949. for _, tt := range tests {
  1950. tt := tt
  1951. t.Run(tt.desc, func(t *testing.T) {
  1952. // Use 2-space indentation on all MarshalOptions.
  1953. tt.mo.Indent = " "
  1954. b, err := tt.mo.Marshal(tt.input)
  1955. if err != nil && !tt.wantErr {
  1956. t.Errorf("Marshal() returned error: %v\n", err)
  1957. }
  1958. if err == nil && tt.wantErr {
  1959. t.Errorf("Marshal() got nil error, want error\n")
  1960. }
  1961. got := string(b)
  1962. if got != tt.want {
  1963. t.Errorf("Marshal()\n<got>\n%v\n<want>\n%v\n", got, tt.want)
  1964. if diff := cmp.Diff(tt.want, got, splitLines); diff != "" {
  1965. t.Errorf("Marshal() diff -want +got\n%v\n", diff)
  1966. }
  1967. }
  1968. })
  1969. }
  1970. }