unmarshaler_test.go 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791
  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 TestUnmarshalOptionsOptional(t *testing.T) {
  897. type inner struct {
  898. Value string `key:"value,options=first|second,optional"`
  899. OptionalValue string `key:"optional_value,options=first|second,optional"`
  900. Foo string `key:"foo,options=[bar,baz]"`
  901. Correct string `key:"correct,options=1|2"`
  902. }
  903. m := map[string]interface{}{
  904. "value": "first",
  905. "foo": "bar",
  906. "correct": "2",
  907. }
  908. var in inner
  909. ast := assert.New(t)
  910. ast.Nil(UnmarshalKey(m, &in))
  911. ast.Equal("first", in.Value)
  912. ast.Equal("", in.OptionalValue)
  913. ast.Equal("bar", in.Foo)
  914. ast.Equal("2", in.Correct)
  915. }
  916. func TestUnmarshalOptionsOptionalWrongValue(t *testing.T) {
  917. type inner struct {
  918. Value string `key:"value,options=first|second,optional"`
  919. OptionalValue string `key:"optional_value,options=first|second,optional"`
  920. WrongValue string `key:"wrong_value,options=first|second,optional"`
  921. }
  922. m := map[string]interface{}{
  923. "value": "first",
  924. "wrong_value": "third",
  925. }
  926. var in inner
  927. assert.NotNil(t, UnmarshalKey(m, &in))
  928. }
  929. func TestUnmarshalStringOptionsWithStringOptionsNotString(t *testing.T) {
  930. type inner struct {
  931. Value string `key:"value,options=first|second"`
  932. Correct string `key:"correct,options=1|2"`
  933. }
  934. m := map[string]interface{}{
  935. "value": "first",
  936. "correct": 2,
  937. }
  938. var in inner
  939. unmarshaler := NewUnmarshaler(defaultKeyName, WithStringValues())
  940. ast := assert.New(t)
  941. ast.NotNil(unmarshaler.Unmarshal(m, &in))
  942. }
  943. func TestUnmarshalStringOptionsWithStringOptions(t *testing.T) {
  944. type inner struct {
  945. Value string `key:"value,options=first|second"`
  946. Correct string `key:"correct,options=1|2"`
  947. }
  948. m := map[string]interface{}{
  949. "value": "first",
  950. "correct": "2",
  951. }
  952. var in inner
  953. unmarshaler := NewUnmarshaler(defaultKeyName, WithStringValues())
  954. ast := assert.New(t)
  955. ast.Nil(unmarshaler.Unmarshal(m, &in))
  956. ast.Equal("first", in.Value)
  957. ast.Equal("2", in.Correct)
  958. }
  959. func TestUnmarshalStringOptionsWithStringOptionsPtr(t *testing.T) {
  960. type inner struct {
  961. Value *string `key:"value,options=first|second"`
  962. Correct *int `key:"correct,options=1|2"`
  963. }
  964. m := map[string]interface{}{
  965. "value": "first",
  966. "correct": "2",
  967. }
  968. var in inner
  969. unmarshaler := NewUnmarshaler(defaultKeyName, WithStringValues())
  970. ast := assert.New(t)
  971. ast.Nil(unmarshaler.Unmarshal(m, &in))
  972. ast.True(*in.Value == "first")
  973. ast.True(*in.Correct == 2)
  974. }
  975. func TestUnmarshalStringOptionsWithStringOptionsIncorrect(t *testing.T) {
  976. type inner struct {
  977. Value string `key:"value,options=first|second"`
  978. Correct string `key:"correct,options=1|2"`
  979. }
  980. m := map[string]interface{}{
  981. "value": "third",
  982. "correct": "2",
  983. }
  984. var in inner
  985. unmarshaler := NewUnmarshaler(defaultKeyName, WithStringValues())
  986. ast := assert.New(t)
  987. ast.NotNil(unmarshaler.Unmarshal(m, &in))
  988. }
  989. func TestUnmarshalStringOptionsWithStringOptionsIncorrectGrouped(t *testing.T) {
  990. type inner struct {
  991. Value string `key:"value,options=[first,second]"`
  992. Correct string `key:"correct,options=1|2"`
  993. }
  994. m := map[string]interface{}{
  995. "value": "third",
  996. "correct": "2",
  997. }
  998. var in inner
  999. unmarshaler := NewUnmarshaler(defaultKeyName, WithStringValues())
  1000. ast := assert.New(t)
  1001. ast.NotNil(unmarshaler.Unmarshal(m, &in))
  1002. }
  1003. func TestUnmarshalWithStringOptionsIncorrect(t *testing.T) {
  1004. type inner struct {
  1005. Value string `key:"value,options=first|second"`
  1006. Incorrect string `key:"incorrect,options=1|2"`
  1007. }
  1008. m := map[string]interface{}{
  1009. "value": "first",
  1010. "incorrect": "3",
  1011. }
  1012. var in inner
  1013. assert.NotNil(t, UnmarshalKey(m, &in))
  1014. }
  1015. func TestUnmarshalWithIntOptionsCorrect(t *testing.T) {
  1016. type inner struct {
  1017. Value string `key:"value,options=first|second"`
  1018. Number int `key:"number,options=1|2"`
  1019. }
  1020. m := map[string]interface{}{
  1021. "value": "first",
  1022. "number": 2,
  1023. }
  1024. var in inner
  1025. ast := assert.New(t)
  1026. ast.Nil(UnmarshalKey(m, &in))
  1027. ast.Equal("first", in.Value)
  1028. ast.Equal(2, in.Number)
  1029. }
  1030. func TestUnmarshalWithIntOptionsCorrectPtr(t *testing.T) {
  1031. type inner struct {
  1032. Value *string `key:"value,options=first|second"`
  1033. Number *int `key:"number,options=1|2"`
  1034. }
  1035. m := map[string]interface{}{
  1036. "value": "first",
  1037. "number": 2,
  1038. }
  1039. var in inner
  1040. ast := assert.New(t)
  1041. ast.Nil(UnmarshalKey(m, &in))
  1042. ast.True(*in.Value == "first")
  1043. ast.True(*in.Number == 2)
  1044. }
  1045. func TestUnmarshalWithIntOptionsIncorrect(t *testing.T) {
  1046. type inner struct {
  1047. Value string `key:"value,options=first|second"`
  1048. Incorrect int `key:"incorrect,options=1|2"`
  1049. }
  1050. m := map[string]interface{}{
  1051. "value": "first",
  1052. "incorrect": 3,
  1053. }
  1054. var in inner
  1055. assert.NotNil(t, UnmarshalKey(m, &in))
  1056. }
  1057. func TestUnmarshalWithUintOptionsCorrect(t *testing.T) {
  1058. type inner struct {
  1059. Value string `key:"value,options=first|second"`
  1060. Number uint `key:"number,options=1|2"`
  1061. }
  1062. m := map[string]interface{}{
  1063. "value": "first",
  1064. "number": uint(2),
  1065. }
  1066. var in inner
  1067. ast := assert.New(t)
  1068. ast.Nil(UnmarshalKey(m, &in))
  1069. ast.Equal("first", in.Value)
  1070. ast.Equal(uint(2), in.Number)
  1071. }
  1072. func TestUnmarshalWithUintOptionsIncorrect(t *testing.T) {
  1073. type inner struct {
  1074. Value string `key:"value,options=first|second"`
  1075. Incorrect uint `key:"incorrect,options=1|2"`
  1076. }
  1077. m := map[string]interface{}{
  1078. "value": "first",
  1079. "incorrect": uint(3),
  1080. }
  1081. var in inner
  1082. assert.NotNil(t, UnmarshalKey(m, &in))
  1083. }
  1084. func TestUnmarshalWithOptionsAndDefault(t *testing.T) {
  1085. type inner struct {
  1086. Value string `key:"value,options=first|second|third,default=second"`
  1087. }
  1088. m := map[string]interface{}{}
  1089. var in inner
  1090. assert.Nil(t, UnmarshalKey(m, &in))
  1091. assert.Equal(t, "second", in.Value)
  1092. }
  1093. func TestUnmarshalWithOptionsAndSet(t *testing.T) {
  1094. type inner struct {
  1095. Value string `key:"value,options=first|second|third,default=second"`
  1096. }
  1097. m := map[string]interface{}{
  1098. "value": "first",
  1099. }
  1100. var in inner
  1101. assert.Nil(t, UnmarshalKey(m, &in))
  1102. assert.Equal(t, "first", in.Value)
  1103. }
  1104. func TestUnmarshalNestedKey(t *testing.T) {
  1105. var c struct {
  1106. ID int `json:"Persons.first.ID"`
  1107. }
  1108. m := map[string]interface{}{
  1109. "Persons": map[string]interface{}{
  1110. "first": map[string]interface{}{
  1111. "ID": 1,
  1112. },
  1113. },
  1114. }
  1115. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &c))
  1116. assert.Equal(t, 1, c.ID)
  1117. }
  1118. func TestUnmarhsalNestedKeyArray(t *testing.T) {
  1119. var c struct {
  1120. First []struct {
  1121. ID int
  1122. } `json:"Persons.first"`
  1123. }
  1124. m := map[string]interface{}{
  1125. "Persons": map[string]interface{}{
  1126. "first": []map[string]interface{}{
  1127. {"ID": 1},
  1128. {"ID": 2},
  1129. },
  1130. },
  1131. }
  1132. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &c))
  1133. assert.Equal(t, 2, len(c.First))
  1134. assert.Equal(t, 1, c.First[0].ID)
  1135. }
  1136. func TestUnmarshalAnonymousOptionalRequiredProvided(t *testing.T) {
  1137. type (
  1138. Foo struct {
  1139. Value string `json:"v"`
  1140. }
  1141. Bar struct {
  1142. Foo `json:",optional"`
  1143. }
  1144. )
  1145. m := map[string]interface{}{
  1146. "v": "anything",
  1147. }
  1148. var b Bar
  1149. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1150. assert.Equal(t, "anything", b.Value)
  1151. }
  1152. func TestUnmarshalAnonymousOptionalRequiredMissed(t *testing.T) {
  1153. type (
  1154. Foo struct {
  1155. Value string `json:"v"`
  1156. }
  1157. Bar struct {
  1158. Foo `json:",optional"`
  1159. }
  1160. )
  1161. m := map[string]interface{}{}
  1162. var b Bar
  1163. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1164. assert.True(t, len(b.Value) == 0)
  1165. }
  1166. func TestUnmarshalAnonymousOptionalOptionalProvided(t *testing.T) {
  1167. type (
  1168. Foo struct {
  1169. Value string `json:"v,optional"`
  1170. }
  1171. Bar struct {
  1172. Foo `json:",optional"`
  1173. }
  1174. )
  1175. m := map[string]interface{}{
  1176. "v": "anything",
  1177. }
  1178. var b Bar
  1179. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1180. assert.Equal(t, "anything", b.Value)
  1181. }
  1182. func TestUnmarshalAnonymousOptionalOptionalMissed(t *testing.T) {
  1183. type (
  1184. Foo struct {
  1185. Value string `json:"v,optional"`
  1186. }
  1187. Bar struct {
  1188. Foo `json:",optional"`
  1189. }
  1190. )
  1191. m := map[string]interface{}{}
  1192. var b Bar
  1193. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1194. assert.True(t, len(b.Value) == 0)
  1195. }
  1196. func TestUnmarshalAnonymousOptionalRequiredBothProvided(t *testing.T) {
  1197. type (
  1198. Foo struct {
  1199. Name string `json:"n"`
  1200. Value string `json:"v"`
  1201. }
  1202. Bar struct {
  1203. Foo `json:",optional"`
  1204. }
  1205. )
  1206. m := map[string]interface{}{
  1207. "n": "kevin",
  1208. "v": "anything",
  1209. }
  1210. var b Bar
  1211. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1212. assert.Equal(t, "kevin", b.Name)
  1213. assert.Equal(t, "anything", b.Value)
  1214. }
  1215. func TestUnmarshalAnonymousOptionalRequiredOneProvidedOneMissed(t *testing.T) {
  1216. type (
  1217. Foo struct {
  1218. Name string `json:"n"`
  1219. Value string `json:"v"`
  1220. }
  1221. Bar struct {
  1222. Foo `json:",optional"`
  1223. }
  1224. )
  1225. m := map[string]interface{}{
  1226. "v": "anything",
  1227. }
  1228. var b Bar
  1229. assert.NotNil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1230. }
  1231. func TestUnmarshalAnonymousOptionalRequiredBothMissed(t *testing.T) {
  1232. type (
  1233. Foo struct {
  1234. Name string `json:"n"`
  1235. Value string `json:"v"`
  1236. }
  1237. Bar struct {
  1238. Foo `json:",optional"`
  1239. }
  1240. )
  1241. m := map[string]interface{}{}
  1242. var b Bar
  1243. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1244. assert.True(t, len(b.Name) == 0)
  1245. assert.True(t, len(b.Value) == 0)
  1246. }
  1247. func TestUnmarshalAnonymousOptionalOneRequiredOneOptionalBothProvided(t *testing.T) {
  1248. type (
  1249. Foo struct {
  1250. Name string `json:"n,optional"`
  1251. Value string `json:"v"`
  1252. }
  1253. Bar struct {
  1254. Foo `json:",optional"`
  1255. }
  1256. )
  1257. m := map[string]interface{}{
  1258. "n": "kevin",
  1259. "v": "anything",
  1260. }
  1261. var b Bar
  1262. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1263. assert.Equal(t, "kevin", b.Name)
  1264. assert.Equal(t, "anything", b.Value)
  1265. }
  1266. func TestUnmarshalAnonymousOptionalOneRequiredOneOptionalBothMissed(t *testing.T) {
  1267. type (
  1268. Foo struct {
  1269. Name string `json:"n,optional"`
  1270. Value string `json:"v"`
  1271. }
  1272. Bar struct {
  1273. Foo `json:",optional"`
  1274. }
  1275. )
  1276. m := map[string]interface{}{}
  1277. var b Bar
  1278. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1279. assert.True(t, len(b.Name) == 0)
  1280. assert.True(t, len(b.Value) == 0)
  1281. }
  1282. func TestUnmarshalAnonymousOptionalOneRequiredOneOptionalRequiredProvidedOptionalMissed(t *testing.T) {
  1283. type (
  1284. Foo struct {
  1285. Name string `json:"n,optional"`
  1286. Value string `json:"v"`
  1287. }
  1288. Bar struct {
  1289. Foo `json:",optional"`
  1290. }
  1291. )
  1292. m := map[string]interface{}{
  1293. "v": "anything",
  1294. }
  1295. var b Bar
  1296. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1297. assert.True(t, len(b.Name) == 0)
  1298. assert.Equal(t, "anything", b.Value)
  1299. }
  1300. func TestUnmarshalAnonymousOptionalOneRequiredOneOptionalRequiredMissedOptionalProvided(t *testing.T) {
  1301. type (
  1302. Foo struct {
  1303. Name string `json:"n,optional"`
  1304. Value string `json:"v"`
  1305. }
  1306. Bar struct {
  1307. Foo `json:",optional"`
  1308. }
  1309. )
  1310. m := map[string]interface{}{
  1311. "n": "anything",
  1312. }
  1313. var b Bar
  1314. assert.NotNil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1315. }
  1316. func TestUnmarshalAnonymousOptionalBothOptionalBothProvided(t *testing.T) {
  1317. type (
  1318. Foo struct {
  1319. Name string `json:"n,optional"`
  1320. Value string `json:"v,optional"`
  1321. }
  1322. Bar struct {
  1323. Foo `json:",optional"`
  1324. }
  1325. )
  1326. m := map[string]interface{}{
  1327. "n": "kevin",
  1328. "v": "anything",
  1329. }
  1330. var b Bar
  1331. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1332. assert.Equal(t, "kevin", b.Name)
  1333. assert.Equal(t, "anything", b.Value)
  1334. }
  1335. func TestUnmarshalAnonymousOptionalBothOptionalOneProvidedOneMissed(t *testing.T) {
  1336. type (
  1337. Foo struct {
  1338. Name string `json:"n,optional"`
  1339. Value string `json:"v,optional"`
  1340. }
  1341. Bar struct {
  1342. Foo `json:",optional"`
  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.True(t, len(b.Name) == 0)
  1351. assert.Equal(t, "anything", b.Value)
  1352. }
  1353. func TestUnmarshalAnonymousOptionalBothOptionalBothMissed(t *testing.T) {
  1354. type (
  1355. Foo struct {
  1356. Name string `json:"n,optional"`
  1357. Value string `json:"v,optional"`
  1358. }
  1359. Bar struct {
  1360. Foo `json:",optional"`
  1361. }
  1362. )
  1363. m := map[string]interface{}{}
  1364. var b Bar
  1365. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1366. assert.True(t, len(b.Name) == 0)
  1367. assert.True(t, len(b.Value) == 0)
  1368. }
  1369. func TestUnmarshalAnonymousRequiredProvided(t *testing.T) {
  1370. type (
  1371. Foo struct {
  1372. Value string `json:"v"`
  1373. }
  1374. Bar struct {
  1375. Foo
  1376. }
  1377. )
  1378. m := map[string]interface{}{
  1379. "v": "anything",
  1380. }
  1381. var b Bar
  1382. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1383. assert.Equal(t, "anything", b.Value)
  1384. }
  1385. func TestUnmarshalAnonymousRequiredMissed(t *testing.T) {
  1386. type (
  1387. Foo struct {
  1388. Value string `json:"v"`
  1389. }
  1390. Bar struct {
  1391. Foo
  1392. }
  1393. )
  1394. m := map[string]interface{}{}
  1395. var b Bar
  1396. assert.NotNil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1397. }
  1398. func TestUnmarshalAnonymousOptionalProvided(t *testing.T) {
  1399. type (
  1400. Foo struct {
  1401. Value string `json:"v,optional"`
  1402. }
  1403. Bar struct {
  1404. Foo
  1405. }
  1406. )
  1407. m := map[string]interface{}{
  1408. "v": "anything",
  1409. }
  1410. var b Bar
  1411. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1412. assert.Equal(t, "anything", b.Value)
  1413. }
  1414. func TestUnmarshalAnonymousOptionalMissed(t *testing.T) {
  1415. type (
  1416. Foo struct {
  1417. Value string `json:"v,optional"`
  1418. }
  1419. Bar struct {
  1420. Foo
  1421. }
  1422. )
  1423. m := map[string]interface{}{}
  1424. var b Bar
  1425. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1426. assert.True(t, len(b.Value) == 0)
  1427. }
  1428. func TestUnmarshalAnonymousRequiredBothProvided(t *testing.T) {
  1429. type (
  1430. Foo struct {
  1431. Name string `json:"n"`
  1432. Value string `json:"v"`
  1433. }
  1434. Bar struct {
  1435. Foo
  1436. }
  1437. )
  1438. m := map[string]interface{}{
  1439. "n": "kevin",
  1440. "v": "anything",
  1441. }
  1442. var b Bar
  1443. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1444. assert.Equal(t, "kevin", b.Name)
  1445. assert.Equal(t, "anything", b.Value)
  1446. }
  1447. func TestUnmarshalAnonymousRequiredOneProvidedOneMissed(t *testing.T) {
  1448. type (
  1449. Foo struct {
  1450. Name string `json:"n"`
  1451. Value string `json:"v"`
  1452. }
  1453. Bar struct {
  1454. Foo
  1455. }
  1456. )
  1457. m := map[string]interface{}{
  1458. "v": "anything",
  1459. }
  1460. var b Bar
  1461. assert.NotNil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1462. }
  1463. func TestUnmarshalAnonymousRequiredBothMissed(t *testing.T) {
  1464. type (
  1465. Foo struct {
  1466. Name string `json:"n"`
  1467. Value string `json:"v"`
  1468. }
  1469. Bar struct {
  1470. Foo
  1471. }
  1472. )
  1473. m := map[string]interface{}{
  1474. "v": "anything",
  1475. }
  1476. var b Bar
  1477. assert.NotNil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1478. }
  1479. func TestUnmarshalAnonymousOneRequiredOneOptionalBothProvided(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. "n": "kevin",
  1491. "v": "anything",
  1492. }
  1493. var b Bar
  1494. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1495. assert.Equal(t, "kevin", b.Name)
  1496. assert.Equal(t, "anything", b.Value)
  1497. }
  1498. func TestUnmarshalAnonymousOneRequiredOneOptionalBothMissed(t *testing.T) {
  1499. type (
  1500. Foo struct {
  1501. Name string `json:"n,optional"`
  1502. Value string `json:"v"`
  1503. }
  1504. Bar struct {
  1505. Foo
  1506. }
  1507. )
  1508. m := map[string]interface{}{}
  1509. var b Bar
  1510. assert.NotNil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1511. }
  1512. func TestUnmarshalAnonymousOneRequiredOneOptionalRequiredProvidedOptionalMissed(t *testing.T) {
  1513. type (
  1514. Foo struct {
  1515. Name string `json:"n,optional"`
  1516. Value string `json:"v"`
  1517. }
  1518. Bar struct {
  1519. Foo
  1520. }
  1521. )
  1522. m := map[string]interface{}{
  1523. "v": "anything",
  1524. }
  1525. var b Bar
  1526. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1527. assert.True(t, len(b.Name) == 0)
  1528. assert.Equal(t, "anything", b.Value)
  1529. }
  1530. func TestUnmarshalAnonymousOneRequiredOneOptionalRequiredMissedOptionalProvided(t *testing.T) {
  1531. type (
  1532. Foo struct {
  1533. Name string `json:"n,optional"`
  1534. Value string `json:"v"`
  1535. }
  1536. Bar struct {
  1537. Foo
  1538. }
  1539. )
  1540. m := map[string]interface{}{
  1541. "n": "anything",
  1542. }
  1543. var b Bar
  1544. assert.NotNil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1545. }
  1546. func TestUnmarshalAnonymousBothOptionalBothProvided(t *testing.T) {
  1547. type (
  1548. Foo struct {
  1549. Name string `json:"n,optional"`
  1550. Value string `json:"v,optional"`
  1551. }
  1552. Bar struct {
  1553. Foo
  1554. }
  1555. )
  1556. m := map[string]interface{}{
  1557. "n": "kevin",
  1558. "v": "anything",
  1559. }
  1560. var b Bar
  1561. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1562. assert.Equal(t, "kevin", b.Name)
  1563. assert.Equal(t, "anything", b.Value)
  1564. }
  1565. func TestUnmarshalAnonymousBothOptionalOneProvidedOneMissed(t *testing.T) {
  1566. type (
  1567. Foo struct {
  1568. Name string `json:"n,optional"`
  1569. Value string `json:"v,optional"`
  1570. }
  1571. Bar struct {
  1572. Foo
  1573. }
  1574. )
  1575. m := map[string]interface{}{
  1576. "v": "anything",
  1577. }
  1578. var b Bar
  1579. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1580. assert.True(t, len(b.Name) == 0)
  1581. assert.Equal(t, "anything", b.Value)
  1582. }
  1583. func TestUnmarshalAnonymousBothOptionalBothMissed(t *testing.T) {
  1584. type (
  1585. Foo struct {
  1586. Name string `json:"n,optional"`
  1587. Value string `json:"v,optional"`
  1588. }
  1589. Bar struct {
  1590. Foo
  1591. }
  1592. )
  1593. m := map[string]interface{}{}
  1594. var b Bar
  1595. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1596. assert.True(t, len(b.Name) == 0)
  1597. assert.True(t, len(b.Value) == 0)
  1598. }
  1599. func TestUnmarshalAnonymousWrappedToMuch(t *testing.T) {
  1600. type (
  1601. Foo struct {
  1602. Name string `json:"n"`
  1603. Value string `json:"v"`
  1604. }
  1605. Bar struct {
  1606. Foo
  1607. }
  1608. )
  1609. m := map[string]interface{}{
  1610. "Foo": map[string]interface{}{
  1611. "n": "name",
  1612. "v": "anything",
  1613. },
  1614. }
  1615. var b Bar
  1616. assert.NotNil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1617. }
  1618. func TestUnmarshalWrappedObject(t *testing.T) {
  1619. type (
  1620. Foo struct {
  1621. Value string `json:"v"`
  1622. }
  1623. Bar struct {
  1624. Inner Foo
  1625. }
  1626. )
  1627. m := map[string]interface{}{
  1628. "Inner": map[string]interface{}{
  1629. "v": "anything",
  1630. },
  1631. }
  1632. var b Bar
  1633. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1634. assert.Equal(t, "anything", b.Inner.Value)
  1635. }
  1636. func TestUnmarshalWrappedObjectOptional(t *testing.T) {
  1637. type (
  1638. Foo struct {
  1639. Hosts []string
  1640. Key string
  1641. }
  1642. Bar struct {
  1643. Inner Foo `json:",optional"`
  1644. Name string
  1645. }
  1646. )
  1647. m := map[string]interface{}{
  1648. "Name": "anything",
  1649. }
  1650. var b Bar
  1651. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1652. assert.Equal(t, "anything", b.Name)
  1653. }
  1654. func TestUnmarshalWrappedObjectOptionalFilled(t *testing.T) {
  1655. type (
  1656. Foo struct {
  1657. Hosts []string
  1658. Key string
  1659. }
  1660. Bar struct {
  1661. Inner Foo `json:",optional"`
  1662. Name string
  1663. }
  1664. )
  1665. hosts := []string{"1", "2"}
  1666. m := map[string]interface{}{
  1667. "Inner": map[string]interface{}{
  1668. "Hosts": hosts,
  1669. "Key": "key",
  1670. },
  1671. "Name": "anything",
  1672. }
  1673. var b Bar
  1674. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1675. assert.EqualValues(t, hosts, b.Inner.Hosts)
  1676. assert.Equal(t, "key", b.Inner.Key)
  1677. assert.Equal(t, "anything", b.Name)
  1678. }
  1679. func TestUnmarshalWrappedNamedObjectOptional(t *testing.T) {
  1680. type (
  1681. Foo struct {
  1682. Host string
  1683. Key string
  1684. }
  1685. Bar struct {
  1686. Inner Foo `json:",optional"`
  1687. Name string
  1688. }
  1689. )
  1690. m := map[string]interface{}{
  1691. "Inner": map[string]interface{}{
  1692. "Host": "thehost",
  1693. "Key": "thekey",
  1694. },
  1695. "Name": "anything",
  1696. }
  1697. var b Bar
  1698. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1699. assert.Equal(t, "thehost", b.Inner.Host)
  1700. assert.Equal(t, "thekey", b.Inner.Key)
  1701. assert.Equal(t, "anything", b.Name)
  1702. }
  1703. func TestUnmarshalWrappedObjectNamedPtr(t *testing.T) {
  1704. type (
  1705. Foo struct {
  1706. Value string `json:"v"`
  1707. }
  1708. Bar struct {
  1709. Inner *Foo `json:"foo,optional"`
  1710. }
  1711. )
  1712. m := map[string]interface{}{
  1713. "foo": map[string]interface{}{
  1714. "v": "anything",
  1715. },
  1716. }
  1717. var b Bar
  1718. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1719. assert.Equal(t, "anything", b.Inner.Value)
  1720. }
  1721. func TestUnmarshalWrappedObjectPtr(t *testing.T) {
  1722. type (
  1723. Foo struct {
  1724. Value string `json:"v"`
  1725. }
  1726. Bar struct {
  1727. Inner *Foo
  1728. }
  1729. )
  1730. m := map[string]interface{}{
  1731. "Inner": map[string]interface{}{
  1732. "v": "anything",
  1733. },
  1734. }
  1735. var b Bar
  1736. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &b))
  1737. assert.Equal(t, "anything", b.Inner.Value)
  1738. }
  1739. func TestUnmarshalInt2String(t *testing.T) {
  1740. type inner struct {
  1741. Int string `key:"int"`
  1742. }
  1743. m := map[string]interface{}{
  1744. "int": 123,
  1745. }
  1746. var in inner
  1747. assert.NotNil(t, UnmarshalKey(m, &in))
  1748. }
  1749. func TestUnmarshalZeroValues(t *testing.T) {
  1750. type inner struct {
  1751. False bool `key:"no"`
  1752. Int int `key:"int"`
  1753. String string `key:"string"`
  1754. }
  1755. m := map[string]interface{}{
  1756. "no": false,
  1757. "int": 0,
  1758. "string": "",
  1759. }
  1760. var in inner
  1761. ast := assert.New(t)
  1762. ast.Nil(UnmarshalKey(m, &in))
  1763. ast.False(in.False)
  1764. ast.Equal(0, in.Int)
  1765. ast.Equal("", in.String)
  1766. }
  1767. func TestUnmarshalUsingDifferentKeys(t *testing.T) {
  1768. type inner struct {
  1769. False bool `key:"no"`
  1770. Int int `key:"int"`
  1771. String string `bson:"string"`
  1772. }
  1773. m := map[string]interface{}{
  1774. "no": false,
  1775. "int": 9,
  1776. "string": "value",
  1777. }
  1778. var in inner
  1779. ast := assert.New(t)
  1780. ast.Nil(UnmarshalKey(m, &in))
  1781. ast.False(in.False)
  1782. ast.Equal(9, in.Int)
  1783. ast.True(len(in.String) == 0)
  1784. }
  1785. func TestUnmarshalNumberRangeInt(t *testing.T) {
  1786. type inner struct {
  1787. Value1 int `key:"value1,range=[1:]"`
  1788. Value2 int8 `key:"value2,range=[1:5]"`
  1789. Value3 int16 `key:"value3,range=[1:5]"`
  1790. Value4 int32 `key:"value4,range=[1:5]"`
  1791. Value5 int64 `key:"value5,range=[1:5]"`
  1792. Value6 uint `key:"value6,range=[:5]"`
  1793. Value8 uint8 `key:"value8,range=[1:5],string"`
  1794. Value9 uint16 `key:"value9,range=[1:5],string"`
  1795. Value10 uint32 `key:"value10,range=[1:5],string"`
  1796. Value11 uint64 `key:"value11,range=[1:5],string"`
  1797. }
  1798. m := map[string]interface{}{
  1799. "value1": 10,
  1800. "value2": int8(1),
  1801. "value3": int16(2),
  1802. "value4": int32(4),
  1803. "value5": int64(5),
  1804. "value6": uint(0),
  1805. "value8": "1",
  1806. "value9": "2",
  1807. "value10": "4",
  1808. "value11": "5",
  1809. }
  1810. var in inner
  1811. ast := assert.New(t)
  1812. ast.Nil(UnmarshalKey(m, &in))
  1813. ast.Equal(10, in.Value1)
  1814. ast.Equal(int8(1), in.Value2)
  1815. ast.Equal(int16(2), in.Value3)
  1816. ast.Equal(int32(4), in.Value4)
  1817. ast.Equal(int64(5), in.Value5)
  1818. ast.Equal(uint(0), in.Value6)
  1819. ast.Equal(uint8(1), in.Value8)
  1820. ast.Equal(uint16(2), in.Value9)
  1821. ast.Equal(uint32(4), in.Value10)
  1822. ast.Equal(uint64(5), in.Value11)
  1823. }
  1824. func TestUnmarshalNumberRangeJsonNumber(t *testing.T) {
  1825. type inner struct {
  1826. Value3 uint `key:"value3,range=(1:5]"`
  1827. Value4 uint8 `key:"value4,range=(1:5]"`
  1828. Value5 uint16 `key:"value5,range=(1:5]"`
  1829. }
  1830. m := map[string]interface{}{
  1831. "value3": json.Number("2"),
  1832. "value4": json.Number("4"),
  1833. "value5": json.Number("5"),
  1834. }
  1835. var in inner
  1836. ast := assert.New(t)
  1837. ast.Nil(UnmarshalKey(m, &in))
  1838. ast.Equal(uint(2), in.Value3)
  1839. ast.Equal(uint8(4), in.Value4)
  1840. ast.Equal(uint16(5), in.Value5)
  1841. type inner1 struct {
  1842. Value int `key:"value,range=(1:5]"`
  1843. }
  1844. m = map[string]interface{}{
  1845. "value": json.Number("a"),
  1846. }
  1847. var in1 inner1
  1848. ast.NotNil(UnmarshalKey(m, &in1))
  1849. }
  1850. func TestUnmarshalNumberRangeIntLeftExclude(t *testing.T) {
  1851. type inner struct {
  1852. Value3 uint `key:"value3,range=(1:5]"`
  1853. Value4 uint32 `key:"value4,default=4,range=(1:5]"`
  1854. Value5 uint64 `key:"value5,range=(1:5]"`
  1855. Value9 int `key:"value9,range=(1:5],string"`
  1856. Value10 int `key:"value10,range=(1:5],string"`
  1857. Value11 int `key:"value11,range=(1:5],string"`
  1858. }
  1859. m := map[string]interface{}{
  1860. "value3": uint(2),
  1861. "value4": uint32(4),
  1862. "value5": uint64(5),
  1863. "value9": "2",
  1864. "value10": "4",
  1865. "value11": "5",
  1866. }
  1867. var in inner
  1868. ast := assert.New(t)
  1869. ast.Nil(UnmarshalKey(m, &in))
  1870. ast.Equal(uint(2), in.Value3)
  1871. ast.Equal(uint32(4), in.Value4)
  1872. ast.Equal(uint64(5), in.Value5)
  1873. ast.Equal(2, in.Value9)
  1874. ast.Equal(4, in.Value10)
  1875. ast.Equal(5, in.Value11)
  1876. }
  1877. func TestUnmarshalNumberRangeIntRightExclude(t *testing.T) {
  1878. type inner struct {
  1879. Value2 uint `key:"value2,range=[1:5)"`
  1880. Value3 uint8 `key:"value3,range=[1:5)"`
  1881. Value4 uint16 `key:"value4,range=[1:5)"`
  1882. Value8 int `key:"value8,range=[1:5),string"`
  1883. Value9 int `key:"value9,range=[1:5),string"`
  1884. Value10 int `key:"value10,range=[1:5),string"`
  1885. }
  1886. m := map[string]interface{}{
  1887. "value2": uint(1),
  1888. "value3": uint8(2),
  1889. "value4": uint16(4),
  1890. "value8": "1",
  1891. "value9": "2",
  1892. "value10": "4",
  1893. }
  1894. var in inner
  1895. ast := assert.New(t)
  1896. ast.Nil(UnmarshalKey(m, &in))
  1897. ast.Equal(uint(1), in.Value2)
  1898. ast.Equal(uint8(2), in.Value3)
  1899. ast.Equal(uint16(4), in.Value4)
  1900. ast.Equal(1, in.Value8)
  1901. ast.Equal(2, in.Value9)
  1902. ast.Equal(4, in.Value10)
  1903. }
  1904. func TestUnmarshalNumberRangeIntExclude(t *testing.T) {
  1905. type inner struct {
  1906. Value3 int `key:"value3,range=(1:5)"`
  1907. Value4 int `key:"value4,range=(1:5)"`
  1908. Value9 int `key:"value9,range=(1:5),string"`
  1909. Value10 int `key:"value10,range=(1:5),string"`
  1910. }
  1911. m := map[string]interface{}{
  1912. "value3": 2,
  1913. "value4": 4,
  1914. "value9": "2",
  1915. "value10": "4",
  1916. }
  1917. var in inner
  1918. ast := assert.New(t)
  1919. ast.Nil(UnmarshalKey(m, &in))
  1920. ast.Equal(2, in.Value3)
  1921. ast.Equal(4, in.Value4)
  1922. ast.Equal(2, in.Value9)
  1923. ast.Equal(4, in.Value10)
  1924. }
  1925. func TestUnmarshalNumberRangeIntOutOfRange(t *testing.T) {
  1926. type inner1 struct {
  1927. Value int64 `key:"value,default=3,range=(1:5)"`
  1928. }
  1929. var in1 inner1
  1930. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1931. "value": int64(1),
  1932. }, &in1))
  1933. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1934. "value": int64(0),
  1935. }, &in1))
  1936. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1937. "value": int64(5),
  1938. }, &in1))
  1939. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1940. "value": json.Number("6"),
  1941. }, &in1))
  1942. type inner2 struct {
  1943. Value int64 `key:"value,optional,range=[1:5)"`
  1944. }
  1945. var in2 inner2
  1946. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1947. "value": int64(0),
  1948. }, &in2))
  1949. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1950. "value": int64(5),
  1951. }, &in2))
  1952. type inner3 struct {
  1953. Value int64 `key:"value,range=(1:5]"`
  1954. }
  1955. var in3 inner3
  1956. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1957. "value": int64(1),
  1958. }, &in3))
  1959. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1960. "value": int64(6),
  1961. }, &in3))
  1962. type inner4 struct {
  1963. Value int64 `key:"value,range=[1:5]"`
  1964. }
  1965. var in4 inner4
  1966. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1967. "value": int64(0),
  1968. }, &in4))
  1969. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  1970. "value": int64(6),
  1971. }, &in4))
  1972. }
  1973. func TestUnmarshalNumberRangeFloat(t *testing.T) {
  1974. type inner struct {
  1975. Value2 float32 `key:"value2,range=[1:5]"`
  1976. Value3 float32 `key:"value3,range=[1:5]"`
  1977. Value4 float64 `key:"value4,range=[1:5]"`
  1978. Value5 float64 `key:"value5,range=[1:5]"`
  1979. Value8 float64 `key:"value8,range=[1:5],string"`
  1980. Value9 float64 `key:"value9,range=[1:5],string"`
  1981. Value10 float64 `key:"value10,range=[1:5],string"`
  1982. Value11 float64 `key:"value11,range=[1:5],string"`
  1983. }
  1984. m := map[string]interface{}{
  1985. "value2": float32(1),
  1986. "value3": float32(2),
  1987. "value4": float64(4),
  1988. "value5": float64(5),
  1989. "value8": "1",
  1990. "value9": "2",
  1991. "value10": "4",
  1992. "value11": "5",
  1993. }
  1994. var in inner
  1995. ast := assert.New(t)
  1996. ast.Nil(UnmarshalKey(m, &in))
  1997. ast.Equal(float32(1), in.Value2)
  1998. ast.Equal(float32(2), in.Value3)
  1999. ast.Equal(float64(4), in.Value4)
  2000. ast.Equal(float64(5), in.Value5)
  2001. ast.Equal(float64(1), in.Value8)
  2002. ast.Equal(float64(2), in.Value9)
  2003. ast.Equal(float64(4), in.Value10)
  2004. ast.Equal(float64(5), in.Value11)
  2005. }
  2006. func TestUnmarshalNumberRangeFloatLeftExclude(t *testing.T) {
  2007. type inner struct {
  2008. Value3 float64 `key:"value3,range=(1:5]"`
  2009. Value4 float64 `key:"value4,range=(1:5]"`
  2010. Value5 float64 `key:"value5,range=(1:5]"`
  2011. Value9 float64 `key:"value9,range=(1:5],string"`
  2012. Value10 float64 `key:"value10,range=(1:5],string"`
  2013. Value11 float64 `key:"value11,range=(1:5],string"`
  2014. }
  2015. m := map[string]interface{}{
  2016. "value3": float64(2),
  2017. "value4": float64(4),
  2018. "value5": float64(5),
  2019. "value9": "2",
  2020. "value10": "4",
  2021. "value11": "5",
  2022. }
  2023. var in inner
  2024. ast := assert.New(t)
  2025. ast.Nil(UnmarshalKey(m, &in))
  2026. ast.Equal(float64(2), in.Value3)
  2027. ast.Equal(float64(4), in.Value4)
  2028. ast.Equal(float64(5), in.Value5)
  2029. ast.Equal(float64(2), in.Value9)
  2030. ast.Equal(float64(4), in.Value10)
  2031. ast.Equal(float64(5), in.Value11)
  2032. }
  2033. func TestUnmarshalNumberRangeFloatRightExclude(t *testing.T) {
  2034. type inner struct {
  2035. Value2 float64 `key:"value2,range=[1:5)"`
  2036. Value3 float64 `key:"value3,range=[1:5)"`
  2037. Value4 float64 `key:"value4,range=[1:5)"`
  2038. Value8 float64 `key:"value8,range=[1:5),string"`
  2039. Value9 float64 `key:"value9,range=[1:5),string"`
  2040. Value10 float64 `key:"value10,range=[1:5),string"`
  2041. }
  2042. m := map[string]interface{}{
  2043. "value2": float64(1),
  2044. "value3": float64(2),
  2045. "value4": float64(4),
  2046. "value8": "1",
  2047. "value9": "2",
  2048. "value10": "4",
  2049. }
  2050. var in inner
  2051. ast := assert.New(t)
  2052. ast.Nil(UnmarshalKey(m, &in))
  2053. ast.Equal(float64(1), in.Value2)
  2054. ast.Equal(float64(2), in.Value3)
  2055. ast.Equal(float64(4), in.Value4)
  2056. ast.Equal(float64(1), in.Value8)
  2057. ast.Equal(float64(2), in.Value9)
  2058. ast.Equal(float64(4), in.Value10)
  2059. }
  2060. func TestUnmarshalNumberRangeFloatExclude(t *testing.T) {
  2061. type inner struct {
  2062. Value3 float64 `key:"value3,range=(1:5)"`
  2063. Value4 float64 `key:"value4,range=(1:5)"`
  2064. Value9 float64 `key:"value9,range=(1:5),string"`
  2065. Value10 float64 `key:"value10,range=(1:5),string"`
  2066. }
  2067. m := map[string]interface{}{
  2068. "value3": float64(2),
  2069. "value4": float64(4),
  2070. "value9": "2",
  2071. "value10": "4",
  2072. }
  2073. var in inner
  2074. ast := assert.New(t)
  2075. ast.Nil(UnmarshalKey(m, &in))
  2076. ast.Equal(float64(2), in.Value3)
  2077. ast.Equal(float64(4), in.Value4)
  2078. ast.Equal(float64(2), in.Value9)
  2079. ast.Equal(float64(4), in.Value10)
  2080. }
  2081. func TestUnmarshalNumberRangeFloatOutOfRange(t *testing.T) {
  2082. type inner1 struct {
  2083. Value float64 `key:"value,range=(1:5)"`
  2084. }
  2085. var in1 inner1
  2086. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  2087. "value": float64(1),
  2088. }, &in1))
  2089. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  2090. "value": float64(0),
  2091. }, &in1))
  2092. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  2093. "value": float64(5),
  2094. }, &in1))
  2095. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  2096. "value": json.Number("6"),
  2097. }, &in1))
  2098. type inner2 struct {
  2099. Value float64 `key:"value,range=[1:5)"`
  2100. }
  2101. var in2 inner2
  2102. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  2103. "value": float64(0),
  2104. }, &in2))
  2105. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  2106. "value": float64(5),
  2107. }, &in2))
  2108. type inner3 struct {
  2109. Value float64 `key:"value,range=(1:5]"`
  2110. }
  2111. var in3 inner3
  2112. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  2113. "value": float64(1),
  2114. }, &in3))
  2115. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  2116. "value": float64(6),
  2117. }, &in3))
  2118. type inner4 struct {
  2119. Value float64 `key:"value,range=[1:5]"`
  2120. }
  2121. var in4 inner4
  2122. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  2123. "value": float64(0),
  2124. }, &in4))
  2125. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  2126. "value": float64(6),
  2127. }, &in4))
  2128. }
  2129. func TestUnmarshalRangeError(t *testing.T) {
  2130. type inner1 struct {
  2131. Value int `key:",range="`
  2132. }
  2133. var in1 inner1
  2134. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  2135. "Value": 1,
  2136. }, &in1))
  2137. type inner2 struct {
  2138. Value int `key:",range=["`
  2139. }
  2140. var in2 inner2
  2141. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  2142. "Value": 1,
  2143. }, &in2))
  2144. type inner3 struct {
  2145. Value int `key:",range=[:"`
  2146. }
  2147. var in3 inner3
  2148. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  2149. "Value": 1,
  2150. }, &in3))
  2151. type inner4 struct {
  2152. Value int `key:",range=[:]"`
  2153. }
  2154. var in4 inner4
  2155. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  2156. "Value": 1,
  2157. }, &in4))
  2158. type inner5 struct {
  2159. Value int `key:",range={:]"`
  2160. }
  2161. var in5 inner5
  2162. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  2163. "Value": 1,
  2164. }, &in5))
  2165. type inner6 struct {
  2166. Value int `key:",range=[:}"`
  2167. }
  2168. var in6 inner6
  2169. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  2170. "Value": 1,
  2171. }, &in6))
  2172. type inner7 struct {
  2173. Value int `key:",range=[]"`
  2174. }
  2175. var in7 inner7
  2176. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  2177. "Value": 1,
  2178. }, &in7))
  2179. type inner8 struct {
  2180. Value int `key:",range=[a:]"`
  2181. }
  2182. var in8 inner8
  2183. assert.NotNil(t, UnmarshalKey(map[string]interface{}{
  2184. "Value": 1,
  2185. }, &in8))
  2186. type inner9 struct {
  2187. Value int `key:",range=[:a]"`
  2188. }
  2189. var in9 inner9
  2190. assert.NotNil(t, UnmarshalKey(map[string]interface{}{
  2191. "Value": 1,
  2192. }, &in9))
  2193. type inner10 struct {
  2194. Value int `key:",range"`
  2195. }
  2196. var in10 inner10
  2197. assert.NotNil(t, UnmarshalKey(map[string]interface{}{
  2198. "Value": 1,
  2199. }, &in10))
  2200. type inner11 struct {
  2201. Value int `key:",range=[1,2]"`
  2202. }
  2203. var in11 inner11
  2204. assert.Equal(t, errNumberRange, UnmarshalKey(map[string]interface{}{
  2205. "Value": "a",
  2206. }, &in11))
  2207. }
  2208. func TestUnmarshalNestedMap(t *testing.T) {
  2209. var c struct {
  2210. Anything map[string]map[string]string `json:"anything"`
  2211. }
  2212. m := map[string]interface{}{
  2213. "anything": map[string]map[string]interface{}{
  2214. "inner": {
  2215. "id": "1",
  2216. "name": "any",
  2217. },
  2218. },
  2219. }
  2220. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &c))
  2221. assert.Equal(t, "1", c.Anything["inner"]["id"])
  2222. }
  2223. func TestUnmarshalNestedMapMismatch(t *testing.T) {
  2224. var c struct {
  2225. Anything map[string]map[string]map[string]string `json:"anything"`
  2226. }
  2227. m := map[string]interface{}{
  2228. "anything": map[string]map[string]interface{}{
  2229. "inner": {
  2230. "name": "any",
  2231. },
  2232. },
  2233. }
  2234. assert.NotNil(t, NewUnmarshaler("json").Unmarshal(m, &c))
  2235. }
  2236. func TestUnmarshalNestedMapSimple(t *testing.T) {
  2237. var c struct {
  2238. Anything map[string]string `json:"anything"`
  2239. }
  2240. m := map[string]interface{}{
  2241. "anything": map[string]interface{}{
  2242. "id": "1",
  2243. "name": "any",
  2244. },
  2245. }
  2246. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &c))
  2247. assert.Equal(t, "1", c.Anything["id"])
  2248. }
  2249. func TestUnmarshalNestedMapSimpleTypeMatch(t *testing.T) {
  2250. var c struct {
  2251. Anything map[string]string `json:"anything"`
  2252. }
  2253. m := map[string]interface{}{
  2254. "anything": map[string]string{
  2255. "id": "1",
  2256. "name": "any",
  2257. },
  2258. }
  2259. assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &c))
  2260. assert.Equal(t, "1", c.Anything["id"])
  2261. }
  2262. func TestUnmarshalValuer(t *testing.T) {
  2263. unmarshaler := NewUnmarshaler(jsonTagKey)
  2264. var foo string
  2265. err := unmarshaler.UnmarshalValuer(nil, foo)
  2266. assert.NotNil(t, err)
  2267. }
  2268. func BenchmarkUnmarshalString(b *testing.B) {
  2269. type inner struct {
  2270. Value string `key:"value"`
  2271. }
  2272. m := map[string]interface{}{
  2273. "value": "first",
  2274. }
  2275. for i := 0; i < b.N; i++ {
  2276. var in inner
  2277. if err := UnmarshalKey(m, &in); err != nil {
  2278. b.Fatal(err)
  2279. }
  2280. }
  2281. }
  2282. func BenchmarkUnmarshalStruct(b *testing.B) {
  2283. b.ReportAllocs()
  2284. m := map[string]interface{}{
  2285. "Ids": []map[string]interface{}{
  2286. {
  2287. "First": 1,
  2288. "Second": 2,
  2289. },
  2290. },
  2291. }
  2292. for i := 0; i < b.N; i++ {
  2293. var v struct {
  2294. Ids []struct {
  2295. First int
  2296. Second int
  2297. }
  2298. }
  2299. if err := UnmarshalKey(m, &v); err != nil {
  2300. b.Fatal(err)
  2301. }
  2302. }
  2303. }
  2304. func BenchmarkMapToStruct(b *testing.B) {
  2305. data := map[string]interface{}{
  2306. "valid": "1",
  2307. "age": "5",
  2308. "name": "liao",
  2309. }
  2310. type anonymous struct {
  2311. Valid bool
  2312. Age int
  2313. Name string
  2314. }
  2315. for i := 0; i < b.N; i++ {
  2316. var an anonymous
  2317. if valid, ok := data["valid"]; ok {
  2318. an.Valid = valid == "1"
  2319. }
  2320. if age, ok := data["age"]; ok {
  2321. ages, _ := age.(string)
  2322. an.Age, _ = strconv.Atoi(ages)
  2323. }
  2324. if name, ok := data["name"]; ok {
  2325. names, _ := name.(string)
  2326. an.Name = names
  2327. }
  2328. }
  2329. }
  2330. func BenchmarkUnmarshal(b *testing.B) {
  2331. data := map[string]interface{}{
  2332. "valid": "1",
  2333. "age": "5",
  2334. "name": "liao",
  2335. }
  2336. type anonymous struct {
  2337. Valid bool `key:"valid,string"`
  2338. Age int `key:"age,string"`
  2339. Name string `key:"name"`
  2340. }
  2341. for i := 0; i < b.N; i++ {
  2342. var an anonymous
  2343. UnmarshalKey(data, &an)
  2344. }
  2345. }
  2346. func TestUnmarshalJsonReaderMultiArray(t *testing.T) {
  2347. payload := `{"a": "133", "b": [["add", "cccd"], ["eeee"]]}`
  2348. var res struct {
  2349. A string `json:"a"`
  2350. B [][]string `json:"b"`
  2351. }
  2352. reader := strings.NewReader(payload)
  2353. err := UnmarshalJsonReader(reader, &res)
  2354. assert.Nil(t, err)
  2355. assert.Equal(t, 2, len(res.B))
  2356. }
  2357. func TestUnmarshalJsonReaderPtrMultiArray(t *testing.T) {
  2358. payload := `{"a": "133", "b": [["add", "cccd"], ["eeee"]]}`
  2359. var res struct {
  2360. A string `json:"a"`
  2361. B [][]*string `json:"b"`
  2362. }
  2363. reader := strings.NewReader(payload)
  2364. err := UnmarshalJsonReader(reader, &res)
  2365. assert.Nil(t, err)
  2366. assert.Equal(t, 2, len(res.B))
  2367. assert.Equal(t, 2, len(res.B[0]))
  2368. }
  2369. func TestUnmarshalJsonReaderPtrArray(t *testing.T) {
  2370. payload := `{"a": "133", "b": ["add", "cccd", "eeee"]}`
  2371. var res struct {
  2372. A string `json:"a"`
  2373. B []*string `json:"b"`
  2374. }
  2375. reader := strings.NewReader(payload)
  2376. err := UnmarshalJsonReader(reader, &res)
  2377. assert.Nil(t, err)
  2378. assert.Equal(t, 3, len(res.B))
  2379. }
  2380. func TestUnmarshalJsonWithoutKey(t *testing.T) {
  2381. payload := `{"A": "1", "B": "2"}`
  2382. var res struct {
  2383. A string `json:""`
  2384. B string `json:","`
  2385. }
  2386. reader := strings.NewReader(payload)
  2387. err := UnmarshalJsonReader(reader, &res)
  2388. assert.Nil(t, err)
  2389. assert.Equal(t, "1", res.A)
  2390. assert.Equal(t, "2", res.B)
  2391. }
  2392. func TestUnmarshalJsonUintNegative(t *testing.T) {
  2393. payload := `{"a": -1}`
  2394. var res struct {
  2395. A uint `json:"a"`
  2396. }
  2397. reader := strings.NewReader(payload)
  2398. err := UnmarshalJsonReader(reader, &res)
  2399. assert.NotNil(t, err)
  2400. }
  2401. func TestUnmarshalJsonDefinedInt(t *testing.T) {
  2402. type Int int
  2403. var res struct {
  2404. A Int `json:"a"`
  2405. }
  2406. payload := `{"a": -1}`
  2407. reader := strings.NewReader(payload)
  2408. err := UnmarshalJsonReader(reader, &res)
  2409. assert.Nil(t, err)
  2410. assert.Equal(t, Int(-1), res.A)
  2411. }
  2412. func TestUnmarshalJsonDefinedString(t *testing.T) {
  2413. type String string
  2414. var res struct {
  2415. A String `json:"a"`
  2416. }
  2417. payload := `{"a": "foo"}`
  2418. reader := strings.NewReader(payload)
  2419. err := UnmarshalJsonReader(reader, &res)
  2420. assert.Nil(t, err)
  2421. assert.Equal(t, String("foo"), res.A)
  2422. }
  2423. func TestUnmarshalJsonDefinedStringPtr(t *testing.T) {
  2424. type String string
  2425. var res struct {
  2426. A *String `json:"a"`
  2427. }
  2428. payload := `{"a": "foo"}`
  2429. reader := strings.NewReader(payload)
  2430. err := UnmarshalJsonReader(reader, &res)
  2431. assert.Nil(t, err)
  2432. assert.Equal(t, String("foo"), *res.A)
  2433. }
  2434. func TestUnmarshalJsonReaderComplex(t *testing.T) {
  2435. type (
  2436. MyInt int
  2437. MyTxt string
  2438. MyTxtArray []string
  2439. Req struct {
  2440. MyInt MyInt `json:"my_int"` // int.. ok
  2441. MyTxtArray MyTxtArray `json:"my_txt_array"`
  2442. MyTxt MyTxt `json:"my_txt"` // but string is not assignable
  2443. Int int `json:"int"`
  2444. Txt string `json:"txt"`
  2445. }
  2446. )
  2447. body := `{
  2448. "my_int": 100,
  2449. "my_txt_array": [
  2450. "a",
  2451. "b"
  2452. ],
  2453. "my_txt": "my_txt",
  2454. "int": 200,
  2455. "txt": "txt"
  2456. }`
  2457. var req Req
  2458. err := UnmarshalJsonReader(strings.NewReader(body), &req)
  2459. assert.Nil(t, err)
  2460. assert.Equal(t, MyInt(100), req.MyInt)
  2461. assert.Equal(t, MyTxt("my_txt"), req.MyTxt)
  2462. assert.EqualValues(t, MyTxtArray([]string{"a", "b"}), req.MyTxtArray)
  2463. assert.Equal(t, 200, req.Int)
  2464. assert.Equal(t, "txt", req.Txt)
  2465. }
  2466. func BenchmarkDefaultValue(b *testing.B) {
  2467. for i := 0; i < b.N; i++ {
  2468. var a struct {
  2469. Ints []int `json:"ints,default=[1,2,3]"`
  2470. Strs []string `json:"strs,default=[foo,bar,baz]"`
  2471. }
  2472. _ = UnmarshalJsonMap(nil, &a)
  2473. if len(a.Strs) != 3 || len(a.Ints) != 3 {
  2474. b.Fatal("failed")
  2475. }
  2476. }
  2477. }