Skip to content

Commit

Permalink
feat: add negative and positive functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bschaatsbergen committed Apr 4, 2024
1 parent baaf5d8 commit c9d3b75
Show file tree
Hide file tree
Showing 11 changed files with 488 additions and 0 deletions.
39 changes: 39 additions & 0 deletions docs/functions/negative.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
page_title: "negative function - terraform-provider-assert"
subcategory: "Numeric Functions"
description: |-
Checks whether a number is negative
---

# function: negative



## Variable Validation Example

```terraform
variable "example" {
type = number
validation {
condition = provider::assert::negative(var.example)
error_message = "Expected ${var.example} to be negative"
}
}
```

## Signature

<!-- signature generated by tfplugindocs -->
```text
negative(number number) bool
```

## Arguments

<!-- arguments generated by tfplugindocs -->
1. `number` (Number) The number to check


## Return Type

The return type of `negative` is a boolean.
39 changes: 39 additions & 0 deletions docs/functions/positive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
page_title: "positive function - terraform-provider-assert"
subcategory: "Numeric Functions"
description: |-
Checks whether a number is positive
---

# function: positive



## Variable Validation Example

```terraform
variable "example" {
type = number
validation {
condition = provider::assert::positive(var.example)
error_message = "Expected ${var.example} to be positive"
}
}
```

## Signature

<!-- signature generated by tfplugindocs -->
```text
positive(number number) bool
```

## Arguments

<!-- arguments generated by tfplugindocs -->
1. `number` (Number) The number to check


## Return Type

The return type of `positive` is a boolean.
7 changes: 7 additions & 0 deletions examples/functions/negative/variable.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
variable "example" {
type = number
validation {
condition = provider::assert::negative(var.example)
error_message = "Expected ${var.example} to be negative"
}
}
7 changes: 7 additions & 0 deletions examples/functions/positive/variable.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
variable "example" {
type = number
validation {
condition = provider::assert::positive(var.example)
error_message = "Expected ${var.example} to be positive"
}
}
50 changes: 50 additions & 0 deletions internal/provider/negative_function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package provider

import (
"context"
"math/big"

"github.com/hashicorp/terraform-plugin-framework/function"
)

var (
_ function.Function = NegativeFunction{}
)

func NewNegativeFunction() function.Function {
return NegativeFunction{}
}

type NegativeFunction struct{}

func (r NegativeFunction) Metadata(_ context.Context, req function.MetadataRequest, resp *function.MetadataResponse) {
resp.Name = "negative"
}

func (r NegativeFunction) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) {
resp.Definition = function.Definition{
Summary: "Checks whether a number is negative",
Parameters: []function.Parameter{
function.NumberParameter{
AllowNullValue: false,
AllowUnknownValues: true,
Description: "The number to check",
Name: "number",
},
},
Return: function.BoolReturn{},
}
}

func (r NegativeFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
var number *big.Float

resp.Error = function.ConcatFuncErrors(req.Arguments.Get(ctx, &number))
if resp.Error != nil {
return
}
resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, number.Cmp(big.NewFloat(0)) == -1))
}
118 changes: 118 additions & 0 deletions internal/provider/negative_function_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package provider

import (
"testing"

"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)

func TestNegativeFunction(t *testing.T) {
t.Parallel()
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(version.Must(version.NewVersion(MinimalRequiredTerraformVersion))),
},
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
output "test" {
value = provider::assert::negative(-5)
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckOutput("test", "true"),
),
},
},
})
}

func TestNegativeFunction_float(t *testing.T) {
t.Parallel()
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(version.Must(version.NewVersion(MinimalRequiredTerraformVersion))),
},
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
output "test" {
value = provider::assert::negative(-50.32132)
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckOutput("test", "true"),
),
},
},
})
}

func TestNegativeFunction_falseCases(t *testing.T) {
t.Parallel()
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(version.Must(version.NewVersion(MinimalRequiredTerraformVersion))),
},
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
output "test" {
value = provider::assert::negative(0)
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckOutput("test", "false"),
),
},
{
Config: `
output "test" {
value = provider::assert::negative(0.0)
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckOutput("test", "false"),
),
},
{
Config: `
output "test" {
value = provider::assert::negative(1)
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckOutput("test", "false"),
),
},
{
Config: `
output "test" {
value = provider::assert::negative(5)
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckOutput("test", "false"),
),
},
{
Config: `
output "test" {
value = provider::assert::negative(50.32132)
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckOutput("test", "false"),
),
},
},
})
}
50 changes: 50 additions & 0 deletions internal/provider/positive_function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package provider

import (
"context"
"math/big"

"github.com/hashicorp/terraform-plugin-framework/function"
)

var (
_ function.Function = PositiveFunction{}
)

func NewPositiveFunction() function.Function {
return PositiveFunction{}
}

type PositiveFunction struct{}

func (r PositiveFunction) Metadata(_ context.Context, req function.MetadataRequest, resp *function.MetadataResponse) {
resp.Name = "positive"
}

func (r PositiveFunction) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) {
resp.Definition = function.Definition{
Summary: "Checks whether a number is positive",
Parameters: []function.Parameter{
function.NumberParameter{
AllowNullValue: false,
AllowUnknownValues: true,
Description: "The number to check",
Name: "number",
},
},
Return: function.BoolReturn{},
}
}

func (r PositiveFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
var number *big.Float

resp.Error = function.ConcatFuncErrors(req.Arguments.Get(ctx, &number))
if resp.Error != nil {
return
}
resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, number.Cmp(big.NewFloat(0)) == 1))
}
Loading

0 comments on commit c9d3b75

Please sign in to comment.