mapstructure_test.go 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546
  1. package mhayaMapStructure
  2. import (
  3. "encoding/json"
  4. "io"
  5. "reflect"
  6. "sort"
  7. "strings"
  8. "testing"
  9. )
  10. type Basic struct {
  11. Vstring string
  12. Vint int
  13. Vint8 int8
  14. Vint16 int16
  15. Vint32 int32
  16. Vint64 int64
  17. Vuint uint
  18. Vbool bool
  19. Vfloat float64
  20. Vextra string
  21. vsilent bool
  22. Vdata interface{}
  23. VjsonInt int
  24. VjsonUint uint
  25. VjsonUint64 uint64
  26. VjsonFloat float64
  27. VjsonNumber json.Number
  28. }
  29. type BasicPointer struct {
  30. Vstring *string
  31. Vint *int
  32. Vuint *uint
  33. Vbool *bool
  34. Vfloat *float64
  35. Vextra *string
  36. vsilent *bool
  37. Vdata *interface{}
  38. VjsonInt *int
  39. VjsonFloat *float64
  40. VjsonNumber *json.Number
  41. }
  42. type BasicSquash struct {
  43. Test Basic `mapstructure:",squash"`
  44. }
  45. type Embedded struct {
  46. Basic
  47. Vunique string
  48. }
  49. type EmbeddedPointer struct {
  50. *Basic
  51. Vunique string
  52. }
  53. type EmbeddedSquash struct {
  54. Basic `mapstructure:",squash"`
  55. Vunique string
  56. }
  57. type EmbeddedPointerSquash struct {
  58. *Basic `mapstructure:",squash"`
  59. Vunique string
  60. }
  61. type EmbeddedAndNamed struct {
  62. Basic
  63. Named Basic
  64. Vunique string
  65. }
  66. type SliceAlias []string
  67. type EmbeddedSlice struct {
  68. SliceAlias `mapstructure:"slice_alias"`
  69. Vunique string
  70. }
  71. type ArrayAlias [2]string
  72. type EmbeddedArray struct {
  73. ArrayAlias `mapstructure:"array_alias"`
  74. Vunique string
  75. }
  76. type SquashOnNonStructType struct {
  77. InvalidSquashType int `mapstructure:",squash"`
  78. }
  79. type Map struct {
  80. Vfoo string
  81. Vother map[string]string
  82. }
  83. type MapOfStruct struct {
  84. Value map[string]Basic
  85. }
  86. type Nested struct {
  87. Vfoo string
  88. Vbar Basic
  89. }
  90. type NestedPointer struct {
  91. Vfoo string
  92. Vbar *Basic
  93. }
  94. type NilInterface struct {
  95. W io.Writer
  96. }
  97. type NilPointer struct {
  98. Value *string
  99. }
  100. type Slice struct {
  101. Vfoo string
  102. Vbar []string
  103. }
  104. type SliceOfAlias struct {
  105. Vfoo string
  106. Vbar SliceAlias
  107. }
  108. type SliceOfStruct struct {
  109. Value []Basic
  110. }
  111. type SlicePointer struct {
  112. Vbar *[]string
  113. }
  114. type Array struct {
  115. Vfoo string
  116. Vbar [2]string
  117. }
  118. type ArrayOfStruct struct {
  119. Value [2]Basic
  120. }
  121. type Func struct {
  122. Foo func() string
  123. }
  124. type Tagged struct {
  125. Extra string `mapstructure:"bar,what,what"`
  126. Value string `mapstructure:"foo"`
  127. }
  128. type Remainder struct {
  129. A string
  130. Extra map[string]interface{} `mapstructure:",remain"`
  131. }
  132. type StructWithOmitEmpty struct {
  133. VisibleStringField string `mapstructure:"visible-string"`
  134. OmitStringField string `mapstructure:"omittable-string,omitempty"`
  135. VisibleIntField int `mapstructure:"visible-int"`
  136. OmitIntField int `mapstructure:"omittable-int,omitempty"`
  137. VisibleFloatField float64 `mapstructure:"visible-float"`
  138. OmitFloatField float64 `mapstructure:"omittable-float,omitempty"`
  139. VisibleSliceField []interface{} `mapstructure:"visible-slice"`
  140. OmitSliceField []interface{} `mapstructure:"omittable-slice,omitempty"`
  141. VisibleMapField map[string]interface{} `mapstructure:"visible-map"`
  142. OmitMapField map[string]interface{} `mapstructure:"omittable-map,omitempty"`
  143. NestedField *Nested `mapstructure:"visible-nested"`
  144. OmitNestedField *Nested `mapstructure:"omittable-nested,omitempty"`
  145. }
  146. type TypeConversionResult struct {
  147. IntToFloat float32
  148. IntToUint uint
  149. IntToBool bool
  150. IntToString string
  151. UintToInt int
  152. UintToFloat float32
  153. UintToBool bool
  154. UintToString string
  155. BoolToInt int
  156. BoolToUint uint
  157. BoolToFloat float32
  158. BoolToString string
  159. FloatToInt int
  160. FloatToUint uint
  161. FloatToBool bool
  162. FloatToString string
  163. SliceUint8ToString string
  164. StringToSliceUint8 []byte
  165. ArrayUint8ToString string
  166. StringToInt int
  167. StringToUint uint
  168. StringToBool bool
  169. StringToFloat float32
  170. StringToStrSlice []string
  171. StringToIntSlice []int
  172. StringToStrArray [1]string
  173. StringToIntArray [1]int
  174. SliceToMap map[string]interface{}
  175. MapToSlice []interface{}
  176. ArrayToMap map[string]interface{}
  177. MapToArray [1]interface{}
  178. }
  179. func TestBasicTypes(t *testing.T) {
  180. t.Parallel()
  181. input := map[string]interface{}{
  182. "vstring": "foo",
  183. "vint": 42,
  184. "vint8": 42,
  185. "vint16": 42,
  186. "vint32": 42,
  187. "vint64": 42,
  188. "Vuint": 42,
  189. "vbool": true,
  190. "Vfloat": 42.42,
  191. "vsilent": true,
  192. "vdata": 42,
  193. "vjsonInt": json.Number("1234"),
  194. "vjsonUint": json.Number("1234"),
  195. "vjsonUint64": json.Number("9223372036854775809"), // 2^63 + 1
  196. "vjsonFloat": json.Number("1234.5"),
  197. "vjsonNumber": json.Number("1234.5"),
  198. }
  199. var result Basic
  200. err := Decode(input, &result)
  201. if err != nil {
  202. t.Errorf("got an err: %s", err.Error())
  203. t.FailNow()
  204. }
  205. if result.Vstring != "foo" {
  206. t.Errorf("vstring value should be 'foo': %#v", result.Vstring)
  207. }
  208. if result.Vint != 42 {
  209. t.Errorf("vint value should be 42: %#v", result.Vint)
  210. }
  211. if result.Vint8 != 42 {
  212. t.Errorf("vint8 value should be 42: %#v", result.Vint)
  213. }
  214. if result.Vint16 != 42 {
  215. t.Errorf("vint16 value should be 42: %#v", result.Vint)
  216. }
  217. if result.Vint32 != 42 {
  218. t.Errorf("vint32 value should be 42: %#v", result.Vint)
  219. }
  220. if result.Vint64 != 42 {
  221. t.Errorf("vint64 value should be 42: %#v", result.Vint)
  222. }
  223. if result.Vuint != 42 {
  224. t.Errorf("vuint value should be 42: %#v", result.Vuint)
  225. }
  226. if result.Vbool != true {
  227. t.Errorf("vbool value should be true: %#v", result.Vbool)
  228. }
  229. if result.Vfloat != 42.42 {
  230. t.Errorf("vfloat value should be 42.42: %#v", result.Vfloat)
  231. }
  232. if result.Vextra != "" {
  233. t.Errorf("vextra value should be empty: %#v", result.Vextra)
  234. }
  235. if result.vsilent != false {
  236. t.Error("vsilent should not be set, it is unexported")
  237. }
  238. if result.Vdata != 42 {
  239. t.Error("vdata should be valid")
  240. }
  241. if result.VjsonInt != 1234 {
  242. t.Errorf("vjsonint value should be 1234: %#v", result.VjsonInt)
  243. }
  244. if result.VjsonUint != 1234 {
  245. t.Errorf("vjsonuint value should be 1234: %#v", result.VjsonUint)
  246. }
  247. if result.VjsonUint64 != 9223372036854775809 {
  248. t.Errorf("vjsonuint64 value should be 9223372036854775809: %#v", result.VjsonUint64)
  249. }
  250. if result.VjsonFloat != 1234.5 {
  251. t.Errorf("vjsonfloat value should be 1234.5: %#v", result.VjsonFloat)
  252. }
  253. if !reflect.DeepEqual(result.VjsonNumber, json.Number("1234.5")) {
  254. t.Errorf("vjsonnumber value should be '1234.5': %T, %#v", result.VjsonNumber, result.VjsonNumber)
  255. }
  256. }
  257. func TestBasic_IntWithFloat(t *testing.T) {
  258. t.Parallel()
  259. input := map[string]interface{}{
  260. "vint": float64(42),
  261. }
  262. var result Basic
  263. err := Decode(input, &result)
  264. if err != nil {
  265. t.Fatalf("got an err: %s", err)
  266. }
  267. }
  268. func TestBasic_Merge(t *testing.T) {
  269. t.Parallel()
  270. input := map[string]interface{}{
  271. "vint": 42,
  272. }
  273. var result Basic
  274. result.Vuint = 100
  275. err := Decode(input, &result)
  276. if err != nil {
  277. t.Fatalf("got an err: %s", err)
  278. }
  279. expected := Basic{
  280. Vint: 42,
  281. Vuint: 100,
  282. }
  283. if !reflect.DeepEqual(result, expected) {
  284. t.Fatalf("bad: %#v", result)
  285. }
  286. }
  287. // Test for issue #46.
  288. func TestBasic_Struct(t *testing.T) {
  289. t.Parallel()
  290. input := map[string]interface{}{
  291. "vdata": map[string]interface{}{
  292. "vstring": "foo",
  293. },
  294. }
  295. var result, inner Basic
  296. result.Vdata = &inner
  297. err := Decode(input, &result)
  298. if err != nil {
  299. t.Fatalf("got an err: %s", err)
  300. }
  301. expected := Basic{
  302. Vdata: &Basic{
  303. Vstring: "foo",
  304. },
  305. }
  306. if !reflect.DeepEqual(result, expected) {
  307. t.Fatalf("bad: %#v", result)
  308. }
  309. }
  310. func TestBasic_interfaceStruct(t *testing.T) {
  311. t.Parallel()
  312. input := map[string]interface{}{
  313. "vstring": "foo",
  314. }
  315. var iface interface{} = &Basic{}
  316. err := Decode(input, &iface)
  317. if err != nil {
  318. t.Fatalf("got an err: %s", err)
  319. }
  320. expected := &Basic{
  321. Vstring: "foo",
  322. }
  323. if !reflect.DeepEqual(iface, expected) {
  324. t.Fatalf("bad: %#v", iface)
  325. }
  326. }
  327. // Issue 187
  328. func TestBasic_interfaceStructNonPtr(t *testing.T) {
  329. t.Parallel()
  330. input := map[string]interface{}{
  331. "vstring": "foo",
  332. }
  333. var iface interface{} = Basic{}
  334. err := Decode(input, &iface)
  335. if err != nil {
  336. t.Fatalf("got an err: %s", err)
  337. }
  338. expected := Basic{
  339. Vstring: "foo",
  340. }
  341. if !reflect.DeepEqual(iface, expected) {
  342. t.Fatalf("bad: %#v", iface)
  343. }
  344. }
  345. func TestDecode_BasicSquash(t *testing.T) {
  346. t.Parallel()
  347. input := map[string]interface{}{
  348. "vstring": "foo",
  349. }
  350. var result BasicSquash
  351. err := Decode(input, &result)
  352. if err != nil {
  353. t.Fatalf("got an err: %s", err.Error())
  354. }
  355. if result.Test.Vstring != "foo" {
  356. t.Errorf("vstring value should be 'foo': %#v", result.Test.Vstring)
  357. }
  358. }
  359. func TestDecodeFrom_BasicSquash(t *testing.T) {
  360. t.Parallel()
  361. var v interface{}
  362. var ok bool
  363. input := BasicSquash{
  364. Test: Basic{
  365. Vstring: "foo",
  366. },
  367. }
  368. var result map[string]interface{}
  369. err := Decode(input, &result)
  370. if err != nil {
  371. t.Fatalf("got an err: %s", err.Error())
  372. }
  373. if _, ok = result["Test"]; ok {
  374. t.Error("test should not be present in map")
  375. }
  376. v, ok = result["Vstring"]
  377. if !ok {
  378. t.Error("vstring should be present in map")
  379. } else if !reflect.DeepEqual(v, "foo") {
  380. t.Errorf("vstring value should be 'foo': %#v", v)
  381. }
  382. }
  383. func TestDecode_Embedded(t *testing.T) {
  384. t.Parallel()
  385. input := map[string]interface{}{
  386. "vstring": "foo",
  387. "Basic": map[string]interface{}{
  388. "vstring": "innerfoo",
  389. },
  390. "vunique": "bar",
  391. }
  392. var result Embedded
  393. err := Decode(input, &result)
  394. if err != nil {
  395. t.Fatalf("got an err: %s", err.Error())
  396. }
  397. if result.Vstring != "innerfoo" {
  398. t.Errorf("vstring value should be 'innerfoo': %#v", result.Vstring)
  399. }
  400. if result.Vunique != "bar" {
  401. t.Errorf("vunique value should be 'bar': %#v", result.Vunique)
  402. }
  403. }
  404. func TestDecode_EmbeddedPointer(t *testing.T) {
  405. t.Parallel()
  406. input := map[string]interface{}{
  407. "vstring": "foo",
  408. "Basic": map[string]interface{}{
  409. "vstring": "innerfoo",
  410. },
  411. "vunique": "bar",
  412. }
  413. var result EmbeddedPointer
  414. err := Decode(input, &result)
  415. if err != nil {
  416. t.Fatalf("err: %s", err)
  417. }
  418. expected := EmbeddedPointer{
  419. Basic: &Basic{
  420. Vstring: "innerfoo",
  421. },
  422. Vunique: "bar",
  423. }
  424. if !reflect.DeepEqual(result, expected) {
  425. t.Fatalf("bad: %#v", result)
  426. }
  427. }
  428. func TestDecode_EmbeddedSlice(t *testing.T) {
  429. t.Parallel()
  430. input := map[string]interface{}{
  431. "slice_alias": []string{"foo", "bar"},
  432. "vunique": "bar",
  433. }
  434. var result EmbeddedSlice
  435. err := Decode(input, &result)
  436. if err != nil {
  437. t.Fatalf("got an err: %s", err.Error())
  438. }
  439. if !reflect.DeepEqual(result.SliceAlias, SliceAlias([]string{"foo", "bar"})) {
  440. t.Errorf("slice value: %#v", result.SliceAlias)
  441. }
  442. if result.Vunique != "bar" {
  443. t.Errorf("vunique value should be 'bar': %#v", result.Vunique)
  444. }
  445. }
  446. func TestDecode_EmbeddedArray(t *testing.T) {
  447. t.Parallel()
  448. input := map[string]interface{}{
  449. "array_alias": [2]string{"foo", "bar"},
  450. "vunique": "bar",
  451. }
  452. var result EmbeddedArray
  453. err := Decode(input, &result)
  454. if err != nil {
  455. t.Fatalf("got an err: %s", err.Error())
  456. }
  457. if !reflect.DeepEqual(result.ArrayAlias, ArrayAlias([2]string{"foo", "bar"})) {
  458. t.Errorf("array value: %#v", result.ArrayAlias)
  459. }
  460. if result.Vunique != "bar" {
  461. t.Errorf("vunique value should be 'bar': %#v", result.Vunique)
  462. }
  463. }
  464. func TestDecode_EmbeddedNoSquash(t *testing.T) {
  465. t.Parallel()
  466. input := map[string]interface{}{
  467. "vstring": "foo",
  468. "vunique": "bar",
  469. }
  470. var result Embedded
  471. err := Decode(input, &result)
  472. if err != nil {
  473. t.Fatalf("got an err: %s", err.Error())
  474. }
  475. if result.Vstring != "" {
  476. t.Errorf("vstring value should be empty: %#v", result.Vstring)
  477. }
  478. if result.Vunique != "bar" {
  479. t.Errorf("vunique value should be 'bar': %#v", result.Vunique)
  480. }
  481. }
  482. func TestDecode_EmbeddedPointerNoSquash(t *testing.T) {
  483. t.Parallel()
  484. input := map[string]interface{}{
  485. "vstring": "foo",
  486. "vunique": "bar",
  487. }
  488. result := EmbeddedPointer{
  489. Basic: &Basic{},
  490. }
  491. err := Decode(input, &result)
  492. if err != nil {
  493. t.Fatalf("err: %s", err)
  494. }
  495. if result.Vstring != "" {
  496. t.Errorf("vstring value should be empty: %#v", result.Vstring)
  497. }
  498. if result.Vunique != "bar" {
  499. t.Errorf("vunique value should be 'bar': %#v", result.Vunique)
  500. }
  501. }
  502. func TestDecode_EmbeddedSquash(t *testing.T) {
  503. t.Parallel()
  504. input := map[string]interface{}{
  505. "vstring": "foo",
  506. "vunique": "bar",
  507. }
  508. var result EmbeddedSquash
  509. err := Decode(input, &result)
  510. if err != nil {
  511. t.Fatalf("got an err: %s", err.Error())
  512. }
  513. if result.Vstring != "foo" {
  514. t.Errorf("vstring value should be 'foo': %#v", result.Vstring)
  515. }
  516. if result.Vunique != "bar" {
  517. t.Errorf("vunique value should be 'bar': %#v", result.Vunique)
  518. }
  519. }
  520. func TestDecodeFrom_EmbeddedSquash(t *testing.T) {
  521. t.Parallel()
  522. var v interface{}
  523. var ok bool
  524. input := EmbeddedSquash{
  525. Basic: Basic{
  526. Vstring: "foo",
  527. },
  528. Vunique: "bar",
  529. }
  530. var result map[string]interface{}
  531. err := Decode(input, &result)
  532. if err != nil {
  533. t.Fatalf("got an err: %s", err.Error())
  534. }
  535. if _, ok = result["Basic"]; ok {
  536. t.Error("basic should not be present in map")
  537. }
  538. v, ok = result["Vstring"]
  539. if !ok {
  540. t.Error("vstring should be present in map")
  541. } else if !reflect.DeepEqual(v, "foo") {
  542. t.Errorf("vstring value should be 'foo': %#v", v)
  543. }
  544. v, ok = result["Vunique"]
  545. if !ok {
  546. t.Error("vunique should be present in map")
  547. } else if !reflect.DeepEqual(v, "bar") {
  548. t.Errorf("vunique value should be 'bar': %#v", v)
  549. }
  550. }
  551. func TestDecode_EmbeddedPointerSquash_FromStructToMap(t *testing.T) {
  552. t.Parallel()
  553. input := EmbeddedPointerSquash{
  554. Basic: &Basic{
  555. Vstring: "foo",
  556. },
  557. Vunique: "bar",
  558. }
  559. var result map[string]interface{}
  560. err := Decode(input, &result)
  561. if err != nil {
  562. t.Fatalf("got an err: %s", err.Error())
  563. }
  564. if result["Vstring"] != "foo" {
  565. t.Errorf("vstring value should be 'foo': %#v", result["Vstring"])
  566. }
  567. if result["Vunique"] != "bar" {
  568. t.Errorf("vunique value should be 'bar': %#v", result["Vunique"])
  569. }
  570. }
  571. func TestDecode_EmbeddedPointerSquash_FromMapToStruct(t *testing.T) {
  572. t.Parallel()
  573. input := map[string]interface{}{
  574. "Vstring": "foo",
  575. "Vunique": "bar",
  576. }
  577. result := EmbeddedPointerSquash{
  578. Basic: &Basic{},
  579. }
  580. err := Decode(input, &result)
  581. if err != nil {
  582. t.Fatalf("got an err: %s", err.Error())
  583. }
  584. if result.Vstring != "foo" {
  585. t.Errorf("vstring value should be 'foo': %#v", result.Vstring)
  586. }
  587. if result.Vunique != "bar" {
  588. t.Errorf("vunique value should be 'bar': %#v", result.Vunique)
  589. }
  590. }
  591. func TestDecode_EmbeddedSquashConfig(t *testing.T) {
  592. t.Parallel()
  593. input := map[string]interface{}{
  594. "vstring": "foo",
  595. "vunique": "bar",
  596. "Named": map[string]interface{}{
  597. "vstring": "baz",
  598. },
  599. }
  600. var result EmbeddedAndNamed
  601. config := &DecoderConfig{
  602. Squash: true,
  603. Result: &result,
  604. }
  605. decoder, err := NewDecoder(config)
  606. if err != nil {
  607. t.Fatalf("err: %s", err)
  608. }
  609. err = decoder.Decode(input)
  610. if err != nil {
  611. t.Fatalf("got an err: %s", err)
  612. }
  613. if result.Vstring != "foo" {
  614. t.Errorf("vstring value should be 'foo': %#v", result.Vstring)
  615. }
  616. if result.Vunique != "bar" {
  617. t.Errorf("vunique value should be 'bar': %#v", result.Vunique)
  618. }
  619. if result.Named.Vstring != "baz" {
  620. t.Errorf("Named.vstring value should be 'baz': %#v", result.Named.Vstring)
  621. }
  622. }
  623. func TestDecodeFrom_EmbeddedSquashConfig(t *testing.T) {
  624. t.Parallel()
  625. input := EmbeddedAndNamed{
  626. Basic: Basic{Vstring: "foo"},
  627. Named: Basic{Vstring: "baz"},
  628. Vunique: "bar",
  629. }
  630. result := map[string]interface{}{}
  631. config := &DecoderConfig{
  632. Squash: true,
  633. Result: &result,
  634. }
  635. decoder, err := NewDecoder(config)
  636. if err != nil {
  637. t.Fatalf("got an err: %s", err.Error())
  638. }
  639. err = decoder.Decode(input)
  640. if err != nil {
  641. t.Fatalf("got an err: %s", err.Error())
  642. }
  643. if _, ok := result["Basic"]; ok {
  644. t.Error("basic should not be present in map")
  645. }
  646. v, ok := result["Vstring"]
  647. if !ok {
  648. t.Error("vstring should be present in map")
  649. } else if !reflect.DeepEqual(v, "foo") {
  650. t.Errorf("vstring value should be 'foo': %#v", v)
  651. }
  652. v, ok = result["Vunique"]
  653. if !ok {
  654. t.Error("vunique should be present in map")
  655. } else if !reflect.DeepEqual(v, "bar") {
  656. t.Errorf("vunique value should be 'bar': %#v", v)
  657. }
  658. v, ok = result["Named"]
  659. if !ok {
  660. t.Error("Named should be present in map")
  661. } else {
  662. named := v.(map[string]interface{})
  663. v, ok := named["Vstring"]
  664. if !ok {
  665. t.Error("Named: vstring should be present in map")
  666. } else if !reflect.DeepEqual(v, "baz") {
  667. t.Errorf("Named: vstring should be 'baz': %#v", v)
  668. }
  669. }
  670. }
  671. func TestDecode_SquashOnNonStructType(t *testing.T) {
  672. t.Parallel()
  673. input := map[string]interface{}{
  674. "InvalidSquashType": 42,
  675. }
  676. var result SquashOnNonStructType
  677. err := Decode(input, &result)
  678. if err == nil {
  679. t.Fatal("unexpected success decoding invalid squash field type")
  680. } else if !strings.Contains(err.Error(), "unsupported type for squash") {
  681. t.Fatalf("unexpected error message for invalid squash field type: %s", err)
  682. }
  683. }
  684. func TestDecode_DecodeHook(t *testing.T) {
  685. t.Parallel()
  686. input := map[string]interface{}{
  687. "vint": "WHAT",
  688. }
  689. decodeHook := func(from reflect.Kind, to reflect.Kind, v interface{}) (interface{}, error) {
  690. if from == reflect.String && to != reflect.String {
  691. return 5, nil
  692. }
  693. return v, nil
  694. }
  695. var result Basic
  696. config := &DecoderConfig{
  697. DecodeHook: decodeHook,
  698. Result: &result,
  699. }
  700. decoder, err := NewDecoder(config)
  701. if err != nil {
  702. t.Fatalf("err: %s", err)
  703. }
  704. err = decoder.Decode(input)
  705. if err != nil {
  706. t.Fatalf("got an err: %s", err)
  707. }
  708. if result.Vint != 5 {
  709. t.Errorf("vint should be 5: %#v", result.Vint)
  710. }
  711. }
  712. func TestDecode_DecodeHookType(t *testing.T) {
  713. t.Parallel()
  714. input := map[string]interface{}{
  715. "vint": "WHAT",
  716. }
  717. decodeHook := func(from reflect.Type, to reflect.Type, v interface{}) (interface{}, error) {
  718. if from.Kind() == reflect.String &&
  719. to.Kind() != reflect.String {
  720. return 5, nil
  721. }
  722. return v, nil
  723. }
  724. var result Basic
  725. config := &DecoderConfig{
  726. DecodeHook: decodeHook,
  727. Result: &result,
  728. }
  729. decoder, err := NewDecoder(config)
  730. if err != nil {
  731. t.Fatalf("err: %s", err)
  732. }
  733. err = decoder.Decode(input)
  734. if err != nil {
  735. t.Fatalf("got an err: %s", err)
  736. }
  737. if result.Vint != 5 {
  738. t.Errorf("vint should be 5: %#v", result.Vint)
  739. }
  740. }
  741. func TestDecode_Nil(t *testing.T) {
  742. t.Parallel()
  743. var input interface{}
  744. result := Basic{
  745. Vstring: "foo",
  746. }
  747. err := Decode(input, &result)
  748. if err != nil {
  749. t.Fatalf("err: %s", err)
  750. }
  751. if result.Vstring != "foo" {
  752. t.Fatalf("bad: %#v", result.Vstring)
  753. }
  754. }
  755. func TestDecode_NilInterfaceHook(t *testing.T) {
  756. t.Parallel()
  757. input := map[string]interface{}{
  758. "w": "",
  759. }
  760. decodeHook := func(f, t reflect.Type, v interface{}) (interface{}, error) {
  761. if t.String() == "io.Writer" {
  762. return nil, nil
  763. }
  764. return v, nil
  765. }
  766. var result NilInterface
  767. config := &DecoderConfig{
  768. DecodeHook: decodeHook,
  769. Result: &result,
  770. }
  771. decoder, err := NewDecoder(config)
  772. if err != nil {
  773. t.Fatalf("err: %s", err)
  774. }
  775. err = decoder.Decode(input)
  776. if err != nil {
  777. t.Fatalf("got an err: %s", err)
  778. }
  779. if result.W != nil {
  780. t.Errorf("W should be nil: %#v", result.W)
  781. }
  782. }
  783. func TestDecode_NilPointerHook(t *testing.T) {
  784. t.Parallel()
  785. input := map[string]interface{}{
  786. "value": "",
  787. }
  788. decodeHook := func(f, t reflect.Type, v interface{}) (interface{}, error) {
  789. if typed, ok := v.(string); ok {
  790. if typed == "" {
  791. return nil, nil
  792. }
  793. }
  794. return v, nil
  795. }
  796. var result NilPointer
  797. config := &DecoderConfig{
  798. DecodeHook: decodeHook,
  799. Result: &result,
  800. }
  801. decoder, err := NewDecoder(config)
  802. if err != nil {
  803. t.Fatalf("err: %s", err)
  804. }
  805. err = decoder.Decode(input)
  806. if err != nil {
  807. t.Fatalf("got an err: %s", err)
  808. }
  809. if result.Value != nil {
  810. t.Errorf("W should be nil: %#v", result.Value)
  811. }
  812. }
  813. func TestDecode_FuncHook(t *testing.T) {
  814. t.Parallel()
  815. input := map[string]interface{}{
  816. "foo": "baz",
  817. }
  818. decodeHook := func(f, t reflect.Type, v interface{}) (interface{}, error) {
  819. if t.Kind() != reflect.Func {
  820. return v, nil
  821. }
  822. val := v.(string)
  823. return func() string { return val }, nil
  824. }
  825. var result Func
  826. config := &DecoderConfig{
  827. DecodeHook: decodeHook,
  828. Result: &result,
  829. }
  830. decoder, err := NewDecoder(config)
  831. if err != nil {
  832. t.Fatalf("err: %s", err)
  833. }
  834. err = decoder.Decode(input)
  835. if err != nil {
  836. t.Fatalf("got an err: %s", err)
  837. }
  838. if result.Foo() != "baz" {
  839. t.Errorf("Foo call result should be 'baz': %s", result.Foo())
  840. }
  841. }
  842. func TestDecode_NonStruct(t *testing.T) {
  843. t.Parallel()
  844. input := map[string]interface{}{
  845. "foo": "bar",
  846. "bar": "baz",
  847. }
  848. var result map[string]string
  849. err := Decode(input, &result)
  850. if err != nil {
  851. t.Fatalf("err: %s", err)
  852. }
  853. if result["foo"] != "bar" {
  854. t.Fatal("foo is not bar")
  855. }
  856. }
  857. func TestDecode_StructMatch(t *testing.T) {
  858. t.Parallel()
  859. input := map[string]interface{}{
  860. "vbar": Basic{
  861. Vstring: "foo",
  862. },
  863. }
  864. var result Nested
  865. err := Decode(input, &result)
  866. if err != nil {
  867. t.Fatalf("got an err: %s", err.Error())
  868. }
  869. if result.Vbar.Vstring != "foo" {
  870. t.Errorf("bad: %#v", result)
  871. }
  872. }
  873. func TestDecode_TypeConversion(t *testing.T) {
  874. input := map[string]interface{}{
  875. "IntToFloat": 42,
  876. "IntToUint": 42,
  877. "IntToBool": 1,
  878. "IntToString": 42,
  879. "UintToInt": 42,
  880. "UintToFloat": 42,
  881. "UintToBool": 42,
  882. "UintToString": 42,
  883. "BoolToInt": true,
  884. "BoolToUint": true,
  885. "BoolToFloat": true,
  886. "BoolToString": true,
  887. "FloatToInt": 42.42,
  888. "FloatToUint": 42.42,
  889. "FloatToBool": 42.42,
  890. "FloatToString": 42.42,
  891. "SliceUint8ToString": []uint8("foo"),
  892. "StringToSliceUint8": "foo",
  893. "ArrayUint8ToString": [3]uint8{'f', 'o', 'o'},
  894. "StringToInt": "42",
  895. "StringToUint": "42",
  896. "StringToBool": "1",
  897. "StringToFloat": "42.42",
  898. "StringToStrSlice": "A",
  899. "StringToIntSlice": "42",
  900. "StringToStrArray": "A",
  901. "StringToIntArray": "42",
  902. "SliceToMap": []interface{}{},
  903. "MapToSlice": map[string]interface{}{},
  904. "ArrayToMap": []interface{}{},
  905. "MapToArray": map[string]interface{}{},
  906. }
  907. expectedResultStrict := TypeConversionResult{
  908. IntToFloat: 42.0,
  909. IntToUint: 42,
  910. UintToInt: 42,
  911. UintToFloat: 42,
  912. BoolToInt: 0,
  913. BoolToUint: 0,
  914. BoolToFloat: 0,
  915. FloatToInt: 42,
  916. FloatToUint: 42,
  917. }
  918. expectedResultWeak := TypeConversionResult{
  919. IntToFloat: 42.0,
  920. IntToUint: 42,
  921. IntToBool: true,
  922. IntToString: "42",
  923. UintToInt: 42,
  924. UintToFloat: 42,
  925. UintToBool: true,
  926. UintToString: "42",
  927. BoolToInt: 1,
  928. BoolToUint: 1,
  929. BoolToFloat: 1,
  930. BoolToString: "1",
  931. FloatToInt: 42,
  932. FloatToUint: 42,
  933. FloatToBool: true,
  934. FloatToString: "42.42",
  935. SliceUint8ToString: "foo",
  936. StringToSliceUint8: []byte("foo"),
  937. ArrayUint8ToString: "foo",
  938. StringToInt: 42,
  939. StringToUint: 42,
  940. StringToBool: true,
  941. StringToFloat: 42.42,
  942. StringToStrSlice: []string{"A"},
  943. StringToIntSlice: []int{42},
  944. StringToStrArray: [1]string{"A"},
  945. StringToIntArray: [1]int{42},
  946. SliceToMap: map[string]interface{}{},
  947. MapToSlice: []interface{}{},
  948. ArrayToMap: map[string]interface{}{},
  949. MapToArray: [1]interface{}{},
  950. }
  951. // Test strict type conversion
  952. var resultStrict TypeConversionResult
  953. err := Decode(input, &resultStrict)
  954. if err == nil {
  955. t.Errorf("should return an error")
  956. }
  957. if !reflect.DeepEqual(resultStrict, expectedResultStrict) {
  958. t.Errorf("expected %v, got: %v", expectedResultStrict, resultStrict)
  959. }
  960. // Test weak type conversion
  961. var decoder *Decoder
  962. var resultWeak TypeConversionResult
  963. config := &DecoderConfig{
  964. WeaklyTypedInput: true,
  965. Result: &resultWeak,
  966. }
  967. decoder, err = NewDecoder(config)
  968. if err != nil {
  969. t.Fatalf("err: %s", err)
  970. }
  971. err = decoder.Decode(input)
  972. if err != nil {
  973. t.Fatalf("got an err: %s", err)
  974. }
  975. if !reflect.DeepEqual(resultWeak, expectedResultWeak) {
  976. t.Errorf("expected \n%#v, got: \n%#v", expectedResultWeak, resultWeak)
  977. }
  978. }
  979. func TestDecoder_ErrorUnused(t *testing.T) {
  980. t.Parallel()
  981. input := map[string]interface{}{
  982. "vstring": "hello",
  983. "foo": "bar",
  984. }
  985. var result Basic
  986. config := &DecoderConfig{
  987. ErrorUnused: true,
  988. Result: &result,
  989. }
  990. decoder, err := NewDecoder(config)
  991. if err != nil {
  992. t.Fatalf("err: %s", err)
  993. }
  994. err = decoder.Decode(input)
  995. if err == nil {
  996. t.Fatal("expected error")
  997. }
  998. }
  999. func TestDecoder_ErrorUnused_NotSetable(t *testing.T) {
  1000. t.Parallel()
  1001. // lowercase vsilent is unexported and cannot be set
  1002. input := map[string]interface{}{
  1003. "vsilent": "false",
  1004. }
  1005. var result Basic
  1006. config := &DecoderConfig{
  1007. ErrorUnused: true,
  1008. Result: &result,
  1009. }
  1010. decoder, err := NewDecoder(config)
  1011. if err != nil {
  1012. t.Fatalf("err: %s", err)
  1013. }
  1014. err = decoder.Decode(input)
  1015. if err == nil {
  1016. t.Fatal("expected error")
  1017. }
  1018. }
  1019. func TestMap(t *testing.T) {
  1020. t.Parallel()
  1021. input := map[string]interface{}{
  1022. "vfoo": "foo",
  1023. "vother": map[interface{}]interface{}{
  1024. "foo": "foo",
  1025. "bar": "bar",
  1026. },
  1027. }
  1028. var result Map
  1029. err := Decode(input, &result)
  1030. if err != nil {
  1031. t.Fatalf("got an error: %s", err)
  1032. }
  1033. if result.Vfoo != "foo" {
  1034. t.Errorf("vfoo value should be 'foo': %#v", result.Vfoo)
  1035. }
  1036. if result.Vother == nil {
  1037. t.Fatal("vother should not be nil")
  1038. }
  1039. if len(result.Vother) != 2 {
  1040. t.Error("vother should have two items")
  1041. }
  1042. if result.Vother["foo"] != "foo" {
  1043. t.Errorf("'foo' key should be foo, got: %#v", result.Vother["foo"])
  1044. }
  1045. if result.Vother["bar"] != "bar" {
  1046. t.Errorf("'bar' key should be bar, got: %#v", result.Vother["bar"])
  1047. }
  1048. }
  1049. func TestMapMerge(t *testing.T) {
  1050. t.Parallel()
  1051. input := map[string]interface{}{
  1052. "vfoo": "foo",
  1053. "vother": map[interface{}]interface{}{
  1054. "foo": "foo",
  1055. "bar": "bar",
  1056. },
  1057. }
  1058. var result Map
  1059. result.Vother = map[string]string{"hello": "world"}
  1060. err := Decode(input, &result)
  1061. if err != nil {
  1062. t.Fatalf("got an error: %s", err)
  1063. }
  1064. if result.Vfoo != "foo" {
  1065. t.Errorf("vfoo value should be 'foo': %#v", result.Vfoo)
  1066. }
  1067. expected := map[string]string{
  1068. "foo": "foo",
  1069. "bar": "bar",
  1070. "hello": "world",
  1071. }
  1072. if !reflect.DeepEqual(result.Vother, expected) {
  1073. t.Errorf("bad: %#v", result.Vother)
  1074. }
  1075. }
  1076. func TestMapOfStruct(t *testing.T) {
  1077. t.Parallel()
  1078. input := map[string]interface{}{
  1079. "value": map[string]interface{}{
  1080. "foo": map[string]string{"vstring": "one"},
  1081. "bar": map[string]string{"vstring": "two"},
  1082. },
  1083. }
  1084. var result MapOfStruct
  1085. err := Decode(input, &result)
  1086. if err != nil {
  1087. t.Fatalf("got an err: %s", err)
  1088. }
  1089. if result.Value == nil {
  1090. t.Fatal("value should not be nil")
  1091. }
  1092. if len(result.Value) != 2 {
  1093. t.Error("value should have two items")
  1094. }
  1095. if result.Value["foo"].Vstring != "one" {
  1096. t.Errorf("foo value should be 'one', got: %s", result.Value["foo"].Vstring)
  1097. }
  1098. if result.Value["bar"].Vstring != "two" {
  1099. t.Errorf("bar value should be 'two', got: %s", result.Value["bar"].Vstring)
  1100. }
  1101. }
  1102. func TestNestedType(t *testing.T) {
  1103. t.Parallel()
  1104. input := map[string]interface{}{
  1105. "vfoo": "foo",
  1106. "vbar": map[string]interface{}{
  1107. "vstring": "foo",
  1108. "vint": 42,
  1109. "vbool": true,
  1110. },
  1111. }
  1112. var result Nested
  1113. err := Decode(input, &result)
  1114. if err != nil {
  1115. t.Fatalf("got an err: %s", err.Error())
  1116. }
  1117. if result.Vfoo != "foo" {
  1118. t.Errorf("vfoo value should be 'foo': %#v", result.Vfoo)
  1119. }
  1120. if result.Vbar.Vstring != "foo" {
  1121. t.Errorf("vstring value should be 'foo': %#v", result.Vbar.Vstring)
  1122. }
  1123. if result.Vbar.Vint != 42 {
  1124. t.Errorf("vint value should be 42: %#v", result.Vbar.Vint)
  1125. }
  1126. if result.Vbar.Vbool != true {
  1127. t.Errorf("vbool value should be true: %#v", result.Vbar.Vbool)
  1128. }
  1129. if result.Vbar.Vextra != "" {
  1130. t.Errorf("vextra value should be empty: %#v", result.Vbar.Vextra)
  1131. }
  1132. }
  1133. func TestNestedTypePointer(t *testing.T) {
  1134. t.Parallel()
  1135. input := map[string]interface{}{
  1136. "vfoo": "foo",
  1137. "vbar": &map[string]interface{}{
  1138. "vstring": "foo",
  1139. "vint": 42,
  1140. "vbool": true,
  1141. },
  1142. }
  1143. var result NestedPointer
  1144. err := Decode(input, &result)
  1145. if err != nil {
  1146. t.Fatalf("got an err: %s", err.Error())
  1147. }
  1148. if result.Vfoo != "foo" {
  1149. t.Errorf("vfoo value should be 'foo': %#v", result.Vfoo)
  1150. }
  1151. if result.Vbar.Vstring != "foo" {
  1152. t.Errorf("vstring value should be 'foo': %#v", result.Vbar.Vstring)
  1153. }
  1154. if result.Vbar.Vint != 42 {
  1155. t.Errorf("vint value should be 42: %#v", result.Vbar.Vint)
  1156. }
  1157. if result.Vbar.Vbool != true {
  1158. t.Errorf("vbool value should be true: %#v", result.Vbar.Vbool)
  1159. }
  1160. if result.Vbar.Vextra != "" {
  1161. t.Errorf("vextra value should be empty: %#v", result.Vbar.Vextra)
  1162. }
  1163. }
  1164. // Test for issue #46.
  1165. func TestNestedTypeInterface(t *testing.T) {
  1166. t.Parallel()
  1167. input := map[string]interface{}{
  1168. "vfoo": "foo",
  1169. "vbar": &map[string]interface{}{
  1170. "vstring": "foo",
  1171. "vint": 42,
  1172. "vbool": true,
  1173. "vdata": map[string]interface{}{
  1174. "vstring": "bar",
  1175. },
  1176. },
  1177. }
  1178. var result NestedPointer
  1179. result.Vbar = new(Basic)
  1180. result.Vbar.Vdata = new(Basic)
  1181. err := Decode(input, &result)
  1182. if err != nil {
  1183. t.Fatalf("got an err: %s", err.Error())
  1184. }
  1185. if result.Vfoo != "foo" {
  1186. t.Errorf("vfoo value should be 'foo': %#v", result.Vfoo)
  1187. }
  1188. if result.Vbar.Vstring != "foo" {
  1189. t.Errorf("vstring value should be 'foo': %#v", result.Vbar.Vstring)
  1190. }
  1191. if result.Vbar.Vint != 42 {
  1192. t.Errorf("vint value should be 42: %#v", result.Vbar.Vint)
  1193. }
  1194. if result.Vbar.Vbool != true {
  1195. t.Errorf("vbool value should be true: %#v", result.Vbar.Vbool)
  1196. }
  1197. if result.Vbar.Vextra != "" {
  1198. t.Errorf("vextra value should be empty: %#v", result.Vbar.Vextra)
  1199. }
  1200. if result.Vbar.Vdata.(*Basic).Vstring != "bar" {
  1201. t.Errorf("vstring value should be 'bar': %#v", result.Vbar.Vdata.(*Basic).Vstring)
  1202. }
  1203. }
  1204. func TestSlice(t *testing.T) {
  1205. t.Parallel()
  1206. inputStringSlice := map[string]interface{}{
  1207. "vfoo": "foo",
  1208. "vbar": []string{"foo", "bar", "baz"},
  1209. }
  1210. inputStringSlicePointer := map[string]interface{}{
  1211. "vfoo": "foo",
  1212. "vbar": &[]string{"foo", "bar", "baz"},
  1213. }
  1214. outputStringSlice := &Slice{
  1215. "foo",
  1216. []string{"foo", "bar", "baz"},
  1217. }
  1218. testSliceInput(t, inputStringSlice, outputStringSlice)
  1219. testSliceInput(t, inputStringSlicePointer, outputStringSlice)
  1220. }
  1221. func TestInvalidSlice(t *testing.T) {
  1222. t.Parallel()
  1223. input := map[string]interface{}{
  1224. "vfoo": "foo",
  1225. "vbar": 42,
  1226. }
  1227. result := Slice{}
  1228. err := Decode(input, &result)
  1229. if err == nil {
  1230. t.Errorf("expected failure")
  1231. }
  1232. }
  1233. func TestSliceOfStruct(t *testing.T) {
  1234. t.Parallel()
  1235. input := map[string]interface{}{
  1236. "value": []map[string]interface{}{
  1237. {"vstring": "one"},
  1238. {"vstring": "two"},
  1239. },
  1240. }
  1241. var result SliceOfStruct
  1242. err := Decode(input, &result)
  1243. if err != nil {
  1244. t.Fatalf("got unexpected error: %s", err)
  1245. }
  1246. if len(result.Value) != 2 {
  1247. t.Fatalf("expected two values, got %d", len(result.Value))
  1248. }
  1249. if result.Value[0].Vstring != "one" {
  1250. t.Errorf("first value should be 'one', got: %s", result.Value[0].Vstring)
  1251. }
  1252. if result.Value[1].Vstring != "two" {
  1253. t.Errorf("second value should be 'two', got: %s", result.Value[1].Vstring)
  1254. }
  1255. }
  1256. func TestSliceCornerCases(t *testing.T) {
  1257. t.Parallel()
  1258. // Input with a map with zero values
  1259. input := map[string]interface{}{}
  1260. var resultWeak []Basic
  1261. err := WeakDecode(input, &resultWeak)
  1262. if err != nil {
  1263. t.Fatalf("got unexpected error: %s", err)
  1264. }
  1265. if len(resultWeak) != 0 {
  1266. t.Errorf("length should be 0")
  1267. }
  1268. // Input with more values
  1269. input = map[string]interface{}{
  1270. "Vstring": "foo",
  1271. }
  1272. resultWeak = nil
  1273. err = WeakDecode(input, &resultWeak)
  1274. if err != nil {
  1275. t.Fatalf("got unexpected error: %s", err)
  1276. }
  1277. if resultWeak[0].Vstring != "foo" {
  1278. t.Errorf("value does not match")
  1279. }
  1280. }
  1281. func TestSliceToMap(t *testing.T) {
  1282. t.Parallel()
  1283. input := []map[string]interface{}{
  1284. {
  1285. "foo": "bar",
  1286. },
  1287. {
  1288. "bar": "baz",
  1289. },
  1290. }
  1291. var result map[string]interface{}
  1292. err := WeakDecode(input, &result)
  1293. if err != nil {
  1294. t.Fatalf("got an error: %s", err)
  1295. }
  1296. expected := map[string]interface{}{
  1297. "foo": "bar",
  1298. "bar": "baz",
  1299. }
  1300. if !reflect.DeepEqual(result, expected) {
  1301. t.Errorf("bad: %#v", result)
  1302. }
  1303. }
  1304. func TestArray(t *testing.T) {
  1305. t.Parallel()
  1306. inputStringArray := map[string]interface{}{
  1307. "vfoo": "foo",
  1308. "vbar": [2]string{"foo", "bar"},
  1309. }
  1310. inputStringArrayPointer := map[string]interface{}{
  1311. "vfoo": "foo",
  1312. "vbar": &[2]string{"foo", "bar"},
  1313. }
  1314. outputStringArray := &Array{
  1315. "foo",
  1316. [2]string{"foo", "bar"},
  1317. }
  1318. testArrayInput(t, inputStringArray, outputStringArray)
  1319. testArrayInput(t, inputStringArrayPointer, outputStringArray)
  1320. }
  1321. func TestInvalidArray(t *testing.T) {
  1322. t.Parallel()
  1323. input := map[string]interface{}{
  1324. "vfoo": "foo",
  1325. "vbar": 42,
  1326. }
  1327. result := Array{}
  1328. err := Decode(input, &result)
  1329. if err == nil {
  1330. t.Errorf("expected failure")
  1331. }
  1332. }
  1333. func TestArrayOfStruct(t *testing.T) {
  1334. t.Parallel()
  1335. input := map[string]interface{}{
  1336. "value": []map[string]interface{}{
  1337. {"vstring": "one"},
  1338. {"vstring": "two"},
  1339. },
  1340. }
  1341. var result ArrayOfStruct
  1342. err := Decode(input, &result)
  1343. if err != nil {
  1344. t.Fatalf("got unexpected error: %s", err)
  1345. }
  1346. if len(result.Value) != 2 {
  1347. t.Fatalf("expected two values, got %d", len(result.Value))
  1348. }
  1349. if result.Value[0].Vstring != "one" {
  1350. t.Errorf("first value should be 'one', got: %s", result.Value[0].Vstring)
  1351. }
  1352. if result.Value[1].Vstring != "two" {
  1353. t.Errorf("second value should be 'two', got: %s", result.Value[1].Vstring)
  1354. }
  1355. }
  1356. func TestArrayToMap(t *testing.T) {
  1357. t.Parallel()
  1358. input := []map[string]interface{}{
  1359. {
  1360. "foo": "bar",
  1361. },
  1362. {
  1363. "bar": "baz",
  1364. },
  1365. }
  1366. var result map[string]interface{}
  1367. err := WeakDecode(input, &result)
  1368. if err != nil {
  1369. t.Fatalf("got an error: %s", err)
  1370. }
  1371. expected := map[string]interface{}{
  1372. "foo": "bar",
  1373. "bar": "baz",
  1374. }
  1375. if !reflect.DeepEqual(result, expected) {
  1376. t.Errorf("bad: %#v", result)
  1377. }
  1378. }
  1379. func TestDecodeTable(t *testing.T) {
  1380. t.Parallel()
  1381. // We need to make new types so that we don't get the short-circuit
  1382. // copy functionality. We want to test the deep copying functionality.
  1383. type BasicCopy Basic
  1384. type NestedPointerCopy NestedPointer
  1385. type MapCopy Map
  1386. tests := []struct {
  1387. name string
  1388. in interface{}
  1389. target interface{}
  1390. out interface{}
  1391. wantErr bool
  1392. }{
  1393. {
  1394. "basic struct input",
  1395. &Basic{
  1396. Vstring: "vstring",
  1397. Vint: 2,
  1398. Vint8: 2,
  1399. Vint16: 2,
  1400. Vint32: 2,
  1401. Vint64: 2,
  1402. Vuint: 3,
  1403. Vbool: true,
  1404. Vfloat: 4.56,
  1405. Vextra: "vextra",
  1406. vsilent: true,
  1407. Vdata: []byte("data"),
  1408. },
  1409. &map[string]interface{}{},
  1410. &map[string]interface{}{
  1411. "Vstring": "vstring",
  1412. "Vint": 2,
  1413. "Vint8": int8(2),
  1414. "Vint16": int16(2),
  1415. "Vint32": int32(2),
  1416. "Vint64": int64(2),
  1417. "Vuint": uint(3),
  1418. "Vbool": true,
  1419. "Vfloat": 4.56,
  1420. "Vextra": "vextra",
  1421. "Vdata": []byte("data"),
  1422. "VjsonInt": 0,
  1423. "VjsonUint": uint(0),
  1424. "VjsonUint64": uint64(0),
  1425. "VjsonFloat": 0.0,
  1426. "VjsonNumber": json.Number(""),
  1427. },
  1428. false,
  1429. },
  1430. {
  1431. "embedded struct input",
  1432. &Embedded{
  1433. Vunique: "vunique",
  1434. Basic: Basic{
  1435. Vstring: "vstring",
  1436. Vint: 2,
  1437. Vint8: 2,
  1438. Vint16: 2,
  1439. Vint32: 2,
  1440. Vint64: 2,
  1441. Vuint: 3,
  1442. Vbool: true,
  1443. Vfloat: 4.56,
  1444. Vextra: "vextra",
  1445. vsilent: true,
  1446. Vdata: []byte("data"),
  1447. },
  1448. },
  1449. &map[string]interface{}{},
  1450. &map[string]interface{}{
  1451. "Vunique": "vunique",
  1452. "Basic": map[string]interface{}{
  1453. "Vstring": "vstring",
  1454. "Vint": 2,
  1455. "Vint8": int8(2),
  1456. "Vint16": int16(2),
  1457. "Vint32": int32(2),
  1458. "Vint64": int64(2),
  1459. "Vuint": uint(3),
  1460. "Vbool": true,
  1461. "Vfloat": 4.56,
  1462. "Vextra": "vextra",
  1463. "Vdata": []byte("data"),
  1464. "VjsonInt": 0,
  1465. "VjsonUint": uint(0),
  1466. "VjsonUint64": uint64(0),
  1467. "VjsonFloat": 0.0,
  1468. "VjsonNumber": json.Number(""),
  1469. },
  1470. },
  1471. false,
  1472. },
  1473. {
  1474. "struct => struct",
  1475. &Basic{
  1476. Vstring: "vstring",
  1477. Vint: 2,
  1478. Vuint: 3,
  1479. Vbool: true,
  1480. Vfloat: 4.56,
  1481. Vextra: "vextra",
  1482. Vdata: []byte("data"),
  1483. vsilent: true,
  1484. },
  1485. &BasicCopy{},
  1486. &BasicCopy{
  1487. Vstring: "vstring",
  1488. Vint: 2,
  1489. Vuint: 3,
  1490. Vbool: true,
  1491. Vfloat: 4.56,
  1492. Vextra: "vextra",
  1493. Vdata: []byte("data"),
  1494. },
  1495. false,
  1496. },
  1497. {
  1498. "struct => struct with pointers",
  1499. &NestedPointer{
  1500. Vfoo: "hello",
  1501. Vbar: nil,
  1502. },
  1503. &NestedPointerCopy{},
  1504. &NestedPointerCopy{
  1505. Vfoo: "hello",
  1506. },
  1507. false,
  1508. },
  1509. {
  1510. "basic pointer to non-pointer",
  1511. &BasicPointer{
  1512. Vstring: stringPtr("vstring"),
  1513. Vint: intPtr(2),
  1514. Vuint: uintPtr(3),
  1515. Vbool: boolPtr(true),
  1516. Vfloat: floatPtr(4.56),
  1517. Vdata: interfacePtr([]byte("data")),
  1518. },
  1519. &Basic{},
  1520. &Basic{
  1521. Vstring: "vstring",
  1522. Vint: 2,
  1523. Vuint: 3,
  1524. Vbool: true,
  1525. Vfloat: 4.56,
  1526. Vdata: []byte("data"),
  1527. },
  1528. false,
  1529. },
  1530. {
  1531. "slice non-pointer to pointer",
  1532. &Slice{},
  1533. &SlicePointer{},
  1534. &SlicePointer{},
  1535. false,
  1536. },
  1537. {
  1538. "slice non-pointer to pointer, zero field",
  1539. &Slice{},
  1540. &SlicePointer{
  1541. Vbar: &[]string{"yo"},
  1542. },
  1543. &SlicePointer{},
  1544. false,
  1545. },
  1546. {
  1547. "slice to slice alias",
  1548. &Slice{},
  1549. &SliceOfAlias{},
  1550. &SliceOfAlias{},
  1551. false,
  1552. },
  1553. {
  1554. "nil map to map",
  1555. &Map{},
  1556. &MapCopy{},
  1557. &MapCopy{},
  1558. false,
  1559. },
  1560. {
  1561. "nil map to non-empty map",
  1562. &Map{},
  1563. &MapCopy{Vother: map[string]string{"foo": "bar"}},
  1564. &MapCopy{},
  1565. false,
  1566. },
  1567. {
  1568. "slice input - should error",
  1569. []string{"foo", "bar"},
  1570. &map[string]interface{}{},
  1571. &map[string]interface{}{},
  1572. true,
  1573. },
  1574. {
  1575. "struct with slice property",
  1576. &Slice{
  1577. Vfoo: "vfoo",
  1578. Vbar: []string{"foo", "bar"},
  1579. },
  1580. &map[string]interface{}{},
  1581. &map[string]interface{}{
  1582. "Vfoo": "vfoo",
  1583. "Vbar": []string{"foo", "bar"},
  1584. },
  1585. false,
  1586. },
  1587. {
  1588. "struct with empty slice",
  1589. &map[string]interface{}{
  1590. "Vbar": []string{},
  1591. },
  1592. &Slice{},
  1593. &Slice{
  1594. Vbar: []string{},
  1595. },
  1596. false,
  1597. },
  1598. {
  1599. "struct with slice of struct property",
  1600. &SliceOfStruct{
  1601. Value: []Basic{
  1602. Basic{
  1603. Vstring: "vstring",
  1604. Vint: 2,
  1605. Vuint: 3,
  1606. Vbool: true,
  1607. Vfloat: 4.56,
  1608. Vextra: "vextra",
  1609. vsilent: true,
  1610. Vdata: []byte("data"),
  1611. },
  1612. },
  1613. },
  1614. &map[string]interface{}{},
  1615. &map[string]interface{}{
  1616. "Value": []Basic{
  1617. Basic{
  1618. Vstring: "vstring",
  1619. Vint: 2,
  1620. Vuint: 3,
  1621. Vbool: true,
  1622. Vfloat: 4.56,
  1623. Vextra: "vextra",
  1624. vsilent: true,
  1625. Vdata: []byte("data"),
  1626. },
  1627. },
  1628. },
  1629. false,
  1630. },
  1631. {
  1632. "struct with map property",
  1633. &Map{
  1634. Vfoo: "vfoo",
  1635. Vother: map[string]string{"vother": "vother"},
  1636. },
  1637. &map[string]interface{}{},
  1638. &map[string]interface{}{
  1639. "Vfoo": "vfoo",
  1640. "Vother": map[string]string{
  1641. "vother": "vother",
  1642. }},
  1643. false,
  1644. },
  1645. {
  1646. "tagged struct",
  1647. &Tagged{
  1648. Extra: "extra",
  1649. Value: "value",
  1650. },
  1651. &map[string]string{},
  1652. &map[string]string{
  1653. "bar": "extra",
  1654. "foo": "value",
  1655. },
  1656. false,
  1657. },
  1658. {
  1659. "omit tag struct",
  1660. &struct {
  1661. Value string `mapstructure:"value"`
  1662. Omit string `mapstructure:"-"`
  1663. }{
  1664. Value: "value",
  1665. Omit: "omit",
  1666. },
  1667. &map[string]string{},
  1668. &map[string]string{
  1669. "value": "value",
  1670. },
  1671. false,
  1672. },
  1673. {
  1674. "decode to wrong map type",
  1675. &struct {
  1676. Value string
  1677. }{
  1678. Value: "string",
  1679. },
  1680. &map[string]int{},
  1681. &map[string]int{},
  1682. true,
  1683. },
  1684. {
  1685. "remainder",
  1686. map[string]interface{}{
  1687. "A": "hello",
  1688. "B": "goodbye",
  1689. "C": "yo",
  1690. },
  1691. &Remainder{},
  1692. &Remainder{
  1693. A: "hello",
  1694. Extra: map[string]interface{}{
  1695. "B": "goodbye",
  1696. "C": "yo",
  1697. },
  1698. },
  1699. false,
  1700. },
  1701. {
  1702. "remainder with no extra",
  1703. map[string]interface{}{
  1704. "A": "hello",
  1705. },
  1706. &Remainder{},
  1707. &Remainder{
  1708. A: "hello",
  1709. Extra: nil,
  1710. },
  1711. false,
  1712. },
  1713. {
  1714. "struct with omitempty tag return non-empty values",
  1715. &struct {
  1716. VisibleField interface{} `mapstructure:"visible"`
  1717. OmitField interface{} `mapstructure:"omittable,omitempty"`
  1718. }{
  1719. VisibleField: nil,
  1720. OmitField: "string",
  1721. },
  1722. &map[string]interface{}{},
  1723. &map[string]interface{}{"visible": nil, "omittable": "string"},
  1724. false,
  1725. },
  1726. {
  1727. "struct with omitempty tag ignore empty values",
  1728. &struct {
  1729. VisibleField interface{} `mapstructure:"visible"`
  1730. OmitField interface{} `mapstructure:"omittable,omitempty"`
  1731. }{
  1732. VisibleField: nil,
  1733. OmitField: nil,
  1734. },
  1735. &map[string]interface{}{},
  1736. &map[string]interface{}{"visible": nil},
  1737. false,
  1738. },
  1739. }
  1740. for _, tt := range tests {
  1741. t.Run(tt.name, func(t *testing.T) {
  1742. if err := Decode(tt.in, tt.target); (err != nil) != tt.wantErr {
  1743. t.Fatalf("%q: TestMapOutputForStructuredInputs() unexpected error: %s", tt.name, err)
  1744. }
  1745. if !reflect.DeepEqual(tt.out, tt.target) {
  1746. t.Fatalf("%q: TestMapOutputForStructuredInputs() expected: %#v, got: %#v", tt.name, tt.out, tt.target)
  1747. }
  1748. })
  1749. }
  1750. }
  1751. func TestInvalidType(t *testing.T) {
  1752. t.Parallel()
  1753. input := map[string]interface{}{
  1754. "vstring": 42,
  1755. }
  1756. var result Basic
  1757. err := Decode(input, &result)
  1758. if err == nil {
  1759. t.Fatal("error should exist")
  1760. }
  1761. derr, ok := err.(*Error)
  1762. if !ok {
  1763. t.Fatalf("error should be kind of Error, instead: %#v", err)
  1764. }
  1765. if derr.Errors[0] !=
  1766. "'Vstring' expected type 'string', got unconvertible type 'int', value: '42'" {
  1767. t.Errorf("got unexpected error: %s", err)
  1768. }
  1769. inputNegIntUint := map[string]interface{}{
  1770. "vuint": -42,
  1771. }
  1772. err = Decode(inputNegIntUint, &result)
  1773. if err == nil {
  1774. t.Fatal("error should exist")
  1775. }
  1776. derr, ok = err.(*Error)
  1777. if !ok {
  1778. t.Fatalf("error should be kind of Error, instead: %#v", err)
  1779. }
  1780. if derr.Errors[0] != "cannot parse 'Vuint', -42 overflows uint" {
  1781. t.Errorf("got unexpected error: %s", err)
  1782. }
  1783. inputNegFloatUint := map[string]interface{}{
  1784. "vuint": -42.0,
  1785. }
  1786. err = Decode(inputNegFloatUint, &result)
  1787. if err == nil {
  1788. t.Fatal("error should exist")
  1789. }
  1790. derr, ok = err.(*Error)
  1791. if !ok {
  1792. t.Fatalf("error should be kind of Error, instead: %#v", err)
  1793. }
  1794. if derr.Errors[0] != "cannot parse 'Vuint', -42.000000 overflows uint" {
  1795. t.Errorf("got unexpected error: %s", err)
  1796. }
  1797. }
  1798. func TestDecodeMetadata(t *testing.T) {
  1799. t.Parallel()
  1800. input := map[string]interface{}{
  1801. "vfoo": "foo",
  1802. "vbar": map[string]interface{}{
  1803. "vstring": "foo",
  1804. "Vuint": 42,
  1805. "vsilent": "false",
  1806. "foo": "bar",
  1807. },
  1808. "bar": "nil",
  1809. }
  1810. var md Metadata
  1811. var result Nested
  1812. err := DecodeMetadata(input, &result, &md)
  1813. if err != nil {
  1814. t.Fatalf("err: %s", err.Error())
  1815. }
  1816. expectedKeys := []string{"Vbar", "Vbar.Vstring", "Vbar.Vuint", "Vfoo"}
  1817. sort.Strings(md.Keys)
  1818. if !reflect.DeepEqual(md.Keys, expectedKeys) {
  1819. t.Fatalf("bad keys: %#v", md.Keys)
  1820. }
  1821. expectedUnused := []string{"Vbar.foo", "Vbar.vsilent", "bar"}
  1822. sort.Strings(md.Unused)
  1823. if !reflect.DeepEqual(md.Unused, expectedUnused) {
  1824. t.Fatalf("bad unused: %#v", md.Unused)
  1825. }
  1826. }
  1827. func TestMetadata(t *testing.T) {
  1828. t.Parallel()
  1829. type testResult struct {
  1830. Vfoo string
  1831. Vbar BasicPointer
  1832. }
  1833. input := map[string]interface{}{
  1834. "vfoo": "foo",
  1835. "vbar": map[string]interface{}{
  1836. "vstring": "foo",
  1837. "Vuint": 42,
  1838. "vsilent": "false",
  1839. "foo": "bar",
  1840. },
  1841. "bar": "nil",
  1842. }
  1843. var md Metadata
  1844. var result testResult
  1845. config := &DecoderConfig{
  1846. Metadata: &md,
  1847. Result: &result,
  1848. }
  1849. decoder, err := NewDecoder(config)
  1850. if err != nil {
  1851. t.Fatalf("err: %s", err)
  1852. }
  1853. err = decoder.Decode(input)
  1854. if err != nil {
  1855. t.Fatalf("err: %s", err.Error())
  1856. }
  1857. expectedKeys := []string{"Vbar", "Vbar.Vstring", "Vbar.Vuint", "Vfoo"}
  1858. sort.Strings(md.Keys)
  1859. if !reflect.DeepEqual(md.Keys, expectedKeys) {
  1860. t.Fatalf("bad keys: %#v", md.Keys)
  1861. }
  1862. expectedUnused := []string{"Vbar.foo", "Vbar.vsilent", "bar"}
  1863. sort.Strings(md.Unused)
  1864. if !reflect.DeepEqual(md.Unused, expectedUnused) {
  1865. t.Fatalf("bad unused: %#v", md.Unused)
  1866. }
  1867. }
  1868. func TestMetadata_Embedded(t *testing.T) {
  1869. t.Parallel()
  1870. input := map[string]interface{}{
  1871. "vstring": "foo",
  1872. "vunique": "bar",
  1873. }
  1874. var md Metadata
  1875. var result EmbeddedSquash
  1876. config := &DecoderConfig{
  1877. Metadata: &md,
  1878. Result: &result,
  1879. }
  1880. decoder, err := NewDecoder(config)
  1881. if err != nil {
  1882. t.Fatalf("err: %s", err)
  1883. }
  1884. err = decoder.Decode(input)
  1885. if err != nil {
  1886. t.Fatalf("err: %s", err.Error())
  1887. }
  1888. expectedKeys := []string{"Vstring", "Vunique"}
  1889. sort.Strings(md.Keys)
  1890. if !reflect.DeepEqual(md.Keys, expectedKeys) {
  1891. t.Fatalf("bad keys: %#v", md.Keys)
  1892. }
  1893. expectedUnused := []string{}
  1894. if !reflect.DeepEqual(md.Unused, expectedUnused) {
  1895. t.Fatalf("bad unused: %#v", md.Unused)
  1896. }
  1897. }
  1898. func TestNonPtrValue(t *testing.T) {
  1899. t.Parallel()
  1900. err := Decode(map[string]interface{}{}, Basic{})
  1901. if err == nil {
  1902. t.Fatal("error should exist")
  1903. }
  1904. if err.Error() != "result must be a pointer" {
  1905. t.Errorf("got unexpected error: %s", err)
  1906. }
  1907. }
  1908. func TestTagged(t *testing.T) {
  1909. t.Parallel()
  1910. input := map[string]interface{}{
  1911. "foo": "bar",
  1912. "bar": "value",
  1913. }
  1914. var result Tagged
  1915. err := Decode(input, &result)
  1916. if err != nil {
  1917. t.Fatalf("unexpected error: %s", err)
  1918. }
  1919. if result.Value != "bar" {
  1920. t.Errorf("value should be 'bar', got: %#v", result.Value)
  1921. }
  1922. if result.Extra != "value" {
  1923. t.Errorf("extra should be 'value', got: %#v", result.Extra)
  1924. }
  1925. }
  1926. func TestWeakDecode(t *testing.T) {
  1927. t.Parallel()
  1928. input := map[string]interface{}{
  1929. "foo": "4",
  1930. "bar": "value",
  1931. }
  1932. var result struct {
  1933. Foo int
  1934. Bar string
  1935. }
  1936. if err := WeakDecode(input, &result); err != nil {
  1937. t.Fatalf("err: %s", err)
  1938. }
  1939. if result.Foo != 4 {
  1940. t.Fatalf("bad: %#v", result)
  1941. }
  1942. if result.Bar != "value" {
  1943. t.Fatalf("bad: %#v", result)
  1944. }
  1945. }
  1946. func TestWeakDecodeMetadata(t *testing.T) {
  1947. t.Parallel()
  1948. input := map[string]interface{}{
  1949. "foo": "4",
  1950. "bar": "value",
  1951. "unused": "value",
  1952. "unexported": "value",
  1953. }
  1954. var md Metadata
  1955. var result struct {
  1956. Foo int
  1957. Bar string
  1958. unexported string
  1959. }
  1960. if err := WeakDecodeMetadata(input, &result, &md); err != nil {
  1961. t.Fatalf("err: %s", err)
  1962. }
  1963. if result.Foo != 4 {
  1964. t.Fatalf("bad: %#v", result)
  1965. }
  1966. if result.Bar != "value" {
  1967. t.Fatalf("bad: %#v", result)
  1968. }
  1969. expectedKeys := []string{"Bar", "Foo"}
  1970. sort.Strings(md.Keys)
  1971. if !reflect.DeepEqual(md.Keys, expectedKeys) {
  1972. t.Fatalf("bad keys: %#v", md.Keys)
  1973. }
  1974. expectedUnused := []string{"unexported", "unused"}
  1975. sort.Strings(md.Unused)
  1976. if !reflect.DeepEqual(md.Unused, expectedUnused) {
  1977. t.Fatalf("bad unused: %#v", md.Unused)
  1978. }
  1979. }
  1980. func TestDecode_StructTaggedWithOmitempty_OmitEmptyValues(t *testing.T) {
  1981. t.Parallel()
  1982. input := &StructWithOmitEmpty{}
  1983. var emptySlice []interface{}
  1984. var emptyMap map[string]interface{}
  1985. var emptyNested *Nested
  1986. expected := &map[string]interface{}{
  1987. "visible-string": "",
  1988. "visible-int": 0,
  1989. "visible-float": 0.0,
  1990. "visible-slice": emptySlice,
  1991. "visible-map": emptyMap,
  1992. "visible-nested": emptyNested,
  1993. }
  1994. actual := &map[string]interface{}{}
  1995. Decode(input, actual)
  1996. if !reflect.DeepEqual(actual, expected) {
  1997. t.Fatalf("Decode() expected: %#v, got: %#v", expected, actual)
  1998. }
  1999. }
  2000. func TestDecode_StructTaggedWithOmitempty_KeepNonEmptyValues(t *testing.T) {
  2001. t.Parallel()
  2002. input := &StructWithOmitEmpty{
  2003. VisibleStringField: "",
  2004. OmitStringField: "string",
  2005. VisibleIntField: 0,
  2006. OmitIntField: 1,
  2007. VisibleFloatField: 0.0,
  2008. OmitFloatField: 1.0,
  2009. VisibleSliceField: nil,
  2010. OmitSliceField: []interface{}{1},
  2011. VisibleMapField: nil,
  2012. OmitMapField: map[string]interface{}{"k": "v"},
  2013. NestedField: nil,
  2014. OmitNestedField: &Nested{},
  2015. }
  2016. var emptySlice []interface{}
  2017. var emptyMap map[string]interface{}
  2018. var emptyNested *Nested
  2019. expected := &map[string]interface{}{
  2020. "visible-string": "",
  2021. "omittable-string": "string",
  2022. "visible-int": 0,
  2023. "omittable-int": 1,
  2024. "visible-float": 0.0,
  2025. "omittable-float": 1.0,
  2026. "visible-slice": emptySlice,
  2027. "omittable-slice": []interface{}{1},
  2028. "visible-map": emptyMap,
  2029. "omittable-map": map[string]interface{}{"k": "v"},
  2030. "visible-nested": emptyNested,
  2031. "omittable-nested": &Nested{},
  2032. }
  2033. actual := &map[string]interface{}{}
  2034. Decode(input, actual)
  2035. if !reflect.DeepEqual(actual, expected) {
  2036. t.Fatalf("Decode() expected: %#v, got: %#v", expected, actual)
  2037. }
  2038. }
  2039. func TestDecode_mapToStruct(t *testing.T) {
  2040. type Target struct {
  2041. String string
  2042. StringPtr *string
  2043. }
  2044. expected := Target{
  2045. String: "hello",
  2046. }
  2047. var target Target
  2048. err := Decode(map[string]interface{}{
  2049. "string": "hello",
  2050. "StringPtr": "goodbye",
  2051. }, &target)
  2052. if err != nil {
  2053. t.Fatalf("got error: %s", err)
  2054. }
  2055. // Pointers fail reflect test so do those manually
  2056. if target.StringPtr == nil || *target.StringPtr != "goodbye" {
  2057. t.Fatalf("bad: %#v", target)
  2058. }
  2059. target.StringPtr = nil
  2060. if !reflect.DeepEqual(target, expected) {
  2061. t.Fatalf("bad: %#v", target)
  2062. }
  2063. }
  2064. func TestDecoder_MatchName(t *testing.T) {
  2065. t.Parallel()
  2066. type Target struct {
  2067. FirstMatch string `mapstructure:"first_match"`
  2068. SecondMatch string
  2069. NoMatch string `mapstructure:"no_match"`
  2070. }
  2071. input := map[string]interface{}{
  2072. "first_match": "foo",
  2073. "SecondMatch": "bar",
  2074. "NO_MATCH": "baz",
  2075. }
  2076. expected := Target{
  2077. FirstMatch: "foo",
  2078. SecondMatch: "bar",
  2079. }
  2080. var actual Target
  2081. config := &DecoderConfig{
  2082. Result: &actual,
  2083. MatchName: func(mapKey, fieldName string) bool {
  2084. return mapKey == fieldName
  2085. },
  2086. }
  2087. decoder, err := NewDecoder(config)
  2088. if err != nil {
  2089. t.Fatalf("err: %s", err)
  2090. }
  2091. err = decoder.Decode(input)
  2092. if err != nil {
  2093. t.Fatalf("err: %s", err)
  2094. }
  2095. if !reflect.DeepEqual(expected, actual) {
  2096. t.Fatalf("Decode() expected: %#v, got: %#v", expected, actual)
  2097. }
  2098. }
  2099. func testSliceInput(t *testing.T, input map[string]interface{}, expected *Slice) {
  2100. var result Slice
  2101. err := Decode(input, &result)
  2102. if err != nil {
  2103. t.Fatalf("got error: %s", err)
  2104. }
  2105. if result.Vfoo != expected.Vfoo {
  2106. t.Errorf("Vfoo expected '%s', got '%s'", expected.Vfoo, result.Vfoo)
  2107. }
  2108. if result.Vbar == nil {
  2109. t.Fatalf("Vbar a slice, got '%#v'", result.Vbar)
  2110. }
  2111. if len(result.Vbar) != len(expected.Vbar) {
  2112. t.Errorf("Vbar length should be %d, got %d", len(expected.Vbar), len(result.Vbar))
  2113. }
  2114. for i, v := range result.Vbar {
  2115. if v != expected.Vbar[i] {
  2116. t.Errorf(
  2117. "Vbar[%d] should be '%#v', got '%#v'",
  2118. i, expected.Vbar[i], v)
  2119. }
  2120. }
  2121. }
  2122. func testArrayInput(t *testing.T, input map[string]interface{}, expected *Array) {
  2123. var result Array
  2124. err := Decode(input, &result)
  2125. if err != nil {
  2126. t.Fatalf("got error: %s", err)
  2127. }
  2128. if result.Vfoo != expected.Vfoo {
  2129. t.Errorf("Vfoo expected '%s', got '%s'", expected.Vfoo, result.Vfoo)
  2130. }
  2131. if result.Vbar == [2]string{} {
  2132. t.Fatalf("Vbar a slice, got '%#v'", result.Vbar)
  2133. }
  2134. if len(result.Vbar) != len(expected.Vbar) {
  2135. t.Errorf("Vbar length should be %d, got %d", len(expected.Vbar), len(result.Vbar))
  2136. }
  2137. for i, v := range result.Vbar {
  2138. if v != expected.Vbar[i] {
  2139. t.Errorf(
  2140. "Vbar[%d] should be '%#v', got '%#v'",
  2141. i, expected.Vbar[i], v)
  2142. }
  2143. }
  2144. }
  2145. func stringPtr(v string) *string { return &v }
  2146. func intPtr(v int) *int { return &v }
  2147. func uintPtr(v uint) *uint { return &v }
  2148. func boolPtr(v bool) *bool { return &v }
  2149. func floatPtr(v float64) *float64 { return &v }
  2150. func interfacePtr(v interface{}) *interface{} { return &v }