unmarshaler_test.go 53 KB

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