encode_test.go 46 KB

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