@@ -1615,3 +1615,44 @@ func TestDecodeArrayBug(t *testing.T) {
16151615 Equal (t , data .G [1 ], "" )
16161616 Equal (t , data .G [2 ], "20" )
16171617}
1618+
1619+ func TestDecoder_RegisterCustomTypeFuncOnSlice (t * testing.T ) {
1620+ type customString string
1621+
1622+ type TestStruct struct {
1623+ Slice []customString `form:"slice"`
1624+ }
1625+
1626+ d := NewDecoder ()
1627+ d .RegisterCustomTypeFunc (func (vals []string ) (i interface {}, e error ) {
1628+ custom := make ([]customString , 0 , len (vals ))
1629+ for i := 0 ; i < len (vals ); i ++ {
1630+ custom = append (custom , customString ("custom" + vals [i ]))
1631+ }
1632+ return custom , nil
1633+ }, []customString {})
1634+
1635+ var v TestStruct
1636+ err := d .Decode (& v , url.Values {"slice" : []string {"v1" , "v2" }})
1637+ Equal (t , err , nil )
1638+ Equal (t , v .Slice , []customString {"customv1" , "customv2" })
1639+ }
1640+
1641+ func TestDecoder_RegisterCustomTypeFunc (t * testing.T ) {
1642+ type customString string
1643+
1644+ type TestStruct struct {
1645+ Slice []customString `form:"slice"`
1646+ }
1647+
1648+ d := NewDecoder ()
1649+ d .RegisterCustomTypeFunc (func (vals []string ) (i interface {}, e error ) {
1650+ return customString ("custom" + vals [0 ]), nil
1651+ }, customString ("" ))
1652+
1653+ var v TestStruct
1654+ err := d .Decode (& v , url.Values {"slice" : []string {"v1" , "v2" }})
1655+ Equal (t , err , nil )
1656+
1657+ Equal (t , v .Slice , []customString {"customv1" , "customv2" })
1658+ }
0 commit comments