Skip to content

Commit 1dbf8d8

Browse files
committed
update mod input params to number type
1 parent bed3b03 commit 1dbf8d8

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### 2.2.2 (Next)
2+
- Update `exp`, `mod`, and `sqrt` functions' input parameter type from `float64` to `number`.
3+
- Update `sqrt` return type to `number`.
4+
15
### 2.2.1
26
- Dynamic input emptiness error message now logs accurate parameter position.
37
- Fix return type for `coalesce_map` function.

stdlib/number/mod.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package numberfunc
33
import (
44
"context"
55
"math"
6+
"math/big"
67

78
"github.com/hashicorp/terraform-plugin-framework/function"
89
"github.com/hashicorp/terraform-plugin-log/tflog"
@@ -30,11 +31,11 @@ func (*modFunction) Definition(_ context.Context, _ function.DefinitionRequest,
3031
Summary: "Determine modulus of a number",
3132
MarkdownDescription: "Return the remainder of the dividend number divided by the divisor number.",
3233
Parameters: []function.Parameter{
33-
function.Float64Parameter{
34+
function.NumberParameter{
3435
Name: "dividend",
3536
Description: "The dividend number from which to divide.",
3637
},
37-
function.Float64Parameter{
38+
function.NumberParameter{
3839
Name: "divisor",
3940
Description: "The divisor number by which to divide.",
4041
},
@@ -45,7 +46,7 @@ func (*modFunction) Definition(_ context.Context, _ function.DefinitionRequest,
4546

4647
func (*modFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
4748
// initialize input parameters
48-
var dividend, divisor float64
49+
var dividend, divisor *big.Float
4950

5051
resp.Error = req.Arguments.Get(ctx, &dividend, &divisor)
5152
if resp.Error != nil {
@@ -55,14 +56,27 @@ func (*modFunction) Run(ctx context.Context, req function.RunRequest, resp *func
5556
ctx = tflog.SetField(ctx, "mod: dividend", dividend)
5657
ctx = tflog.SetField(ctx, "mod: divisor", divisor)
5758

59+
// convert to float64
60+
floatDividend, _ := dividend.Float64()
61+
if math.IsNaN(floatDividend) || math.IsInf(floatDividend, 0) {
62+
resp.Error = function.ConcatFuncErrors(resp.Error, function.NewArgumentFuncError(0, "mod: dividend is beyond the limits of float64"))
63+
}
64+
floatDivisor, _ := divisor.Float64()
65+
if math.IsNaN(floatDivisor) || math.IsInf(floatDivisor, 0) {
66+
resp.Error = function.ConcatFuncErrors(resp.Error, function.NewArgumentFuncError(1, "mod: divisor is beyond the limits of float64"))
67+
}
68+
if resp.Error != nil {
69+
return
70+
}
71+
5872
// validate input parameters
59-
if divisor == 0 {
73+
if floatDivisor == 0 {
6074
resp.Error = function.NewArgumentFuncError(1, "mod: divisor cannot be zero")
6175
return
6276
}
6377

6478
// determine the modulus
65-
modulus := math.Mod(dividend, divisor)
79+
modulus := math.Mod(floatDividend, floatDivisor)
6680
ctx = tflog.SetField(ctx, "mod: modulus", modulus)
6781

6882
// store the result as a float64

stdlib/number/mod_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package numberfunc_test
22

33
import (
4+
"math/big"
45
"testing"
56

67
"github.com/hashicorp/terraform-plugin-framework/attr"
@@ -49,6 +50,24 @@ func TestModFunction(test *testing.T) {
4950
Error: function.NewArgumentFuncError(1, "mod: divisor cannot be zero"),
5051
},
5152
},
53+
"dividend-beyond-upper-limit": {
54+
Request: function.RunRequest{
55+
Arguments: function.NewArgumentsData([]attr.Value{types.NumberValue(func() *big.Float { f := new(big.Float); f.SetString("1e+310"); return f }()), types.Float64Value(10)}),
56+
},
57+
Expected: function.RunResponse{
58+
Result: resultData,
59+
Error: function.NewArgumentFuncError(0, "mod: dividend is beyond the limits of float64"),
60+
},
61+
},
62+
"divisor-beyond-upper-limit": {
63+
Request: function.RunRequest{
64+
Arguments: function.NewArgumentsData([]attr.Value{types.Float64Value(10), types.NumberValue(func() *big.Float { f := new(big.Float); f.SetString("1e+310"); return f }())}),
65+
},
66+
Expected: function.RunResponse{
67+
Result: resultData,
68+
Error: function.NewArgumentFuncError(1, "mod: divisor is beyond the limits of float64"),
69+
},
70+
},
5271
}
5372

5473
util.UnitTests(testCases, resultData, numberfunc.NewModFunction(), test)

0 commit comments

Comments
 (0)