git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/schema/decoder_test.go (about)

     1  // Copyright 2012 The Gorilla Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package schema
     6  
     7  import (
     8  	"encoding/hex"
     9  	"errors"
    10  	"reflect"
    11  	"strings"
    12  	"testing"
    13  	"time"
    14  )
    15  
    16  type IntAlias int
    17  
    18  type rudeBool bool
    19  
    20  func (id *rudeBool) UnmarshalText(text []byte) error {
    21  	value := string(text)
    22  	switch {
    23  	case strings.EqualFold("Yup", value):
    24  		*id = true
    25  	case strings.EqualFold("Nope", value):
    26  		*id = false
    27  	default:
    28  		return errors.New("value must be yup or nope")
    29  	}
    30  	return nil
    31  }
    32  
    33  // All cases we want to cover, in a nutshell.
    34  type S1 struct {
    35  	F01 int         `schema:"f1"`
    36  	F02 *int        `schema:"f2"`
    37  	F03 []int       `schema:"f3"`
    38  	F04 []*int      `schema:"f4"`
    39  	F05 *[]int      `schema:"f5"`
    40  	F06 *[]*int     `schema:"f6"`
    41  	F07 S2          `schema:"f7"`
    42  	F08 *S1         `schema:"f8"`
    43  	F09 int         `schema:"-"`
    44  	F10 []S1        `schema:"f10"`
    45  	F11 []*S1       `schema:"f11"`
    46  	F12 *[]S1       `schema:"f12"`
    47  	F13 *[]*S1      `schema:"f13"`
    48  	F14 int         `schema:"f14"`
    49  	F15 IntAlias    `schema:"f15"`
    50  	F16 []IntAlias  `schema:"f16"`
    51  	F17 S19         `schema:"f17"`
    52  	F18 rudeBool    `schema:"f18"`
    53  	F19 *rudeBool   `schema:"f19"`
    54  	F20 []rudeBool  `schema:"f20"`
    55  	F21 []*rudeBool `schema:"f21"`
    56  }
    57  
    58  type S2 struct {
    59  	F01 *[]*int `schema:"f1"`
    60  }
    61  
    62  type S19 [2]byte
    63  
    64  func (id *S19) UnmarshalText(text []byte) error {
    65  	buf, err := hex.DecodeString(string(text))
    66  	if err != nil {
    67  		return err
    68  	}
    69  	if len(buf) > len(*id) {
    70  		return errors.New("out of range")
    71  	}
    72  	copy((*id)[:], buf)
    73  	return nil
    74  }
    75  
    76  func TestAll(t *testing.T) {
    77  	v := map[string][]string{
    78  		"f1":             {"1"},
    79  		"f2":             {"2"},
    80  		"f3":             {"31", "32"},
    81  		"f4":             {"41", "42"},
    82  		"f5":             {"51", "52"},
    83  		"f6":             {"61", "62"},
    84  		"f7.f1":          {"71", "72"},
    85  		"f8.f8.f7.f1":    {"81", "82"},
    86  		"f9":             {"9"},
    87  		"f10.0.f10.0.f6": {"101", "102"},
    88  		"f10.0.f10.1.f6": {"103", "104"},
    89  		"f11.0.f11.0.f6": {"111", "112"},
    90  		"f11.0.f11.1.f6": {"113", "114"},
    91  		"f12.0.f12.0.f6": {"121", "122"},
    92  		"f12.0.f12.1.f6": {"123", "124"},
    93  		"f13.0.f13.0.f6": {"131", "132"},
    94  		"f13.0.f13.1.f6": {"133", "134"},
    95  		"f14":            {},
    96  		"f15":            {"151"},
    97  		"f16":            {"161", "162"},
    98  		"f17":            {"1a2b"},
    99  		"f18":            {"yup"},
   100  		"f19":            {"nope"},
   101  		"f20":            {"nope", "yup"},
   102  		"f21":            {"yup", "nope"},
   103  	}
   104  	f2 := 2
   105  	f41, f42 := 41, 42
   106  	f61, f62 := 61, 62
   107  	f71, f72 := 71, 72
   108  	f81, f82 := 81, 82
   109  	f101, f102, f103, f104 := 101, 102, 103, 104
   110  	f111, f112, f113, f114 := 111, 112, 113, 114
   111  	f121, f122, f123, f124 := 121, 122, 123, 124
   112  	f131, f132, f133, f134 := 131, 132, 133, 134
   113  	var f151 IntAlias = 151
   114  	var f161, f162 IntAlias = 161, 162
   115  	var f152, f153 rudeBool = true, false
   116  	e := S1{
   117  		F01: 1,
   118  		F02: &f2,
   119  		F03: []int{31, 32},
   120  		F04: []*int{&f41, &f42},
   121  		F05: &[]int{51, 52},
   122  		F06: &[]*int{&f61, &f62},
   123  		F07: S2{
   124  			F01: &[]*int{&f71, &f72},
   125  		},
   126  		F08: &S1{
   127  			F08: &S1{
   128  				F07: S2{
   129  					F01: &[]*int{&f81, &f82},
   130  				},
   131  			},
   132  		},
   133  		F09: 0,
   134  		F10: []S1{
   135  			S1{
   136  				F10: []S1{
   137  					S1{F06: &[]*int{&f101, &f102}},
   138  					S1{F06: &[]*int{&f103, &f104}},
   139  				},
   140  			},
   141  		},
   142  		F11: []*S1{
   143  			&S1{
   144  				F11: []*S1{
   145  					&S1{F06: &[]*int{&f111, &f112}},
   146  					&S1{F06: &[]*int{&f113, &f114}},
   147  				},
   148  			},
   149  		},
   150  		F12: &[]S1{
   151  			S1{
   152  				F12: &[]S1{
   153  					S1{F06: &[]*int{&f121, &f122}},
   154  					S1{F06: &[]*int{&f123, &f124}},
   155  				},
   156  			},
   157  		},
   158  		F13: &[]*S1{
   159  			&S1{
   160  				F13: &[]*S1{
   161  					&S1{F06: &[]*int{&f131, &f132}},
   162  					&S1{F06: &[]*int{&f133, &f134}},
   163  				},
   164  			},
   165  		},
   166  		F14: 0,
   167  		F15: f151,
   168  		F16: []IntAlias{f161, f162},
   169  		F17: S19{0x1a, 0x2b},
   170  		F18: f152,
   171  		F19: &f153,
   172  		F20: []rudeBool{f153, f152},
   173  		F21: []*rudeBool{&f152, &f153},
   174  	}
   175  
   176  	s := &S1{}
   177  	_ = NewDecoder().Decode(s, v)
   178  
   179  	vals := func(values []*int) []int {
   180  		r := make([]int, len(values))
   181  		for k, v := range values {
   182  			r[k] = *v
   183  		}
   184  		return r
   185  	}
   186  
   187  	if s.F01 != e.F01 {
   188  		t.Errorf("f1: expected %v, got %v", e.F01, s.F01)
   189  	}
   190  	if s.F02 == nil {
   191  		t.Errorf("f2: expected %v, got nil", *e.F02)
   192  	} else if *s.F02 != *e.F02 {
   193  		t.Errorf("f2: expected %v, got %v", *e.F02, *s.F02)
   194  	}
   195  	if s.F03 == nil {
   196  		t.Errorf("f3: expected %v, got nil", e.F03)
   197  	} else if len(s.F03) != 2 || s.F03[0] != e.F03[0] || s.F03[1] != e.F03[1] {
   198  		t.Errorf("f3: expected %v, got %v", e.F03, s.F03)
   199  	}
   200  	if s.F04 == nil {
   201  		t.Errorf("f4: expected %v, got nil", e.F04)
   202  	} else {
   203  		if len(s.F04) != 2 || *(s.F04)[0] != *(e.F04)[0] || *(s.F04)[1] != *(e.F04)[1] {
   204  			t.Errorf("f4: expected %v, got %v", vals(e.F04), vals(s.F04))
   205  		}
   206  	}
   207  	if s.F05 == nil {
   208  		t.Errorf("f5: expected %v, got nil", e.F05)
   209  	} else {
   210  		sF05, eF05 := *s.F05, *e.F05
   211  		if len(sF05) != 2 || sF05[0] != eF05[0] || sF05[1] != eF05[1] {
   212  			t.Errorf("f5: expected %v, got %v", eF05, sF05)
   213  		}
   214  	}
   215  	if s.F06 == nil {
   216  		t.Errorf("f6: expected %v, got nil", vals(*e.F06))
   217  	} else {
   218  		sF06, eF06 := *s.F06, *e.F06
   219  		if len(sF06) != 2 || *(sF06)[0] != *(eF06)[0] || *(sF06)[1] != *(eF06)[1] {
   220  			t.Errorf("f6: expected %v, got %v", vals(eF06), vals(sF06))
   221  		}
   222  	}
   223  	if s.F07.F01 == nil {
   224  		t.Errorf("f7.f1: expected %v, got nil", vals(*e.F07.F01))
   225  	} else {
   226  		sF07, eF07 := *s.F07.F01, *e.F07.F01
   227  		if len(sF07) != 2 || *(sF07)[0] != *(eF07)[0] || *(sF07)[1] != *(eF07)[1] {
   228  			t.Errorf("f7.f1: expected %v, got %v", vals(eF07), vals(sF07))
   229  		}
   230  	}
   231  	if s.F08 == nil {
   232  		t.Errorf("f8: got nil")
   233  	} else if s.F08.F08 == nil {
   234  		t.Errorf("f8.f8: got nil")
   235  	} else if s.F08.F08.F07.F01 == nil {
   236  		t.Errorf("f8.f8.f7.f1: expected %v, got nil", vals(*e.F08.F08.F07.F01))
   237  	} else {
   238  		sF08, eF08 := *s.F08.F08.F07.F01, *e.F08.F08.F07.F01
   239  		if len(sF08) != 2 || *(sF08)[0] != *(eF08)[0] || *(sF08)[1] != *(eF08)[1] {
   240  			t.Errorf("f8.f8.f7.f1: expected %v, got %v", vals(eF08), vals(sF08))
   241  		}
   242  	}
   243  	if s.F09 != e.F09 {
   244  		t.Errorf("f9: expected %v, got %v", e.F09, s.F09)
   245  	}
   246  	if s.F10 == nil {
   247  		t.Errorf("f10: got nil")
   248  	} else if len(s.F10) != 1 {
   249  		t.Errorf("f10: expected 1 element, got %v", s.F10)
   250  	} else {
   251  		if len(s.F10[0].F10) != 2 {
   252  			t.Errorf("f10.0.f10: expected 1 element, got %v", s.F10[0].F10)
   253  		} else {
   254  			sF10, eF10 := *s.F10[0].F10[0].F06, *e.F10[0].F10[0].F06
   255  			if sF10 == nil {
   256  				t.Errorf("f10.0.f10.0.f6: expected %v, got nil", vals(eF10))
   257  			} else {
   258  				if len(sF10) != 2 || *(sF10)[0] != *(eF10)[0] || *(sF10)[1] != *(eF10)[1] {
   259  					t.Errorf("f10.0.f10.0.f6: expected %v, got %v", vals(eF10), vals(sF10))
   260  				}
   261  			}
   262  			sF10, eF10 = *s.F10[0].F10[1].F06, *e.F10[0].F10[1].F06
   263  			if sF10 == nil {
   264  				t.Errorf("f10.0.f10.0.f6: expected %v, got nil", vals(eF10))
   265  			} else {
   266  				if len(sF10) != 2 || *(sF10)[0] != *(eF10)[0] || *(sF10)[1] != *(eF10)[1] {
   267  					t.Errorf("f10.0.f10.0.f6: expected %v, got %v", vals(eF10), vals(sF10))
   268  				}
   269  			}
   270  		}
   271  	}
   272  	if s.F11 == nil {
   273  		t.Errorf("f11: got nil")
   274  	} else if len(s.F11) != 1 {
   275  		t.Errorf("f11: expected 1 element, got %v", s.F11)
   276  	} else {
   277  		if len(s.F11[0].F11) != 2 {
   278  			t.Errorf("f11.0.f11: expected 1 element, got %v", s.F11[0].F11)
   279  		} else {
   280  			sF11, eF11 := *s.F11[0].F11[0].F06, *e.F11[0].F11[0].F06
   281  			if sF11 == nil {
   282  				t.Errorf("f11.0.f11.0.f6: expected %v, got nil", vals(eF11))
   283  			} else {
   284  				if len(sF11) != 2 || *(sF11)[0] != *(eF11)[0] || *(sF11)[1] != *(eF11)[1] {
   285  					t.Errorf("f11.0.f11.0.f6: expected %v, got %v", vals(eF11), vals(sF11))
   286  				}
   287  			}
   288  			sF11, eF11 = *s.F11[0].F11[1].F06, *e.F11[0].F11[1].F06
   289  			if sF11 == nil {
   290  				t.Errorf("f11.0.f11.0.f6: expected %v, got nil", vals(eF11))
   291  			} else {
   292  				if len(sF11) != 2 || *(sF11)[0] != *(eF11)[0] || *(sF11)[1] != *(eF11)[1] {
   293  					t.Errorf("f11.0.f11.0.f6: expected %v, got %v", vals(eF11), vals(sF11))
   294  				}
   295  			}
   296  		}
   297  	}
   298  	if s.F12 == nil {
   299  		t.Errorf("f12: got nil")
   300  	} else if len(*s.F12) != 1 {
   301  		t.Errorf("f12: expected 1 element, got %v", *s.F12)
   302  	} else {
   303  		sF12, eF12 := *(s.F12), *(e.F12)
   304  		if len(*sF12[0].F12) != 2 {
   305  			t.Errorf("f12.0.f12: expected 1 element, got %v", *sF12[0].F12)
   306  		} else {
   307  			sF122, eF122 := *(*sF12[0].F12)[0].F06, *(*eF12[0].F12)[0].F06
   308  			if sF122 == nil {
   309  				t.Errorf("f12.0.f12.0.f6: expected %v, got nil", vals(eF122))
   310  			} else {
   311  				if len(sF122) != 2 || *(sF122)[0] != *(eF122)[0] || *(sF122)[1] != *(eF122)[1] {
   312  					t.Errorf("f12.0.f12.0.f6: expected %v, got %v", vals(eF122), vals(sF122))
   313  				}
   314  			}
   315  			sF122, eF122 = *(*sF12[0].F12)[1].F06, *(*eF12[0].F12)[1].F06
   316  			if sF122 == nil {
   317  				t.Errorf("f12.0.f12.0.f6: expected %v, got nil", vals(eF122))
   318  			} else {
   319  				if len(sF122) != 2 || *(sF122)[0] != *(eF122)[0] || *(sF122)[1] != *(eF122)[1] {
   320  					t.Errorf("f12.0.f12.0.f6: expected %v, got %v", vals(eF122), vals(sF122))
   321  				}
   322  			}
   323  		}
   324  	}
   325  	if s.F13 == nil {
   326  		t.Errorf("f13: got nil")
   327  	} else if len(*s.F13) != 1 {
   328  		t.Errorf("f13: expected 1 element, got %v", *s.F13)
   329  	} else {
   330  		sF13, eF13 := *(s.F13), *(e.F13)
   331  		if len(*sF13[0].F13) != 2 {
   332  			t.Errorf("f13.0.f13: expected 1 element, got %v", *sF13[0].F13)
   333  		} else {
   334  			sF132, eF132 := *(*sF13[0].F13)[0].F06, *(*eF13[0].F13)[0].F06
   335  			if sF132 == nil {
   336  				t.Errorf("f13.0.f13.0.f6: expected %v, got nil", vals(eF132))
   337  			} else {
   338  				if len(sF132) != 2 || *(sF132)[0] != *(eF132)[0] || *(sF132)[1] != *(eF132)[1] {
   339  					t.Errorf("f13.0.f13.0.f6: expected %v, got %v", vals(eF132), vals(sF132))
   340  				}
   341  			}
   342  			sF132, eF132 = *(*sF13[0].F13)[1].F06, *(*eF13[0].F13)[1].F06
   343  			if sF132 == nil {
   344  				t.Errorf("f13.0.f13.0.f6: expected %v, got nil", vals(eF132))
   345  			} else {
   346  				if len(sF132) != 2 || *(sF132)[0] != *(eF132)[0] || *(sF132)[1] != *(eF132)[1] {
   347  					t.Errorf("f13.0.f13.0.f6: expected %v, got %v", vals(eF132), vals(sF132))
   348  				}
   349  			}
   350  		}
   351  	}
   352  	if s.F14 != e.F14 {
   353  		t.Errorf("f14: expected %v, got %v", e.F14, s.F14)
   354  	}
   355  	if s.F15 != e.F15 {
   356  		t.Errorf("f15: expected %v, got %v", e.F15, s.F15)
   357  	}
   358  	if s.F16 == nil {
   359  		t.Errorf("f16: nil")
   360  	} else if len(s.F16) != len(e.F16) {
   361  		t.Errorf("f16: expected len %d, got %d", len(e.F16), len(s.F16))
   362  	} else if !reflect.DeepEqual(s.F16, e.F16) {
   363  		t.Errorf("f16: expected %v, got %v", e.F16, s.F16)
   364  	}
   365  	if s.F17 != e.F17 {
   366  		t.Errorf("f17: expected %v, got %v", e.F17, s.F17)
   367  	}
   368  	if s.F18 != e.F18 {
   369  		t.Errorf("f18: expected %v, got %v", e.F18, s.F18)
   370  	}
   371  	if *s.F19 != *e.F19 {
   372  		t.Errorf("f19: expected %v, got %v", *e.F19, *s.F19)
   373  	}
   374  	if s.F20 == nil {
   375  		t.Errorf("f20: nil")
   376  	} else if len(s.F20) != len(e.F20) {
   377  		t.Errorf("f20: expected %v, got %v", e.F20, s.F20)
   378  	} else if !reflect.DeepEqual(s.F20, e.F20) {
   379  		t.Errorf("f20: expected %v, got %v", e.F20, s.F20)
   380  	}
   381  	if s.F21 == nil {
   382  		t.Errorf("f21: nil")
   383  	} else if len(s.F21) != len(e.F21) {
   384  		t.Errorf("f21: expected length %d, got %d", len(e.F21), len(s.F21))
   385  	} else if !reflect.DeepEqual(s.F21, e.F21) {
   386  		t.Errorf("f21: expected %v, got %v", e.F21, s.F21)
   387  	}
   388  }
   389  
   390  func BenchmarkAll(b *testing.B) {
   391  	v := map[string][]string{
   392  		"f1":             {"1"},
   393  		"f2":             {"2"},
   394  		"f3":             {"31", "32"},
   395  		"f4":             {"41", "42"},
   396  		"f5":             {"51", "52"},
   397  		"f6":             {"61", "62"},
   398  		"f7.f1":          {"71", "72"},
   399  		"f8.f8.f7.f1":    {"81", "82"},
   400  		"f9":             {"9"},
   401  		"f10.0.f10.0.f6": {"101", "102"},
   402  		"f10.0.f10.1.f6": {"103", "104"},
   403  		"f11.0.f11.0.f6": {"111", "112"},
   404  		"f11.0.f11.1.f6": {"113", "114"},
   405  		"f12.0.f12.0.f6": {"121", "122"},
   406  		"f12.0.f12.1.f6": {"123", "124"},
   407  		"f13.0.f13.0.f6": {"131", "132"},
   408  		"f13.0.f13.1.f6": {"133", "134"},
   409  	}
   410  
   411  	b.ResetTimer()
   412  
   413  	for i := 0; i < b.N; i++ {
   414  		s := &S1{}
   415  		_ = NewDecoder().Decode(s, v)
   416  	}
   417  }
   418  
   419  // ----------------------------------------------------------------------------
   420  
   421  type S3 struct {
   422  	F01 bool
   423  	F02 float32
   424  	F03 float64
   425  	F04 int
   426  	F05 int8
   427  	F06 int16
   428  	F07 int32
   429  	F08 int64
   430  	F09 string
   431  	F10 uint
   432  	F11 uint8
   433  	F12 uint16
   434  	F13 uint32
   435  	F14 uint64
   436  }
   437  
   438  func TestDefaultConverters(t *testing.T) {
   439  	v := map[string][]string{
   440  		"F01": {"true"},
   441  		"F02": {"4.2"},
   442  		"F03": {"4.3"},
   443  		"F04": {"-42"},
   444  		"F05": {"-43"},
   445  		"F06": {"-44"},
   446  		"F07": {"-45"},
   447  		"F08": {"-46"},
   448  		"F09": {"foo"},
   449  		"F10": {"42"},
   450  		"F11": {"43"},
   451  		"F12": {"44"},
   452  		"F13": {"45"},
   453  		"F14": {"46"},
   454  	}
   455  	e := S3{
   456  		F01: true,
   457  		F02: 4.2,
   458  		F03: 4.3,
   459  		F04: -42,
   460  		F05: -43,
   461  		F06: -44,
   462  		F07: -45,
   463  		F08: -46,
   464  		F09: "foo",
   465  		F10: 42,
   466  		F11: 43,
   467  		F12: 44,
   468  		F13: 45,
   469  		F14: 46,
   470  	}
   471  	s := &S3{}
   472  	_ = NewDecoder().Decode(s, v)
   473  	if s.F01 != e.F01 {
   474  		t.Errorf("F01: expected %v, got %v", e.F01, s.F01)
   475  	}
   476  	if s.F02 != e.F02 {
   477  		t.Errorf("F02: expected %v, got %v", e.F02, s.F02)
   478  	}
   479  	if s.F03 != e.F03 {
   480  		t.Errorf("F03: expected %v, got %v", e.F03, s.F03)
   481  	}
   482  	if s.F04 != e.F04 {
   483  		t.Errorf("F04: expected %v, got %v", e.F04, s.F04)
   484  	}
   485  	if s.F05 != e.F05 {
   486  		t.Errorf("F05: expected %v, got %v", e.F05, s.F05)
   487  	}
   488  	if s.F06 != e.F06 {
   489  		t.Errorf("F06: expected %v, got %v", e.F06, s.F06)
   490  	}
   491  	if s.F07 != e.F07 {
   492  		t.Errorf("F07: expected %v, got %v", e.F07, s.F07)
   493  	}
   494  	if s.F08 != e.F08 {
   495  		t.Errorf("F08: expected %v, got %v", e.F08, s.F08)
   496  	}
   497  	if s.F09 != e.F09 {
   498  		t.Errorf("F09: expected %v, got %v", e.F09, s.F09)
   499  	}
   500  	if s.F10 != e.F10 {
   501  		t.Errorf("F10: expected %v, got %v", e.F10, s.F10)
   502  	}
   503  	if s.F11 != e.F11 {
   504  		t.Errorf("F11: expected %v, got %v", e.F11, s.F11)
   505  	}
   506  	if s.F12 != e.F12 {
   507  		t.Errorf("F12: expected %v, got %v", e.F12, s.F12)
   508  	}
   509  	if s.F13 != e.F13 {
   510  		t.Errorf("F13: expected %v, got %v", e.F13, s.F13)
   511  	}
   512  	if s.F14 != e.F14 {
   513  		t.Errorf("F14: expected %v, got %v", e.F14, s.F14)
   514  	}
   515  }
   516  
   517  func TestOn(t *testing.T) {
   518  	v := map[string][]string{
   519  		"F01": {"on"},
   520  	}
   521  	s := S3{}
   522  	err := NewDecoder().Decode(&s, v)
   523  	if err != nil {
   524  		t.Fatal(err)
   525  	}
   526  	if !s.F01 {
   527  		t.Fatal("Value was not set to true")
   528  	}
   529  }
   530  
   531  // ----------------------------------------------------------------------------
   532  
   533  func TestInlineStruct(t *testing.T) {
   534  	s1 := &struct {
   535  		F01 bool
   536  	}{}
   537  	s2 := &struct {
   538  		F01 int
   539  	}{}
   540  	v1 := map[string][]string{
   541  		"F01": {"true"},
   542  	}
   543  	v2 := map[string][]string{
   544  		"F01": {"42"},
   545  	}
   546  	decoder := NewDecoder()
   547  	_ = decoder.Decode(s1, v1)
   548  	if s1.F01 != true {
   549  		t.Errorf("s1: expected %v, got %v", true, s1.F01)
   550  	}
   551  	_ = decoder.Decode(s2, v2)
   552  	if s2.F01 != 42 {
   553  		t.Errorf("s2: expected %v, got %v", 42, s2.F01)
   554  	}
   555  }
   556  
   557  // ----------------------------------------------------------------------------
   558  
   559  type Foo struct {
   560  	F01 int
   561  	F02 Bar
   562  	Bif []Baz
   563  }
   564  
   565  type Bar struct {
   566  	F01 string
   567  	F02 string
   568  	F03 string
   569  	F14 string
   570  	S05 string
   571  	Str string
   572  }
   573  
   574  type Baz struct {
   575  	F99 []string
   576  }
   577  
   578  func TestSimpleExample(t *testing.T) {
   579  	data := map[string][]string{
   580  		"F01":       {"1"},
   581  		"F02.F01":   {"S1"},
   582  		"F02.F02":   {"S2"},
   583  		"F02.F03":   {"S3"},
   584  		"F02.F14":   {"S4"},
   585  		"F02.S05":   {"S5"},
   586  		"F02.Str":   {"Str"},
   587  		"Bif.0.F99": {"A", "B", "C"},
   588  	}
   589  
   590  	e := &Foo{
   591  		F01: 1,
   592  		F02: Bar{
   593  			F01: "S1",
   594  			F02: "S2",
   595  			F03: "S3",
   596  			F14: "S4",
   597  			S05: "S5",
   598  			Str: "Str",
   599  		},
   600  		Bif: []Baz{{
   601  			F99: []string{"A", "B", "C"}},
   602  		},
   603  	}
   604  
   605  	s := &Foo{}
   606  	_ = NewDecoder().Decode(s, data)
   607  
   608  	if s.F01 != e.F01 {
   609  		t.Errorf("F01: expected %v, got %v", e.F01, s.F01)
   610  	}
   611  	if s.F02.F01 != e.F02.F01 {
   612  		t.Errorf("F02.F01: expected %v, got %v", e.F02.F01, s.F02.F01)
   613  	}
   614  	if s.F02.F02 != e.F02.F02 {
   615  		t.Errorf("F02.F02: expected %v, got %v", e.F02.F02, s.F02.F02)
   616  	}
   617  	if s.F02.F03 != e.F02.F03 {
   618  		t.Errorf("F02.F03: expected %v, got %v", e.F02.F03, s.F02.F03)
   619  	}
   620  	if s.F02.F14 != e.F02.F14 {
   621  		t.Errorf("F02.F14: expected %v, got %v", e.F02.F14, s.F02.F14)
   622  	}
   623  	if s.F02.S05 != e.F02.S05 {
   624  		t.Errorf("F02.S05: expected %v, got %v", e.F02.S05, s.F02.S05)
   625  	}
   626  	if s.F02.Str != e.F02.Str {
   627  		t.Errorf("F02.Str: expected %v, got %v", e.F02.Str, s.F02.Str)
   628  	}
   629  	if len(s.Bif) != len(e.Bif) {
   630  		t.Errorf("Bif len: expected %d, got %d", len(e.Bif), len(s.Bif))
   631  	} else {
   632  		if len(s.Bif[0].F99) != len(e.Bif[0].F99) {
   633  			t.Errorf("Bif[0].F99 len: expected %d, got %d", len(e.Bif[0].F99), len(s.Bif[0].F99))
   634  		}
   635  	}
   636  }
   637  
   638  // ----------------------------------------------------------------------------
   639  
   640  type S4 struct {
   641  	F01 int64
   642  	F02 float64
   643  	F03 bool
   644  	F04 rudeBool
   645  }
   646  
   647  func TestConversionError(t *testing.T) {
   648  	data := map[string][]string{
   649  		"F01": {"foo"},
   650  		"F02": {"bar"},
   651  		"F03": {"baz"},
   652  		"F04": {"not-a-yes-or-nope"},
   653  	}
   654  	s := &S4{}
   655  	e := NewDecoder().Decode(s, data)
   656  
   657  	m := e.(MultiError)
   658  	if len(m) != 4 {
   659  		t.Errorf("Expected 3 errors, got %v", m)
   660  	}
   661  }
   662  
   663  // ----------------------------------------------------------------------------
   664  
   665  type S5 struct {
   666  	F01 []string
   667  }
   668  
   669  func TestEmptyValue(t *testing.T) {
   670  	data := map[string][]string{
   671  		"F01": {"", "foo"},
   672  	}
   673  	s := &S5{}
   674  	err := NewDecoder().Decode(s, data)
   675  	if err != nil {
   676  		t.Fatalf("Failed to decode: %v", err)
   677  	}
   678  	if len(s.F01) != 1 {
   679  		t.Errorf("Expected 1 values in F01")
   680  	}
   681  }
   682  
   683  func TestEmptyValueZeroEmpty(t *testing.T) {
   684  	data := map[string][]string{
   685  		"F01": {"", "foo"},
   686  	}
   687  	s := S5{}
   688  	d := NewDecoder()
   689  	d.ZeroEmpty(true)
   690  	err := d.Decode(&s, data)
   691  	if err != nil {
   692  		t.Fatal(err)
   693  	}
   694  	if len(s.F01) != 2 {
   695  		t.Errorf("Expected 1 values in F01")
   696  	}
   697  }
   698  
   699  // ----------------------------------------------------------------------------
   700  
   701  type S6 struct {
   702  	id string
   703  }
   704  
   705  func TestUnexportedField(t *testing.T) {
   706  	data := map[string][]string{
   707  		"id": {"identifier"},
   708  	}
   709  	s := &S6{}
   710  	err := NewDecoder().Decode(s, data)
   711  	if err != nil {
   712  		t.Fatalf("Failed to decode: %v", err)
   713  	}
   714  	if s.id != "" {
   715  		t.Errorf("Unexported field expected to be ignored")
   716  	}
   717  }
   718  
   719  // ----------------------------------------------------------------------------
   720  
   721  type S7 struct {
   722  	ID string
   723  }
   724  
   725  func TestMultipleValues(t *testing.T) {
   726  	data := map[string][]string{
   727  		"ID": {"0", "1"},
   728  	}
   729  
   730  	s := S7{}
   731  	err := NewDecoder().Decode(&s, data)
   732  	if err != nil {
   733  		t.Fatalf("Failed to decode: %v", err)
   734  	}
   735  	if s.ID != "1" {
   736  		t.Errorf("Last defined value must be used when multiple values for same field are provided")
   737  	}
   738  }
   739  
   740  type S8 struct {
   741  	ID string `json:"id"`
   742  }
   743  
   744  func TestSetAliasTag(t *testing.T) {
   745  	data := map[string][]string{
   746  		"id": {"foo"},
   747  	}
   748  
   749  	s := S8{}
   750  	dec := NewDecoder()
   751  	dec.SetAliasTag("json")
   752  	err := dec.Decode(&s, data)
   753  	if err != nil {
   754  		t.Fatalf("Failed to decode: %v", err)
   755  	}
   756  	if s.ID != "foo" {
   757  		t.Fatalf("Bad value: got %q, want %q", s.ID, "foo")
   758  	}
   759  }
   760  
   761  func TestZeroEmpty(t *testing.T) {
   762  	data := map[string][]string{
   763  		"F01": {""},
   764  		"F03": {"true"},
   765  	}
   766  	s := S4{1, 1, false, false}
   767  	d := NewDecoder()
   768  	d.ZeroEmpty(true)
   769  
   770  	err := d.Decode(&s, data)
   771  	if err != nil {
   772  		t.Fatal(err)
   773  	}
   774  	if s.F01 != 0 {
   775  		t.Errorf("F01: got %v, want %v", s.F01, 0)
   776  	}
   777  	if s.F02 != 1 {
   778  		t.Errorf("F02: got %v, want %v", s.F02, 1)
   779  	}
   780  	if s.F03 != true {
   781  		t.Errorf("F03: got %v, want %v", s.F03, true)
   782  	}
   783  }
   784  
   785  func TestNoZeroEmpty(t *testing.T) {
   786  	data := map[string][]string{
   787  		"F01": {""},
   788  		"F03": {"true"},
   789  	}
   790  	s := S4{1, 1, false, false}
   791  	d := NewDecoder()
   792  	d.ZeroEmpty(false)
   793  	err := d.Decode(&s, data)
   794  	if err != nil {
   795  		t.Fatal(err)
   796  	}
   797  	if s.F01 != 1 {
   798  		t.Errorf("F01: got %v, want %v", s.F01, 1)
   799  	}
   800  	if s.F02 != 1 {
   801  		t.Errorf("F02: got %v, want %v", s.F02, 1)
   802  	}
   803  	if s.F03 != true {
   804  		t.Errorf("F03: got %v, want %v", s.F03, true)
   805  	}
   806  	if s.F04 != false {
   807  		t.Errorf("F04: got %v, want %v", s.F04, false)
   808  	}
   809  }
   810  
   811  // ----------------------------------------------------------------------------
   812  
   813  type S9 struct {
   814  	Id string
   815  }
   816  
   817  type S10 struct {
   818  	S9
   819  }
   820  
   821  func TestEmbeddedField(t *testing.T) {
   822  	data := map[string][]string{
   823  		"Id": {"identifier"},
   824  	}
   825  	s := &S10{}
   826  	err := NewDecoder().Decode(s, data)
   827  	if err != nil {
   828  		t.Fatalf("Failed to decode: %v", err)
   829  	}
   830  	if s.Id != "identifier" {
   831  		t.Errorf("Missing support for embedded fields")
   832  	}
   833  }
   834  
   835  type S11 struct {
   836  	S10
   837  }
   838  
   839  func TestMultipleLevelEmbeddedField(t *testing.T) {
   840  	data := map[string][]string{
   841  		"Id": {"identifier"},
   842  	}
   843  	s := &S11{}
   844  	err := NewDecoder().Decode(s, data)
   845  	if s.Id != "identifier" {
   846  		t.Errorf("Missing support for multiple-level embedded fields (%v)", err)
   847  	}
   848  }
   849  
   850  func TestInvalidPath(t *testing.T) {
   851  	data := map[string][]string{
   852  		"Foo.Bar": {"baz"},
   853  	}
   854  	s := S9{}
   855  	err := NewDecoder().Decode(&s, data)
   856  	expectedErr := `schema: invalid path "Foo.Bar"`
   857  	if err.Error() != expectedErr {
   858  		t.Fatalf("got %q, want %q", err, expectedErr)
   859  	}
   860  }
   861  
   862  func TestInvalidPathIgnoreUnknownKeys(t *testing.T) {
   863  	data := map[string][]string{
   864  		"Foo.Bar": {"baz"},
   865  	}
   866  	s := S9{}
   867  	dec := NewDecoder()
   868  	dec.IgnoreUnknownKeys(true)
   869  	err := dec.Decode(&s, data)
   870  	if err != nil {
   871  		t.Fatal(err)
   872  	}
   873  }
   874  
   875  // ----------------------------------------------------------------------------
   876  
   877  type S1NT struct {
   878  	F1  int
   879  	F2  *int
   880  	F3  []int
   881  	F4  []*int
   882  	F5  *[]int
   883  	F6  *[]*int
   884  	F7  S2
   885  	F8  *S1
   886  	F9  int `schema:"-"`
   887  	F10 []S1
   888  	F11 []*S1
   889  	F12 *[]S1
   890  	F13 *[]*S1
   891  }
   892  
   893  func TestAllNT(t *testing.T) {
   894  	v := map[string][]string{
   895  		"f1":             {"1"},
   896  		"f2":             {"2"},
   897  		"f3":             {"31", "32"},
   898  		"f4":             {"41", "42"},
   899  		"f5":             {"51", "52"},
   900  		"f6":             {"61", "62"},
   901  		"f7.f1":          {"71", "72"},
   902  		"f8.f8.f7.f1":    {"81", "82"},
   903  		"f9":             {"9"},
   904  		"f10.0.f10.0.f6": {"101", "102"},
   905  		"f10.0.f10.1.f6": {"103", "104"},
   906  		"f11.0.f11.0.f6": {"111", "112"},
   907  		"f11.0.f11.1.f6": {"113", "114"},
   908  		"f12.0.f12.0.f6": {"121", "122"},
   909  		"f12.0.f12.1.f6": {"123", "124"},
   910  		"f13.0.f13.0.f6": {"131", "132"},
   911  		"f13.0.f13.1.f6": {"133", "134"},
   912  	}
   913  	f2 := 2
   914  	f41, f42 := 41, 42
   915  	f61, f62 := 61, 62
   916  	f71, f72 := 71, 72
   917  	f81, f82 := 81, 82
   918  	f101, f102, f103, f104 := 101, 102, 103, 104
   919  	f111, f112, f113, f114 := 111, 112, 113, 114
   920  	f121, f122, f123, f124 := 121, 122, 123, 124
   921  	f131, f132, f133, f134 := 131, 132, 133, 134
   922  	e := S1NT{
   923  		F1: 1,
   924  		F2: &f2,
   925  		F3: []int{31, 32},
   926  		F4: []*int{&f41, &f42},
   927  		F5: &[]int{51, 52},
   928  		F6: &[]*int{&f61, &f62},
   929  		F7: S2{
   930  			F01: &[]*int{&f71, &f72},
   931  		},
   932  		F8: &S1{
   933  			F08: &S1{
   934  				F07: S2{
   935  					F01: &[]*int{&f81, &f82},
   936  				},
   937  			},
   938  		},
   939  		F9: 0,
   940  		F10: []S1{
   941  			S1{
   942  				F10: []S1{
   943  					S1{F06: &[]*int{&f101, &f102}},
   944  					S1{F06: &[]*int{&f103, &f104}},
   945  				},
   946  			},
   947  		},
   948  		F11: []*S1{
   949  			&S1{
   950  				F11: []*S1{
   951  					&S1{F06: &[]*int{&f111, &f112}},
   952  					&S1{F06: &[]*int{&f113, &f114}},
   953  				},
   954  			},
   955  		},
   956  		F12: &[]S1{
   957  			S1{
   958  				F12: &[]S1{
   959  					S1{F06: &[]*int{&f121, &f122}},
   960  					S1{F06: &[]*int{&f123, &f124}},
   961  				},
   962  			},
   963  		},
   964  		F13: &[]*S1{
   965  			&S1{
   966  				F13: &[]*S1{
   967  					&S1{F06: &[]*int{&f131, &f132}},
   968  					&S1{F06: &[]*int{&f133, &f134}},
   969  				},
   970  			},
   971  		},
   972  	}
   973  
   974  	s := &S1NT{}
   975  	_ = NewDecoder().Decode(s, v)
   976  
   977  	vals := func(values []*int) []int {
   978  		r := make([]int, len(values))
   979  		for k, v := range values {
   980  			r[k] = *v
   981  		}
   982  		return r
   983  	}
   984  
   985  	if s.F1 != e.F1 {
   986  		t.Errorf("f1: expected %v, got %v", e.F1, s.F1)
   987  	}
   988  	if s.F2 == nil {
   989  		t.Errorf("f2: expected %v, got nil", *e.F2)
   990  	} else if *s.F2 != *e.F2 {
   991  		t.Errorf("f2: expected %v, got %v", *e.F2, *s.F2)
   992  	}
   993  	if s.F3 == nil {
   994  		t.Errorf("f3: expected %v, got nil", e.F3)
   995  	} else if len(s.F3) != 2 || s.F3[0] != e.F3[0] || s.F3[1] != e.F3[1] {
   996  		t.Errorf("f3: expected %v, got %v", e.F3, s.F3)
   997  	}
   998  	if s.F4 == nil {
   999  		t.Errorf("f4: expected %v, got nil", e.F4)
  1000  	} else {
  1001  		if len(s.F4) != 2 || *(s.F4)[0] != *(e.F4)[0] || *(s.F4)[1] != *(e.F4)[1] {
  1002  			t.Errorf("f4: expected %v, got %v", vals(e.F4), vals(s.F4))
  1003  		}
  1004  	}
  1005  	if s.F5 == nil {
  1006  		t.Errorf("f5: expected %v, got nil", e.F5)
  1007  	} else {
  1008  		sF5, eF5 := *s.F5, *e.F5
  1009  		if len(sF5) != 2 || sF5[0] != eF5[0] || sF5[1] != eF5[1] {
  1010  			t.Errorf("f5: expected %v, got %v", eF5, sF5)
  1011  		}
  1012  	}
  1013  	if s.F6 == nil {
  1014  		t.Errorf("f6: expected %v, got nil", vals(*e.F6))
  1015  	} else {
  1016  		sF6, eF6 := *s.F6, *e.F6
  1017  		if len(sF6) != 2 || *(sF6)[0] != *(eF6)[0] || *(sF6)[1] != *(eF6)[1] {
  1018  			t.Errorf("f6: expected %v, got %v", vals(eF6), vals(sF6))
  1019  		}
  1020  	}
  1021  	if s.F7.F01 == nil {
  1022  		t.Errorf("f7.f1: expected %v, got nil", vals(*e.F7.F01))
  1023  	} else {
  1024  		sF7, eF7 := *s.F7.F01, *e.F7.F01
  1025  		if len(sF7) != 2 || *(sF7)[0] != *(eF7)[0] || *(sF7)[1] != *(eF7)[1] {
  1026  			t.Errorf("f7.f1: expected %v, got %v", vals(eF7), vals(sF7))
  1027  		}
  1028  	}
  1029  	if s.F8 == nil {
  1030  		t.Errorf("f8: got nil")
  1031  	} else if s.F8.F08 == nil {
  1032  		t.Errorf("f8.f8: got nil")
  1033  	} else if s.F8.F08.F07.F01 == nil {
  1034  		t.Errorf("f8.f8.f7.f1: expected %v, got nil", vals(*e.F8.F08.F07.F01))
  1035  	} else {
  1036  		sF8, eF8 := *s.F8.F08.F07.F01, *e.F8.F08.F07.F01
  1037  		if len(sF8) != 2 || *(sF8)[0] != *(eF8)[0] || *(sF8)[1] != *(eF8)[1] {
  1038  			t.Errorf("f8.f8.f7.f1: expected %v, got %v", vals(eF8), vals(sF8))
  1039  		}
  1040  	}
  1041  	if s.F9 != e.F9 {
  1042  		t.Errorf("f9: expected %v, got %v", e.F9, s.F9)
  1043  	}
  1044  	if s.F10 == nil {
  1045  		t.Errorf("f10: got nil")
  1046  	} else if len(s.F10) != 1 {
  1047  		t.Errorf("f10: expected 1 element, got %v", s.F10)
  1048  	} else {
  1049  		if len(s.F10[0].F10) != 2 {
  1050  			t.Errorf("f10.0.f10: expected 1 element, got %v", s.F10[0].F10)
  1051  		} else {
  1052  			sF10, eF10 := *s.F10[0].F10[0].F06, *e.F10[0].F10[0].F06
  1053  			if sF10 == nil {
  1054  				t.Errorf("f10.0.f10.0.f6: expected %v, got nil", vals(eF10))
  1055  			} else {
  1056  				if len(sF10) != 2 || *(sF10)[0] != *(eF10)[0] || *(sF10)[1] != *(eF10)[1] {
  1057  					t.Errorf("f10.0.f10.0.f6: expected %v, got %v", vals(eF10), vals(sF10))
  1058  				}
  1059  			}
  1060  			sF10, eF10 = *s.F10[0].F10[1].F06, *e.F10[0].F10[1].F06
  1061  			if sF10 == nil {
  1062  				t.Errorf("f10.0.f10.0.f6: expected %v, got nil", vals(eF10))
  1063  			} else {
  1064  				if len(sF10) != 2 || *(sF10)[0] != *(eF10)[0] || *(sF10)[1] != *(eF10)[1] {
  1065  					t.Errorf("f10.0.f10.0.f6: expected %v, got %v", vals(eF10), vals(sF10))
  1066  				}
  1067  			}
  1068  		}
  1069  	}
  1070  	if s.F11 == nil {
  1071  		t.Errorf("f11: got nil")
  1072  	} else if len(s.F11) != 1 {
  1073  		t.Errorf("f11: expected 1 element, got %v", s.F11)
  1074  	} else {
  1075  		if len(s.F11[0].F11) != 2 {
  1076  			t.Errorf("f11.0.f11: expected 1 element, got %v", s.F11[0].F11)
  1077  		} else {
  1078  			sF11, eF11 := *s.F11[0].F11[0].F06, *e.F11[0].F11[0].F06
  1079  			if sF11 == nil {
  1080  				t.Errorf("f11.0.f11.0.f6: expected %v, got nil", vals(eF11))
  1081  			} else {
  1082  				if len(sF11) != 2 || *(sF11)[0] != *(eF11)[0] || *(sF11)[1] != *(eF11)[1] {
  1083  					t.Errorf("f11.0.f11.0.f6: expected %v, got %v", vals(eF11), vals(sF11))
  1084  				}
  1085  			}
  1086  			sF11, eF11 = *s.F11[0].F11[1].F06, *e.F11[0].F11[1].F06
  1087  			if sF11 == nil {
  1088  				t.Errorf("f11.0.f11.0.f6: expected %v, got nil", vals(eF11))
  1089  			} else {
  1090  				if len(sF11) != 2 || *(sF11)[0] != *(eF11)[0] || *(sF11)[1] != *(eF11)[1] {
  1091  					t.Errorf("f11.0.f11.0.f6: expected %v, got %v", vals(eF11), vals(sF11))
  1092  				}
  1093  			}
  1094  		}
  1095  	}
  1096  	if s.F12 == nil {
  1097  		t.Errorf("f12: got nil")
  1098  	} else if len(*s.F12) != 1 {
  1099  		t.Errorf("f12: expected 1 element, got %v", *s.F12)
  1100  	} else {
  1101  		sF12, eF12 := *(s.F12), *(e.F12)
  1102  		if len(*sF12[0].F12) != 2 {
  1103  			t.Errorf("f12.0.f12: expected 1 element, got %v", *sF12[0].F12)
  1104  		} else {
  1105  			sF122, eF122 := *(*sF12[0].F12)[0].F06, *(*eF12[0].F12)[0].F06
  1106  			if sF122 == nil {
  1107  				t.Errorf("f12.0.f12.0.f6: expected %v, got nil", vals(eF122))
  1108  			} else {
  1109  				if len(sF122) != 2 || *(sF122)[0] != *(eF122)[0] || *(sF122)[1] != *(eF122)[1] {
  1110  					t.Errorf("f12.0.f12.0.f6: expected %v, got %v", vals(eF122), vals(sF122))
  1111  				}
  1112  			}
  1113  			sF122, eF122 = *(*sF12[0].F12)[1].F06, *(*eF12[0].F12)[1].F06
  1114  			if sF122 == nil {
  1115  				t.Errorf("f12.0.f12.0.f6: expected %v, got nil", vals(eF122))
  1116  			} else {
  1117  				if len(sF122) != 2 || *(sF122)[0] != *(eF122)[0] || *(sF122)[1] != *(eF122)[1] {
  1118  					t.Errorf("f12.0.f12.0.f6: expected %v, got %v", vals(eF122), vals(sF122))
  1119  				}
  1120  			}
  1121  		}
  1122  	}
  1123  	if s.F13 == nil {
  1124  		t.Errorf("f13: got nil")
  1125  	} else if len(*s.F13) != 1 {
  1126  		t.Errorf("f13: expected 1 element, got %v", *s.F13)
  1127  	} else {
  1128  		sF13, eF13 := *(s.F13), *(e.F13)
  1129  		if len(*sF13[0].F13) != 2 {
  1130  			t.Errorf("f13.0.f13: expected 1 element, got %v", *sF13[0].F13)
  1131  		} else {
  1132  			sF132, eF132 := *(*sF13[0].F13)[0].F06, *(*eF13[0].F13)[0].F06
  1133  			if sF132 == nil {
  1134  				t.Errorf("f13.0.f13.0.f6: expected %v, got nil", vals(eF132))
  1135  			} else {
  1136  				if len(sF132) != 2 || *(sF132)[0] != *(eF132)[0] || *(sF132)[1] != *(eF132)[1] {
  1137  					t.Errorf("f13.0.f13.0.f6: expected %v, got %v", vals(eF132), vals(sF132))
  1138  				}
  1139  			}
  1140  			sF132, eF132 = *(*sF13[0].F13)[1].F06, *(*eF13[0].F13)[1].F06
  1141  			if sF132 == nil {
  1142  				t.Errorf("f13.0.f13.0.f6: expected %v, got nil", vals(eF132))
  1143  			} else {
  1144  				if len(sF132) != 2 || *(sF132)[0] != *(eF132)[0] || *(sF132)[1] != *(eF132)[1] {
  1145  					t.Errorf("f13.0.f13.0.f6: expected %v, got %v", vals(eF132), vals(sF132))
  1146  				}
  1147  			}
  1148  		}
  1149  	}
  1150  }
  1151  
  1152  // ----------------------------------------------------------------------------
  1153  
  1154  type S12A struct {
  1155  	ID []int
  1156  }
  1157  
  1158  func TestCSVSlice(t *testing.T) {
  1159  	data := map[string][]string{
  1160  		"ID": {"0,1"},
  1161  	}
  1162  
  1163  	s := S12A{}
  1164  	err := NewDecoder().Decode(&s, data)
  1165  	if err != nil {
  1166  		t.Fatalf("Failed to decode: %v", err)
  1167  	}
  1168  	if len(s.ID) != 2 {
  1169  		t.Errorf("Expected two values in the result list, got %+v", s.ID)
  1170  	}
  1171  	if s.ID[0] != 0 || s.ID[1] != 1 {
  1172  		t.Errorf("Expected []{0, 1} got %+v", s)
  1173  	}
  1174  }
  1175  
  1176  type S12B struct {
  1177  	ID []string
  1178  }
  1179  
  1180  // Decode should not split on , into a slice for string only
  1181  func TestCSVStringSlice(t *testing.T) {
  1182  	data := map[string][]string{
  1183  		"ID": {"0,1"},
  1184  	}
  1185  
  1186  	s := S12B{}
  1187  	err := NewDecoder().Decode(&s, data)
  1188  	if err != nil {
  1189  		t.Fatalf("Failed to decode: %v", err)
  1190  	}
  1191  	if len(s.ID) != 1 {
  1192  		t.Errorf("Expected one value in the result list, got %+v", s.ID)
  1193  	}
  1194  	if s.ID[0] != "0,1" {
  1195  		t.Errorf("Expected []{0, 1} got %+v", s)
  1196  	}
  1197  }
  1198  
  1199  // Invalid data provided by client should not panic (github issue 33)
  1200  func TestInvalidDataProvidedByClient(t *testing.T) {
  1201  	defer func() {
  1202  		if r := recover(); r != nil {
  1203  			t.Errorf("Panicked calling decoder.Decode: %v", r)
  1204  		}
  1205  	}()
  1206  
  1207  	type S struct {
  1208  		f string // nolint:unused
  1209  	}
  1210  
  1211  	data := map[string][]string{
  1212  		"f.f": {"v"},
  1213  	}
  1214  
  1215  	err := NewDecoder().Decode(new(S), data)
  1216  	if err == nil {
  1217  		t.Errorf("invalid path in decoder.Decode should return an error.")
  1218  	}
  1219  }
  1220  
  1221  // underlying cause of error in issue 33
  1222  func TestInvalidPathInCacheParsePath(t *testing.T) {
  1223  	type S struct {
  1224  		f string // nolint:unused
  1225  	}
  1226  
  1227  	typ := reflect.ValueOf(new(S)).Elem().Type()
  1228  	c := newCache()
  1229  	_, err := c.parsePath("f.f", typ)
  1230  	if err == nil {
  1231  		t.Errorf("invalid path in cache.parsePath should return an error.")
  1232  	}
  1233  }
  1234  
  1235  // issue 32
  1236  func TestDecodeToTypedField(t *testing.T) {
  1237  	type Aa bool
  1238  	s1 := &struct{ Aa }{}
  1239  	v1 := map[string][]string{"Aa": {"true"}}
  1240  	err := NewDecoder().Decode(s1, v1)
  1241  	if err != nil {
  1242  		t.Fatalf("Failed to decode: %v", err)
  1243  	}
  1244  	if s1.Aa != Aa(true) {
  1245  		t.Errorf("s1: expected %v, got %v", true, s1.Aa)
  1246  	}
  1247  }
  1248  
  1249  // issue 37
  1250  func TestRegisterConverter(t *testing.T) {
  1251  	type Aa int
  1252  	type Bb int
  1253  	s1 := &struct {
  1254  		Aa
  1255  		Bb
  1256  	}{}
  1257  	decoder := NewDecoder()
  1258  
  1259  	decoder.RegisterConverter(s1.Aa, func(s string) reflect.Value { return reflect.ValueOf(1) })
  1260  	decoder.RegisterConverter(s1.Bb, func(s string) reflect.Value { return reflect.ValueOf(2) })
  1261  
  1262  	v1 := map[string][]string{"Aa": {"4"}, "Bb": {"5"}}
  1263  	err := decoder.Decode(s1, v1)
  1264  	if err != nil {
  1265  		t.Fatalf("Failed to decode: %v", err)
  1266  	}
  1267  
  1268  	if s1.Aa != Aa(1) {
  1269  		t.Errorf("s1.Aa: expected %v, got %v", 1, s1.Aa)
  1270  	}
  1271  	if s1.Bb != Bb(2) {
  1272  		t.Errorf("s1.Bb: expected %v, got %v", 2, s1.Bb)
  1273  	}
  1274  }
  1275  
  1276  // Issue #40
  1277  func TestRegisterConverterSlice(t *testing.T) {
  1278  	decoder := NewDecoder()
  1279  	decoder.RegisterConverter([]string{}, func(input string) reflect.Value {
  1280  		return reflect.ValueOf(strings.Split(input, ","))
  1281  	})
  1282  
  1283  	result := struct {
  1284  		Multiple []string `schema:"multiple"`
  1285  	}{}
  1286  
  1287  	expected := []string{"one", "two", "three"}
  1288  	err := decoder.Decode(&result, map[string][]string{
  1289  		"multiple": []string{"one,two,three"},
  1290  	})
  1291  	if err != nil {
  1292  		t.Fatalf("Failed to decode: %v", err)
  1293  	}
  1294  	for i := range expected {
  1295  		if got, want := expected[i], result.Multiple[i]; got != want {
  1296  			t.Errorf("%d: got %s, want %s", i, got, want)
  1297  		}
  1298  	}
  1299  }
  1300  
  1301  func TestRegisterConverterMap(t *testing.T) {
  1302  	decoder := NewDecoder()
  1303  	decoder.IgnoreUnknownKeys(false)
  1304  	decoder.RegisterConverter(map[string]string{}, func(input string) reflect.Value {
  1305  		m := make(map[string]string)
  1306  		for _, pair := range strings.Split(input, ",") {
  1307  			parts := strings.Split(pair, ":")
  1308  			switch len(parts) {
  1309  			case 2:
  1310  				m[parts[0]] = parts[1]
  1311  			}
  1312  		}
  1313  		return reflect.ValueOf(m)
  1314  	})
  1315  
  1316  	result := struct {
  1317  		Multiple map[string]string `schema:"multiple"`
  1318  	}{}
  1319  
  1320  	err := decoder.Decode(&result, map[string][]string{
  1321  		"multiple": []string{"a:one,b:two"},
  1322  	})
  1323  	if err != nil {
  1324  		t.Fatal(err)
  1325  	}
  1326  	expected := map[string]string{"a": "one", "b": "two"}
  1327  	for k, v := range expected {
  1328  		got, ok := result.Multiple[k]
  1329  		if !ok {
  1330  			t.Fatalf("got %v, want %v", result.Multiple, expected)
  1331  		}
  1332  		if got != v {
  1333  			t.Errorf("got %s, want %s", got, v)
  1334  		}
  1335  	}
  1336  }
  1337  
  1338  type S13 struct {
  1339  	Value []S14
  1340  }
  1341  
  1342  type S14 struct {
  1343  	F1 string
  1344  	F2 string
  1345  }
  1346  
  1347  func (n *S14) UnmarshalText(text []byte) error {
  1348  	textParts := strings.Split(string(text), " ")
  1349  	if len(textParts) < 2 {
  1350  		return errors.New("Not a valid name!")
  1351  	}
  1352  
  1353  	n.F1, n.F2 = textParts[0], textParts[len(textParts)-1]
  1354  	return nil
  1355  }
  1356  
  1357  type S15 struct {
  1358  	Value []S16
  1359  }
  1360  
  1361  type S16 struct {
  1362  	F1 string
  1363  	F2 string
  1364  }
  1365  
  1366  func TestCustomTypeSlice(t *testing.T) {
  1367  	data := map[string][]string{
  1368  		"Value.0": []string{"Louisa May Alcott"},
  1369  		"Value.1": []string{"Florence Nightingale"},
  1370  		"Value.2": []string{"Clara Barton"},
  1371  	}
  1372  
  1373  	s := S13{}
  1374  	decoder := NewDecoder()
  1375  
  1376  	if err := decoder.Decode(&s, data); err != nil {
  1377  		t.Fatal(err)
  1378  	}
  1379  
  1380  	if len(s.Value) != 3 {
  1381  		t.Fatalf("Expected 3 values in the result list, got %+v", s.Value)
  1382  	}
  1383  	if s.Value[0].F1 != "Louisa" || s.Value[0].F2 != "Alcott" {
  1384  		t.Errorf("Expected S14{'Louisa', 'Alcott'} got %+v", s.Value[0])
  1385  	}
  1386  	if s.Value[1].F1 != "Florence" || s.Value[1].F2 != "Nightingale" {
  1387  		t.Errorf("Expected S14{'Florence', 'Nightingale'} got %+v", s.Value[1])
  1388  	}
  1389  	if s.Value[2].F1 != "Clara" || s.Value[2].F2 != "Barton" {
  1390  		t.Errorf("Expected S14{'Clara', 'Barton'} got %+v", s.Value[2])
  1391  	}
  1392  }
  1393  
  1394  func TestCustomTypeSliceWithError(t *testing.T) {
  1395  	data := map[string][]string{
  1396  		"Value.0": []string{"Louisa May Alcott"},
  1397  		"Value.1": []string{"Florence Nightingale"},
  1398  		"Value.2": []string{"Clara"},
  1399  	}
  1400  
  1401  	s := S13{}
  1402  	decoder := NewDecoder()
  1403  
  1404  	if err := decoder.Decode(&s, data); err == nil {
  1405  		t.Error("Not detecting error in conversion")
  1406  	}
  1407  }
  1408  
  1409  func TestNoTextUnmarshalerTypeSlice(t *testing.T) {
  1410  	data := map[string][]string{
  1411  		"Value.0": []string{"Louisa May Alcott"},
  1412  		"Value.1": []string{"Florence Nightingale"},
  1413  		"Value.2": []string{"Clara Barton"},
  1414  	}
  1415  
  1416  	s := S15{}
  1417  	decoder := NewDecoder()
  1418  
  1419  	if err := decoder.Decode(&s, data); err == nil {
  1420  		t.Error("Not detecting when there's no converter")
  1421  	}
  1422  }
  1423  
  1424  // ----------------------------------------------------------------------------
  1425  
  1426  type S17 struct {
  1427  	Value S14
  1428  }
  1429  
  1430  type S18 struct {
  1431  	Value S16
  1432  }
  1433  
  1434  func TestCustomType(t *testing.T) {
  1435  	data := map[string][]string{
  1436  		"Value": []string{"Louisa May Alcott"},
  1437  	}
  1438  
  1439  	s := S17{}
  1440  	decoder := NewDecoder()
  1441  
  1442  	if err := decoder.Decode(&s, data); err != nil {
  1443  		t.Fatal(err)
  1444  	}
  1445  
  1446  	if s.Value.F1 != "Louisa" || s.Value.F2 != "Alcott" {
  1447  		t.Errorf("Expected S14{'Louisa', 'Alcott'} got %+v", s.Value)
  1448  	}
  1449  }
  1450  
  1451  func TestCustomTypeWithError(t *testing.T) {
  1452  	data := map[string][]string{
  1453  		"Value": []string{"Louisa"},
  1454  	}
  1455  
  1456  	s := S17{}
  1457  	decoder := NewDecoder()
  1458  
  1459  	if err := decoder.Decode(&s, data); err == nil {
  1460  		t.Error("Not detecting error in conversion")
  1461  	}
  1462  }
  1463  
  1464  func TestNoTextUnmarshalerType(t *testing.T) {
  1465  	data := map[string][]string{
  1466  		"Value": []string{"Louisa May Alcott"},
  1467  	}
  1468  
  1469  	s := S18{}
  1470  	decoder := NewDecoder()
  1471  
  1472  	if err := decoder.Decode(&s, data); err == nil {
  1473  		t.Error("Not detecting when there's no converter")
  1474  	}
  1475  }
  1476  
  1477  func TestExpectedType(t *testing.T) {
  1478  	data := map[string][]string{
  1479  		"bools":   []string{"1", "a"},
  1480  		"date":    []string{"invalid"},
  1481  		"Foo.Bar": []string{"a", "b"},
  1482  	}
  1483  
  1484  	type B struct {
  1485  		Bar *int
  1486  	}
  1487  	type A struct {
  1488  		Bools []bool    `schema:"bools"`
  1489  		Date  time.Time `schema:"date"`
  1490  		Foo   B
  1491  	}
  1492  
  1493  	a := A{}
  1494  
  1495  	err := NewDecoder().Decode(&a, data)
  1496  
  1497  	e := err.(MultiError)["bools"].(ConversionError)
  1498  	if e.Type != reflect.TypeOf(false) && e.Index == 1 {
  1499  		t.Errorf("Expected bool, index: 1 got %+v, index: %d", e.Type, e.Index)
  1500  	}
  1501  	e = err.(MultiError)["date"].(ConversionError)
  1502  	if e.Type != reflect.TypeOf(time.Time{}) {
  1503  		t.Errorf("Expected time.Time got %+v", e.Type)
  1504  	}
  1505  	e = err.(MultiError)["Foo.Bar"].(ConversionError)
  1506  	if e.Type != reflect.TypeOf(0) {
  1507  		t.Errorf("Expected int got %+v", e.Type)
  1508  	}
  1509  }
  1510  
  1511  type R1 struct {
  1512  	A string `schema:"a,required"`
  1513  	B struct {
  1514  		C int     `schema:"c,required"`
  1515  		D float64 `schema:"d"`
  1516  		E string  `schema:"e,required"`
  1517  	} `schema:"b"`
  1518  	F []string `schema:"f,required"`
  1519  	G []int    `schema:"g,othertag"`
  1520  	H bool     `schema:"h,required"`
  1521  }
  1522  
  1523  func TestRequiredField(t *testing.T) {
  1524  	var a R1
  1525  	v := map[string][]string{
  1526  		"a":   []string{"bbb"},
  1527  		"b.c": []string{"88"},
  1528  		"b.d": []string{"9"},
  1529  		"f":   []string{""},
  1530  		"h":   []string{"true"},
  1531  	}
  1532  	err := NewDecoder().Decode(&a, v)
  1533  	if err == nil {
  1534  		t.Errorf("error nil, b.e is empty expect")
  1535  		return
  1536  	}
  1537  	// b.e empty
  1538  	v["b.e"] = []string{""} // empty string
  1539  	err = NewDecoder().Decode(&a, v)
  1540  	if err == nil {
  1541  		t.Errorf("error nil, b.e is empty expect")
  1542  		return
  1543  	}
  1544  	if expected := `b.e is empty`; err.Error() != expected {
  1545  		t.Errorf("got %q, want %q", err, expected)
  1546  	}
  1547  
  1548  	// all fields ok
  1549  	v["b.e"] = []string{"nonempty"}
  1550  	err = NewDecoder().Decode(&a, v)
  1551  	if err != nil {
  1552  		t.Errorf("error: %v", err)
  1553  		return
  1554  	}
  1555  
  1556  	// set f empty
  1557  	v["f"] = []string{}
  1558  	err = NewDecoder().Decode(&a, v)
  1559  	if err == nil {
  1560  		t.Errorf("error nil, f is empty expect")
  1561  		return
  1562  	}
  1563  	if expected := `f is empty`; err.Error() != expected {
  1564  		t.Errorf("got %q, want %q", err, expected)
  1565  	}
  1566  	v["f"] = []string{"nonempty"}
  1567  
  1568  	// b.c type int with empty string
  1569  	v["b.c"] = []string{""}
  1570  	err = NewDecoder().Decode(&a, v)
  1571  	if err == nil {
  1572  		t.Errorf("error nil, b.c is empty expect")
  1573  		return
  1574  	}
  1575  	v["b.c"] = []string{"3"}
  1576  
  1577  	// h type bool with empty string
  1578  	v["h"] = []string{""}
  1579  	err = NewDecoder().Decode(&a, v)
  1580  	if err == nil {
  1581  		t.Errorf("error nil, h is empty expect")
  1582  		return
  1583  	}
  1584  	if expected := `h is empty`; err.Error() != expected {
  1585  		t.Errorf("got %q, want %q", err, expected)
  1586  	}
  1587  }
  1588  
  1589  type R2 struct {
  1590  	A struct {
  1591  		B int `schema:"b"`
  1592  	} `schema:"a,required"`
  1593  }
  1594  
  1595  func TestRequiredStructFiled(t *testing.T) {
  1596  	v := map[string][]string{
  1597  		"a.b": []string{"3"},
  1598  	}
  1599  	var a R2
  1600  	err := NewDecoder().Decode(&a, v)
  1601  	if err != nil {
  1602  		t.Errorf("error: %v", err)
  1603  	}
  1604  }
  1605  
  1606  func TestRequiredFieldIsMissingCorrectError(t *testing.T) {
  1607  	type RM1S struct {
  1608  		A string `schema:"rm1aa,required"`
  1609  		B string `schema:"rm1bb,required"`
  1610  	}
  1611  	type RM1 struct {
  1612  		RM1S
  1613  	}
  1614  
  1615  	var a RM1
  1616  	v := map[string][]string{
  1617  		"rm1aa": {"aaa"},
  1618  	}
  1619  	expectedError := "RM1S.rm1bb is empty"
  1620  	err := NewDecoder().Decode(&a, v)
  1621  	if err.Error() != expectedError {
  1622  		t.Errorf("expected %v, got %v", expectedError, err)
  1623  	}
  1624  }
  1625  
  1626  type AS1 struct {
  1627  	A int32 `schema:"a,required"`
  1628  	E int32 `schema:"e,required"`
  1629  }
  1630  type AS2 struct {
  1631  	AS1
  1632  	B string `schema:"b,required"`
  1633  }
  1634  type AS3 struct {
  1635  	C int32 `schema:"c"`
  1636  }
  1637  
  1638  type AS4 struct {
  1639  	AS3
  1640  	D string `schema:"d"`
  1641  }
  1642  
  1643  func TestAnonymousStructField(t *testing.T) {
  1644  	patterns := []map[string][]string{
  1645  		{
  1646  			"a": {"1"},
  1647  			"e": {"2"},
  1648  			"b": {"abc"},
  1649  		},
  1650  		{
  1651  			"AS1.a": {"1"},
  1652  			"AS1.e": {"2"},
  1653  			"b":     {"abc"},
  1654  		},
  1655  	}
  1656  	for _, v := range patterns {
  1657  		a := AS2{}
  1658  		err := NewDecoder().Decode(&a, v)
  1659  		if err != nil {
  1660  			t.Errorf("Decode failed %s, %#v", err, v)
  1661  			continue
  1662  		}
  1663  		if a.A != 1 {
  1664  			t.Errorf("A: expected %v, got %v", 1, a.A)
  1665  		}
  1666  		if a.E != 2 {
  1667  			t.Errorf("E: expected %v, got %v", 2, a.E)
  1668  		}
  1669  		if a.B != "abc" {
  1670  			t.Errorf("B: expected %v, got %v", "abc", a.B)
  1671  		}
  1672  		if a.AS1.A != 1 {
  1673  			t.Errorf("AS1.A: expected %v, got %v", 1, a.AS1.A)
  1674  		}
  1675  		if a.AS1.E != 2 {
  1676  			t.Errorf("AS1.E: expected %v, got %v", 2, a.AS1.E)
  1677  		}
  1678  	}
  1679  	a := AS2{}
  1680  	err := NewDecoder().Decode(&a, map[string][]string{
  1681  		"e": {"2"},
  1682  		"b": {"abc"},
  1683  	})
  1684  	if err == nil {
  1685  		t.Errorf("error nil, a is empty expect")
  1686  	}
  1687  	patterns = []map[string][]string{
  1688  		{
  1689  			"c": {"1"},
  1690  			"d": {"abc"},
  1691  		},
  1692  		{
  1693  			"AS3.c": {"1"},
  1694  			"d":     {"abc"},
  1695  		},
  1696  	}
  1697  	for _, v := range patterns {
  1698  		a := AS4{}
  1699  		err := NewDecoder().Decode(&a, v)
  1700  		if err != nil {
  1701  			t.Errorf("Decode failed %s, %#v", err, v)
  1702  			continue
  1703  		}
  1704  		if a.C != 1 {
  1705  			t.Errorf("C: expected %v, got %v", 1, a.C)
  1706  		}
  1707  		if a.D != "abc" {
  1708  			t.Errorf("D: expected %v, got %v", "abc", a.D)
  1709  		}
  1710  		if a.AS3.C != 1 {
  1711  			t.Errorf("AS3.C: expected %v, got %v", 1, a.AS3.C)
  1712  		}
  1713  	}
  1714  }
  1715  
  1716  func TestAmbiguousStructField(t *testing.T) {
  1717  	type I1 struct {
  1718  		X int
  1719  	}
  1720  	type I2 struct {
  1721  		I1
  1722  	}
  1723  	type B1 struct {
  1724  		X bool
  1725  	}
  1726  	type B2 struct {
  1727  		B1
  1728  	}
  1729  	type IB struct {
  1730  		I1
  1731  		B1
  1732  	}
  1733  	type S struct {
  1734  		I1
  1735  		I2
  1736  		B1
  1737  		B2
  1738  		IB
  1739  	}
  1740  	dst := S{}
  1741  	src := map[string][]string{
  1742  		"X":    {"123"},
  1743  		"IB.X": {"123"},
  1744  	}
  1745  	dec := NewDecoder()
  1746  	dec.IgnoreUnknownKeys(false)
  1747  	err := dec.Decode(&dst, src)
  1748  	e, ok := err.(MultiError)
  1749  	if !ok || len(e) != 2 {
  1750  		t.Errorf("Expected 2 errors, got %#v", err)
  1751  	}
  1752  	if expected := (UnknownKeyError{Key: "X"}); e["X"] != expected {
  1753  		t.Errorf("X: expected %#v, got %#v", expected, e["X"])
  1754  	}
  1755  	if expected := (UnknownKeyError{Key: "IB.X"}); e["IB.X"] != expected {
  1756  		t.Errorf("X: expected %#v, got %#v", expected, e["IB.X"])
  1757  	}
  1758  	dec.IgnoreUnknownKeys(true)
  1759  	err = dec.Decode(&dst, src)
  1760  	if err != nil {
  1761  		t.Errorf("Decode failed %v", err)
  1762  	}
  1763  
  1764  	expected := S{
  1765  		I1: I1{X: 123},
  1766  		I2: I2{I1: I1{X: 234}},
  1767  		B1: B1{X: true},
  1768  		B2: B2{B1: B1{X: true}},
  1769  		IB: IB{I1: I1{X: 345}, B1: B1{X: true}},
  1770  	}
  1771  	patterns := []map[string][]string{
  1772  		{
  1773  			"I1.X":    {"123"},
  1774  			"I2.X":    {"234"},
  1775  			"B1.X":    {"true"},
  1776  			"B2.X":    {"1"},
  1777  			"IB.I1.X": {"345"},
  1778  			"IB.B1.X": {"on"},
  1779  		},
  1780  		{
  1781  			"I1.X":    {"123"},
  1782  			"I2.I1.X": {"234"},
  1783  			"B1.X":    {"true"},
  1784  			"B2.B1.X": {"1"},
  1785  			"IB.I1.X": {"345"},
  1786  			"IB.B1.X": {"on"},
  1787  		},
  1788  	}
  1789  	for _, src := range patterns {
  1790  		dst := S{}
  1791  		dec := NewDecoder()
  1792  		dec.IgnoreUnknownKeys(false)
  1793  		err := dec.Decode(&dst, src)
  1794  		if err != nil {
  1795  			t.Errorf("Decode failed %v, %#v", err, src)
  1796  		}
  1797  		if !reflect.DeepEqual(expected, dst) {
  1798  			t.Errorf("Expected %+v, got %+v", expected, dst)
  1799  		}
  1800  	}
  1801  }
  1802  
  1803  func TestComprehensiveDecodingErrors(t *testing.T) {
  1804  	type I1 struct {
  1805  		V int  `schema:",required"`
  1806  		P *int `schema:",required"`
  1807  	}
  1808  	type I2 struct {
  1809  		I1
  1810  		J I1
  1811  	}
  1812  	type S1 struct {
  1813  		V string  `schema:"v,required"`
  1814  		P *string `schema:"p,required"`
  1815  	}
  1816  	type S2 struct {
  1817  		S1 `schema:"s"`
  1818  		T  S1 `schema:"t"`
  1819  	}
  1820  	type D struct {
  1821  		I2
  1822  		X S2 `schema:"x"`
  1823  		Y S2 `schema:"-"`
  1824  	}
  1825  	patterns := []map[string][]string{
  1826  		{
  1827  			"V":       {"invalid"}, // invalid
  1828  			"I2.I1.P": {},          // empty
  1829  			"I2.J.V":  {""},        // empty
  1830  			"I2.J.P":  {"123"},     // ok
  1831  			"x.s.v":   {""},        // empty
  1832  			"x.s.p":   {""},        // ok
  1833  			"x.t.v":   {"abc"},     // ok
  1834  			"x.t.p":   {},          // empty
  1835  			"Y.s.v":   {"ignored"}, // unknown
  1836  		},
  1837  		{
  1838  			"V":     {"invalid"}, // invalid
  1839  			"P":     {},          // empty
  1840  			"J.V":   {""},        // empty
  1841  			"J.P":   {"123"},     // ok
  1842  			"x.v":   {""},        // empty
  1843  			"x.p":   {""},        // ok
  1844  			"x.t.v": {"abc"},     // ok
  1845  			"x.t.p": {},          // empty
  1846  			"Y.s.v": {"ignored"}, // unknown
  1847  		},
  1848  	}
  1849  	for _, src := range patterns {
  1850  		dst := D{}
  1851  		dec := NewDecoder()
  1852  		dec.IgnoreUnknownKeys(false)
  1853  		err := dec.Decode(&dst, src)
  1854  		e, ok := err.(MultiError)
  1855  		if !ok || len(e) != 6 {
  1856  			t.Errorf("Expected 6 errors, got %#v", err)
  1857  		}
  1858  		if cerr, ok := e["V"].(ConversionError); !ok {
  1859  			t.Errorf("%s: expected %#v, got %#v", "I2.I1.V", ConversionError{Key: "V"}, cerr)
  1860  		}
  1861  		if key, expected := "I2.I1.P", (EmptyFieldError{Key: "I2.I1.P"}); e[key] != expected {
  1862  			t.Errorf("%s: expected %#v, got %#v", key, expected, e[key])
  1863  		}
  1864  		if key, expected := "I2.J.V", (EmptyFieldError{Key: "I2.J.V"}); e[key] != expected {
  1865  			t.Errorf("%s: expected %#v, got %#v", key, expected, e[key])
  1866  		}
  1867  		if key, expected := "x.s.v", (EmptyFieldError{Key: "x.s.v"}); e[key] != expected {
  1868  			t.Errorf("%s: expected %#v, got %#v", key, expected, e[key])
  1869  		}
  1870  		if key, expected := "x.t.p", (EmptyFieldError{Key: "x.t.p"}); e[key] != expected {
  1871  			t.Errorf("%s: expected %#v, got %#v", key, expected, e[key])
  1872  		}
  1873  		if key, expected := "Y.s.v", (UnknownKeyError{Key: "Y.s.v"}); e[key] != expected {
  1874  			t.Errorf("%s: expected %#v, got %#v", key, expected, e[key])
  1875  		}
  1876  		if expected := 123; dst.I2.J.P == nil || *dst.I2.J.P != expected {
  1877  			t.Errorf("I2.J.P: expected %#v, got %#v", expected, dst.I2.J.P)
  1878  		}
  1879  		if expected := ""; dst.X.S1.P == nil || *dst.X.S1.P != expected {
  1880  			t.Errorf("X.S1.P: expected %#v, got %#v", expected, dst.X.S1.P)
  1881  		}
  1882  		if expected := "abc"; dst.X.T.V != expected {
  1883  			t.Errorf("X.T.V: expected %#v, got %#v", expected, dst.X.T.V)
  1884  		}
  1885  	}
  1886  }
  1887  
  1888  // Test to ensure that a registered converter overrides the default text unmarshaler.
  1889  func TestRegisterConverterOverridesTextUnmarshaler(t *testing.T) {
  1890  	type MyTime time.Time
  1891  	s1 := &struct {
  1892  		MyTime
  1893  	}{}
  1894  	decoder := NewDecoder()
  1895  
  1896  	ts := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
  1897  	decoder.RegisterConverter(s1.MyTime, func(s string) reflect.Value { return reflect.ValueOf(ts) })
  1898  
  1899  	v1 := map[string][]string{"MyTime": {"4"}}
  1900  	err := decoder.Decode(s1, v1)
  1901  	if err != nil {
  1902  		t.Fatalf("Failed to decode: %v", err)
  1903  	}
  1904  
  1905  	if s1.MyTime != MyTime(ts) {
  1906  		t.Errorf("s1.Aa: expected %v, got %v", ts, s1.MyTime)
  1907  	}
  1908  }
  1909  
  1910  type S20E string
  1911  
  1912  func (e *S20E) UnmarshalText(text []byte) error {
  1913  	*e = S20E("x")
  1914  	return nil
  1915  }
  1916  
  1917  type S20 []S20E
  1918  
  1919  func (s *S20) UnmarshalText(text []byte) error {
  1920  	*s = S20{"a", "b", "c"}
  1921  	return nil
  1922  }
  1923  
  1924  // Test to ensure that when a custom type based on a slice implements an
  1925  // encoding.TextUnmarshaler interface that it takes precedence over any
  1926  // implementations by its elements.
  1927  func TestTextUnmarshalerTypeSlice(t *testing.T) {
  1928  	data := map[string][]string{
  1929  		"Value": []string{"a,b,c"},
  1930  	}
  1931  	s := struct {
  1932  		Value S20
  1933  	}{}
  1934  	decoder := NewDecoder()
  1935  	if err := decoder.Decode(&s, data); err != nil {
  1936  		t.Fatal("Error while decoding:", err)
  1937  	}
  1938  	expected := S20{"a", "b", "c"}
  1939  	if !reflect.DeepEqual(expected, s.Value) {
  1940  		t.Errorf("Expected %v errors, got %v", expected, s.Value)
  1941  	}
  1942  }
  1943  
  1944  type S21E struct{ ElementValue string }
  1945  
  1946  func (e *S21E) UnmarshalText(text []byte) error {
  1947  	*e = S21E{"x"}
  1948  	return nil
  1949  }
  1950  
  1951  type S21 []S21E
  1952  
  1953  func (s *S21) UnmarshalText(text []byte) error {
  1954  	*s = S21{{"a"}}
  1955  	return nil
  1956  }
  1957  
  1958  type S21B []S21E
  1959  
  1960  // Test to ensure that if custom type base on a slice of structs implements an
  1961  // encoding.TextUnmarshaler interface it is unaffected by the special path
  1962  // requirements imposed on a slice of structs.
  1963  func TestTextUnmarshalerTypeSliceOfStructs(t *testing.T) {
  1964  	data := map[string][]string{
  1965  		"Value": []string{"raw a"},
  1966  	}
  1967  	// Implements encoding.TextUnmarshaler, should not throw invalid path
  1968  	// error.
  1969  	s := struct {
  1970  		Value S21
  1971  	}{}
  1972  	decoder := NewDecoder()
  1973  	if err := decoder.Decode(&s, data); err != nil {
  1974  		t.Fatal("Error while decoding:", err)
  1975  	}
  1976  	expected := S21{{"a"}}
  1977  	if !reflect.DeepEqual(expected, s.Value) {
  1978  		t.Errorf("Expected %v errors, got %v", expected, s.Value)
  1979  	}
  1980  	// Does not implement encoding.TextUnmarshaler, should throw invalid
  1981  	// path error.
  1982  	sb := struct {
  1983  		Value S21B
  1984  	}{}
  1985  	if err := decoder.Decode(&sb, data); err == errInvalidPath {
  1986  		t.Fatal("Expecting invalid path error", err)
  1987  	}
  1988  }
  1989  
  1990  type S22 string
  1991  
  1992  func (s *S22) UnmarshalText(text []byte) error {
  1993  	*s = S22("a")
  1994  	return nil
  1995  }
  1996  
  1997  // Test to ensure that when a field that should be decoded into a type
  1998  // implementing the encoding.TextUnmarshaler interface is set to an empty value
  1999  // that the UnmarshalText method is utilized over other methods of decoding,
  2000  // especially including simply setting the zero value.
  2001  func TestTextUnmarshalerEmpty(t *testing.T) {
  2002  	data := map[string][]string{
  2003  		"Value": []string{""}, // empty value
  2004  	}
  2005  	// Implements encoding.TextUnmarshaler, should use the type's
  2006  	// UnmarshalText method.
  2007  	s := struct {
  2008  		Value S22
  2009  	}{}
  2010  	decoder := NewDecoder()
  2011  	if err := decoder.Decode(&s, data); err != nil {
  2012  		t.Fatal("Error while decoding:", err)
  2013  	}
  2014  	expected := S22("a")
  2015  	if expected != s.Value {
  2016  		t.Errorf("Expected %v errors, got %v", expected, s.Value)
  2017  	}
  2018  }
  2019  
  2020  type S23n struct {
  2021  	F2 string `schema:"F2"`
  2022  	F3 string `schema:"F3"`
  2023  }
  2024  
  2025  type S23e struct {
  2026  	*S23n
  2027  	F1 string `schema:"F1"`
  2028  }
  2029  
  2030  type S23 []*S23e
  2031  
  2032  func TestUnmashalPointerToEmbedded(t *testing.T) {
  2033  	data := map[string][]string{
  2034  		"A.0.F2": []string{"raw a"},
  2035  		"A.0.F3": []string{"raw b"},
  2036  	}
  2037  
  2038  	// Implements encoding.TextUnmarshaler, should not throw invalid path
  2039  	// error.
  2040  	s := struct {
  2041  		Value S23 `schema:"A"`
  2042  	}{}
  2043  	decoder := NewDecoder()
  2044  
  2045  	if err := decoder.Decode(&s, data); err != nil {
  2046  		t.Fatal("Error while decoding:", err)
  2047  	}
  2048  
  2049  	expected := S23{
  2050  		&S23e{
  2051  			S23n: &S23n{"raw a", "raw b"},
  2052  		},
  2053  	}
  2054  	if !reflect.DeepEqual(expected, s.Value) {
  2055  		t.Errorf("Expected %v errors, got %v", expected, s.Value)
  2056  	}
  2057  }