unmarshaler_test.go 56 KB

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