unmarshaler_test.go 52 KB

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