1package stdlib
2
3import (
4	"fmt"
5
6	"github.com/zclconf/go-cty/cty"
7	"github.com/zclconf/go-cty/cty/convert"
8	"github.com/zclconf/go-cty/cty/function"
9)
10
11var EqualFunc = function.New(&function.Spec{
12	Params: []function.Parameter{
13		{
14			Name:             "a",
15			Type:             cty.DynamicPseudoType,
16			AllowUnknown:     true,
17			AllowDynamicType: true,
18			AllowNull:        true,
19		},
20		{
21			Name:             "b",
22			Type:             cty.DynamicPseudoType,
23			AllowUnknown:     true,
24			AllowDynamicType: true,
25			AllowNull:        true,
26		},
27	},
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
31	},
32})
33
34var NotEqualFunc = function.New(&function.Spec{
35	Params: []function.Parameter{
36		{
37			Name:             "a",
38			Type:             cty.DynamicPseudoType,
39			AllowUnknown:     true,
40			AllowDynamicType: true,
41			AllowNull:        true,
42		},
43		{
44			Name:             "b",
45			Type:             cty.DynamicPseudoType,
46			AllowUnknown:     true,
47			AllowDynamicType: true,
48			AllowNull:        true,
49		},
50	},
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
54	},
55})
56
57var CoalesceFunc = function.New(&function.Spec{
58	Params: []function.Parameter{},
59	VarParam: &function.Parameter{
60		Name:             "vals",
61		Type:             cty.DynamicPseudoType,
62		AllowUnknown:     true,
63		AllowDynamicType: true,
64		AllowNull:        true,
65	},
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()
70		}
71		retType, _ := convert.UnifyUnsafe(argTypes)
72		if retType == cty.NilType {
73			return cty.NilType, fmt.Errorf("all arguments must have the same type")
74		}
75		return retType, nil
76	},
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
81			}
82			if argVal.IsNull() {
83				continue
84			}
85
86			return convert.Convert(argVal, retType)
87		}
88		return cty.NilVal, fmt.Errorf("no non-null arguments")
89	},
90})
91
92// Equal determines whether the two given values are equal, returning a
93// bool value.
94func Equal(a cty.Value, b cty.Value) (cty.Value, error) {
95	return EqualFunc.Call([]cty.Value{a, b})
96}
97
98// NotEqual is the opposite of Equal.
99func NotEqual(a cty.Value, b cty.Value) (cty.Value, error) {
100	return NotEqualFunc.Call([]cty.Value{a, b})
101}
102
103// Coalesce returns the first of the given arguments that is not null. If
104// all arguments are null, an error is produced.
105func Coalesce(vals ...cty.Value) (cty.Value, error) {
106	return CoalesceFunc.Call(vals)
107}
108