-
Notifications
You must be signed in to change notification settings - Fork 296
/
path.go
80 lines (67 loc) · 1.34 KB
/
path.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package value
import (
"fmt"
"go.starlark.net/starlark"
)
type LocalPath struct {
t *starlark.Thread
Value string
IsSet bool
}
func NewLocalPathUnpacker(t *starlark.Thread) LocalPath {
return LocalPath{
t: t,
}
}
func (p *LocalPath) Unpack(v starlark.Value) error {
str, err := ValueToAbsPath(p.t, v)
if err != nil {
return err
}
p.Value = str
p.IsSet = true
return nil
}
type LocalPathList struct {
t *starlark.Thread
Value []string
}
func NewLocalPathListUnpacker(t *starlark.Thread) LocalPathList {
return LocalPathList{
t: t,
}
}
func (p *LocalPathList) Unpack(v starlark.Value) error {
_, ok := AsString(v)
if ok {
str, err := ValueToAbsPath(p.t, v)
if err != nil {
return err
}
p.Value = []string{str}
return nil
}
var iter starlark.Iterator
switch x := v.(type) {
case *starlark.List:
iter = x.Iterate()
case starlark.Tuple:
iter = x.Iterate()
case starlark.NoneType:
return nil
default:
return fmt.Errorf("value should be a string or List or Tuple of strings, but is of type %s", v.Type())
}
defer iter.Done()
values := []string{}
var item starlark.Value
for iter.Next(&item) {
str, err := ValueToAbsPath(p.t, item)
if err != nil {
return fmt.Errorf("unpacking list item at index %d: %v", len(values), err)
}
values = append(values, str)
}
p.Value = values
return nil
}