6 "github.com/zclconf/go-cty/cty"
7 "github.com/zclconf/go-cty/cty/convert"
8 "github.com/zclconf/go-cty/cty/function"
11 var EqualFunc = function.New(&function.Spec{
12 Params: []function.Parameter{
15 Type: cty.DynamicPseudoType,
17 AllowDynamicType: true,
22 Type: cty.DynamicPseudoType,
24 AllowDynamicType: true,
28 Type: function.StaticReturnType(cty.Bool),
29 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
30 return args[0].Equals(args[1]), nil
34 var NotEqualFunc = function.New(&function.Spec{
35 Params: []function.Parameter{
38 Type: cty.DynamicPseudoType,
40 AllowDynamicType: true,
45 Type: cty.DynamicPseudoType,
47 AllowDynamicType: true,
51 Type: function.StaticReturnType(cty.Bool),
52 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
53 return args[0].Equals(args[1]).Not(), nil
57 var CoalesceFunc = function.New(&function.Spec{
58 Params: []function.Parameter{},
59 VarParam: &function.Parameter{
61 Type: cty.DynamicPseudoType,
63 AllowDynamicType: true,
66 Type: func(args []cty.Value) (ret cty.Type, err error) {
67 argTypes := make([]cty.Type, len(args))
68 for i, val := range args {
69 argTypes[i] = val.Type()
71 retType, _ := convert.UnifyUnsafe(argTypes)
72 if retType == cty.NilType {
73 return cty.NilType, fmt.Errorf("all arguments must have the same type")
77 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
78 for _, argVal := range args {
79 if !argVal.IsKnown() {
80 return cty.UnknownVal(retType), nil
86 return convert.Convert(argVal, retType)
88 return cty.NilVal, fmt.Errorf("no non-null arguments")
92 // Equal determines whether the two given values are equal, returning a
94 func Equal(a cty.Value, b cty.Value) (cty.Value, error) {
95 return EqualFunc.Call([]cty.Value{a, b})
98 // NotEqual is the opposite of Equal.
99 func NotEqual(a cty.Value, b cty.Value) (cty.Value, error) {
100 return NotEqualFunc.Call([]cty.Value{a, b})
103 // Coalesce returns the first of the given arguments that is not null. If
104 // all arguments are null, an error is produced.
105 func Coalesce(vals ...cty.Value) (cty.Value, error) {
106 return CoalesceFunc.Call(vals)