-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
string.go
42 lines (39 loc) · 845 Bytes
/
string.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package strings
import "github.com/plgd-dev/hub/v2/pkg/config/property/urischeme"
func ToString(v interface{}) (string, bool) {
if v == nil {
return "", false
}
val, ok := v.(string)
return val, ok
}
func ToStringArray(v interface{}) ([]string, bool) {
switch val := v.(type) {
case []string:
return val, true
case urischeme.URIScheme:
return []string{string(val)}, true
case []urischeme.URIScheme:
var result []string
for _, v := range val {
result = append(result, string(v))
}
return result, true
case []interface{}:
var result []string
for _, v := range val {
if s, ok := ToString(v); ok {
result = append(result, s)
}
}
if len(val) != len(result) {
return nil, false
}
return result, true
case string:
return []string{val}, true
case nil:
return nil, true
}
return nil, false
}