-
Notifications
You must be signed in to change notification settings - Fork 289
/
locators.go
125 lines (102 loc) · 3.13 KB
/
locators.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package k8s
import (
"fmt"
"go.starlark.net/starlark"
"github.com/pkg/errors"
"github.com/tilt-dev/tilt/internal/k8s"
"github.com/tilt-dev/tilt/internal/tiltfile/value"
)
// Deserializing locators from starlark values.
type JSONPathImageLocatorListSpec struct {
Specs []JSONPathImageLocatorSpec
}
func (s JSONPathImageLocatorListSpec) IsEmpty() bool {
return len(s.Specs) == 0
}
func (s *JSONPathImageLocatorListSpec) Unpack(v starlark.Value) error {
list := value.ValueOrSequenceToSlice(v)
for _, item := range list {
spec := JSONPathImageLocatorSpec{}
err := spec.Unpack(item)
if err != nil {
return err
}
s.Specs = append(s.Specs, spec)
}
return nil
}
func (s JSONPathImageLocatorListSpec) ToImageLocators(selector k8s.ObjectSelector) ([]k8s.ImageLocator, error) {
result := []k8s.ImageLocator{}
for _, spec := range s.Specs {
locator, err := spec.ToImageLocator(selector)
if err != nil {
return nil, err
}
result = append(result, locator)
}
return result, nil
}
type JSONPathImageLocatorSpec struct {
jsonPath string
}
func (s *JSONPathImageLocatorSpec) Unpack(v starlark.Value) error {
var ok bool
s.jsonPath, ok = starlark.AsString(v)
if !ok {
return fmt.Errorf("Expected string, got: %s", v)
}
return nil
}
func (s JSONPathImageLocatorSpec) ToImageLocator(selector k8s.ObjectSelector) (k8s.ImageLocator, error) {
return k8s.NewJSONPathImageLocator(selector, s.jsonPath)
}
type JSONPathImageObjectLocatorSpec struct {
jsonPath string
repoField string
tagField string
}
func (s JSONPathImageObjectLocatorSpec) IsEmpty() bool {
return s == JSONPathImageObjectLocatorSpec{}
}
func (s *JSONPathImageObjectLocatorSpec) Unpack(v starlark.Value) error {
d, ok := v.(*starlark.Dict)
if !ok {
return fmt.Errorf("Expected dict of the form {'json_path': str, 'repo_field': str, 'tag_field': str}. Actual: %s", v)
}
values, err := validateStringDict(d, []string{"json_path", "repo_field", "tag_field"})
if err != nil {
return errors.Wrap(err, "Expected dict of the form {'json_path': str, 'repo_field': str, 'tag_field': str}")
}
s.jsonPath, s.repoField, s.tagField = values[0], values[1], values[2]
return nil
}
func (s JSONPathImageObjectLocatorSpec) ToImageLocator(selector k8s.ObjectSelector) (k8s.ImageLocator, error) {
return k8s.NewJSONPathImageObjectLocator(selector, s.jsonPath, s.repoField, s.tagField)
}
func validateStringDict(d *starlark.Dict, expectedFields []string) ([]string, error) {
indices := map[string]int{}
result := make([]string, len(expectedFields))
for i, f := range expectedFields {
indices[f] = i
}
for _, item := range d.Items() {
key, val := item[0], item[1]
keyString, ok := starlark.AsString(key)
if !ok {
return nil, fmt.Errorf("Unexpected key: %s", key)
}
index, ok := indices[keyString]
if !ok {
return nil, fmt.Errorf("Unexpected key: %s", key)
}
valString, ok := starlark.AsString(val)
if !ok {
return nil, fmt.Errorf("Expected string at key %q. Got: %s", key, val.Type())
}
result[index] = valString
}
if len(d.Items()) != len(expectedFields) {
return nil, fmt.Errorf("Missing keys. Actual keys: %s", d.Keys())
}
return result, nil
}