unmarshaler_test.go 53 KB

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