unmarshaler_test.go 61 KB

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