encode_test.go 45 KB

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