encode_test.go 45 KB

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