6 "github.com/zclconf/go-cty/cty/convert"
8 "github.com/zclconf/go-cty/cty"
9 "github.com/zclconf/go-cty/cty/function"
12 var SetHasElementFunc = function.New(&function.Spec{
13 Params: []function.Parameter{
16 Type: cty.Set(cty.DynamicPseudoType),
17 AllowDynamicType: true,
21 Type: cty.DynamicPseudoType,
22 AllowDynamicType: true,
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
31 var SetUnionFunc = function.New(&function.Spec{
32 Params: []function.Parameter{
35 Type: cty.Set(cty.DynamicPseudoType),
36 AllowDynamicType: true,
39 VarParam: &function.Parameter{
41 Type: cty.Set(cty.DynamicPseudoType),
42 AllowDynamicType: true,
44 Type: setOperationReturnType,
45 Impl: setOperationImpl(func(s1, s2 cty.ValueSet) cty.ValueSet {
50 var SetIntersectionFunc = function.New(&function.Spec{
51 Params: []function.Parameter{
54 Type: cty.Set(cty.DynamicPseudoType),
55 AllowDynamicType: true,
58 VarParam: &function.Parameter{
60 Type: cty.Set(cty.DynamicPseudoType),
61 AllowDynamicType: true,
63 Type: setOperationReturnType,
64 Impl: setOperationImpl(func(s1, s2 cty.ValueSet) cty.ValueSet {
65 return s1.Intersection(s2)
69 var SetSubtractFunc = function.New(&function.Spec{
70 Params: []function.Parameter{
73 Type: cty.Set(cty.DynamicPseudoType),
74 AllowDynamicType: true,
78 Type: cty.Set(cty.DynamicPseudoType),
79 AllowDynamicType: true,
82 Type: setOperationReturnType,
83 Impl: setOperationImpl(func(s1, s2 cty.ValueSet) cty.ValueSet {
84 return s1.Subtract(s2)
88 var SetSymmetricDifferenceFunc = function.New(&function.Spec{
89 Params: []function.Parameter{
92 Type: cty.Set(cty.DynamicPseudoType),
93 AllowDynamicType: true,
96 VarParam: &function.Parameter{
98 Type: cty.Set(cty.DynamicPseudoType),
99 AllowDynamicType: true,
101 Type: setOperationReturnType,
102 Impl: setOperationImpl(func(s1, s2 cty.ValueSet) cty.ValueSet {
103 return s1.Subtract(s2)
107 // SetHasElement determines whether the given set contains the given value as an
109 func SetHasElement(set cty.Value, elem cty.Value) (cty.Value, error) {
110 return SetHasElementFunc.Call([]cty.Value{set, elem})
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.
118 // The union operation is performed after type conversion, which may result
119 // in some previously-distinct values being conflated.
121 // At least one set must be provided.
122 func SetUnion(sets ...cty.Value) (cty.Value, error) {
123 return SetUnionFunc.Call(sets)
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.
131 // The intersection operation is performed after type conversion, which may
132 // result in some previously-distinct values being conflated.
134 // At least one set must be provided.
135 func SetIntersection(sets ...cty.Value) (cty.Value, error) {
136 return SetIntersectionFunc.Call(sets)
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
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})
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
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)
163 func setOperationReturnType(args []cty.Value) (ret cty.Type, err error) {
165 for _, arg := range args {
166 etys = append(etys, arg.Type().ElementType())
168 newEty, _ := convert.UnifyUnsafe(etys)
169 if newEty == cty.NilType {
170 return cty.NilType, fmt.Errorf("given sets must all have compatible element types")
172 return cty.Set(newEty), nil
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) {
178 first, err = convert.Convert(first, retType)
180 return cty.NilVal, function.NewArgError(0, err)
183 set := first.AsValueSet()
184 for i, arg := range args[1:] {
185 arg, err := convert.Convert(arg, retType)
187 return cty.NilVal, function.NewArgError(i+1, err)
190 argSet := arg.AsValueSet()
193 return cty.SetValFromValueSet(set), nil