-
Notifications
You must be signed in to change notification settings - Fork 296
/
label.go
95 lines (78 loc) · 2.09 KB
/
label.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package value
import (
"fmt"
"strings"
"go.starlark.net/starlark"
validation "k8s.io/apimachinery/pkg/util/validation"
)
type LabelValue string
func (lv *LabelValue) Unpack(v starlark.Value) error {
str, ok := AsString(v)
if !ok {
return fmt.Errorf("Value should be convertible to string, but is type %s", v.Type())
}
validationErrors := validation.IsQualifiedName(str)
if len(validationErrors) != 0 {
return fmt.Errorf("Invalid label %q: %s", str, strings.Join(validationErrors, ", "))
}
validLabelValueErrors := validation.IsValidLabelValue(str)
if len(validLabelValueErrors) != 0 {
return fmt.Errorf("Invalid label %q: %s", str, strings.Join(validLabelValueErrors, ", "))
}
// Tilt assumes prefixed labels are not added by the user and thus doesn't use them
// for resource grouping. For now, disallow users from adding prefixes so that they're
// not confused when they don't show up in resource groups.
if strings.Contains(str, "/") {
return fmt.Errorf("Invalid label %q: cannot contain /", str)
}
*lv = LabelValue(str)
return nil
}
func (lv *LabelValue) String() string {
return string(*lv)
}
type LabelSet struct {
Values map[string]string
}
var _ starlark.Unpacker = &LabelSet{}
// Unpack an argument that can either be expressed as
// a string or as a list of strings.
func (ls *LabelSet) Unpack(v starlark.Value) error {
ls.Values = nil
if v == nil {
return nil
}
_, ok := v.(starlark.String)
if ok {
var l LabelValue
err := l.Unpack(v)
if err != nil {
return err
}
ls.Values = map[string]string{l.String(): l.String()}
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 label or List or Tuple of labels, but is of type %s", v.Type())
}
defer iter.Done()
var item starlark.Value
ls.Values = make(map[string]string)
for iter.Next(&item) {
var l LabelValue
err := l.Unpack(item)
if err != nil {
return err
}
ls.Values[l.String()] = l.String()
}
return nil
}