-
Notifications
You must be signed in to change notification settings - Fork 550
/
utils.go
273 lines (236 loc) · 7.28 KB
/
utils.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package validators
import (
"fmt"
"github.com/golang/protobuf/proto"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"k8s.io/apimachinery/pkg/util/sets"
"github.com/flyteorg/flyte/flyteidl/gen/pb-go/flyteidl/core"
)
func containsBindingByVariableName(bindings []*core.Binding, name string) (found bool) {
for _, b := range bindings {
if b.Var == name {
return true
}
}
return false
}
func findVariableByName(vars *core.VariableMap, name string) (variable *core.Variable, found bool) {
if vars == nil || vars.Variables == nil {
return nil, false
}
variable, found = vars.Variables[name]
return
}
// Gets literal type for scalar value. This can be used to compare the underlying type of two scalars for compatibility.
func literalTypeForScalar(scalar *core.Scalar) *core.LiteralType {
// TODO: Should we just pass the type information with the value? That way we don't have to guess?
var literalType *core.LiteralType
switch v := scalar.GetValue().(type) {
case *core.Scalar_Primitive:
literalType = literalTypeForPrimitive(scalar.GetPrimitive())
case *core.Scalar_Blob:
if scalar.GetBlob().GetMetadata() == nil {
return nil
}
literalType = &core.LiteralType{Type: &core.LiteralType_Blob{Blob: scalar.GetBlob().GetMetadata().GetType()}}
case *core.Scalar_Binary:
literalType = &core.LiteralType{Type: &core.LiteralType_Simple{Simple: core.SimpleType_BINARY}}
case *core.Scalar_Schema:
literalType = &core.LiteralType{
Type: &core.LiteralType_Schema{
Schema: scalar.GetSchema().Type,
},
}
case *core.Scalar_StructuredDataset:
if v.StructuredDataset == nil || v.StructuredDataset.Metadata == nil {
return &core.LiteralType{
Type: &core.LiteralType_StructuredDatasetType{},
}
}
literalType = &core.LiteralType{
Type: &core.LiteralType_StructuredDatasetType{
StructuredDatasetType: scalar.GetStructuredDataset().GetMetadata().StructuredDatasetType,
},
}
case *core.Scalar_NoneType:
literalType = &core.LiteralType{Type: &core.LiteralType_Simple{Simple: core.SimpleType_NONE}}
case *core.Scalar_Error:
literalType = &core.LiteralType{Type: &core.LiteralType_Simple{Simple: core.SimpleType_ERROR}}
case *core.Scalar_Generic:
literalType = &core.LiteralType{Type: &core.LiteralType_Simple{Simple: core.SimpleType_STRUCT}}
case *core.Scalar_Union:
literalType = &core.LiteralType{
Type: &core.LiteralType_UnionType{
UnionType: &core.UnionType{
Variants: []*core.LiteralType{
scalar.GetUnion().GetType(),
},
},
},
}
default:
return nil
}
return literalType
}
func literalTypeForPrimitive(primitive *core.Primitive) *core.LiteralType {
simpleType := core.SimpleType_NONE
switch primitive.GetValue().(type) {
case *core.Primitive_Integer:
simpleType = core.SimpleType_INTEGER
case *core.Primitive_FloatValue:
simpleType = core.SimpleType_FLOAT
case *core.Primitive_StringValue:
simpleType = core.SimpleType_STRING
case *core.Primitive_Boolean:
simpleType = core.SimpleType_BOOLEAN
case *core.Primitive_Datetime:
simpleType = core.SimpleType_DATETIME
case *core.Primitive_Duration:
simpleType = core.SimpleType_DURATION
}
return &core.LiteralType{Type: &core.LiteralType_Simple{Simple: simpleType}}
}
func buildVariablesIndex(params *core.VariableMap) (map[string]*core.Variable, sets.String) {
paramMap := make(map[string]*core.Variable, len(params.Variables))
paramSet := sets.NewString()
for paramName, param := range params.Variables {
paramMap[paramName] = param
paramSet.Insert(paramName)
}
return paramMap, paramSet
}
func filterVariables(vars *core.VariableMap, varNames sets.String) *core.VariableMap {
res := &core.VariableMap{
Variables: make(map[string]*core.Variable, len(varNames)),
}
for paramName, param := range vars.Variables {
if varNames.Has(paramName) {
res.Variables[paramName] = param
}
}
return res
}
func withVariableName(param *core.Variable) (newParam *core.Variable, ok bool) {
if raw, err := proto.Marshal(param); err == nil {
newParam = &core.Variable{}
if err = proto.Unmarshal(raw, newParam); err == nil {
ok = true
}
}
return
}
func UnionDistinctVariableMaps(m1, m2 map[string]*core.Variable) (map[string]*core.Variable, error) {
res := make(map[string]*core.Variable, len(m1)+len(m2))
for k, v := range m1 {
res[k] = v
}
for k, v := range m2 {
if existingV, exists := res[k]; exists {
if v.Type.String() != existingV.Type.String() {
return nil, fmt.Errorf("key already exists with a different type. %v has type [%v] on one side "+
"and type [%v] on the other", k, existingV.Type.String(), v.Type.String())
}
}
res[k] = v
}
return res, nil
}
func buildMultipleTypeUnion(innerType []*core.LiteralType) *core.LiteralType {
var variants []*core.LiteralType
isNested := false
for _, x := range innerType {
unionType := x.GetCollectionType().GetUnionType()
if unionType != nil {
isNested = true
variants = append(variants, unionType.Variants...)
} else {
variants = append(variants, x)
}
}
unionLiteralType := &core.LiteralType{
Type: &core.LiteralType_UnionType{
UnionType: &core.UnionType{
Variants: variants,
},
},
}
if isNested {
return &core.LiteralType{
Type: &core.LiteralType_CollectionType{
CollectionType: unionLiteralType,
},
}
}
return unionLiteralType
}
func literalTypeForLiterals(literals []*core.Literal) *core.LiteralType {
innerType := make([]*core.LiteralType, 0, 1)
innerTypeSet := sets.NewString()
var noneType *core.LiteralType
for _, x := range literals {
otherType := LiteralTypeForLiteral(x)
otherTypeKey := otherType.String()
if _, ok := x.GetValue().(*core.Literal_Collection); ok {
if x.GetCollection().GetLiterals() == nil {
noneType = otherType
continue
}
}
if !innerTypeSet.Has(otherTypeKey) {
innerType = append(innerType, otherType)
innerTypeSet.Insert(otherTypeKey)
}
}
// only add none type if there aren't other types
if len(innerType) == 0 && noneType != nil {
innerType = append(innerType, noneType)
}
if len(innerType) == 0 {
return &core.LiteralType{
Type: &core.LiteralType_Simple{Simple: core.SimpleType_NONE},
}
} else if len(innerType) == 1 {
return innerType[0]
}
// sort inner types to ensure consistent union types are generated
slices.SortFunc(innerType, func(a, b *core.LiteralType) int {
aStr := a.String()
bStr := b.String()
if aStr < bStr {
return -1
} else if aStr > bStr {
return 1
}
return 0
})
return buildMultipleTypeUnion(innerType)
}
// LiteralTypeForLiteral gets LiteralType for literal, nil if the value of literal is unknown, or type collection/map of
// type None if the literal is a non-homogeneous type.
func LiteralTypeForLiteral(l *core.Literal) *core.LiteralType {
switch l.GetValue().(type) {
case *core.Literal_Scalar:
return literalTypeForScalar(l.GetScalar())
case *core.Literal_Collection:
return &core.LiteralType{
Type: &core.LiteralType_CollectionType{
CollectionType: literalTypeForLiterals(l.GetCollection().Literals),
},
}
case *core.Literal_Map:
return &core.LiteralType{
Type: &core.LiteralType_MapValueType{
MapValueType: literalTypeForLiterals(maps.Values(l.GetMap().Literals)),
},
}
}
return nil
}
func GetTagForType(x *core.LiteralType) string {
if x.GetStructure() == nil {
return ""
}
return x.GetStructure().GetTag()
}