2016-12-02 01:20:09 +01:00
|
|
|
// Copyright 2016 Marapongo, Inc. All rights reserved.
|
|
|
|
|
|
|
|
package encoding
|
|
|
|
|
2016-12-02 22:23:18 +01:00
|
|
|
// StringSlice checks a weakly typed interface ptr to see if it's a []string; if yes, the resulting converted array
|
2016-12-02 01:20:09 +01:00
|
|
|
// is returned with a "true"; otherwise, nil with a "false" is returned.
|
2016-12-02 22:23:18 +01:00
|
|
|
func StringSlice(i interface{}) ([]string, bool) {
|
2016-12-02 01:20:09 +01:00
|
|
|
// First try a direct conversion.
|
|
|
|
if s, ok := i.([]string); ok {
|
|
|
|
return s, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, see if it's an untyped array, and then convert each element.
|
|
|
|
if a, ok := i.([]interface{}); ok {
|
|
|
|
ss := make([]string, 0, len(a))
|
|
|
|
for _, e := range a {
|
|
|
|
if s, ok := e.(string); ok {
|
|
|
|
ss = append(ss, s)
|
|
|
|
} else {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ss, true
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, false
|
|
|
|
}
|