unmarshaler_test.go 52 KB

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