unmarshaler_test.go 57 KB

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