encode_test.go 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950
  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: func() proto.Message {
  880. m := &pb2.Scalars{
  881. OptString: scalar.String("no unknowns"),
  882. }
  883. m.ProtoReflect().SetUnknown(pack.Message{
  884. pack.Tag{101, pack.BytesType}, pack.String("hello world"),
  885. }.Marshal())
  886. return m
  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: "extensions of repeated fields",
  934. input: func() proto.Message {
  935. m := &pb2.Extensions{}
  936. setExtension(m, pb2.E_RptExtEnum, &[]pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE})
  937. setExtension(m, pb2.E_RptExtFixed32, &[]uint32{42, 47})
  938. setExtension(m, pb2.E_RptExtNested, &[]*pb2.Nested{
  939. &pb2.Nested{OptString: scalar.String("one")},
  940. &pb2.Nested{OptString: scalar.String("two")},
  941. &pb2.Nested{OptString: scalar.String("three")},
  942. })
  943. return m
  944. }(),
  945. want: `{
  946. "[pb2.rpt_ext_enum]": [
  947. "TEN",
  948. 101,
  949. "ONE"
  950. ],
  951. "[pb2.rpt_ext_fixed32]": [
  952. 42,
  953. 47
  954. ],
  955. "[pb2.rpt_ext_nested]": [
  956. {
  957. "optString": "one"
  958. },
  959. {
  960. "optString": "two"
  961. },
  962. {
  963. "optString": "three"
  964. }
  965. ]
  966. }`,
  967. }, {
  968. desc: "extensions of non-repeated fields in another message",
  969. input: func() proto.Message {
  970. m := &pb2.Extensions{}
  971. setExtension(m, pb2.E_ExtensionsContainer_OptExtBool, true)
  972. setExtension(m, pb2.E_ExtensionsContainer_OptExtString, "extension field")
  973. setExtension(m, pb2.E_ExtensionsContainer_OptExtEnum, pb2.Enum_TEN)
  974. setExtension(m, pb2.E_ExtensionsContainer_OptExtNested, &pb2.Nested{
  975. OptString: scalar.String("nested in an extension"),
  976. OptNested: &pb2.Nested{
  977. OptString: scalar.String("another nested in an extension"),
  978. },
  979. })
  980. return m
  981. }(),
  982. want: `{
  983. "[pb2.ExtensionsContainer.opt_ext_bool]": true,
  984. "[pb2.ExtensionsContainer.opt_ext_enum]": "TEN",
  985. "[pb2.ExtensionsContainer.opt_ext_nested]": {
  986. "optString": "nested in an extension",
  987. "optNested": {
  988. "optString": "another nested in an extension"
  989. }
  990. },
  991. "[pb2.ExtensionsContainer.opt_ext_string]": "extension field"
  992. }`,
  993. }, {
  994. desc: "extensions of repeated fields in another message",
  995. input: func() proto.Message {
  996. m := &pb2.Extensions{
  997. OptString: scalar.String("non-extension field"),
  998. OptBool: scalar.Bool(true),
  999. OptInt32: scalar.Int32(42),
  1000. }
  1001. setExtension(m, pb2.E_ExtensionsContainer_RptExtEnum, &[]pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE})
  1002. setExtension(m, pb2.E_ExtensionsContainer_RptExtString, &[]string{"hello", "world"})
  1003. setExtension(m, pb2.E_ExtensionsContainer_RptExtNested, &[]*pb2.Nested{
  1004. &pb2.Nested{OptString: scalar.String("one")},
  1005. &pb2.Nested{OptString: scalar.String("two")},
  1006. &pb2.Nested{OptString: scalar.String("three")},
  1007. })
  1008. return m
  1009. }(),
  1010. want: `{
  1011. "optString": "non-extension field",
  1012. "optBool": true,
  1013. "optInt32": 42,
  1014. "[pb2.ExtensionsContainer.rpt_ext_enum]": [
  1015. "TEN",
  1016. 101,
  1017. "ONE"
  1018. ],
  1019. "[pb2.ExtensionsContainer.rpt_ext_nested]": [
  1020. {
  1021. "optString": "one"
  1022. },
  1023. {
  1024. "optString": "two"
  1025. },
  1026. {
  1027. "optString": "three"
  1028. }
  1029. ],
  1030. "[pb2.ExtensionsContainer.rpt_ext_string]": [
  1031. "hello",
  1032. "world"
  1033. ]
  1034. }`,
  1035. }, {
  1036. desc: "MessageSet",
  1037. input: func() proto.Message {
  1038. m := &pb2.MessageSet{}
  1039. setExtension(m, pb2.E_MessageSetExtension_MessageSetExtension, &pb2.MessageSetExtension{
  1040. OptString: scalar.String("a messageset extension"),
  1041. })
  1042. setExtension(m, pb2.E_MessageSetExtension_NotMessageSetExtension, &pb2.MessageSetExtension{
  1043. OptString: scalar.String("not a messageset extension"),
  1044. })
  1045. setExtension(m, pb2.E_MessageSetExtension_ExtNested, &pb2.Nested{
  1046. OptString: scalar.String("just a regular extension"),
  1047. })
  1048. return m
  1049. }(),
  1050. want: `{
  1051. "[pb2.MessageSetExtension]": {
  1052. "optString": "a messageset extension"
  1053. },
  1054. "[pb2.MessageSetExtension.ext_nested]": {
  1055. "optString": "just a regular extension"
  1056. },
  1057. "[pb2.MessageSetExtension.not_message_set_extension]": {
  1058. "optString": "not a messageset extension"
  1059. }
  1060. }`,
  1061. }, {
  1062. desc: "not real MessageSet 1",
  1063. input: func() proto.Message {
  1064. m := &pb2.FakeMessageSet{}
  1065. setExtension(m, pb2.E_FakeMessageSetExtension_MessageSetExtension, &pb2.FakeMessageSetExtension{
  1066. OptString: scalar.String("not a messageset extension"),
  1067. })
  1068. return m
  1069. }(),
  1070. want: `{
  1071. "[pb2.FakeMessageSetExtension.message_set_extension]": {
  1072. "optString": "not a messageset extension"
  1073. }
  1074. }`,
  1075. }, {
  1076. desc: "not real MessageSet 2",
  1077. input: func() proto.Message {
  1078. m := &pb2.MessageSet{}
  1079. setExtension(m, pb2.E_MessageSetExtension, &pb2.FakeMessageSetExtension{
  1080. OptString: scalar.String("another not a messageset extension"),
  1081. })
  1082. return m
  1083. }(),
  1084. want: `{
  1085. "[pb2.message_set_extension]": {
  1086. "optString": "another not a messageset extension"
  1087. }
  1088. }`,
  1089. }, {
  1090. desc: "BoolValue empty",
  1091. input: &wrapperspb.BoolValue{},
  1092. want: `false`,
  1093. }, {
  1094. desc: "BoolValue",
  1095. input: &wrapperspb.BoolValue{Value: true},
  1096. want: `true`,
  1097. }, {
  1098. desc: "Int32Value empty",
  1099. input: &wrapperspb.Int32Value{},
  1100. want: `0`,
  1101. }, {
  1102. desc: "Int32Value",
  1103. input: &wrapperspb.Int32Value{Value: 42},
  1104. want: `42`,
  1105. }, {
  1106. desc: "Int64Value",
  1107. input: &wrapperspb.Int64Value{Value: 42},
  1108. want: `"42"`,
  1109. }, {
  1110. desc: "UInt32Value",
  1111. input: &wrapperspb.UInt32Value{Value: 42},
  1112. want: `42`,
  1113. }, {
  1114. desc: "UInt64Value",
  1115. input: &wrapperspb.UInt64Value{Value: 42},
  1116. want: `"42"`,
  1117. }, {
  1118. desc: "FloatValue",
  1119. input: &wrapperspb.FloatValue{Value: 1.02},
  1120. want: `1.02`,
  1121. }, {
  1122. desc: "FloatValue Infinity",
  1123. input: &wrapperspb.FloatValue{Value: float32(math.Inf(-1))},
  1124. want: `"-Infinity"`,
  1125. }, {
  1126. desc: "DoubleValue",
  1127. input: &wrapperspb.DoubleValue{Value: 1.02},
  1128. want: `1.02`,
  1129. }, {
  1130. desc: "DoubleValue NaN",
  1131. input: &wrapperspb.DoubleValue{Value: math.NaN()},
  1132. want: `"NaN"`,
  1133. }, {
  1134. desc: "StringValue empty",
  1135. input: &wrapperspb.StringValue{},
  1136. want: `""`,
  1137. }, {
  1138. desc: "StringValue",
  1139. input: &wrapperspb.StringValue{Value: "谷歌"},
  1140. want: `"谷歌"`,
  1141. }, {
  1142. desc: "StringValue with invalid UTF8 error",
  1143. input: &wrapperspb.StringValue{Value: "abc\xff"},
  1144. wantErr: true,
  1145. }, {
  1146. desc: "StringValue field with invalid UTF8 error",
  1147. input: &pb2.KnownTypes{
  1148. OptString: &wrapperspb.StringValue{Value: "abc\xff"},
  1149. },
  1150. wantErr: true,
  1151. }, {
  1152. desc: "BytesValue",
  1153. input: &wrapperspb.BytesValue{Value: []byte("hello")},
  1154. want: `"aGVsbG8="`,
  1155. }, {
  1156. desc: "Empty",
  1157. input: &emptypb.Empty{},
  1158. want: `{}`,
  1159. }, {
  1160. desc: "NullValue field",
  1161. input: &pb2.KnownTypes{OptNull: new(structpb.NullValue)},
  1162. want: `{
  1163. "optNull": null
  1164. }`,
  1165. }, {
  1166. desc: "Value empty",
  1167. input: &structpb.Value{},
  1168. wantErr: true,
  1169. }, {
  1170. desc: "Value empty field",
  1171. input: &pb2.KnownTypes{
  1172. OptValue: &structpb.Value{},
  1173. },
  1174. wantErr: true,
  1175. }, {
  1176. desc: "Value contains NullValue",
  1177. input: &structpb.Value{Kind: &structpb.Value_NullValue{}},
  1178. want: `null`,
  1179. }, {
  1180. desc: "Value contains BoolValue",
  1181. input: &structpb.Value{Kind: &structpb.Value_BoolValue{}},
  1182. want: `false`,
  1183. }, {
  1184. desc: "Value contains NumberValue",
  1185. input: &structpb.Value{Kind: &structpb.Value_NumberValue{1.02}},
  1186. want: `1.02`,
  1187. }, {
  1188. desc: "Value contains StringValue",
  1189. input: &structpb.Value{Kind: &structpb.Value_StringValue{"hello"}},
  1190. want: `"hello"`,
  1191. }, {
  1192. desc: "Value contains StringValue with invalid UTF8",
  1193. input: &structpb.Value{Kind: &structpb.Value_StringValue{"\xff"}},
  1194. wantErr: true,
  1195. }, {
  1196. desc: "Value contains Struct",
  1197. input: &structpb.Value{
  1198. Kind: &structpb.Value_StructValue{
  1199. &structpb.Struct{
  1200. Fields: map[string]*structpb.Value{
  1201. "null": {Kind: &structpb.Value_NullValue{}},
  1202. "number": {Kind: &structpb.Value_NumberValue{}},
  1203. "string": {Kind: &structpb.Value_StringValue{}},
  1204. "struct": {Kind: &structpb.Value_StructValue{}},
  1205. "list": {Kind: &structpb.Value_ListValue{}},
  1206. "bool": {Kind: &structpb.Value_BoolValue{}},
  1207. },
  1208. },
  1209. },
  1210. },
  1211. want: `{
  1212. "bool": false,
  1213. "list": [],
  1214. "null": null,
  1215. "number": 0,
  1216. "string": "",
  1217. "struct": {}
  1218. }`,
  1219. }, {
  1220. desc: "Value contains ListValue",
  1221. input: &structpb.Value{
  1222. Kind: &structpb.Value_ListValue{
  1223. &structpb.ListValue{
  1224. Values: []*structpb.Value{
  1225. {Kind: &structpb.Value_BoolValue{}},
  1226. {Kind: &structpb.Value_NullValue{}},
  1227. {Kind: &structpb.Value_NumberValue{}},
  1228. {Kind: &structpb.Value_StringValue{}},
  1229. {Kind: &structpb.Value_StructValue{}},
  1230. {Kind: &structpb.Value_ListValue{}},
  1231. },
  1232. },
  1233. },
  1234. },
  1235. want: `[
  1236. false,
  1237. null,
  1238. 0,
  1239. "",
  1240. {},
  1241. []
  1242. ]`,
  1243. }, {
  1244. desc: "Struct with nil map",
  1245. input: &structpb.Struct{},
  1246. want: `{}`,
  1247. }, {
  1248. desc: "Struct with empty map",
  1249. input: &structpb.Struct{
  1250. Fields: map[string]*structpb.Value{},
  1251. },
  1252. want: `{}`,
  1253. }, {
  1254. desc: "Struct",
  1255. input: &structpb.Struct{
  1256. Fields: map[string]*structpb.Value{
  1257. "bool": {Kind: &structpb.Value_BoolValue{true}},
  1258. "null": {Kind: &structpb.Value_NullValue{}},
  1259. "number": {Kind: &structpb.Value_NumberValue{3.1415}},
  1260. "string": {Kind: &structpb.Value_StringValue{"hello"}},
  1261. "struct": {
  1262. Kind: &structpb.Value_StructValue{
  1263. &structpb.Struct{
  1264. Fields: map[string]*structpb.Value{
  1265. "string": {Kind: &structpb.Value_StringValue{"world"}},
  1266. },
  1267. },
  1268. },
  1269. },
  1270. "list": {
  1271. Kind: &structpb.Value_ListValue{
  1272. &structpb.ListValue{
  1273. Values: []*structpb.Value{
  1274. {Kind: &structpb.Value_BoolValue{}},
  1275. {Kind: &structpb.Value_NullValue{}},
  1276. {Kind: &structpb.Value_NumberValue{}},
  1277. },
  1278. },
  1279. },
  1280. },
  1281. },
  1282. },
  1283. want: `{
  1284. "bool": true,
  1285. "list": [
  1286. false,
  1287. null,
  1288. 0
  1289. ],
  1290. "null": null,
  1291. "number": 3.1415,
  1292. "string": "hello",
  1293. "struct": {
  1294. "string": "world"
  1295. }
  1296. }`,
  1297. }, {
  1298. desc: "Struct message with invalid UTF8 string",
  1299. input: &structpb.Struct{
  1300. Fields: map[string]*structpb.Value{
  1301. "string": {Kind: &structpb.Value_StringValue{"\xff"}},
  1302. },
  1303. },
  1304. wantErr: true,
  1305. }, {
  1306. desc: "ListValue with nil values",
  1307. input: &structpb.ListValue{},
  1308. want: `[]`,
  1309. }, {
  1310. desc: "ListValue with empty values",
  1311. input: &structpb.ListValue{
  1312. Values: []*structpb.Value{},
  1313. },
  1314. want: `[]`,
  1315. }, {
  1316. desc: "ListValue",
  1317. input: &structpb.ListValue{
  1318. Values: []*structpb.Value{
  1319. {Kind: &structpb.Value_BoolValue{true}},
  1320. {Kind: &structpb.Value_NullValue{}},
  1321. {Kind: &structpb.Value_NumberValue{3.1415}},
  1322. {Kind: &structpb.Value_StringValue{"hello"}},
  1323. {
  1324. Kind: &structpb.Value_ListValue{
  1325. &structpb.ListValue{
  1326. Values: []*structpb.Value{
  1327. {Kind: &structpb.Value_BoolValue{}},
  1328. {Kind: &structpb.Value_NullValue{}},
  1329. {Kind: &structpb.Value_NumberValue{}},
  1330. },
  1331. },
  1332. },
  1333. },
  1334. {
  1335. Kind: &structpb.Value_StructValue{
  1336. &structpb.Struct{
  1337. Fields: map[string]*structpb.Value{
  1338. "string": {Kind: &structpb.Value_StringValue{"world"}},
  1339. },
  1340. },
  1341. },
  1342. },
  1343. },
  1344. },
  1345. want: `[
  1346. true,
  1347. null,
  1348. 3.1415,
  1349. "hello",
  1350. [
  1351. false,
  1352. null,
  1353. 0
  1354. ],
  1355. {
  1356. "string": "world"
  1357. }
  1358. ]`,
  1359. }, {
  1360. desc: "ListValue with invalid UTF8 string",
  1361. input: &structpb.ListValue{
  1362. Values: []*structpb.Value{
  1363. {Kind: &structpb.Value_StringValue{"\xff"}},
  1364. },
  1365. },
  1366. wantErr: true,
  1367. }, {
  1368. desc: "Duration empty",
  1369. input: &durationpb.Duration{},
  1370. want: `"0s"`,
  1371. }, {
  1372. desc: "Duration with secs",
  1373. input: &durationpb.Duration{Seconds: 3},
  1374. want: `"3s"`,
  1375. }, {
  1376. desc: "Duration with -secs",
  1377. input: &durationpb.Duration{Seconds: -3},
  1378. want: `"-3s"`,
  1379. }, {
  1380. desc: "Duration with nanos",
  1381. input: &durationpb.Duration{Nanos: 1e6},
  1382. want: `"0.001s"`,
  1383. }, {
  1384. desc: "Duration with -nanos",
  1385. input: &durationpb.Duration{Nanos: -1e6},
  1386. want: `"-0.001s"`,
  1387. }, {
  1388. desc: "Duration with large secs",
  1389. input: &durationpb.Duration{Seconds: 1e10, Nanos: 1},
  1390. want: `"10000000000.000000001s"`,
  1391. }, {
  1392. desc: "Duration with 6-digit nanos",
  1393. input: &durationpb.Duration{Nanos: 1e4},
  1394. want: `"0.000010s"`,
  1395. }, {
  1396. desc: "Duration with 3-digit nanos",
  1397. input: &durationpb.Duration{Nanos: 1e6},
  1398. want: `"0.001s"`,
  1399. }, {
  1400. desc: "Duration with -secs -nanos",
  1401. input: &durationpb.Duration{Seconds: -123, Nanos: -450},
  1402. want: `"-123.000000450s"`,
  1403. }, {
  1404. desc: "Duration max value",
  1405. input: &durationpb.Duration{Seconds: 315576000000, Nanos: 999999999},
  1406. want: `"315576000000.999999999s"`,
  1407. }, {
  1408. desc: "Duration min value",
  1409. input: &durationpb.Duration{Seconds: -315576000000, Nanos: -999999999},
  1410. want: `"-315576000000.999999999s"`,
  1411. }, {
  1412. desc: "Duration with +secs -nanos",
  1413. input: &durationpb.Duration{Seconds: 1, Nanos: -1},
  1414. wantErr: true,
  1415. }, {
  1416. desc: "Duration with -secs +nanos",
  1417. input: &durationpb.Duration{Seconds: -1, Nanos: 1},
  1418. wantErr: true,
  1419. }, {
  1420. desc: "Duration with +secs out of range",
  1421. input: &durationpb.Duration{Seconds: 315576000001},
  1422. wantErr: true,
  1423. }, {
  1424. desc: "Duration with -secs out of range",
  1425. input: &durationpb.Duration{Seconds: -315576000001},
  1426. wantErr: true,
  1427. }, {
  1428. desc: "Duration with +nanos out of range",
  1429. input: &durationpb.Duration{Seconds: 0, Nanos: 1e9},
  1430. wantErr: true,
  1431. }, {
  1432. desc: "Duration with -nanos out of range",
  1433. input: &durationpb.Duration{Seconds: 0, Nanos: -1e9},
  1434. wantErr: true,
  1435. }, {
  1436. desc: "Timestamp zero",
  1437. input: &timestamppb.Timestamp{},
  1438. want: `"1970-01-01T00:00:00Z"`,
  1439. }, {
  1440. desc: "Timestamp",
  1441. input: &timestamppb.Timestamp{Seconds: 1553036601},
  1442. want: `"2019-03-19T23:03:21Z"`,
  1443. }, {
  1444. desc: "Timestamp with nanos",
  1445. input: &timestamppb.Timestamp{Seconds: 1553036601, Nanos: 1},
  1446. want: `"2019-03-19T23:03:21.000000001Z"`,
  1447. }, {
  1448. desc: "Timestamp with 6-digit nanos",
  1449. input: &timestamppb.Timestamp{Nanos: 1e3},
  1450. want: `"1970-01-01T00:00:00.000001Z"`,
  1451. }, {
  1452. desc: "Timestamp with 3-digit nanos",
  1453. input: &timestamppb.Timestamp{Nanos: 1e7},
  1454. want: `"1970-01-01T00:00:00.010Z"`,
  1455. }, {
  1456. desc: "Timestamp max value",
  1457. input: &timestamppb.Timestamp{Seconds: 253402300799, Nanos: 999999999},
  1458. want: `"9999-12-31T23:59:59.999999999Z"`,
  1459. }, {
  1460. desc: "Timestamp min value",
  1461. input: &timestamppb.Timestamp{Seconds: -62135596800},
  1462. want: `"0001-01-01T00:00:00Z"`,
  1463. }, {
  1464. desc: "Timestamp with +secs out of range",
  1465. input: &timestamppb.Timestamp{Seconds: 253402300800},
  1466. wantErr: true,
  1467. }, {
  1468. desc: "Timestamp with -secs out of range",
  1469. input: &timestamppb.Timestamp{Seconds: -62135596801},
  1470. wantErr: true,
  1471. }, {
  1472. desc: "Timestamp with -nanos",
  1473. input: &timestamppb.Timestamp{Nanos: -1},
  1474. wantErr: true,
  1475. }, {
  1476. desc: "Timestamp with +nanos out of range",
  1477. input: &timestamppb.Timestamp{Nanos: 1e9},
  1478. wantErr: true,
  1479. }, {
  1480. desc: "FieldMask empty",
  1481. input: &fieldmaskpb.FieldMask{},
  1482. want: `""`,
  1483. }, {
  1484. desc: "FieldMask",
  1485. input: &fieldmaskpb.FieldMask{
  1486. Paths: []string{
  1487. "foo",
  1488. "foo_bar",
  1489. "foo.bar_qux",
  1490. "_foo",
  1491. },
  1492. },
  1493. want: `"foo,fooBar,foo.barQux,Foo"`,
  1494. }, {
  1495. desc: "FieldMask error 1",
  1496. input: &fieldmaskpb.FieldMask{
  1497. Paths: []string{"foo_"},
  1498. },
  1499. wantErr: true,
  1500. }, {
  1501. desc: "FieldMask error 2",
  1502. input: &fieldmaskpb.FieldMask{
  1503. Paths: []string{"foo__bar"},
  1504. },
  1505. wantErr: true,
  1506. }, {
  1507. desc: "Any empty",
  1508. input: &anypb.Any{},
  1509. want: `{}`,
  1510. }, {
  1511. desc: "Any with non-custom message",
  1512. mo: protojson.MarshalOptions{
  1513. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
  1514. },
  1515. input: func() proto.Message {
  1516. m := &pb2.Nested{
  1517. OptString: scalar.String("embedded inside Any"),
  1518. OptNested: &pb2.Nested{
  1519. OptString: scalar.String("inception"),
  1520. },
  1521. }
  1522. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1523. if err != nil {
  1524. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1525. }
  1526. return &anypb.Any{
  1527. TypeUrl: "foo/pb2.Nested",
  1528. Value: b,
  1529. }
  1530. }(),
  1531. want: `{
  1532. "@type": "foo/pb2.Nested",
  1533. "optString": "embedded inside Any",
  1534. "optNested": {
  1535. "optString": "inception"
  1536. }
  1537. }`,
  1538. }, {
  1539. desc: "Any with empty embedded message",
  1540. mo: protojson.MarshalOptions{
  1541. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
  1542. },
  1543. input: &anypb.Any{TypeUrl: "foo/pb2.Nested"},
  1544. want: `{
  1545. "@type": "foo/pb2.Nested"
  1546. }`,
  1547. }, {
  1548. desc: "Any without registered type",
  1549. mo: protojson.MarshalOptions{Resolver: preg.NewTypes()},
  1550. input: &anypb.Any{TypeUrl: "foo/pb2.Nested"},
  1551. wantErr: true,
  1552. }, {
  1553. desc: "Any with missing required",
  1554. mo: protojson.MarshalOptions{
  1555. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.PartialRequired{})),
  1556. },
  1557. input: func() proto.Message {
  1558. m := &pb2.PartialRequired{
  1559. OptString: scalar.String("embedded inside Any"),
  1560. }
  1561. b, err := proto.MarshalOptions{
  1562. AllowPartial: true,
  1563. Deterministic: true,
  1564. }.Marshal(m)
  1565. if err != nil {
  1566. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1567. }
  1568. return &anypb.Any{
  1569. TypeUrl: string(m.ProtoReflect().Descriptor().FullName()),
  1570. Value: b,
  1571. }
  1572. }(),
  1573. want: `{
  1574. "@type": "pb2.PartialRequired",
  1575. "optString": "embedded inside Any"
  1576. }`,
  1577. }, {
  1578. desc: "Any with partial required and AllowPartial",
  1579. mo: protojson.MarshalOptions{
  1580. AllowPartial: true,
  1581. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.PartialRequired{})),
  1582. },
  1583. input: func() proto.Message {
  1584. m := &pb2.PartialRequired{
  1585. OptString: scalar.String("embedded inside Any"),
  1586. }
  1587. b, err := proto.MarshalOptions{
  1588. AllowPartial: true,
  1589. Deterministic: true,
  1590. }.Marshal(m)
  1591. if err != nil {
  1592. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1593. }
  1594. return &anypb.Any{
  1595. TypeUrl: string(m.ProtoReflect().Descriptor().FullName()),
  1596. Value: b,
  1597. }
  1598. }(),
  1599. want: `{
  1600. "@type": "pb2.PartialRequired",
  1601. "optString": "embedded inside Any"
  1602. }`,
  1603. }, {
  1604. desc: "Any with invalid UTF8",
  1605. mo: protojson.MarshalOptions{
  1606. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
  1607. },
  1608. input: func() proto.Message {
  1609. m := &pb2.Nested{
  1610. OptString: scalar.String("abc\xff"),
  1611. }
  1612. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1613. if err != nil {
  1614. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1615. }
  1616. return &anypb.Any{
  1617. TypeUrl: "foo/pb2.Nested",
  1618. Value: b,
  1619. }
  1620. }(),
  1621. wantErr: true,
  1622. }, {
  1623. desc: "Any with invalid value",
  1624. mo: protojson.MarshalOptions{
  1625. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
  1626. },
  1627. input: &anypb.Any{
  1628. TypeUrl: "foo/pb2.Nested",
  1629. Value: dhex("80"),
  1630. },
  1631. wantErr: true,
  1632. }, {
  1633. desc: "Any with BoolValue",
  1634. mo: protojson.MarshalOptions{
  1635. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&wrapperspb.BoolValue{})),
  1636. },
  1637. input: func() proto.Message {
  1638. m := &wrapperspb.BoolValue{Value: true}
  1639. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1640. if err != nil {
  1641. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1642. }
  1643. return &anypb.Any{
  1644. TypeUrl: "type.googleapis.com/google.protobuf.BoolValue",
  1645. Value: b,
  1646. }
  1647. }(),
  1648. want: `{
  1649. "@type": "type.googleapis.com/google.protobuf.BoolValue",
  1650. "value": true
  1651. }`,
  1652. }, {
  1653. desc: "Any with Empty",
  1654. mo: protojson.MarshalOptions{
  1655. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&emptypb.Empty{})),
  1656. },
  1657. input: func() proto.Message {
  1658. m := &emptypb.Empty{}
  1659. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1660. if err != nil {
  1661. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1662. }
  1663. return &anypb.Any{
  1664. TypeUrl: "type.googleapis.com/google.protobuf.Empty",
  1665. Value: b,
  1666. }
  1667. }(),
  1668. want: `{
  1669. "@type": "type.googleapis.com/google.protobuf.Empty",
  1670. "value": {}
  1671. }`,
  1672. }, {
  1673. desc: "Any with StringValue containing invalid UTF8",
  1674. mo: protojson.MarshalOptions{
  1675. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&wrapperspb.StringValue{})),
  1676. },
  1677. input: func() proto.Message {
  1678. m := &wrapperspb.StringValue{Value: "abcd"}
  1679. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1680. if err != nil {
  1681. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1682. }
  1683. return &anypb.Any{
  1684. TypeUrl: "google.protobuf.StringValue",
  1685. Value: bytes.Replace(b, []byte("abcd"), []byte("abc\xff"), -1),
  1686. }
  1687. }(),
  1688. wantErr: true,
  1689. }, {
  1690. desc: "Any with Int64Value",
  1691. mo: protojson.MarshalOptions{
  1692. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&wrapperspb.Int64Value{})),
  1693. },
  1694. input: func() proto.Message {
  1695. m := &wrapperspb.Int64Value{Value: 42}
  1696. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1697. if err != nil {
  1698. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1699. }
  1700. return &anypb.Any{
  1701. TypeUrl: "google.protobuf.Int64Value",
  1702. Value: b,
  1703. }
  1704. }(),
  1705. want: `{
  1706. "@type": "google.protobuf.Int64Value",
  1707. "value": "42"
  1708. }`,
  1709. }, {
  1710. desc: "Any with Duration",
  1711. mo: protojson.MarshalOptions{
  1712. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&durationpb.Duration{})),
  1713. },
  1714. input: func() proto.Message {
  1715. m := &durationpb.Duration{}
  1716. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1717. if err != nil {
  1718. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1719. }
  1720. return &anypb.Any{
  1721. TypeUrl: "type.googleapis.com/google.protobuf.Duration",
  1722. Value: b,
  1723. }
  1724. }(),
  1725. want: `{
  1726. "@type": "type.googleapis.com/google.protobuf.Duration",
  1727. "value": "0s"
  1728. }`,
  1729. }, {
  1730. desc: "Any with empty Value",
  1731. mo: protojson.MarshalOptions{
  1732. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&structpb.Value{})),
  1733. },
  1734. input: func() proto.Message {
  1735. m := &structpb.Value{}
  1736. b, err := proto.Marshal(m)
  1737. if err != nil {
  1738. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1739. }
  1740. return &anypb.Any{
  1741. TypeUrl: "type.googleapis.com/google.protobuf.Value",
  1742. Value: b,
  1743. }
  1744. }(),
  1745. wantErr: true,
  1746. }, {
  1747. desc: "Any with Value of StringValue",
  1748. mo: protojson.MarshalOptions{
  1749. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&structpb.Value{})),
  1750. },
  1751. input: func() proto.Message {
  1752. m := &structpb.Value{Kind: &structpb.Value_StringValue{"abcd"}}
  1753. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1754. if err != nil {
  1755. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1756. }
  1757. return &anypb.Any{
  1758. TypeUrl: "type.googleapis.com/google.protobuf.Value",
  1759. Value: bytes.Replace(b, []byte("abcd"), []byte("abc\xff"), -1),
  1760. }
  1761. }(),
  1762. wantErr: true,
  1763. }, {
  1764. desc: "Any with Value of NullValue",
  1765. mo: protojson.MarshalOptions{
  1766. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&structpb.Value{})),
  1767. },
  1768. input: func() proto.Message {
  1769. m := &structpb.Value{Kind: &structpb.Value_NullValue{}}
  1770. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1771. if err != nil {
  1772. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1773. }
  1774. return &anypb.Any{
  1775. TypeUrl: "type.googleapis.com/google.protobuf.Value",
  1776. Value: b,
  1777. }
  1778. }(),
  1779. want: `{
  1780. "@type": "type.googleapis.com/google.protobuf.Value",
  1781. "value": null
  1782. }`,
  1783. }, {
  1784. desc: "Any with Struct",
  1785. mo: protojson.MarshalOptions{
  1786. Resolver: preg.NewTypes(
  1787. pimpl.Export{}.MessageTypeOf(&structpb.Struct{}),
  1788. pimpl.Export{}.MessageTypeOf(&structpb.Value{}),
  1789. pimpl.Export{}.MessageTypeOf(&wrapperspb.BoolValue{}),
  1790. pimpl.Export{}.EnumTypeOf(structpb.NullValue_NULL_VALUE),
  1791. pimpl.Export{}.MessageTypeOf(&wrapperspb.StringValue{}),
  1792. ),
  1793. },
  1794. input: func() proto.Message {
  1795. m := &structpb.Struct{
  1796. Fields: map[string]*structpb.Value{
  1797. "bool": {Kind: &structpb.Value_BoolValue{true}},
  1798. "null": {Kind: &structpb.Value_NullValue{}},
  1799. "string": {Kind: &structpb.Value_StringValue{"hello"}},
  1800. "struct": {
  1801. Kind: &structpb.Value_StructValue{
  1802. &structpb.Struct{
  1803. Fields: map[string]*structpb.Value{
  1804. "string": {Kind: &structpb.Value_StringValue{"world"}},
  1805. },
  1806. },
  1807. },
  1808. },
  1809. },
  1810. }
  1811. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1812. if err != nil {
  1813. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1814. }
  1815. return &anypb.Any{
  1816. TypeUrl: "google.protobuf.Struct",
  1817. Value: b,
  1818. }
  1819. }(),
  1820. want: `{
  1821. "@type": "google.protobuf.Struct",
  1822. "value": {
  1823. "bool": true,
  1824. "null": null,
  1825. "string": "hello",
  1826. "struct": {
  1827. "string": "world"
  1828. }
  1829. }
  1830. }`,
  1831. }, {
  1832. desc: "Any with missing type_url",
  1833. mo: protojson.MarshalOptions{
  1834. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&wrapperspb.BoolValue{})),
  1835. },
  1836. input: func() proto.Message {
  1837. m := &wrapperspb.BoolValue{Value: true}
  1838. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1839. if err != nil {
  1840. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1841. }
  1842. return &anypb.Any{
  1843. Value: b,
  1844. }
  1845. }(),
  1846. wantErr: true,
  1847. }, {
  1848. desc: "well known types as field values",
  1849. mo: protojson.MarshalOptions{
  1850. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&emptypb.Empty{})),
  1851. },
  1852. input: &pb2.KnownTypes{
  1853. OptBool: &wrapperspb.BoolValue{Value: false},
  1854. OptInt32: &wrapperspb.Int32Value{Value: 42},
  1855. OptInt64: &wrapperspb.Int64Value{Value: 42},
  1856. OptUint32: &wrapperspb.UInt32Value{Value: 42},
  1857. OptUint64: &wrapperspb.UInt64Value{Value: 42},
  1858. OptFloat: &wrapperspb.FloatValue{Value: 1.23},
  1859. OptDouble: &wrapperspb.DoubleValue{Value: 3.1415},
  1860. OptString: &wrapperspb.StringValue{Value: "hello"},
  1861. OptBytes: &wrapperspb.BytesValue{Value: []byte("hello")},
  1862. OptDuration: &durationpb.Duration{Seconds: 123},
  1863. OptTimestamp: &timestamppb.Timestamp{Seconds: 1553036601},
  1864. OptStruct: &structpb.Struct{
  1865. Fields: map[string]*structpb.Value{
  1866. "string": {Kind: &structpb.Value_StringValue{"hello"}},
  1867. },
  1868. },
  1869. OptList: &structpb.ListValue{
  1870. Values: []*structpb.Value{
  1871. {Kind: &structpb.Value_NullValue{}},
  1872. {Kind: &structpb.Value_StringValue{}},
  1873. {Kind: &structpb.Value_StructValue{}},
  1874. {Kind: &structpb.Value_ListValue{}},
  1875. },
  1876. },
  1877. OptValue: &structpb.Value{
  1878. Kind: &structpb.Value_StringValue{"world"},
  1879. },
  1880. OptEmpty: &emptypb.Empty{},
  1881. OptAny: &anypb.Any{
  1882. TypeUrl: "google.protobuf.Empty",
  1883. },
  1884. OptFieldmask: &fieldmaskpb.FieldMask{
  1885. Paths: []string{"foo_bar", "bar_foo"},
  1886. },
  1887. },
  1888. want: `{
  1889. "optBool": false,
  1890. "optInt32": 42,
  1891. "optInt64": "42",
  1892. "optUint32": 42,
  1893. "optUint64": "42",
  1894. "optFloat": 1.23,
  1895. "optDouble": 3.1415,
  1896. "optString": "hello",
  1897. "optBytes": "aGVsbG8=",
  1898. "optDuration": "123s",
  1899. "optTimestamp": "2019-03-19T23:03:21Z",
  1900. "optStruct": {
  1901. "string": "hello"
  1902. },
  1903. "optList": [
  1904. null,
  1905. "",
  1906. {},
  1907. []
  1908. ],
  1909. "optValue": "world",
  1910. "optEmpty": {},
  1911. "optAny": {
  1912. "@type": "google.protobuf.Empty",
  1913. "value": {}
  1914. },
  1915. "optFieldmask": "fooBar,barFoo"
  1916. }`,
  1917. }}
  1918. for _, tt := range tests {
  1919. tt := tt
  1920. t.Run(tt.desc, func(t *testing.T) {
  1921. // Use 2-space indentation on all MarshalOptions.
  1922. tt.mo.Indent = " "
  1923. b, err := tt.mo.Marshal(tt.input)
  1924. if err != nil && !tt.wantErr {
  1925. t.Errorf("Marshal() returned error: %v\n", err)
  1926. }
  1927. if err == nil && tt.wantErr {
  1928. t.Errorf("Marshal() got nil error, want error\n")
  1929. }
  1930. got := string(b)
  1931. if got != tt.want {
  1932. t.Errorf("Marshal()\n<got>\n%v\n<want>\n%v\n", got, tt.want)
  1933. if diff := cmp.Diff(tt.want, got, splitLines); diff != "" {
  1934. t.Errorf("Marshal() diff -want +got\n%v\n", diff)
  1935. }
  1936. }
  1937. })
  1938. }
  1939. }