unmarshaler_test.go 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520
  1. package mapping
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. "strings"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/tal-tech/go-zero/core/stringx"
  10. )
  11. // because json.Number doesn't support strconv.ParseUint(...),
  12. // so we only can test to 62 bits.
  13. const maxUintBitsToTest = 62
  14. func TestUnmarshalWithFullNameNotStruct(t *testing.T) {
  15. var s map[string]interface{}
  16. content := []byte(`{"name":"xiaoming"}`)
  17. err := UnmarshalJsonBytes(content, &s)
  18. assert.Equal(t, errValueNotStruct, err)
  19. }
  20. func TestUnmarshalWithoutTagName(t *testing.T) {
  21. type inner struct {
  22. Optional bool `key:",optional"`
  23. }
  24. m := map[string]interface{}{
  25. "Optional": true,
  26. }
  27. var in inner
  28. assert.Nil(t, UnmarshalKey(m, &in))
  29. assert.True(t, in.Optional)
  30. }
  31. func TestUnmarshalBool(t *testing.T) {
  32. type inner struct {
  33. True bool `key:"yes"`
  34. False bool `key:"no"`
  35. TrueFromOne bool `key:"yesone,string"`
  36. FalseFromZero bool `key:"nozero,string"`
  37. TrueFromTrue bool `key:"yestrue,string"`
  38. FalseFromFalse bool `key:"nofalse,string"`
  39. DefaultTrue bool `key:"defaulttrue,default=1"`
  40. Optional bool `key:"optional,optional"`
  41. }
  42. m := map[string]interface{}{
  43. "yes": true,
  44. "no": false,
  45. "yesone": "1",
  46. "nozero": "0",
  47. "yestrue": "true",
  48. "nofalse": "false",
  49. }
  50. var in inner
  51. ast := assert.New(t)
  52. ast.Nil(UnmarshalKey(m, &in))
  53. ast.True(in.True)
  54. ast.False(in.False)
  55. ast.True(in.TrueFromOne)
  56. ast.False(in.FalseFromZero)
  57. ast.True(in.TrueFromTrue)
  58. ast.False(in.FalseFromFalse)
  59. ast.True(in.DefaultTrue)
  60. }
  61. func TestUnmarshalDuration(t *testing.T) {
  62. type inner struct {
  63. Duration time.Duration `key:"duration"`
  64. LessDuration time.Duration `key:"less"`
  65. MoreDuration time.Duration `key:"more"`
  66. }
  67. m := map[string]interface{}{
  68. "duration": "5s",
  69. "less": "100ms",
  70. "more": "24h",
  71. }
  72. var in inner
  73. assert.Nil(t, UnmarshalKey(m, &in))
  74. assert.Equal(t, time.Second*5, in.Duration)
  75. assert.Equal(t, time.Millisecond*100, in.LessDuration)
  76. assert.Equal(t, time.Hour*24, in.MoreDuration)
  77. }
  78. func TestUnmarshalDurationDefault(t *testing.T) {
  79. type inner struct {
  80. Int int `key:"int"`
  81. Duration time.Duration `key:"duration,default=5s"`
  82. }
  83. m := map[string]interface{}{
  84. "int": 5,
  85. }
  86. var in inner
  87. assert.Nil(t, UnmarshalKey(m, &in))
  88. assert.Equal(t, 5, in.Int)
  89. assert.Equal(t, time.Second*5, in.Duration)
  90. }
  91. func TestUnmarshalDurationPtr(t *testing.T) {
  92. type inner struct {
  93. Duration *time.Duration `key:"duration"`
  94. }
  95. m := map[string]interface{}{
  96. "duration": "5s",
  97. }
  98. var in inner
  99. assert.Nil(t, UnmarshalKey(m, &in))
  100. assert.Equal(t, time.Second*5, *in.Duration)
  101. }
  102. func TestUnmarshalDurationPtrDefault(t *testing.T) {
  103. type inner struct {
  104. Int int `key:"int"`
  105. Value *int `key:",default=5"`
  106. Duration *time.Duration `key:"duration,default=5s"`
  107. }
  108. m := map[string]interface{}{
  109. "int": 5,
  110. }
  111. var in inner
  112. assert.Nil(t, UnmarshalKey(m, &in))
  113. assert.Equal(t, 5, in.Int)
  114. assert.Equal(t, 5, *in.Value)
  115. assert.Equal(t, time.Second*5, *in.Duration)
  116. }
  117. func TestUnmarshalInt(t *testing.T) {
  118. type inner struct {
  119. Int int `key:"int"`
  120. IntFromStr int `key:"intstr,string"`
  121. Int8 int8 `key:"int8"`
  122. Int8FromStr int8 `key:"int8str,string"`
  123. Int16 int16 `key:"int16"`
  124. Int16FromStr int16 `key:"int16str,string"`
  125. Int32 int32 `key:"int32"`
  126. Int32FromStr int32 `key:"int32str,string"`
  127. Int64 int64 `key:"int64"`
  128. Int64FromStr int64 `key:"int64str,string"`
  129. DefaultInt int64 `key:"defaultint,default=11"`
  130. Optional int `key:"optional,optional"`
  131. }
  132. m := map[string]interface{}{
  133. "int": 1,
  134. "intstr": "2",
  135. "int8": int8(3),
  136. "int8str": "4",
  137. "int16": int16(5),
  138. "int16str": "6",
  139. "int32": int32(7),
  140. "int32str": "8",
  141. "int64": int64(9),
  142. "int64str": "10",
  143. }
  144. var in inner
  145. ast := assert.New(t)
  146. ast.Nil(UnmarshalKey(m, &in))
  147. ast.Equal(1, in.Int)
  148. ast.Equal(2, in.IntFromStr)
  149. ast.Equal(int8(3), in.Int8)
  150. ast.Equal(int8(4), in.Int8FromStr)
  151. ast.Equal(int16(5), in.Int16)
  152. ast.Equal(int16(6), in.Int16FromStr)
  153. ast.Equal(int32(7), in.Int32)
  154. ast.Equal(int32(8), in.Int32FromStr)
  155. ast.Equal(int64(9), in.Int64)
  156. ast.Equal(int64(10), in.Int64FromStr)
  157. ast.Equal(int64(11), in.DefaultInt)
  158. }
  159. func TestUnmarshalIntPtr(t *testing.T) {
  160. type inner struct {
  161. Int *int `key:"int"`
  162. }
  163. m := map[string]interface{}{
  164. "int": 1,
  165. }
  166. var in inner
  167. assert.Nil(t, UnmarshalKey(m, &in))
  168. assert.NotNil(t, in.Int)
  169. assert.Equal(t, 1, *in.Int)
  170. }
  171. func TestUnmarshalIntWithDefault(t *testing.T) {
  172. type inner struct {
  173. Int int `key:"int,default=5"`
  174. }
  175. m := map[string]interface{}{
  176. "int": 1,
  177. }
  178. var in inner
  179. assert.Nil(t, UnmarshalKey(m, &in))
  180. assert.Equal(t, 1, in.Int)
  181. }
  182. func TestUnmarshalUint(t *testing.T) {
  183. type inner struct {
  184. Uint uint `key:"uint"`
  185. UintFromStr uint `key:"uintstr,string"`
  186. Uint8 uint8 `key:"uint8"`
  187. Uint8FromStr uint8 `key:"uint8str,string"`
  188. Uint16 uint16 `key:"uint16"`
  189. Uint16FromStr uint16 `key:"uint16str,string"`
  190. Uint32 uint32 `key:"uint32"`
  191. Uint32FromStr uint32 `key:"uint32str,string"`
  192. Uint64 uint64 `key:"uint64"`
  193. Uint64FromStr uint64 `key:"uint64str,string"`
  194. DefaultUint uint `key:"defaultuint,default=11"`
  195. Optional uint `key:"optional,optional"`
  196. }
  197. m := map[string]interface{}{
  198. "uint": uint(1),
  199. "uintstr": "2",
  200. "uint8": uint8(3),
  201. "uint8str": "4",
  202. "uint16": uint16(5),
  203. "uint16str": "6",
  204. "uint32": uint32(7),
  205. "uint32str": "8",
  206. "uint64": uint64(9),
  207. "uint64str": "10",
  208. }
  209. var in inner
  210. ast := assert.New(t)
  211. ast.Nil(UnmarshalKey(m, &in))
  212. ast.Equal(uint(1), in.Uint)
  213. ast.Equal(uint(2), in.UintFromStr)
  214. ast.Equal(uint8(3), in.Uint8)
  215. ast.Equal(uint8(4), in.Uint8FromStr)
  216. ast.Equal(uint16(5), in.Uint16)
  217. ast.Equal(uint16(6), in.Uint16FromStr)
  218. ast.Equal(uint32(7), in.Uint32)
  219. ast.Equal(uint32(8), in.Uint32FromStr)
  220. ast.Equal(uint64(9), in.Uint64)
  221. ast.Equal(uint64(10), in.Uint64FromStr)
  222. ast.Equal(uint(11), in.DefaultUint)
  223. }
  224. func TestUnmarshalFloat(t *testing.T) {
  225. type inner struct {
  226. Float32 float32 `key:"float32"`
  227. Float32Str float32 `key:"float32str,string"`
  228. Float64 float64 `key:"float64"`
  229. Float64Str float64 `key:"float64str,string"`
  230. DefaultFloat float32 `key:"defaultfloat,default=5.5"`
  231. Optional float32 `key:",optional"`
  232. }
  233. m := map[string]interface{}{
  234. "float32": float32(1.5),
  235. "float32str": "2.5",
  236. "float64": float64(3.5),
  237. "float64str": "4.5",
  238. }
  239. var in inner
  240. ast := assert.New(t)
  241. ast.Nil(UnmarshalKey(m, &in))
  242. ast.Equal(float32(1.5), in.Float32)
  243. ast.Equal(float32(2.5), in.Float32Str)
  244. ast.Equal(3.5, in.Float64)
  245. ast.Equal(4.5, in.Float64Str)
  246. ast.Equal(float32(5.5), in.DefaultFloat)
  247. }
  248. func TestUnmarshalInt64Slice(t *testing.T) {
  249. var v struct {
  250. Ages []int64 `key:"ages"`
  251. }
  252. m := map[string]interface{}{
  253. "ages": []int64{1, 2},
  254. }
  255. ast := assert.New(t)
  256. ast.Nil(UnmarshalKey(m, &v))
  257. ast.ElementsMatch([]int64{1, 2}, v.Ages)
  258. }
  259. func TestUnmarshalIntSlice(t *testing.T) {
  260. var v struct {
  261. Ages []int `key:"ages"`
  262. }
  263. m := map[string]interface{}{
  264. "ages": []int{1, 2},
  265. }
  266. ast := assert.New(t)
  267. ast.Nil(UnmarshalKey(m, &v))
  268. ast.ElementsMatch([]int{1, 2}, v.Ages)
  269. }
  270. func TestUnmarshalString(t *testing.T) {
  271. type inner struct {
  272. Name string `key:"name"`
  273. NameStr string `key:"namestr,string"`
  274. NotPresent string `key:",optional"`
  275. NotPresentWithTag string `key:"notpresent,optional"`
  276. DefaultString string `key:"defaultstring,default=hello"`
  277. Optional string `key:",optional"`
  278. }
  279. m := map[string]interface{}{
  280. "name": "kevin",
  281. "namestr": "namewithstring",
  282. }
  283. var in inner
  284. ast := assert.New(t)
  285. ast.Nil(UnmarshalKey(m, &in))
  286. ast.Equal("kevin", in.Name)
  287. ast.Equal("namewithstring", in.NameStr)
  288. ast.Empty(in.NotPresent)
  289. ast.Empty(in.NotPresentWithTag)
  290. ast.Equal("hello", in.DefaultString)
  291. }
  292. func TestUnmarshalStringWithMissing(t *testing.T) {
  293. type inner struct {
  294. Name string `key:"name"`
  295. }
  296. m := map[string]interface{}{}
  297. var in inner
  298. assert.NotNil(t, UnmarshalKey(m, &in))
  299. }
  300. func TestUnmarshalStringSliceFromString(t *testing.T) {
  301. var v struct {
  302. Names []string `key:"names"`
  303. }
  304. m := map[string]interface{}{
  305. "names": `["first", "second"]`,
  306. }
  307. ast := assert.New(t)
  308. ast.Nil(UnmarshalKey(m, &v))
  309. ast.Equal(2, len(v.Names))
  310. ast.Equal("first", v.Names[0])
  311. ast.Equal("second", v.Names[1])
  312. }
  313. func TestUnmarshalIntSliceFromString(t *testing.T) {
  314. var v struct {
  315. Values []int `key:"values"`
  316. }
  317. m := map[string]interface{}{
  318. "values": `[1, 2]`,
  319. }
  320. ast := assert.New(t)
  321. ast.Nil(UnmarshalKey(m, &v))
  322. ast.Equal(2, len(v.Values))
  323. ast.Equal(1, v.Values[0])
  324. ast.Equal(2, v.Values[1])
  325. }
  326. func TestUnmarshalStruct(t *testing.T) {
  327. type address struct {
  328. City string `key:"city"`
  329. ZipCode int `key:"zipcode,string"`
  330. DefaultString string `key:"defaultstring,default=hello"`
  331. Optional string `key:",optional"`
  332. }
  333. type inner struct {
  334. Name string `key:"name"`
  335. Address address `key:"address"`
  336. }
  337. m := map[string]interface{}{
  338. "name": "kevin",
  339. "address": map[string]interface{}{
  340. "city": "shanghai",
  341. "zipcode": "200000",
  342. },
  343. }
  344. var in inner
  345. ast := assert.New(t)
  346. ast.Nil(UnmarshalKey(m, &in))
  347. ast.Equal("kevin", in.Name)
  348. ast.Equal("shanghai", in.Address.City)
  349. ast.Equal(200000, in.Address.ZipCode)
  350. ast.Equal("hello", in.Address.DefaultString)
  351. }
  352. func TestUnmarshalStructOptionalDepends(t *testing.T) {
  353. type address struct {
  354. City string `key:"city"`
  355. Optional string `key:",optional"`
  356. OptionalDepends string `key:",optional=Optional"`
  357. }
  358. type inner struct {
  359. Name string `key:"name"`
  360. Address address `key:"address"`
  361. }
  362. tests := []struct {
  363. input map[string]string
  364. pass bool
  365. }{
  366. {
  367. pass: true,
  368. },
  369. {
  370. input: map[string]string{
  371. "OptionalDepends": "b",
  372. },
  373. pass: false,
  374. },
  375. {
  376. input: map[string]string{
  377. "Optional": "a",
  378. },
  379. pass: false,
  380. },
  381. {
  382. input: map[string]string{
  383. "Optional": "a",
  384. "OptionalDepends": "b",
  385. },
  386. pass: true,
  387. },
  388. }
  389. for _, test := range tests {
  390. t.Run(stringx.Rand(), func(t *testing.T) {
  391. m := map[string]interface{}{
  392. "name": "kevin",
  393. "address": map[string]interface{}{
  394. "city": "shanghai",
  395. },
  396. }
  397. for k, v := range test.input {
  398. m["address"].(map[string]interface{})[k] = v
  399. }
  400. var in inner
  401. ast := assert.New(t)
  402. if test.pass {
  403. ast.Nil(UnmarshalKey(m, &in))
  404. ast.Equal("kevin", in.Name)
  405. ast.Equal("shanghai", in.Address.City)
  406. ast.Equal(test.input["Optional"], in.Address.Optional)
  407. ast.Equal(test.input["OptionalDepends"], in.Address.OptionalDepends)
  408. } else {
  409. ast.NotNil(UnmarshalKey(m, &in))
  410. }
  411. })
  412. }
  413. }
  414. func TestUnmarshalStructOptionalDependsNot(t *testing.T) {
  415. type address struct {
  416. City string `key:"city"`
  417. Optional string `key:",optional"`
  418. OptionalDepends string `key:",optional=!Optional"`
  419. }
  420. type inner struct {
  421. Name string `key:"name"`
  422. Address address `key:"address"`
  423. }
  424. tests := []struct {
  425. input map[string]string
  426. pass bool
  427. }{
  428. {
  429. input: map[string]string{},
  430. pass: false,
  431. },
  432. {
  433. input: map[string]string{
  434. "Optional": "a",
  435. "OptionalDepends": "b",
  436. },
  437. pass: false,
  438. },
  439. {
  440. input: map[string]string{
  441. "Optional": "a",
  442. },
  443. pass: true,
  444. },
  445. {
  446. input: map[string]string{
  447. "OptionalDepends": "b",
  448. },
  449. pass: true,
  450. },
  451. }
  452. for _, test := range tests {
  453. t.Run(stringx.Rand(), func(t *testing.T) {
  454. m := map[string]interface{}{
  455. "name": "kevin",
  456. "address": map[string]interface{}{
  457. "city": "shanghai",
  458. },
  459. }
  460. for k, v := range test.input {
  461. m["address"].(map[string]interface{})[k] = v
  462. }
  463. var in inner
  464. ast := assert.New(t)
  465. if test.pass {
  466. ast.Nil(UnmarshalKey(m, &in))
  467. ast.Equal("kevin", in.Name)
  468. ast.Equal("shanghai", in.Address.City)
  469. ast.Equal(test.input["Optional"], in.Address.Optional)
  470. ast.Equal(test.input["OptionalDepends"], in.Address.OptionalDepends)
  471. } else {
  472. ast.NotNil(UnmarshalKey(m, &in))
  473. }
  474. })
  475. }
  476. }
  477. func TestUnmarshalStructOptionalDependsNotErrorDetails(t *testing.T) {
  478. type address struct {
  479. Optional string `key:",optional"`
  480. OptionalDepends string `key:",optional=!Optional"`
  481. }
  482. type inner struct {
  483. Name string `key:"name"`
  484. Address address `key:"address"`
  485. }
  486. m := map[string]interface{}{
  487. "name": "kevin",
  488. }
  489. var in inner
  490. err := UnmarshalKey(m, &in)
  491. assert.NotNil(t, err)
  492. }
  493. func TestUnmarshalStructOptionalDependsNotNested(t *testing.T) {
  494. type address struct {
  495. Optional string `key:",optional"`
  496. OptionalDepends string `key:",optional=!Optional"`
  497. }
  498. type combo struct {
  499. Name string `key:"name,optional"`
  500. Address address `key:"address"`
  501. }
  502. type inner struct {
  503. Name string `key:"name"`
  504. Combo combo `key:"combo"`
  505. }
  506. m := map[string]interface{}{
  507. "name": "kevin",
  508. }
  509. var in inner
  510. err := UnmarshalKey(m, &in)
  511. assert.NotNil(t, err)
  512. }
  513. func TestUnmarshalStructOptionalNestedDifferentKey(t *testing.T) {
  514. type address struct {
  515. Optional string `dkey:",optional"`
  516. OptionalDepends string `key:",optional"`
  517. }
  518. type combo struct {
  519. Name string `key:"name,optional"`
  520. Address address `key:"address"`
  521. }
  522. type inner struct {
  523. Name string `key:"name"`
  524. Combo combo `key:"combo"`
  525. }
  526. m := map[string]interface{}{
  527. "name": "kevin",
  528. }
  529. var in inner
  530. assert.NotNil(t, UnmarshalKey(m, &in))
  531. }
  532. func TestUnmarshalStructOptionalDependsNotEnoughValue(t *testing.T) {
  533. type address struct {
  534. Optional string `key:",optional"`
  535. OptionalDepends string `key:",optional=!"`
  536. }
  537. type inner struct {
  538. Name string `key:"name"`
  539. Address address `key:"address"`
  540. }
  541. m := map[string]interface{}{
  542. "name": "kevin",
  543. "address": map[string]interface{}{},
  544. }
  545. var in inner
  546. err := UnmarshalKey(m, &in)
  547. assert.NotNil(t, err)
  548. }
  549. func TestUnmarshalAnonymousStructOptionalDepends(t *testing.T) {
  550. type AnonAddress struct {
  551. City string `key:"city"`
  552. Optional string `key:",optional"`
  553. OptionalDepends string `key:",optional=Optional"`
  554. }
  555. type inner struct {
  556. Name string `key:"name"`
  557. AnonAddress
  558. }
  559. tests := []struct {
  560. input map[string]string
  561. pass bool
  562. }{
  563. {
  564. pass: true,
  565. },
  566. {
  567. input: map[string]string{
  568. "OptionalDepends": "b",
  569. },
  570. pass: false,
  571. },
  572. {
  573. input: map[string]string{
  574. "Optional": "a",
  575. },
  576. pass: false,
  577. },
  578. {
  579. input: map[string]string{
  580. "Optional": "a",
  581. "OptionalDepends": "b",
  582. },
  583. pass: true,
  584. },
  585. }
  586. for _, test := range tests {
  587. t.Run(stringx.Rand(), func(t *testing.T) {
  588. m := map[string]interface{}{
  589. "name": "kevin",
  590. "city": "shanghai",
  591. }
  592. for k, v := range test.input {
  593. m[k] = v
  594. }
  595. var in inner
  596. ast := assert.New(t)
  597. if test.pass {
  598. ast.Nil(UnmarshalKey(m, &in))
  599. ast.Equal("kevin", in.Name)
  600. ast.Equal("shanghai", in.City)
  601. ast.Equal(test.input["Optional"], in.Optional)
  602. ast.Equal(test.input["OptionalDepends"], in.OptionalDepends)
  603. } else {
  604. ast.NotNil(UnmarshalKey(m, &in))
  605. }
  606. })
  607. }
  608. }
  609. func TestUnmarshalStructPtr(t *testing.T) {
  610. type address struct {
  611. City string `key:"city"`
  612. ZipCode int `key:"zipcode,string"`
  613. DefaultString string `key:"defaultstring,default=hello"`
  614. Optional string `key:",optional"`
  615. }
  616. type inner struct {
  617. Name string `key:"name"`
  618. Address *address `key:"address"`
  619. }
  620. m := map[string]interface{}{
  621. "name": "kevin",
  622. "address": map[string]interface{}{
  623. "city": "shanghai",
  624. "zipcode": "200000",
  625. },
  626. }
  627. var in inner
  628. ast := assert.New(t)
  629. ast.Nil(UnmarshalKey(m, &in))
  630. ast.Equal("kevin", in.Name)
  631. ast.Equal("shanghai", in.Address.City)
  632. ast.Equal(200000, in.Address.ZipCode)
  633. ast.Equal("hello", in.Address.DefaultString)
  634. }
  635. func TestUnmarshalWithStringIgnored(t *testing.T) {
  636. type inner struct {
  637. True bool `key:"yes"`
  638. False bool `key:"no"`
  639. Int int `key:"int"`
  640. Int8 int8 `key:"int8"`
  641. Int16 int16 `key:"int16"`
  642. Int32 int32 `key:"int32"`
  643. Int64 int64 `key:"int64"`
  644. Uint uint `key:"uint"`
  645. Uint8 uint8 `key:"uint8"`
  646. Uint16 uint16 `key:"uint16"`
  647. Uint32 uint32 `key:"uint32"`
  648. Uint64 uint64 `key:"uint64"`
  649. Float32 float32 `key:"float32"`
  650. Float64 float64 `key:"float64"`
  651. }
  652. m := map[string]interface{}{
  653. "yes": "1",
  654. "no": "0",
  655. "int": "1",
  656. "int8": "3",
  657. "int16": "5",
  658. "int32": "7",
  659. "int64": "9",
  660. "uint": "1",
  661. "uint8": "3",
  662. "uint16": "5",
  663. "uint32": "7",
  664. "uint64": "9",
  665. "float32": "1.5",
  666. "float64": "3.5",
  667. }
  668. var in inner
  669. um := NewUnmarshaler("key", WithStringValues())
  670. ast := assert.New(t)
  671. ast.Nil(um.Unmarshal(m, &in))
  672. ast.True(in.True)
  673. ast.False(in.False)
  674. ast.Equal(1, in.Int)
  675. ast.Equal(int8(3), in.Int8)
  676. ast.Equal(int16(5), in.Int16)
  677. ast.Equal(int32(7), in.Int32)
  678. ast.Equal(int64(9), in.Int64)
  679. ast.Equal(uint(1), in.Uint)
  680. ast.Equal(uint8(3), in.Uint8)
  681. ast.Equal(uint16(5), in.Uint16)
  682. ast.Equal(uint32(7), in.Uint32)
  683. ast.Equal(uint64(9), in.Uint64)
  684. ast.Equal(float32(1.5), in.Float32)
  685. ast.Equal(3.5, in.Float64)
  686. }
  687. func TestUnmarshalJsonNumberInt64(t *testing.T) {
  688. for i := 0; i <= maxUintBitsToTest; i++ {
  689. var intValue int64 = 1 << uint(i)
  690. strValue := strconv.FormatInt(intValue, 10)
  691. number := json.Number(strValue)
  692. m := map[string]interface{}{
  693. "ID": number,
  694. }
  695. var v struct {
  696. ID int64
  697. }
  698. assert.Nil(t, UnmarshalKey(m, &v))
  699. assert.Equal(t, intValue, v.ID)
  700. }
  701. }
  702. func TestUnmarshalJsonNumberUint64(t *testing.T) {
  703. for i := 0; i <= maxUintBitsToTest; i++ {
  704. var intValue uint64 = 1 << uint(i)
  705. strValue := strconv.FormatUint(intValue, 10)
  706. number := json.Number(strValue)
  707. m := map[string]interface{}{
  708. "ID": number,
  709. }
  710. var v struct {
  711. ID uint64
  712. }
  713. assert.Nil(t, UnmarshalKey(m, &v))
  714. assert.Equal(t, intValue, v.ID)
  715. }
  716. }
  717. func TestUnmarshalJsonNumberUint64Ptr(t *testing.T) {
  718. for i := 0; i <= maxUintBitsToTest; i++ {
  719. var intValue uint64 = 1 << uint(i)
  720. strValue := strconv.FormatUint(intValue, 10)
  721. number := json.Number(strValue)
  722. m := map[string]interface{}{
  723. "ID": number,
  724. }
  725. var v struct {
  726. ID *uint64
  727. }
  728. ast := assert.New(t)
  729. ast.Nil(UnmarshalKey(m, &v))
  730. ast.NotNil(v.ID)
  731. ast.Equal(intValue, *v.ID)
  732. }
  733. }
  734. func TestUnmarshalMapOfInt(t *testing.T) {
  735. m := map[string]interface{}{
  736. "Ids": map[string]bool{"first": true},
  737. }
  738. var v struct {
  739. Ids map[string]bool
  740. }
  741. assert.Nil(t, UnmarshalKey(m, &v))
  742. assert.True(t, v.Ids["first"])
  743. }
  744. func TestUnmarshalMapOfStructError(t *testing.T) {
  745. m := map[string]interface{}{
  746. "Ids": map[string]interface{}{"first": "second"},
  747. }
  748. var v struct {
  749. Ids map[string]struct {
  750. Name string
  751. }
  752. }
  753. assert.NotNil(t, UnmarshalKey(m, &v))
  754. }
  755. func TestUnmarshalSlice(t *testing.T) {
  756. m := map[string]interface{}{
  757. "Ids": []interface{}{"first", "second"},
  758. }
  759. var v struct {
  760. Ids []string
  761. }
  762. ast := assert.New(t)
  763. ast.Nil(UnmarshalKey(m, &v))
  764. ast.Equal(2, len(v.Ids))
  765. ast.Equal("first", v.Ids[0])
  766. ast.Equal("second", v.Ids[1])
  767. }
  768. func TestUnmarshalSliceOfStruct(t *testing.T) {
  769. m := map[string]interface{}{
  770. "Ids": []map[string]interface{}{
  771. {
  772. "First": 1,
  773. "Second": 2,
  774. },
  775. },
  776. }
  777. var v struct {
  778. Ids []struct {
  779. First int
  780. Second int
  781. }
  782. }
  783. ast := assert.New(t)
  784. ast.Nil(UnmarshalKey(m, &v))
  785. ast.Equal(1, len(v.Ids))
  786. ast.Equal(1, v.Ids[0].First)
  787. ast.Equal(2, v.Ids[0].Second)
  788. }
  789. func TestUnmarshalWithStringOptionsCorrect(t *testing.T) {
  790. type inner struct {
  791. Value string `key:"value,options=first|second"`
  792. Correct string `key:"correct,options=1|2"`
  793. }
  794. m := map[string]interface{}{
  795. "value": "first",
  796. "correct": "2",
  797. }
  798. var in inner
  799. ast := assert.New(t)
  800. ast.Nil(UnmarshalKey(m, &in))
  801. ast.Equal("first", in.Value)
  802. ast.Equal("2", in.Correct)
  803. }
  804. func TestUnmarshalStringOptionsWithStringOptionsNotString(t *testing.T) {
  805. type inner struct {
  806. Value string `key:"value,options=first|second"`
  807. Correct string `key:"correct,options=1|2"`
  808. }
  809. m := map[string]interface{}{
  810. "value": "first",
  811. "correct": 2,
  812. }
  813. var in inner
  814. unmarshaler := NewUnmarshaler(defaultKeyName, WithStringValues())
  815. ast := assert.New(t)
  816. ast.NotNil(unmarshaler.Unmarshal(m, &in))
  817. }
  818. func TestUnmarshalStringOptionsWithStringOptions(t *testing.T) {
  819. type inner struct {
  820. Value string `key:"value,options=first|second"`
  821. Correct string `key:"correct,options=1|2"`
  822. }
  823. m := map[string]interface{}{
  824. "value": "first",
  825. "correct": "2",
  826. }
  827. var in inner
  828. unmarshaler := NewUnmarshaler(defaultKeyName, WithStringValues())
  829. ast := assert.New(t)
  830. ast.Nil(unmarshaler.Unmarshal(m, &in))
  831. ast.Equal("first", in.Value)
  832. ast.Equal("2", in.Correct)
  833. }
  834. func TestUnmarshalStringOptionsWithStringOptionsPtr(t *testing.T) {
  835. type inner struct {
  836. Value *string `key:"value,options=first|second"`
  837. Correct *int `key:"correct,options=1|2"`
  838. }
  839. m := map[string]interface{}{
  840. "value": "first",
  841. "correct": "2",
  842. }
  843. var in inner
  844. unmarshaler := NewUnmarshaler(defaultKeyName, WithStringValues())
  845. ast := assert.New(t)
  846. ast.Nil(unmarshaler.Unmarshal(m, &in))
  847. ast.True(*in.Value == "first")
  848. ast.True(*in.Correct == 2)
  849. }
  850. func TestUnmarshalStringOptionsWithStringOptionsIncorrect(t *testing.T) {
  851. type inner struct {
  852. Value string `key:"value,options=first|second"`
  853. Correct string `key:"correct,options=1|2"`
  854. }
  855. m := map[string]interface{}{
  856. "value": "third",
  857. "correct": "2",
  858. }
  859. var in inner
  860. unmarshaler := NewUnmarshaler(defaultKeyName, WithStringValues())
  861. ast := assert.New(t)
  862. ast.NotNil(unmarshaler.Unmarshal(m, &in))
  863. }
  864. func TestUnmarshalWithStringOptionsIncorrect(t *testing.T) {
  865. type inner struct {
  866. Value string `key:"value,options=first|second"`
  867. Incorrect string `key:"incorrect,options=1|2"`
  868. }
  869. m := map[string]interface{}{
  870. "value": "first",
  871. "incorrect": "3",
  872. }
  873. var in inner
  874. assert.NotNil(t, UnmarshalKey(m, &in))
  875. }
  876. func TestUnmarshalWithIntOptionsCorrect(t *testing.T) {
  877. type inner struct {
  878. Value string `key:"value,options=first|second"`
  879. Number int `key:"number,options=1|2"`
  880. }
  881. m := map[string]interface{}{
  882. "value": "first",
  883. "number": 2,
  884. }
  885. var in inner
  886. ast := assert.New(t)
  887. ast.Nil(UnmarshalKey(m, &in))
  888. ast.Equal("first", in.Value)
  889. ast.Equal(2, in.Number)
  890. }
  891. func TestUnmarshalWithIntOptionsCorrectPtr(t *testing.T) {
  892. type inner struct {
  893. Value *string `key:"value,options=first|second"`
  894. Number *int `key:"number,options=1|2"`
  895. }
  896. m := map[string]interface{}{
  897. "value": "first",
  898. "number": 2,
  899. }
  900. var in inner
  901. ast := assert.New(t)
  902. ast.Nil(UnmarshalKey(m, &in))
  903. ast.True(*in.Value == "first")
  904. ast.True(*in.Number == 2)
  905. }
  906. func TestUnmarshalWithIntOptionsIncorrect(t *testing.T) {
  907. type inner struct {
  908. Value string `key:"value,options=first|second"`
  909. Incorrect int `key:"incorrect,options=1|2"`
  910. }
  911. m := map[string]interface{}{
  912. "value": "first",
  913. "incorrect": 3,
  914. }
  915. var in inner
  916. assert.NotNil(t, UnmarshalKey(m, &in))
  917. }
  918. func TestUnmarshalWithUintOptionsCorrect(t *testing.T) {
  919. type inner struct {
  920. Value string `key:"value,options=first|second"`
  921. Number uint `key:"number,options=1|2"`
  922. }
  923. m := map[string]interface{}{
  924. "value": "first",
  925. "number": uint(2),
  926. }
  927. var in inner
  928. ast := assert.New(t)
  929. ast.Nil(UnmarshalKey(m, &in))
  930. ast.Equal("first", in.Value)
  931. ast.Equal(uint(2), in.Number)
  932. }
  933. func TestUnmarshalWithUintOptionsIncorrect(t *testing.T) {
  934. type inner struct {
  935. Value string `key:"value,options=first|second"`
  936. Incorrect uint `key:"incorrect,options=1|2"`
  937. }
  938. m := map[string]interface{}{
  939. "value": "first",
  940. "incorrect": uint(3),
  941. }
  942. var in inner
  943. assert.NotNil(t, UnmarshalKey(m, &in))
  944. }
  945. func TestUnmarshalWithOptionsAndDefault(t *testing.T) {
  946. type inner struct {
  947. Value string `key:"value,options=first|second|third,default=second"`
  948. }
  949. m := map[string]interface{}{}
  950. var in inner
  951. assert.Nil(t, UnmarshalKey(m, &in))
  952. assert.Equal(t, "second", in.Value)
  953. }
  954. func TestUnmarshalWithOptionsAndSet(t *testing.T) {
  955. type inner struct {
  956. Value string `key:"value,options=first|second|third,default=second"`
  957. }
  958. m := map[string]interface{}{
  959. "value": "first",
  960. }
  961. var in inner
  962. assert.Nil(t, UnmarshalKey(m, &in))
  963. assert.Equal(t, "first", in.Value)
  964. }
  965. func TestUnmarshalNestedKey(t *testing.T) {
  966. var c struct {
  967. ID int `json:"Persons.first.ID"`
  968. }
  969. m := map[string]interface{}{
  970. "Persons": map[string]interface{}{
  971. "first": map[string]interface{}{
  972. "ID": 1,
  973. },
  974. },
  975. }
  976. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &c))
  977. assert.Equal(t, 1, c.ID)
  978. }
  979. func TestUnmarhsalNestedKeyArray(t *testing.T) {
  980. var c struct {
  981. First []struct {
  982. ID int
  983. } `json:"Persons.first"`
  984. }
  985. m := map[string]interface{}{
  986. "Persons": map[string]interface{}{
  987. "first": []map[string]interface{}{
  988. {"ID": 1},
  989. {"ID": 2},
  990. },
  991. },
  992. }
  993. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &c))
  994. assert.Equal(t, 2, len(c.First))
  995. assert.Equal(t, 1, c.First[0].ID)
  996. }
  997. func TestUnmarshalAnonymousOptionalRequiredProvided(t *testing.T) {
  998. type (
  999. Foo struct {
  1000. Value string `json:"v"`
  1001. }
  1002. Bar struct {
  1003. Foo `json:",optional"`
  1004. }
  1005. )
  1006. m := map[string]interface{}{
  1007. "v": "anything",
  1008. }
  1009. var b Bar
  1010. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1011. assert.Equal(t, "anything", b.Value)
  1012. }
  1013. func TestUnmarshalAnonymousOptionalRequiredMissed(t *testing.T) {
  1014. type (
  1015. Foo struct {
  1016. Value string `json:"v"`
  1017. }
  1018. Bar struct {
  1019. Foo `json:",optional"`
  1020. }
  1021. )
  1022. m := map[string]interface{}{}
  1023. var b Bar
  1024. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1025. assert.True(t, len(b.Value) == 0)
  1026. }
  1027. func TestUnmarshalAnonymousOptionalOptionalProvided(t *testing.T) {
  1028. type (
  1029. Foo struct {
  1030. Value string `json:"v,optional"`
  1031. }
  1032. Bar struct {
  1033. Foo `json:",optional"`
  1034. }
  1035. )
  1036. m := map[string]interface{}{
  1037. "v": "anything",
  1038. }
  1039. var b Bar
  1040. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1041. assert.Equal(t, "anything", b.Value)
  1042. }
  1043. func TestUnmarshalAnonymousOptionalOptionalMissed(t *testing.T) {
  1044. type (
  1045. Foo struct {
  1046. Value string `json:"v,optional"`
  1047. }
  1048. Bar struct {
  1049. Foo `json:",optional"`
  1050. }
  1051. )
  1052. m := map[string]interface{}{}
  1053. var b Bar
  1054. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1055. assert.True(t, len(b.Value) == 0)
  1056. }
  1057. func TestUnmarshalAnonymousOptionalRequiredBothProvided(t *testing.T) {
  1058. type (
  1059. Foo struct {
  1060. Name string `json:"n"`
  1061. Value string `json:"v"`
  1062. }
  1063. Bar struct {
  1064. Foo `json:",optional"`
  1065. }
  1066. )
  1067. m := map[string]interface{}{
  1068. "n": "kevin",
  1069. "v": "anything",
  1070. }
  1071. var b Bar
  1072. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1073. assert.Equal(t, "kevin", b.Name)
  1074. assert.Equal(t, "anything", b.Value)
  1075. }
  1076. func TestUnmarshalAnonymousOptionalRequiredOneProvidedOneMissed(t *testing.T) {
  1077. type (
  1078. Foo struct {
  1079. Name string `json:"n"`
  1080. Value string `json:"v"`
  1081. }
  1082. Bar struct {
  1083. Foo `json:",optional"`
  1084. }
  1085. )
  1086. m := map[string]interface{}{
  1087. "v": "anything",
  1088. }
  1089. var b Bar
  1090. assert.NotNil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1091. }
  1092. func TestUnmarshalAnonymousOptionalRequiredBothMissed(t *testing.T) {
  1093. type (
  1094. Foo struct {
  1095. Name string `json:"n"`
  1096. Value string `json:"v"`
  1097. }
  1098. Bar struct {
  1099. Foo `json:",optional"`
  1100. }
  1101. )
  1102. m := map[string]interface{}{}
  1103. var b Bar
  1104. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1105. assert.True(t, len(b.Name) == 0)
  1106. assert.True(t, len(b.Value) == 0)
  1107. }
  1108. func TestUnmarshalAnonymousOptionalOneRequiredOneOptionalBothProvided(t *testing.T) {
  1109. type (
  1110. Foo struct {
  1111. Name string `json:"n,optional"`
  1112. Value string `json:"v"`
  1113. }
  1114. Bar struct {
  1115. Foo `json:",optional"`
  1116. }
  1117. )
  1118. m := map[string]interface{}{
  1119. "n": "kevin",
  1120. "v": "anything",
  1121. }
  1122. var b Bar
  1123. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1124. assert.Equal(t, "kevin", b.Name)
  1125. assert.Equal(t, "anything", b.Value)
  1126. }
  1127. func TestUnmarshalAnonymousOptionalOneRequiredOneOptionalBothMissed(t *testing.T) {
  1128. type (
  1129. Foo struct {
  1130. Name string `json:"n,optional"`
  1131. Value string `json:"v"`
  1132. }
  1133. Bar struct {
  1134. Foo `json:",optional"`
  1135. }
  1136. )
  1137. m := map[string]interface{}{}
  1138. var b Bar
  1139. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1140. assert.True(t, len(b.Name) == 0)
  1141. assert.True(t, len(b.Value) == 0)
  1142. }
  1143. func TestUnmarshalAnonymousOptionalOneRequiredOneOptionalRequiredProvidedOptionalMissed(t *testing.T) {
  1144. type (
  1145. Foo struct {
  1146. Name string `json:"n,optional"`
  1147. Value string `json:"v"`
  1148. }
  1149. Bar struct {
  1150. Foo `json:",optional"`
  1151. }
  1152. )
  1153. m := map[string]interface{}{
  1154. "v": "anything",
  1155. }
  1156. var b Bar
  1157. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1158. assert.True(t, len(b.Name) == 0)
  1159. assert.Equal(t, "anything", b.Value)
  1160. }
  1161. func TestUnmarshalAnonymousOptionalOneRequiredOneOptionalRequiredMissedOptionalProvided(t *testing.T) {
  1162. type (
  1163. Foo struct {
  1164. Name string `json:"n,optional"`
  1165. Value string `json:"v"`
  1166. }
  1167. Bar struct {
  1168. Foo `json:",optional"`
  1169. }
  1170. )
  1171. m := map[string]interface{}{
  1172. "n": "anything",
  1173. }
  1174. var b Bar
  1175. assert.NotNil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1176. }
  1177. func TestUnmarshalAnonymousOptionalBothOptionalBothProvided(t *testing.T) {
  1178. type (
  1179. Foo struct {
  1180. Name string `json:"n,optional"`
  1181. Value string `json:"v,optional"`
  1182. }
  1183. Bar struct {
  1184. Foo `json:",optional"`
  1185. }
  1186. )
  1187. m := map[string]interface{}{
  1188. "n": "kevin",
  1189. "v": "anything",
  1190. }
  1191. var b Bar
  1192. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1193. assert.Equal(t, "kevin", b.Name)
  1194. assert.Equal(t, "anything", b.Value)
  1195. }
  1196. func TestUnmarshalAnonymousOptionalBothOptionalOneProvidedOneMissed(t *testing.T) {
  1197. type (
  1198. Foo struct {
  1199. Name string `json:"n,optional"`
  1200. Value string `json:"v,optional"`
  1201. }
  1202. Bar struct {
  1203. Foo `json:",optional"`
  1204. }
  1205. )
  1206. m := map[string]interface{}{
  1207. "v": "anything",
  1208. }
  1209. var b Bar
  1210. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1211. assert.True(t, len(b.Name) == 0)
  1212. assert.Equal(t, "anything", b.Value)
  1213. }
  1214. func TestUnmarshalAnonymousOptionalBothOptionalBothMissed(t *testing.T) {
  1215. type (
  1216. Foo struct {
  1217. Name string `json:"n,optional"`
  1218. Value string `json:"v,optional"`
  1219. }
  1220. Bar struct {
  1221. Foo `json:",optional"`
  1222. }
  1223. )
  1224. m := map[string]interface{}{}
  1225. var b Bar
  1226. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1227. assert.True(t, len(b.Name) == 0)
  1228. assert.True(t, len(b.Value) == 0)
  1229. }
  1230. func TestUnmarshalAnonymousRequiredProvided(t *testing.T) {
  1231. type (
  1232. Foo struct {
  1233. Value string `json:"v"`
  1234. }
  1235. Bar struct {
  1236. Foo
  1237. }
  1238. )
  1239. m := map[string]interface{}{
  1240. "v": "anything",
  1241. }
  1242. var b Bar
  1243. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1244. assert.Equal(t, "anything", b.Value)
  1245. }
  1246. func TestUnmarshalAnonymousRequiredMissed(t *testing.T) {
  1247. type (
  1248. Foo struct {
  1249. Value string `json:"v"`
  1250. }
  1251. Bar struct {
  1252. Foo
  1253. }
  1254. )
  1255. m := map[string]interface{}{}
  1256. var b Bar
  1257. assert.NotNil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1258. }
  1259. func TestUnmarshalAnonymousOptionalProvided(t *testing.T) {
  1260. type (
  1261. Foo struct {
  1262. Value string `json:"v,optional"`
  1263. }
  1264. Bar struct {
  1265. Foo
  1266. }
  1267. )
  1268. m := map[string]interface{}{
  1269. "v": "anything",
  1270. }
  1271. var b Bar
  1272. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1273. assert.Equal(t, "anything", b.Value)
  1274. }
  1275. func TestUnmarshalAnonymousOptionalMissed(t *testing.T) {
  1276. type (
  1277. Foo struct {
  1278. Value string `json:"v,optional"`
  1279. }
  1280. Bar struct {
  1281. Foo
  1282. }
  1283. )
  1284. m := map[string]interface{}{}
  1285. var b Bar
  1286. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1287. assert.True(t, len(b.Value) == 0)
  1288. }
  1289. func TestUnmarshalAnonymousRequiredBothProvided(t *testing.T) {
  1290. type (
  1291. Foo struct {
  1292. Name string `json:"n"`
  1293. Value string `json:"v"`
  1294. }
  1295. Bar struct {
  1296. Foo
  1297. }
  1298. )
  1299. m := map[string]interface{}{
  1300. "n": "kevin",
  1301. "v": "anything",
  1302. }
  1303. var b Bar
  1304. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1305. assert.Equal(t, "kevin", b.Name)
  1306. assert.Equal(t, "anything", b.Value)
  1307. }
  1308. func TestUnmarshalAnonymousRequiredOneProvidedOneMissed(t *testing.T) {
  1309. type (
  1310. Foo struct {
  1311. Name string `json:"n"`
  1312. Value string `json:"v"`
  1313. }
  1314. Bar struct {
  1315. Foo
  1316. }
  1317. )
  1318. m := map[string]interface{}{
  1319. "v": "anything",
  1320. }
  1321. var b Bar
  1322. assert.NotNil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1323. }
  1324. func TestUnmarshalAnonymousRequiredBothMissed(t *testing.T) {
  1325. type (
  1326. Foo struct {
  1327. Name string `json:"n"`
  1328. Value string `json:"v"`
  1329. }
  1330. Bar struct {
  1331. Foo
  1332. }
  1333. )
  1334. m := map[string]interface{}{
  1335. "v": "anything",
  1336. }
  1337. var b Bar
  1338. assert.NotNil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1339. }
  1340. func TestUnmarshalAnonymousOneRequiredOneOptionalBothProvided(t *testing.T) {
  1341. type (
  1342. Foo struct {
  1343. Name string `json:"n,optional"`
  1344. Value string `json:"v"`
  1345. }
  1346. Bar struct {
  1347. Foo
  1348. }
  1349. )
  1350. m := map[string]interface{}{
  1351. "n": "kevin",
  1352. "v": "anything",
  1353. }
  1354. var b Bar
  1355. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1356. assert.Equal(t, "kevin", b.Name)
  1357. assert.Equal(t, "anything", b.Value)
  1358. }
  1359. func TestUnmarshalAnonymousOneRequiredOneOptionalBothMissed(t *testing.T) {
  1360. type (
  1361. Foo struct {
  1362. Name string `json:"n,optional"`
  1363. Value string `json:"v"`
  1364. }
  1365. Bar struct {
  1366. Foo
  1367. }
  1368. )
  1369. m := map[string]interface{}{}
  1370. var b Bar
  1371. assert.NotNil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1372. }
  1373. func TestUnmarshalAnonymousOneRequiredOneOptionalRequiredProvidedOptionalMissed(t *testing.T) {
  1374. type (
  1375. Foo struct {
  1376. Name string `json:"n,optional"`
  1377. Value string `json:"v"`
  1378. }
  1379. Bar struct {
  1380. Foo
  1381. }
  1382. )
  1383. m := map[string]interface{}{
  1384. "v": "anything",
  1385. }
  1386. var b Bar
  1387. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1388. assert.True(t, len(b.Name) == 0)
  1389. assert.Equal(t, "anything", b.Value)
  1390. }
  1391. func TestUnmarshalAnonymousOneRequiredOneOptionalRequiredMissedOptionalProvided(t *testing.T) {
  1392. type (
  1393. Foo struct {
  1394. Name string `json:"n,optional"`
  1395. Value string `json:"v"`
  1396. }
  1397. Bar struct {
  1398. Foo
  1399. }
  1400. )
  1401. m := map[string]interface{}{
  1402. "n": "anything",
  1403. }
  1404. var b Bar
  1405. assert.NotNil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1406. }
  1407. func TestUnmarshalAnonymousBothOptionalBothProvided(t *testing.T) {
  1408. type (
  1409. Foo struct {
  1410. Name string `json:"n,optional"`
  1411. Value string `json:"v,optional"`
  1412. }
  1413. Bar struct {
  1414. Foo
  1415. }
  1416. )
  1417. m := map[string]interface{}{
  1418. "n": "kevin",
  1419. "v": "anything",
  1420. }
  1421. var b Bar
  1422. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1423. assert.Equal(t, "kevin", b.Name)
  1424. assert.Equal(t, "anything", b.Value)
  1425. }
  1426. func TestUnmarshalAnonymousBothOptionalOneProvidedOneMissed(t *testing.T) {
  1427. type (
  1428. Foo struct {
  1429. Name string `json:"n,optional"`
  1430. Value string `json:"v,optional"`
  1431. }
  1432. Bar struct {
  1433. Foo
  1434. }
  1435. )
  1436. m := map[string]interface{}{
  1437. "v": "anything",
  1438. }
  1439. var b Bar
  1440. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1441. assert.True(t, len(b.Name) == 0)
  1442. assert.Equal(t, "anything", b.Value)
  1443. }
  1444. func TestUnmarshalAnonymousBothOptionalBothMissed(t *testing.T) {
  1445. type (
  1446. Foo struct {
  1447. Name string `json:"n,optional"`
  1448. Value string `json:"v,optional"`
  1449. }
  1450. Bar struct {
  1451. Foo
  1452. }
  1453. )
  1454. m := map[string]interface{}{}
  1455. var b Bar
  1456. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1457. assert.True(t, len(b.Name) == 0)
  1458. assert.True(t, len(b.Value) == 0)
  1459. }
  1460. func TestUnmarshalAnonymousWrappedToMuch(t *testing.T) {
  1461. type (
  1462. Foo struct {
  1463. Name string `json:"n"`
  1464. Value string `json:"v"`
  1465. }
  1466. Bar struct {
  1467. Foo
  1468. }
  1469. )
  1470. m := map[string]interface{}{
  1471. "Foo": map[string]interface{}{
  1472. "n": "name",
  1473. "v": "anything",
  1474. },
  1475. }
  1476. var b Bar
  1477. assert.NotNil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1478. }
  1479. func TestUnmarshalWrappedObject(t *testing.T) {
  1480. type (
  1481. Foo struct {
  1482. Value string `json:"v"`
  1483. }
  1484. Bar struct {
  1485. Inner Foo
  1486. }
  1487. )
  1488. m := map[string]interface{}{
  1489. "Inner": map[string]interface{}{
  1490. "v": "anything",
  1491. },
  1492. }
  1493. var b Bar
  1494. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1495. assert.Equal(t, "anything", b.Inner.Value)
  1496. }
  1497. func TestUnmarshalWrappedObjectOptional(t *testing.T) {
  1498. type (
  1499. Foo struct {
  1500. Hosts []string
  1501. Key string
  1502. }
  1503. Bar struct {
  1504. Inner Foo `json:",optional"`
  1505. Name string
  1506. }
  1507. )
  1508. m := map[string]interface{}{
  1509. "Name": "anything",
  1510. }
  1511. var b Bar
  1512. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1513. assert.Equal(t, "anything", b.Name)
  1514. }
  1515. func TestUnmarshalWrappedObjectOptionalFilled(t *testing.T) {
  1516. type (
  1517. Foo struct {
  1518. Hosts []string
  1519. Key string
  1520. }
  1521. Bar struct {
  1522. Inner Foo `json:",optional"`
  1523. Name string
  1524. }
  1525. )
  1526. hosts := []string{"1", "2"}
  1527. m := map[string]interface{}{
  1528. "Inner": map[string]interface{}{
  1529. "Hosts": hosts,
  1530. "Key": "key",
  1531. },
  1532. "Name": "anything",
  1533. }
  1534. var b Bar
  1535. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1536. assert.EqualValues(t, hosts, b.Inner.Hosts)
  1537. assert.Equal(t, "key", b.Inner.Key)
  1538. assert.Equal(t, "anything", b.Name)
  1539. }
  1540. func TestUnmarshalWrappedNamedObjectOptional(t *testing.T) {
  1541. type (
  1542. Foo struct {
  1543. Host string
  1544. Key string
  1545. }
  1546. Bar struct {
  1547. Inner Foo `json:",optional"`
  1548. Name string
  1549. }
  1550. )
  1551. m := map[string]interface{}{
  1552. "Inner": map[string]interface{}{
  1553. "Host": "thehost",
  1554. "Key": "thekey",
  1555. },
  1556. "Name": "anything",
  1557. }
  1558. var b Bar
  1559. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1560. assert.Equal(t, "thehost", b.Inner.Host)
  1561. assert.Equal(t, "thekey", b.Inner.Key)
  1562. assert.Equal(t, "anything", b.Name)
  1563. }
  1564. func TestUnmarshalWrappedObjectNamedPtr(t *testing.T) {
  1565. type (
  1566. Foo struct {
  1567. Value string `json:"v"`
  1568. }
  1569. Bar struct {
  1570. Inner *Foo `json:"foo,optional"`
  1571. }
  1572. )
  1573. m := map[string]interface{}{
  1574. "foo": map[string]interface{}{
  1575. "v": "anything",
  1576. },
  1577. }
  1578. var b Bar
  1579. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1580. assert.Equal(t, "anything", b.Inner.Value)
  1581. }
  1582. func TestUnmarshalWrappedObjectPtr(t *testing.T) {
  1583. type (
  1584. Foo struct {
  1585. Value string `json:"v"`
  1586. }
  1587. Bar struct {
  1588. Inner *Foo
  1589. }
  1590. )
  1591. m := map[string]interface{}{
  1592. "Inner": map[string]interface{}{
  1593. "v": "anything",
  1594. },
  1595. }
  1596. var b Bar
  1597. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1598. assert.Equal(t, "anything", b.Inner.Value)
  1599. }
  1600. func TestUnmarshalInt2String(t *testing.T) {
  1601. type inner struct {
  1602. Int string `key:"int"`
  1603. }
  1604. m := map[string]interface{}{
  1605. "int": 123,
  1606. }
  1607. var in inner
  1608. assert.NotNil(t, UnmarshalKey(m, &in))
  1609. }
  1610. func TestUnmarshalZeroValues(t *testing.T) {
  1611. type inner struct {
  1612. False bool `key:"no"`
  1613. Int int `key:"int"`
  1614. String string `key:"string"`
  1615. }
  1616. m := map[string]interface{}{
  1617. "no": false,
  1618. "int": 0,
  1619. "string": "",
  1620. }
  1621. var in inner
  1622. ast := assert.New(t)
  1623. ast.Nil(UnmarshalKey(m, &in))
  1624. ast.False(in.False)
  1625. ast.Equal(0, in.Int)
  1626. ast.Equal("", in.String)
  1627. }
  1628. func TestUnmarshalUsingDifferentKeys(t *testing.T) {
  1629. type inner struct {
  1630. False bool `key:"no"`
  1631. Int int `key:"int"`
  1632. String string `bson:"string"`
  1633. }
  1634. m := map[string]interface{}{
  1635. "no": false,
  1636. "int": 9,
  1637. "string": "value",
  1638. }
  1639. var in inner
  1640. ast := assert.New(t)
  1641. ast.Nil(UnmarshalKey(m, &in))
  1642. ast.False(in.False)
  1643. ast.Equal(9, in.Int)
  1644. ast.True(len(in.String) == 0)
  1645. }
  1646. func TestUnmarshalNumberRangeInt(t *testing.T) {
  1647. type inner struct {
  1648. Value1 int `key:"value1,range=[1:]"`
  1649. Value2 int8 `key:"value2,range=[1:5]"`
  1650. Value3 int16 `key:"value3,range=[1:5]"`
  1651. Value4 int32 `key:"value4,range=[1:5]"`
  1652. Value5 int64 `key:"value5,range=[1:5]"`
  1653. Value6 uint `key:"value6,range=[:5]"`
  1654. Value8 uint8 `key:"value8,range=[1:5],string"`
  1655. Value9 uint16 `key:"value9,range=[1:5],string"`
  1656. Value10 uint32 `key:"value10,range=[1:5],string"`
  1657. Value11 uint64 `key:"value11,range=[1:5],string"`
  1658. }
  1659. m := map[string]interface{}{
  1660. "value1": 10,
  1661. "value2": int8(1),
  1662. "value3": int16(2),
  1663. "value4": int32(4),
  1664. "value5": int64(5),
  1665. "value6": uint(0),
  1666. "value8": "1",
  1667. "value9": "2",
  1668. "value10": "4",
  1669. "value11": "5",
  1670. }
  1671. var in inner
  1672. ast := assert.New(t)
  1673. ast.Nil(UnmarshalKey(m, &in))
  1674. ast.Equal(10, in.Value1)
  1675. ast.Equal(int8(1), in.Value2)
  1676. ast.Equal(int16(2), in.Value3)
  1677. ast.Equal(int32(4), in.Value4)
  1678. ast.Equal(int64(5), in.Value5)
  1679. ast.Equal(uint(0), in.Value6)
  1680. ast.Equal(uint8(1), in.Value8)
  1681. ast.Equal(uint16(2), in.Value9)
  1682. ast.Equal(uint32(4), in.Value10)
  1683. ast.Equal(uint64(5), in.Value11)
  1684. }
  1685. func TestUnmarshalNumberRangeJsonNumber(t *testing.T) {
  1686. type inner struct {
  1687. Value3 uint `key:"value3,range=(1:5]"`
  1688. Value4 uint8 `key:"value4,range=(1:5]"`
  1689. Value5 uint16 `key:"value5,range=(1:5]"`
  1690. }
  1691. m := map[string]interface{}{
  1692. "value3": json.Number("2"),
  1693. "value4": json.Number("4"),
  1694. "value5": json.Number("5"),
  1695. }
  1696. var in inner
  1697. ast := assert.New(t)
  1698. ast.Nil(UnmarshalKey(m, &in))
  1699. ast.Equal(uint(2), in.Value3)
  1700. ast.Equal(uint8(4), in.Value4)
  1701. ast.Equal(uint16(5), in.Value5)
  1702. type inner1 struct {
  1703. Value int `key:"value,range=(1:5]"`
  1704. }
  1705. m = map[string]interface{}{
  1706. "value": json.Number("a"),
  1707. }
  1708. var in1 inner1
  1709. ast.NotNil(UnmarshalKey(m, &in1))
  1710. }
  1711. func TestUnmarshalNumberRangeIntLeftExclude(t *testing.T) {
  1712. type inner struct {
  1713. Value3 uint `key:"value3,range=(1:5]"`
  1714. Value4 uint32 `key:"value4,default=4,range=(1:5]"`
  1715. Value5 uint64 `key:"value5,range=(1:5]"`
  1716. Value9 int `key:"value9,range=(1:5],string"`
  1717. Value10 int `key:"value10,range=(1:5],string"`
  1718. Value11 int `key:"value11,range=(1:5],string"`
  1719. }
  1720. m := map[string]interface{}{
  1721. "value3": uint(2),
  1722. "value4": uint32(4),
  1723. "value5": uint64(5),
  1724. "value9": "2",
  1725. "value10": "4",
  1726. "value11": "5",
  1727. }
  1728. var in inner
  1729. ast := assert.New(t)
  1730. ast.Nil(UnmarshalKey(m, &in))
  1731. ast.Equal(uint(2), in.Value3)
  1732. ast.Equal(uint32(4), in.Value4)
  1733. ast.Equal(uint64(5), in.Value5)
  1734. ast.Equal(2, in.Value9)
  1735. ast.Equal(4, in.Value10)
  1736. ast.Equal(5, in.Value11)
  1737. }
  1738. func TestUnmarshalNumberRangeIntRightExclude(t *testing.T) {
  1739. type inner struct {
  1740. Value2 uint `key:"value2,range=[1:5)"`
  1741. Value3 uint8 `key:"value3,range=[1:5)"`
  1742. Value4 uint16 `key:"value4,range=[1:5)"`
  1743. Value8 int `key:"value8,range=[1:5),string"`
  1744. Value9 int `key:"value9,range=[1:5),string"`
  1745. Value10 int `key:"value10,range=[1:5),string"`
  1746. }
  1747. m := map[string]interface{}{
  1748. "value2": uint(1),
  1749. "value3": uint8(2),
  1750. "value4": uint16(4),
  1751. "value8": "1",
  1752. "value9": "2",
  1753. "value10": "4",
  1754. }
  1755. var in inner
  1756. ast := assert.New(t)
  1757. ast.Nil(UnmarshalKey(m, &in))
  1758. ast.Equal(uint(1), in.Value2)
  1759. ast.Equal(uint8(2), in.Value3)
  1760. ast.Equal(uint16(4), in.Value4)
  1761. ast.Equal(1, in.Value8)
  1762. ast.Equal(2, in.Value9)
  1763. ast.Equal(4, in.Value10)
  1764. }
  1765. func TestUnmarshalNumberRangeIntExclude(t *testing.T) {
  1766. type inner struct {
  1767. Value3 int `key:"value3,range=(1:5)"`
  1768. Value4 int `key:"value4,range=(1:5)"`
  1769. Value9 int `key:"value9,range=(1:5),string"`
  1770. Value10 int `key:"value10,range=(1:5),string"`
  1771. }
  1772. m := map[string]interface{}{
  1773. "value3": 2,
  1774. "value4": 4,
  1775. "value9": "2",
  1776. "value10": "4",
  1777. }
  1778. var in inner
  1779. ast := assert.New(t)
  1780. ast.Nil(UnmarshalKey(m, &in))
  1781. ast.Equal(2, in.Value3)
  1782. ast.Equal(4, in.Value4)
  1783. ast.Equal(2, in.Value9)
  1784. ast.Equal(4, in.Value10)
  1785. }
  1786. func TestUnmarshalNumberRangeIntOutOfRange(t *testing.T) {
  1787. type inner1 struct {
  1788. Value int64 `key:"value,default=3,range=(1:5)"`
  1789. }
  1790. var in1 inner1
  1791. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1792. "value": int64(1),
  1793. }, &in1))
  1794. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1795. "value": int64(0),
  1796. }, &in1))
  1797. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1798. "value": int64(5),
  1799. }, &in1))
  1800. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1801. "value": json.Number("6"),
  1802. }, &in1))
  1803. type inner2 struct {
  1804. Value int64 `key:"value,optional,range=[1:5)"`
  1805. }
  1806. var in2 inner2
  1807. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1808. "value": int64(0),
  1809. }, &in2))
  1810. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1811. "value": int64(5),
  1812. }, &in2))
  1813. type inner3 struct {
  1814. Value int64 `key:"value,range=(1:5]"`
  1815. }
  1816. var in3 inner3
  1817. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1818. "value": int64(1),
  1819. }, &in3))
  1820. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1821. "value": int64(6),
  1822. }, &in3))
  1823. type inner4 struct {
  1824. Value int64 `key:"value,range=[1:5]"`
  1825. }
  1826. var in4 inner4
  1827. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1828. "value": int64(0),
  1829. }, &in4))
  1830. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1831. "value": int64(6),
  1832. }, &in4))
  1833. }
  1834. func TestUnmarshalNumberRangeFloat(t *testing.T) {
  1835. type inner struct {
  1836. Value2 float32 `key:"value2,range=[1:5]"`
  1837. Value3 float32 `key:"value3,range=[1:5]"`
  1838. Value4 float64 `key:"value4,range=[1:5]"`
  1839. Value5 float64 `key:"value5,range=[1:5]"`
  1840. Value8 float64 `key:"value8,range=[1:5],string"`
  1841. Value9 float64 `key:"value9,range=[1:5],string"`
  1842. Value10 float64 `key:"value10,range=[1:5],string"`
  1843. Value11 float64 `key:"value11,range=[1:5],string"`
  1844. }
  1845. m := map[string]interface{}{
  1846. "value2": float32(1),
  1847. "value3": float32(2),
  1848. "value4": float64(4),
  1849. "value5": float64(5),
  1850. "value8": "1",
  1851. "value9": "2",
  1852. "value10": "4",
  1853. "value11": "5",
  1854. }
  1855. var in inner
  1856. ast := assert.New(t)
  1857. ast.Nil(UnmarshalKey(m, &in))
  1858. ast.Equal(float32(1), in.Value2)
  1859. ast.Equal(float32(2), in.Value3)
  1860. ast.Equal(float64(4), in.Value4)
  1861. ast.Equal(float64(5), in.Value5)
  1862. ast.Equal(float64(1), in.Value8)
  1863. ast.Equal(float64(2), in.Value9)
  1864. ast.Equal(float64(4), in.Value10)
  1865. ast.Equal(float64(5), in.Value11)
  1866. }
  1867. func TestUnmarshalNumberRangeFloatLeftExclude(t *testing.T) {
  1868. type inner struct {
  1869. Value3 float64 `key:"value3,range=(1:5]"`
  1870. Value4 float64 `key:"value4,range=(1:5]"`
  1871. Value5 float64 `key:"value5,range=(1:5]"`
  1872. Value9 float64 `key:"value9,range=(1:5],string"`
  1873. Value10 float64 `key:"value10,range=(1:5],string"`
  1874. Value11 float64 `key:"value11,range=(1:5],string"`
  1875. }
  1876. m := map[string]interface{}{
  1877. "value3": float64(2),
  1878. "value4": float64(4),
  1879. "value5": float64(5),
  1880. "value9": "2",
  1881. "value10": "4",
  1882. "value11": "5",
  1883. }
  1884. var in inner
  1885. ast := assert.New(t)
  1886. ast.Nil(UnmarshalKey(m, &in))
  1887. ast.Equal(float64(2), in.Value3)
  1888. ast.Equal(float64(4), in.Value4)
  1889. ast.Equal(float64(5), in.Value5)
  1890. ast.Equal(float64(2), in.Value9)
  1891. ast.Equal(float64(4), in.Value10)
  1892. ast.Equal(float64(5), in.Value11)
  1893. }
  1894. func TestUnmarshalNumberRangeFloatRightExclude(t *testing.T) {
  1895. type inner struct {
  1896. Value2 float64 `key:"value2,range=[1:5)"`
  1897. Value3 float64 `key:"value3,range=[1:5)"`
  1898. Value4 float64 `key:"value4,range=[1:5)"`
  1899. Value8 float64 `key:"value8,range=[1:5),string"`
  1900. Value9 float64 `key:"value9,range=[1:5),string"`
  1901. Value10 float64 `key:"value10,range=[1:5),string"`
  1902. }
  1903. m := map[string]interface{}{
  1904. "value2": float64(1),
  1905. "value3": float64(2),
  1906. "value4": float64(4),
  1907. "value8": "1",
  1908. "value9": "2",
  1909. "value10": "4",
  1910. }
  1911. var in inner
  1912. ast := assert.New(t)
  1913. ast.Nil(UnmarshalKey(m, &in))
  1914. ast.Equal(float64(1), in.Value2)
  1915. ast.Equal(float64(2), in.Value3)
  1916. ast.Equal(float64(4), in.Value4)
  1917. ast.Equal(float64(1), in.Value8)
  1918. ast.Equal(float64(2), in.Value9)
  1919. ast.Equal(float64(4), in.Value10)
  1920. }
  1921. func TestUnmarshalNumberRangeFloatExclude(t *testing.T) {
  1922. type inner struct {
  1923. Value3 float64 `key:"value3,range=(1:5)"`
  1924. Value4 float64 `key:"value4,range=(1:5)"`
  1925. Value9 float64 `key:"value9,range=(1:5),string"`
  1926. Value10 float64 `key:"value10,range=(1:5),string"`
  1927. }
  1928. m := map[string]interface{}{
  1929. "value3": float64(2),
  1930. "value4": float64(4),
  1931. "value9": "2",
  1932. "value10": "4",
  1933. }
  1934. var in inner
  1935. ast := assert.New(t)
  1936. ast.Nil(UnmarshalKey(m, &in))
  1937. ast.Equal(float64(2), in.Value3)
  1938. ast.Equal(float64(4), in.Value4)
  1939. ast.Equal(float64(2), in.Value9)
  1940. ast.Equal(float64(4), in.Value10)
  1941. }
  1942. func TestUnmarshalNumberRangeFloatOutOfRange(t *testing.T) {
  1943. type inner1 struct {
  1944. Value float64 `key:"value,range=(1:5)"`
  1945. }
  1946. var in1 inner1
  1947. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1948. "value": float64(1),
  1949. }, &in1))
  1950. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1951. "value": float64(0),
  1952. }, &in1))
  1953. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1954. "value": float64(5),
  1955. }, &in1))
  1956. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1957. "value": json.Number("6"),
  1958. }, &in1))
  1959. type inner2 struct {
  1960. Value float64 `key:"value,range=[1:5)"`
  1961. }
  1962. var in2 inner2
  1963. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1964. "value": float64(0),
  1965. }, &in2))
  1966. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1967. "value": float64(5),
  1968. }, &in2))
  1969. type inner3 struct {
  1970. Value float64 `key:"value,range=(1:5]"`
  1971. }
  1972. var in3 inner3
  1973. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1974. "value": float64(1),
  1975. }, &in3))
  1976. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1977. "value": float64(6),
  1978. }, &in3))
  1979. type inner4 struct {
  1980. Value float64 `key:"value,range=[1:5]"`
  1981. }
  1982. var in4 inner4
  1983. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1984. "value": float64(0),
  1985. }, &in4))
  1986. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1987. "value": float64(6),
  1988. }, &in4))
  1989. }
  1990. func TestUnmarshalRangeError(t *testing.T) {
  1991. type inner1 struct {
  1992. Value int `key:",range="`
  1993. }
  1994. var in1 inner1
  1995. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1996. "Value": 1,
  1997. }, &in1))
  1998. type inner2 struct {
  1999. Value int `key:",range=["`
  2000. }
  2001. var in2 inner2
  2002. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  2003. "Value": 1,
  2004. }, &in2))
  2005. type inner3 struct {
  2006. Value int `key:",range=[:"`
  2007. }
  2008. var in3 inner3
  2009. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  2010. "Value": 1,
  2011. }, &in3))
  2012. type inner4 struct {
  2013. Value int `key:",range=[:]"`
  2014. }
  2015. var in4 inner4
  2016. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  2017. "Value": 1,
  2018. }, &in4))
  2019. type inner5 struct {
  2020. Value int `key:",range={:]"`
  2021. }
  2022. var in5 inner5
  2023. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  2024. "Value": 1,
  2025. }, &in5))
  2026. type inner6 struct {
  2027. Value int `key:",range=[:}"`
  2028. }
  2029. var in6 inner6
  2030. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  2031. "Value": 1,
  2032. }, &in6))
  2033. type inner7 struct {
  2034. Value int `key:",range=[]"`
  2035. }
  2036. var in7 inner7
  2037. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  2038. "Value": 1,
  2039. }, &in7))
  2040. type inner8 struct {
  2041. Value int `key:",range=[a:]"`
  2042. }
  2043. var in8 inner8
  2044. assert.NotNil(t, UnmarshalKey(map[string]interface{}{
  2045. "Value": 1,
  2046. }, &in8))
  2047. type inner9 struct {
  2048. Value int `key:",range=[:a]"`
  2049. }
  2050. var in9 inner9
  2051. assert.NotNil(t, UnmarshalKey(map[string]interface{}{
  2052. "Value": 1,
  2053. }, &in9))
  2054. type inner10 struct {
  2055. Value int `key:",range"`
  2056. }
  2057. var in10 inner10
  2058. assert.NotNil(t, UnmarshalKey(map[string]interface{}{
  2059. "Value": 1,
  2060. }, &in10))
  2061. type inner11 struct {
  2062. Value int `key:",range=[1,2]"`
  2063. }
  2064. var in11 inner11
  2065. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  2066. "Value": "a",
  2067. }, &in11))
  2068. }
  2069. func TestUnmarshalNestedMap(t *testing.T) {
  2070. var c struct {
  2071. Anything map[string]map[string]string `json:"anything"`
  2072. }
  2073. m := map[string]interface{}{
  2074. "anything": map[string]map[string]interface{}{
  2075. "inner": {
  2076. "id": "1",
  2077. "name": "any",
  2078. },
  2079. },
  2080. }
  2081. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &c))
  2082. assert.Equal(t, "1", c.Anything["inner"]["id"])
  2083. }
  2084. func TestUnmarshalNestedMapMismatch(t *testing.T) {
  2085. var c struct {
  2086. Anything map[string]map[string]map[string]string `json:"anything"`
  2087. }
  2088. m := map[string]interface{}{
  2089. "anything": map[string]map[string]interface{}{
  2090. "inner": {
  2091. "name": "any",
  2092. },
  2093. },
  2094. }
  2095. assert.NotNil(t, NewUnmarshaler("json").Unmarshal(m, &c))
  2096. }
  2097. func TestUnmarshalNestedMapSimple(t *testing.T) {
  2098. var c struct {
  2099. Anything map[string]string `json:"anything"`
  2100. }
  2101. m := map[string]interface{}{
  2102. "anything": map[string]interface{}{
  2103. "id": "1",
  2104. "name": "any",
  2105. },
  2106. }
  2107. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &c))
  2108. assert.Equal(t, "1", c.Anything["id"])
  2109. }
  2110. func TestUnmarshalNestedMapSimpleTypeMatch(t *testing.T) {
  2111. var c struct {
  2112. Anything map[string]string `json:"anything"`
  2113. }
  2114. m := map[string]interface{}{
  2115. "anything": map[string]string{
  2116. "id": "1",
  2117. "name": "any",
  2118. },
  2119. }
  2120. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &c))
  2121. assert.Equal(t, "1", c.Anything["id"])
  2122. }
  2123. func TestUnmarshalValuer(t *testing.T) {
  2124. unmarshaler := NewUnmarshaler(jsonTagKey)
  2125. var foo string
  2126. err := unmarshaler.UnmarshalValuer(nil, foo)
  2127. assert.NotNil(t, err)
  2128. }
  2129. func BenchmarkUnmarshalString(b *testing.B) {
  2130. type inner struct {
  2131. Value string `key:"value"`
  2132. }
  2133. m := map[string]interface{}{
  2134. "value": "first",
  2135. }
  2136. for i := 0; i < b.N; i++ {
  2137. var in inner
  2138. if err := UnmarshalKey(m, &in); err != nil {
  2139. b.Fatal(err)
  2140. }
  2141. }
  2142. }
  2143. func BenchmarkUnmarshalStruct(b *testing.B) {
  2144. b.ReportAllocs()
  2145. m := map[string]interface{}{
  2146. "Ids": []map[string]interface{}{
  2147. {
  2148. "First": 1,
  2149. "Second": 2,
  2150. },
  2151. },
  2152. }
  2153. for i := 0; i < b.N; i++ {
  2154. var v struct {
  2155. Ids []struct {
  2156. First int
  2157. Second int
  2158. }
  2159. }
  2160. if err := UnmarshalKey(m, &v); err != nil {
  2161. b.Fatal(err)
  2162. }
  2163. }
  2164. }
  2165. func BenchmarkMapToStruct(b *testing.B) {
  2166. data := map[string]interface{}{
  2167. "valid": "1",
  2168. "age": "5",
  2169. "name": "liao",
  2170. }
  2171. type anonymous struct {
  2172. Valid bool
  2173. Age int
  2174. Name string
  2175. }
  2176. for i := 0; i < b.N; i++ {
  2177. var an anonymous
  2178. if valid, ok := data["valid"]; ok {
  2179. an.Valid = valid == "1"
  2180. }
  2181. if age, ok := data["age"]; ok {
  2182. ages, _ := age.(string)
  2183. an.Age, _ = strconv.Atoi(ages)
  2184. }
  2185. if name, ok := data["name"]; ok {
  2186. names, _ := name.(string)
  2187. an.Name = names
  2188. }
  2189. }
  2190. }
  2191. func BenchmarkUnmarshal(b *testing.B) {
  2192. data := map[string]interface{}{
  2193. "valid": "1",
  2194. "age": "5",
  2195. "name": "liao",
  2196. }
  2197. type anonymous struct {
  2198. Valid bool `key:"valid,string"`
  2199. Age int `key:"age,string"`
  2200. Name string `key:"name"`
  2201. }
  2202. for i := 0; i < b.N; i++ {
  2203. var an anonymous
  2204. UnmarshalKey(data, &an)
  2205. }
  2206. }
  2207. func TestUnmarshalJsonReaderMultiArray(t *testing.T) {
  2208. payload := `{"a": "133", "b": [["add", "cccd"], ["eeee"]]}`
  2209. var res struct {
  2210. A string `json:"a"`
  2211. B [][]string `json:"b"`
  2212. }
  2213. reader := strings.NewReader(payload)
  2214. err := UnmarshalJsonReader(reader, &res)
  2215. assert.Nil(t, err)
  2216. assert.Equal(t, 2, len(res.B))
  2217. }
  2218. func TestUnmarshalJsonReaderPtrMultiArray(t *testing.T) {
  2219. payload := `{"a": "133", "b": [["add", "cccd"], ["eeee"]]}`
  2220. var res struct {
  2221. A string `json:"a"`
  2222. B [][]*string `json:"b"`
  2223. }
  2224. reader := strings.NewReader(payload)
  2225. err := UnmarshalJsonReader(reader, &res)
  2226. assert.Nil(t, err)
  2227. assert.Equal(t, 2, len(res.B))
  2228. assert.Equal(t, 2, len(res.B[0]))
  2229. }
  2230. func TestUnmarshalJsonReaderPtrArray(t *testing.T) {
  2231. payload := `{"a": "133", "b": ["add", "cccd", "eeee"]}`
  2232. var res struct {
  2233. A string `json:"a"`
  2234. B []*string `json:"b"`
  2235. }
  2236. reader := strings.NewReader(payload)
  2237. err := UnmarshalJsonReader(reader, &res)
  2238. assert.Nil(t, err)
  2239. assert.Equal(t, 3, len(res.B))
  2240. }