Skip to content

Commit 8819d7b

Browse files
committed
adding truncate function support
1 parent 1b780a1 commit 8819d7b

File tree

6 files changed

+72
-3
lines changed

6 files changed

+72
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### 2.3.0 (Next)
2+
- Add `truncate` function.
3+
14
### 2.2.2
25
- Update `exp`, `mod`, and `sqrt` functions' input parameter type from `float64` to `number`.
36
- Update `sqrt` return type to `number`.

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ The documentation overview for custom functions (available for Terraform version
4848
- [mod](functions/mod.html)
4949
- [round](functions/round.html)
5050
- [sqrt](functions/sqrt.html)
51+
- [truncate](functions/truncate.html)
5152

5253
### Map
5354
- [coalesce_map](functions/coalesce_map.html)
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# Return the rounding of of 1.2:
1+
# Return the rounding of 1.2:
22
provider::stdlib::round(1.2)
33
# result => 1
44

5-
# Return the rounding of of 1.8:
5+
# Return the rounding of 1.8:
66
provider::stdlib::round(1.8)
77
# result => 2
88

9-
# Return the rounding of of 1.5:
9+
# Return the rounding of 1.5:
1010
provider::stdlib::round(1.5)
1111
# result => 2
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Return the truncation of 1.2345:
2+
provider::stdlib::truncate(1.2345)
3+
# result => 1
4+
5+
# Return the truncation of -67.89:
6+
provider::stdlib::truncate(-67.89)
7+
# result => -67

stdlib/number/truncate_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package numberfunc_test
2+
3+
import (
4+
"math/big"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-framework/attr"
8+
"github.com/hashicorp/terraform-plugin-framework/function"
9+
"github.com/hashicorp/terraform-plugin-framework/types"
10+
11+
util "github.com/mschuchard/terraform-provider-stdlib/internal"
12+
numberfunc "github.com/mschuchard/terraform-provider-stdlib/stdlib/number"
13+
)
14+
15+
func TestTruncateFunction(test *testing.T) {
16+
// initialize initial result data
17+
resultData := function.NewResultData(types.Int64Unknown())
18+
19+
testCases := util.TestCases{
20+
"truncate-one": {
21+
Request: function.RunRequest{
22+
Arguments: function.NewArgumentsData([]attr.Value{types.Float64Value(1.2345)}),
23+
},
24+
Expected: function.RunResponse{
25+
Result: function.NewResultData(types.Int64Value(1)),
26+
},
27+
},
28+
"truncate-negative-sixty-seven": {
29+
Request: function.RunRequest{
30+
Arguments: function.NewArgumentsData([]attr.Value{types.Float64Value(-67.89)}),
31+
},
32+
Expected: function.RunResponse{
33+
Result: function.NewResultData(types.Int64Value(-67)),
34+
},
35+
},
36+
"beyond-upper-limit": {
37+
Request: function.RunRequest{
38+
Arguments: function.NewArgumentsData([]attr.Value{types.NumberValue(func() *big.Float { f := new(big.Float); f.SetString("1e+310"); return f }())}),
39+
},
40+
Expected: function.RunResponse{
41+
Result: resultData,
42+
Error: function.NewArgumentFuncError(0, "truncate: input number is beyond the limits of float64"),
43+
},
44+
},
45+
"overflow": {
46+
Request: function.RunRequest{
47+
Arguments: function.NewArgumentsData([]attr.Value{types.Float64Value(9.223372036854777e+18)}), // math.MaxInt64 + 1
48+
},
49+
Expected: function.RunResponse{
50+
Result: resultData,
51+
Error: function.NewArgumentFuncError(0, "truncate: truncated input number is beyond the limits of int64"),
52+
},
53+
},
54+
}
55+
56+
util.UnitTests(testCases, resultData, numberfunc.NewTruncateFunction(), test)
57+
}

stdlib/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ func (*stdlibProvider) Functions(_ context.Context) []func() function.Function {
102102
numberfunc.NewModFunction,
103103
numberfunc.NewRoundFunction,
104104
numberfunc.NewSqrtFunction,
105+
numberfunc.NewTruncateFunction,
105106
slicefunc.NewCompareListFunction,
106107
slicefunc.NewElementsDeleteFunction,
107108
slicefunc.NewInsertFunction,

0 commit comments

Comments
 (0)