unmarshaler_test.go 56 KB

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