1package stdlib
2
3import (
4	"github.com/zclconf/go-cty/cty"
5	"github.com/zclconf/go-cty/cty/function"
6)
7
8var NotFunc = function.New(&function.Spec{
9	Params: []function.Parameter{
10		{
11			Name:             "val",
12			Type:             cty.Bool,
13			AllowDynamicType: true,
14			AllowMarked:      true,
15		},
16	},
17	Type: function.StaticReturnType(cty.Bool),
18	Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
19		return args[0].Not(), nil
20	},
21})
22
23var AndFunc = function.New(&function.Spec{
24	Params: []function.Parameter{
25		{
26			Name:             "a",
27			Type:             cty.Bool,
28			AllowDynamicType: true,
29			AllowMarked:      true,
30		},
31		{
32			Name:             "b",
33			Type:             cty.Bool,
34			AllowDynamicType: true,
35			AllowMarked:      true,
36		},
37	},
38	Type: function.StaticReturnType(cty.Bool),
39	Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
40		return args[0].And(args[1]), nil
41	},
42})
43
44var OrFunc = function.New(&function.Spec{
45	Params: []function.Parameter{
46		{
47			Name:             "a",
48			Type:             cty.Bool,
49			AllowDynamicType: true,
50			AllowMarked:      true,
51		},
52		{
53			Name:             "b",
54			Type:             cty.Bool,
55			AllowDynamicType: true,
56			AllowMarked:      true,
57		},
58	},
59	Type: function.StaticReturnType(cty.Bool),
60	Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
61		return args[0].Or(args[1]), nil
62	},
63})
64
65// Not returns the logical complement of the given boolean value.
66func Not(num cty.Value) (cty.Value, error) {
67	return NotFunc.Call([]cty.Value{num})
68}
69
70// And returns true if and only if both of the given boolean values are true.
71func And(a, b cty.Value) (cty.Value, error) {
72	return AndFunc.Call([]cty.Value{a, b})
73}
74
75// Or returns true if either of the given boolean values are true.
76func Or(a, b cty.Value) (cty.Value, error) {
77	return OrFunc.Call([]cty.Value{a, b})
78}
79