7 "github.com/zclconf/go-cty/cty"
8 "github.com/zclconf/go-cty/cty/function"
11 var AbsoluteFunc = function.New(&function.Spec{
12 Params: []function.Parameter{
16 AllowDynamicType: true,
19 Type: function.StaticReturnType(cty.Number),
20 Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
21 return args[0].Absolute(), nil
25 var AddFunc = function.New(&function.Spec{
26 Params: []function.Parameter{
30 AllowDynamicType: true,
35 AllowDynamicType: true,
38 Type: function.StaticReturnType(cty.Number),
39 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
40 // big.Float.Add can panic if the input values are opposing infinities,
41 // so we must catch that here in order to remain within
42 // the cty Function abstraction.
44 if r := recover(); r != nil {
45 if _, ok := r.(big.ErrNaN); ok {
47 err = fmt.Errorf("can't compute sum of opposing infinities")
49 // not a panic we recognize
54 return args[0].Add(args[1]), nil
58 var SubtractFunc = function.New(&function.Spec{
59 Params: []function.Parameter{
63 AllowDynamicType: true,
68 AllowDynamicType: true,
71 Type: function.StaticReturnType(cty.Number),
72 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
73 // big.Float.Sub can panic if the input values are infinities,
74 // so we must catch that here in order to remain within
75 // the cty Function abstraction.
77 if r := recover(); r != nil {
78 if _, ok := r.(big.ErrNaN); ok {
80 err = fmt.Errorf("can't subtract infinity from itself")
82 // not a panic we recognize
87 return args[0].Subtract(args[1]), nil
91 var MultiplyFunc = function.New(&function.Spec{
92 Params: []function.Parameter{
96 AllowDynamicType: true,
101 AllowDynamicType: true,
104 Type: function.StaticReturnType(cty.Number),
105 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
106 // big.Float.Mul can panic if the input values are both zero or both
107 // infinity, so we must catch that here in order to remain within
108 // the cty Function abstraction.
110 if r := recover(); r != nil {
111 if _, ok := r.(big.ErrNaN); ok {
113 err = fmt.Errorf("can't multiply zero by infinity")
115 // not a panic we recognize
121 return args[0].Multiply(args[1]), nil
125 var DivideFunc = function.New(&function.Spec{
126 Params: []function.Parameter{
130 AllowDynamicType: true,
135 AllowDynamicType: true,
138 Type: function.StaticReturnType(cty.Number),
139 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
140 // big.Float.Quo can panic if the input values are both zero or both
141 // infinity, so we must catch that here in order to remain within
142 // the cty Function abstraction.
144 if r := recover(); r != nil {
145 if _, ok := r.(big.ErrNaN); ok {
147 err = fmt.Errorf("can't divide zero by zero or infinity by infinity")
149 // not a panic we recognize
155 return args[0].Divide(args[1]), nil
159 var ModuloFunc = function.New(&function.Spec{
160 Params: []function.Parameter{
164 AllowDynamicType: true,
169 AllowDynamicType: true,
172 Type: function.StaticReturnType(cty.Number),
173 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
174 // big.Float.Mul can panic if the input values are both zero or both
175 // infinity, so we must catch that here in order to remain within
176 // the cty Function abstraction.
178 if r := recover(); r != nil {
179 if _, ok := r.(big.ErrNaN); ok {
181 err = fmt.Errorf("can't use modulo with zero and infinity")
183 // not a panic we recognize
189 return args[0].Modulo(args[1]), nil
193 var GreaterThanFunc = function.New(&function.Spec{
194 Params: []function.Parameter{
198 AllowDynamicType: true,
203 AllowDynamicType: true,
206 Type: function.StaticReturnType(cty.Bool),
207 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
208 return args[0].GreaterThan(args[1]), nil
212 var GreaterThanOrEqualToFunc = function.New(&function.Spec{
213 Params: []function.Parameter{
217 AllowDynamicType: true,
222 AllowDynamicType: true,
225 Type: function.StaticReturnType(cty.Bool),
226 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
227 return args[0].GreaterThanOrEqualTo(args[1]), nil
231 var LessThanFunc = function.New(&function.Spec{
232 Params: []function.Parameter{
236 AllowDynamicType: true,
241 AllowDynamicType: true,
244 Type: function.StaticReturnType(cty.Bool),
245 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
246 return args[0].LessThan(args[1]), nil
250 var LessThanOrEqualToFunc = function.New(&function.Spec{
251 Params: []function.Parameter{
255 AllowDynamicType: true,
260 AllowDynamicType: true,
263 Type: function.StaticReturnType(cty.Bool),
264 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
265 return args[0].LessThanOrEqualTo(args[1]), nil
269 var NegateFunc = function.New(&function.Spec{
270 Params: []function.Parameter{
274 AllowDynamicType: true,
277 Type: function.StaticReturnType(cty.Number),
278 Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
279 return args[0].Negate(), nil
283 var MinFunc = function.New(&function.Spec{
284 Params: []function.Parameter{},
285 VarParam: &function.Parameter{
288 AllowDynamicType: true,
290 Type: function.StaticReturnType(cty.Number),
291 Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
293 return cty.NilVal, fmt.Errorf("must pass at least one number")
296 min := cty.PositiveInfinity
297 for _, num := range args {
298 if num.LessThan(min).True() {
307 var MaxFunc = function.New(&function.Spec{
308 Params: []function.Parameter{},
309 VarParam: &function.Parameter{
312 AllowDynamicType: true,
314 Type: function.StaticReturnType(cty.Number),
315 Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
317 return cty.NilVal, fmt.Errorf("must pass at least one number")
320 max := cty.NegativeInfinity
321 for _, num := range args {
322 if num.GreaterThan(max).True() {
331 var IntFunc = function.New(&function.Spec{
332 Params: []function.Parameter{
336 AllowDynamicType: true,
339 Type: function.StaticReturnType(cty.Number),
340 Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
341 bf := args[0].AsBigFloat()
346 bf = (&big.Float{}).SetInt(bi)
347 return cty.NumberVal(bf), nil
351 // Absolute returns the magnitude of the given number, without its sign.
352 // That is, it turns negative values into positive values.
353 func Absolute(num cty.Value) (cty.Value, error) {
354 return AbsoluteFunc.Call([]cty.Value{num})
357 // Add returns the sum of the two given numbers.
358 func Add(a cty.Value, b cty.Value) (cty.Value, error) {
359 return AddFunc.Call([]cty.Value{a, b})
362 // Subtract returns the difference between the two given numbers.
363 func Subtract(a cty.Value, b cty.Value) (cty.Value, error) {
364 return SubtractFunc.Call([]cty.Value{a, b})
367 // Multiply returns the product of the two given numbers.
368 func Multiply(a cty.Value, b cty.Value) (cty.Value, error) {
369 return MultiplyFunc.Call([]cty.Value{a, b})
372 // Divide returns a divided by b, where both a and b are numbers.
373 func Divide(a cty.Value, b cty.Value) (cty.Value, error) {
374 return DivideFunc.Call([]cty.Value{a, b})
377 // Negate returns the given number multipled by -1.
378 func Negate(num cty.Value) (cty.Value, error) {
379 return NegateFunc.Call([]cty.Value{num})
382 // LessThan returns true if a is less than b.
383 func LessThan(a cty.Value, b cty.Value) (cty.Value, error) {
384 return LessThanFunc.Call([]cty.Value{a, b})
387 // LessThanOrEqualTo returns true if a is less than b.
388 func LessThanOrEqualTo(a cty.Value, b cty.Value) (cty.Value, error) {
389 return LessThanOrEqualToFunc.Call([]cty.Value{a, b})
392 // GreaterThan returns true if a is less than b.
393 func GreaterThan(a cty.Value, b cty.Value) (cty.Value, error) {
394 return GreaterThanFunc.Call([]cty.Value{a, b})
397 // GreaterThanOrEqualTo returns true if a is less than b.
398 func GreaterThanOrEqualTo(a cty.Value, b cty.Value) (cty.Value, error) {
399 return GreaterThanOrEqualToFunc.Call([]cty.Value{a, b})
402 // Modulo returns the remainder of a divided by b under integer division,
403 // where both a and b are numbers.
404 func Modulo(a cty.Value, b cty.Value) (cty.Value, error) {
405 return ModuloFunc.Call([]cty.Value{a, b})
408 // Min returns the minimum number from the given numbers.
409 func Min(numbers ...cty.Value) (cty.Value, error) {
410 return MinFunc.Call(numbers)
413 // Max returns the maximum number from the given numbers.
414 func Max(numbers ...cty.Value) (cty.Value, error) {
415 return MaxFunc.Call(numbers)
418 // Int removes the fractional component of the given number returning an
419 // integer representing the whole number component, rounding towards zero.
420 // For example, -1.5 becomes -1.
422 // If an infinity is passed to Int, an error is returned.
423 func Int(num cty.Value) (cty.Value, error) {
424 if num == cty.PositiveInfinity || num == cty.NegativeInfinity {
425 return cty.NilVal, fmt.Errorf("can't truncate infinity to an integer")
427 return IntFunc.Call([]cty.Value{num})