encode_test.go 45 KB

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