port ceil function

This commit is contained in:
Kristin Laemmert 2018-05-23 12:41:55 -07:00 committed by Martin Atkins
parent c4f4dddff5
commit 4ad3676934
3 changed files with 80 additions and 1 deletions

33
lang/funcs/number.go Normal file
View File

@ -0,0 +1,33 @@
package funcs
import (
"math"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
"github.com/zclconf/go-cty/cty/gocty"
)
// CeilFunc contructs a function that returns the closest whole number greater
// than or equal to the given value.
var CeilFunc = function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "num",
Type: cty.Number,
},
},
Type: function.StaticReturnType(cty.Number),
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
var val float64
if err := gocty.FromCtyValue(args[0], &val); err != nil {
return cty.UnknownVal(cty.String), err
}
return cty.NumberIntVal(int64(math.Ceil(val))), nil
},
})
// Ceil returns the closest whole number greater than or equal to the given value.
func Ceil(num cty.Value) (cty.Value, error) {
return CeilFunc.Call([]cty.Value{num})
}

46
lang/funcs/number_test.go Normal file
View File

@ -0,0 +1,46 @@
package funcs
import (
"fmt"
"testing"
"github.com/zclconf/go-cty/cty"
)
func TestCeil(t *testing.T) {
tests := []struct {
Num cty.Value
Want cty.Value
Err bool
}{
{
cty.NumberFloatVal(-1.8),
cty.NumberFloatVal(-1),
false,
},
{
cty.NumberFloatVal(1.2),
cty.NumberFloatVal(2),
false,
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("Ceil(%#v)", test.Num), func(t *testing.T) {
got, err := Ceil(test.Num)
if test.Err {
if err == nil {
t.Fatal("succeeded; want error")
}
return
} else if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if !got.RawEquals(test.Want) {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
}
})
}
}

View File

@ -37,7 +37,7 @@ func (s *Scope) Functions() map[string]function.Function {
"base64sha256": funcs.Base64Sha256Func,
"base64sha512": funcs.Base64Sha512Func,
"bcrypt": funcs.BcryptFunc,
"ceil": unimplFunc, // TODO
"ceil": funcs.CeilFunc,
"chomp": unimplFunc, // TODO
"cidrhost": unimplFunc, // TODO
"cidrnetmask": unimplFunc, // TODO