Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing conv.Join to handle non-interface{} arrays #226

Merged
merged 1 commit into from
Nov 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 32 additions & 15 deletions conv/conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ func Join(in interface{}, sep string) string {

var a []interface{}
a, ok = in.([]interface{})
if !ok {
var err error
a, err = interfaceSlice(in)
ok = err == nil
}
if ok {
b := make([]string, len(a))
for i := range a {
Expand All @@ -48,6 +53,18 @@ func Join(in interface{}, sep string) string {
return ""
}

func interfaceSlice(slice interface{}) ([]interface{}, error) {
s := reflect.ValueOf(slice)
if s.Kind() != reflect.Slice {
return nil, fmt.Errorf("interfaceSlice given a non-slice type %T", s)
}
ret := make([]interface{}, s.Len())
for i := 0; i < s.Len(); i++ {
ret[i] = s.Index(i).Interface()
}
return ret, nil
}

// Has determines whether or not a given object has a property with the given key
func Has(in interface{}, key string) bool {
av := reflect.ValueOf(in)
Expand All @@ -61,28 +78,28 @@ func Has(in interface{}, key string) bool {
}

func toString(in interface{}) string {
if in == nil {
return "nil"
}
if s, ok := in.(string); ok {
return s
}
if s, ok := in.(fmt.Stringer); ok {
return s.String()
}
if i, ok := in.(int); ok {
return strconv.Itoa(i)
}
if u, ok := in.(uint64); ok {
return strconv.FormatUint(u, 10)
}
if f, ok := in.(float64); ok {
return strconv.FormatFloat(f, 'f', -1, 64)
}
if b, ok := in.(bool); ok {
return strconv.FormatBool(b)
}
if in == nil {
return "nil"
val := reflect.Indirect(reflect.ValueOf(in))
switch val.Kind() {
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
return strconv.FormatInt(val.Int(), 10)
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint, reflect.Uint64:
return strconv.FormatUint(val.Uint(), 10)
case reflect.Float32, reflect.Float64:
return strconv.FormatFloat(val.Float(), 'f', -1, 64)
case reflect.Bool:
return strconv.FormatBool(val.Bool())
default:
return fmt.Sprintf("%s", in)
}
return fmt.Sprintf("%s", in)
}

// MustParseInt - wrapper for strconv.ParseInt that returns 0 in the case of error
Expand Down
5 changes: 5 additions & 0 deletions conv/conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ func TestJoin(t *testing.T) {
assert.Equal(t, "foo,\nbar", Join([]interface{}{"foo", "bar"}, ",\n"))
// Join handles all kinds of scalar types too...
assert.Equal(t, "42-18446744073709551615", Join([]interface{}{42, uint64(18446744073709551615)}, "-"))
assert.Equal(t, "42,100", Join([]int{42, 100}, ","))
assert.Equal(t, "42,100", Join([]int64{42, 100}, ","))
assert.Equal(t, "42,100", Join([]uint64{42, 100}, ","))
assert.Equal(t, "true,false", Join([]bool{true, false}, ","))
assert.Equal(t, "1,2", Join([]float64{1, 2}, ","))
assert.Equal(t, "1,,true,3.14,foo,nil", Join([]interface{}{1, "", true, 3.14, "foo", nil}, ","))
// and best-effort with weird types
assert.Equal(t, "[foo],bar", Join([]interface{}{[]string{"foo"}, "bar"}, ","))
Expand Down