Skip to content

Commit

Permalink
add support for new mod function
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuchard committed Aug 19, 2024
1 parent 859115f commit 7d2fa04
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### 1.5.0 (Next)
- Do not coerce `number` type ID to `string` type ID (may cause superficial plan changes to existing states).
- Add `exp` function.
- Add `exp` and `mod` functions.

### 1.4.1
- Add `sorted` parameter to `list_index` function.
Expand Down
2 changes: 1 addition & 1 deletion examples/data-sources/stdlib_exp/data-source.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ data "stdlib_exp" "zero" {
# => 1

# Return the base e exponential of 1.0986122
data "stdlib_exp" "deciamal" {
data "stdlib_exp" "decimal" {
param = 1.0986122
}
# => 2.9999997339956828
20 changes: 20 additions & 0 deletions examples/data-sources/stdlib_mod/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Return the remainder of 4 / 2
data "stdlib_mod" "zero" {
dividend = 4
divisor = 2
}
# => 0

# Return the remainder of 5 / 3
data "stdlib_mod" "integer" {
dividend = 5
divisor = 3
}
# => 2

# Return the remainder of 10 / 3.5
data "stdlib_mod" "decimal" {
dividend = 10
divisor = 4.75
}
# => 0.5
3 changes: 1 addition & 2 deletions stdlib/number/exp_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"

"github.com/mschuchard/terraform-provider-stdlib/internal"
)

Expand Down Expand Up @@ -50,7 +49,7 @@ func (_ *expDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, re
Description: "Function result storing the base-e exponential of the input parameter.",
},
},
MarkdownDescription: "Return the base-e exponential of an inpurt parameter.",
MarkdownDescription: "Return the base-e exponential of an input parameter.",
}
}

Expand Down
2 changes: 1 addition & 1 deletion stdlib/number/mod_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"

util "github.com/mschuchard/terraform-provider-stdlib/internal"
"github.com/mschuchard/terraform-provider-stdlib/internal"
)

// ensure the implementation satisfies the expected interfaces
Expand Down
66 changes: 66 additions & 0 deletions stdlib/number/mod_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package numberfunc_test

import (
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"

"github.com/mschuchard/terraform-provider-stdlib/stdlib"
)

func TestAccMod(test *testing.T) {
// invoke test
resource.ParallelTest(test, resource.TestCase{
ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// test basic 0 remainder
{
Config: `data "stdlib_mod" "test" {
dividend = 4
divisor = 2
}`,
Check: resource.ComposeAggregateTestCheckFunc(
// verify input params are stored correctly
resource.TestCheckResourceAttr("data.stdlib_mod.test", "dividend", "4"),
resource.TestCheckResourceAttr("data.stdlib_mod.test", "divisor", "2"),
// verify remainder result is stored correctly
resource.TestCheckResourceAttr("data.stdlib_mod.test", "result", "0"),
// verify id stored correctly
resource.TestCheckResourceAttr("data.stdlib_mod.test", "id", "2"),
),
},
// test basic integer remainder
{
Config: `data "stdlib_mod" "test" {
dividend = 5
divisor = 3
}`,
Check: resource.ComposeAggregateTestCheckFunc(
// verify input params are stored correctly
resource.TestCheckResourceAttr("data.stdlib_mod.test", "dividend", "5"),
resource.TestCheckResourceAttr("data.stdlib_mod.test", "divisor", "3"),
// verify remainder result is stored correctly
resource.TestCheckResourceAttr("data.stdlib_mod.test", "result", "2"),
// verify id stored correctly
resource.TestCheckResourceAttr("data.stdlib_mod.test", "id", "3"),
),
},
// test basic float remainder
{
Config: `data "stdlib_mod" "test" {
dividend = 10
divisor = 4.75
}`,
Check: resource.ComposeAggregateTestCheckFunc(
// verify input params are stored correctly
resource.TestCheckResourceAttr("data.stdlib_mod.test", "dividend", "10"),
resource.TestCheckResourceAttr("data.stdlib_mod.test", "divisor", "4.75"),
// verify remainder result is stored correctly
resource.TestCheckResourceAttr("data.stdlib_mod.test", "result", "0.5"),
// verify id stored correctly
resource.TestCheckResourceAttr("data.stdlib_mod.test", "id", "4.75"),
),
},
},
})
}
1 change: 1 addition & 0 deletions stdlib/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (_ *stdlibProvider) DataSources(_ context.Context) []func() datasource.Data
mapfunc.NewKeysDeleteDataSource,
multiple.NewEmptyDataSource,
numberfunc.NewExpDataSource,
numberfunc.NewModDataSource,
slicefunc.NewCompareListDataSource,
slicefunc.NewInsertDataSource,
slicefunc.NewLastElementDataSource,
Expand Down

0 comments on commit 7d2fa04

Please sign in to comment.