Skip to content

Commit

Permalink
Add std.round function in standard library.
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitjangid committed Apr 11, 2023
1 parent 4df8c17 commit de15d34
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 2 deletions.
4 changes: 3 additions & 1 deletion builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,7 @@ var builtinExponent = liftNumeric(func(f float64) float64 {
_, exponent := math.Frexp(f)
return float64(exponent)
})
var builtinRound = liftNumeric(math.Round)

func liftBitwise(f func(int64, int64) int64, positiveRightArg bool) func(*interpreter, value, value) (value, error) {
return func(i *interpreter, xv, yv value) (value, error) {
Expand Down Expand Up @@ -1483,7 +1484,7 @@ func tomlEncodeString(s string) string {
}

// tomlEncodeKey encodes a key - returning same string if it does not need quoting,
// otherwise return it quoted; returns empty key as ''
// otherwise return it quoted; returns empty key as
func tomlEncodeKey(s string) string {
bareAllowed := true

Expand Down Expand Up @@ -2195,6 +2196,7 @@ var funcBuiltins = buildBuiltinMap([]builtin{
&unaryBuiltin{name: "exp", function: builtinExp, params: ast.Identifiers{"x"}},
&unaryBuiltin{name: "mantissa", function: builtinMantissa, params: ast.Identifiers{"x"}},
&unaryBuiltin{name: "exponent", function: builtinExponent, params: ast.Identifiers{"x"}},
&unaryBuiltin{name: "round", function: builtinRound, params: ast.Identifiers{"x"}},
&binaryBuiltin{name: "pow", function: builtinPow, params: ast.Identifiers{"x", "n"}},
&binaryBuiltin{name: "modulo", function: builtinModulo, params: ast.Identifiers{"x", "y"}},
&unaryBuiltin{name: "md5", function: builtinMd5, params: ast.Identifiers{"s"}},
Expand Down
3 changes: 2 additions & 1 deletion linter/internal/types/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func prepareStdlib(g *typeGraph) {
"asin": g.newSimpleFuncType(numberType, "x"),
"acos": g.newSimpleFuncType(numberType, "x"),
"atan": g.newSimpleFuncType(numberType, "x"),
"round": g.newSimpleFuncType(numberType, "x"),

// Assertions and debugging
"assertEqual": g.newSimpleFuncType(boolType, "a", "b"),
Expand Down Expand Up @@ -174,7 +175,7 @@ func prepareStdlib(g *typeGraph) {

// Boolean

"xor": g.newSimpleFuncType(boolType, "x", "y"),
"xor": g.newSimpleFuncType(boolType, "x", "y"),
}

fieldContains := map[string][]placeholderID{}
Expand Down
1 change: 1 addition & 0 deletions testdata/builtinRound.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
1 change: 1 addition & 0 deletions testdata/builtinRound.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.round(2.3)
Empty file.

0 comments on commit de15d34

Please sign in to comment.