encode_test.go 45 KB

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