Skip to content

Commit

Permalink
Allow []string to be converted to *int64
Browse files Browse the repository at this point in the history
Not something we hit before
  • Loading branch information
smarterclayton committed Sep 21, 2015
1 parent 9f93dce commit a02bcef
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pkg/conversion/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,12 @@ func (c *Converter) defaultConvert(sv, dv reflect.Value, scope *scope) error {
return nil
}
dv.Set(reflect.New(dt.Elem()))
return c.convert(sv.Elem(), dv.Elem(), scope)
switch st.Kind() {
case reflect.Ptr, reflect.Interface:
return c.convert(sv.Elem(), dv.Elem(), scope)
default:
return c.convert(sv, dv.Elem(), scope)
}
case reflect.Map:
if sv.IsNil() {
// Don't copy a nil ptr!
Expand Down
28 changes: 28 additions & 0 deletions pkg/conversion/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,34 @@ func TestConverter_byteSlice(t *testing.T) {
}
}

func TestConverter_MismatchedTypes(t *testing.T) {
c := NewConverter()

err := c.RegisterConversionFunc(
func(in *[]string, out *int, s Scope) error {
if str, err := strconv.Atoi((*in)[0]); err != nil {
return err
} else {
*out = str
return nil
}
},
)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

src := []string{"5"}
var dest *int
err = c.Convert(&src, &dest, 0, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if e, a := 5, *dest; e != a {
t.Errorf("expected %#v, got %#v", e, a)
}
}

func TestConverter_DefaultConvert(t *testing.T) {
type A struct {
Foo string
Expand Down

0 comments on commit a02bcef

Please sign in to comment.