-
Notifications
You must be signed in to change notification settings - Fork 288
/
string.go
47 lines (40 loc) · 887 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
43
44
45
46
47
package value
import "go.starlark.net/starlark"
type ImplicitStringer interface {
ImplicitString() string
}
// Wrapper around starlark.AsString
func AsString(x starlark.Value) (string, bool) {
is, ok := x.(ImplicitStringer)
if ok {
return is.ImplicitString(), true
}
return starlark.AsString(x)
}
// Unpack an argument that can either be expressed as
// a string or as a list of strings.
func AsStringOrStringList(x starlark.Value) ([]string, bool) {
if x == nil {
return []string{}, true
}
s, ok := AsString(x)
if ok {
return []string{s}, true
}
iterable, ok := x.(starlark.Iterable)
if ok {
result := []string{}
iter := iterable.Iterate()
defer iter.Done()
var item starlark.Value
for iter.Next(&item) {
s, ok := AsString(item)
if !ok {
return nil, false
}
result = append(result, s)
}
return result, true
}
return nil, false
}