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