diff options
Diffstat (limited to 'vendor/github.com/zclconf/go-cty/cty/function/stdlib/set.go')
-rw-r--r-- | vendor/github.com/zclconf/go-cty/cty/function/stdlib/set.go | 195 |
1 files changed, 195 insertions, 0 deletions
diff --git a/vendor/github.com/zclconf/go-cty/cty/function/stdlib/set.go b/vendor/github.com/zclconf/go-cty/cty/function/stdlib/set.go new file mode 100644 index 0000000..100078f --- /dev/null +++ b/vendor/github.com/zclconf/go-cty/cty/function/stdlib/set.go | |||
@@ -0,0 +1,195 @@ | |||
1 | package stdlib | ||
2 | |||
3 | import ( | ||
4 | "fmt" | ||
5 | |||
6 | "github.com/zclconf/go-cty/cty/convert" | ||
7 | |||
8 | "github.com/zclconf/go-cty/cty" | ||
9 | "github.com/zclconf/go-cty/cty/function" | ||
10 | ) | ||
11 | |||
12 | var SetHasElementFunc = function.New(&function.Spec{ | ||
13 | Params: []function.Parameter{ | ||
14 | { | ||
15 | Name: "set", | ||
16 | Type: cty.Set(cty.DynamicPseudoType), | ||
17 | AllowDynamicType: true, | ||
18 | }, | ||
19 | { | ||
20 | Name: "elem", | ||
21 | Type: cty.DynamicPseudoType, | ||
22 | AllowDynamicType: true, | ||
23 | }, | ||
24 | }, | ||
25 | Type: function.StaticReturnType(cty.Bool), | ||
26 | Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) { | ||
27 | return args[0].HasElement(args[1]), nil | ||
28 | }, | ||
29 | }) | ||
30 | |||
31 | var SetUnionFunc = function.New(&function.Spec{ | ||
32 | Params: []function.Parameter{ | ||
33 | { | ||
34 | Name: "first_set", | ||
35 | Type: cty.Set(cty.DynamicPseudoType), | ||
36 | AllowDynamicType: true, | ||
37 | }, | ||
38 | }, | ||
39 | VarParam: &function.Parameter{ | ||
40 | Name: "other_sets", | ||
41 | Type: cty.Set(cty.DynamicPseudoType), | ||
42 | AllowDynamicType: true, | ||
43 | }, | ||
44 | Type: setOperationReturnType, | ||
45 | Impl: setOperationImpl(func(s1, s2 cty.ValueSet) cty.ValueSet { | ||
46 | return s1.Union(s2) | ||
47 | }), | ||
48 | }) | ||
49 | |||
50 | var SetIntersectionFunc = function.New(&function.Spec{ | ||
51 | Params: []function.Parameter{ | ||
52 | { | ||
53 | Name: "first_set", | ||
54 | Type: cty.Set(cty.DynamicPseudoType), | ||
55 | AllowDynamicType: true, | ||
56 | }, | ||
57 | }, | ||
58 | VarParam: &function.Parameter{ | ||
59 | Name: "other_sets", | ||
60 | Type: cty.Set(cty.DynamicPseudoType), | ||
61 | AllowDynamicType: true, | ||
62 | }, | ||
63 | Type: setOperationReturnType, | ||
64 | Impl: setOperationImpl(func(s1, s2 cty.ValueSet) cty.ValueSet { | ||
65 | return s1.Intersection(s2) | ||
66 | }), | ||
67 | }) | ||
68 | |||
69 | var SetSubtractFunc = function.New(&function.Spec{ | ||
70 | Params: []function.Parameter{ | ||
71 | { | ||
72 | Name: "a", | ||
73 | Type: cty.Set(cty.DynamicPseudoType), | ||
74 | AllowDynamicType: true, | ||
75 | }, | ||
76 | { | ||
77 | Name: "b", | ||
78 | Type: cty.Set(cty.DynamicPseudoType), | ||
79 | AllowDynamicType: true, | ||
80 | }, | ||
81 | }, | ||
82 | Type: setOperationReturnType, | ||
83 | Impl: setOperationImpl(func(s1, s2 cty.ValueSet) cty.ValueSet { | ||
84 | return s1.Subtract(s2) | ||
85 | }), | ||
86 | }) | ||
87 | |||
88 | var SetSymmetricDifferenceFunc = function.New(&function.Spec{ | ||
89 | Params: []function.Parameter{ | ||
90 | { | ||
91 | Name: "first_set", | ||
92 | Type: cty.Set(cty.DynamicPseudoType), | ||
93 | AllowDynamicType: true, | ||
94 | }, | ||
95 | }, | ||
96 | VarParam: &function.Parameter{ | ||
97 | Name: "other_sets", | ||
98 | Type: cty.Set(cty.DynamicPseudoType), | ||
99 | AllowDynamicType: true, | ||
100 | }, | ||
101 | Type: setOperationReturnType, | ||
102 | Impl: setOperationImpl(func(s1, s2 cty.ValueSet) cty.ValueSet { | ||
103 | return s1.Subtract(s2) | ||
104 | }), | ||
105 | }) | ||
106 | |||
107 | // SetHasElement determines whether the given set contains the given value as an | ||
108 | // element. | ||
109 | func SetHasElement(set cty.Value, elem cty.Value) (cty.Value, error) { | ||
110 | return SetHasElementFunc.Call([]cty.Value{set, elem}) | ||
111 | } | ||
112 | |||
113 | // SetUnion returns a new set containing all of the elements from the given | ||
114 | // sets, which must have element types that can all be converted to some | ||
115 | // common type using the standard type unification rules. If conversion | ||
116 | // is not possible, an error is returned. | ||
117 | // | ||
118 | // The union operation is performed after type conversion, which may result | ||
119 | // in some previously-distinct values being conflated. | ||
120 | // | ||
121 | // At least one set must be provided. | ||
122 | func SetUnion(sets ...cty.Value) (cty.Value, error) { | ||
123 | return SetUnionFunc.Call(sets) | ||
124 | } | ||
125 | |||
126 | // Intersection returns a new set containing the elements that exist | ||
127 | // in all of the given sets, which must have element types that can all be | ||
128 | // converted to some common type using the standard type unification rules. | ||
129 | // If conversion is not possible, an error is returned. | ||
130 | // | ||
131 | // The intersection operation is performed after type conversion, which may | ||
132 | // result in some previously-distinct values being conflated. | ||
133 | // | ||
134 | // At least one set must be provided. | ||
135 | func SetIntersection(sets ...cty.Value) (cty.Value, error) { | ||
136 | return SetIntersectionFunc.Call(sets) | ||
137 | } | ||
138 | |||
139 | // SetSubtract returns a new set containing the elements from the | ||
140 | // first set that are not present in the second set. The sets must have | ||
141 | // element types that can both be converted to some common type using the | ||
142 | // standard type unification rules. If conversion is not possible, an error | ||
143 | // is returned. | ||
144 | // | ||
145 | // The subtract operation is performed after type conversion, which may | ||
146 | // result in some previously-distinct values being conflated. | ||
147 | func SetSubtract(a, b cty.Value) (cty.Value, error) { | ||
148 | return SetSubtractFunc.Call([]cty.Value{a, b}) | ||
149 | } | ||
150 | |||
151 | // SetSymmetricDifference returns a new set containing elements that appear | ||
152 | // in any of the given sets but not multiple. The sets must have | ||
153 | // element types that can all be converted to some common type using the | ||
154 | // standard type unification rules. If conversion is not possible, an error | ||
155 | // is returned. | ||
156 | // | ||
157 | // The difference operation is performed after type conversion, which may | ||
158 | // result in some previously-distinct values being conflated. | ||
159 | func SetSymmetricDifference(sets ...cty.Value) (cty.Value, error) { | ||
160 | return SetSymmetricDifferenceFunc.Call(sets) | ||
161 | } | ||
162 | |||
163 | func setOperationReturnType(args []cty.Value) (ret cty.Type, err error) { | ||
164 | var etys []cty.Type | ||
165 | for _, arg := range args { | ||
166 | etys = append(etys, arg.Type().ElementType()) | ||
167 | } | ||
168 | newEty, _ := convert.UnifyUnsafe(etys) | ||
169 | if newEty == cty.NilType { | ||
170 | return cty.NilType, fmt.Errorf("given sets must all have compatible element types") | ||
171 | } | ||
172 | return cty.Set(newEty), nil | ||
173 | } | ||
174 | |||
175 | func setOperationImpl(f func(s1, s2 cty.ValueSet) cty.ValueSet) function.ImplFunc { | ||
176 | return func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) { | ||
177 | first := args[0] | ||
178 | first, err = convert.Convert(first, retType) | ||
179 | if err != nil { | ||
180 | return cty.NilVal, function.NewArgError(0, err) | ||
181 | } | ||
182 | |||
183 | set := first.AsValueSet() | ||
184 | for i, arg := range args[1:] { | ||
185 | arg, err := convert.Convert(arg, retType) | ||
186 | if err != nil { | ||
187 | return cty.NilVal, function.NewArgError(i+1, err) | ||
188 | } | ||
189 | |||
190 | argSet := arg.AsValueSet() | ||
191 | set = f(set, argSet) | ||
192 | } | ||
193 | return cty.SetValFromValueSet(set), nil | ||
194 | } | ||
195 | } | ||