Skip to content

Commit

Permalink
feat: implement notnull function
Browse files Browse the repository at this point in the history
  • Loading branch information
bschaatsbergen committed Mar 12, 2024
1 parent 78d1731 commit 3c17d37
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 144 deletions.
12 changes: 6 additions & 6 deletions docs/functions/example.md → docs/functions/notnull.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "example function - assert"
page_title: "notnull function - assert"
subcategory: ""
description: |-
Example function
Not null function
---

# function: example
# function: notnull

Echoes given argument as result
Checks that the input is not null



## Signature

<!-- signature generated by tfplugindocs -->
```text
example(input string) string
notnull(set set of dynamic) bool
```

## Arguments

<!-- arguments generated by tfplugindocs -->
1. `input` (String) String to echo
1. `set` (Set of Dynamic)

50 changes: 0 additions & 50 deletions internal/provider/example_function.go

This file was deleted.

79 changes: 0 additions & 79 deletions internal/provider/example_function_test.go

This file was deleted.

53 changes: 53 additions & 0 deletions internal/provider/notnull_function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package provider

import (
"context"

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

var (
_ function.Function = NotNullFunction{}
)

func NewNotNullFunction() function.Function {
return NotNullFunction{}
}

type NotNullFunction struct{}

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

func (r NotNullFunction) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) {
resp.Definition = function.Definition{
Summary: "Not null function",
MarkdownDescription: "Checks that the input is not null",
Parameters: []function.Parameter{
function.ObjectParameter{
AllowNullValue: true,
AllowUnknownValues: true,
Description: "The input to check",
Name: "input",
},
},
Return: function.BoolReturn{},
}
}

func (r NotNullFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
var data types.Object

resp.Error = function.ConcatFuncErrors(req.Arguments.Get(ctx, &data))

if resp.Error != nil {
return
}

resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, !data.IsNull()))
}
63 changes: 63 additions & 0 deletions internal/provider/notnull_function_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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 TestExampleFunction_basic(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(version.Must(version.NewVersion("1.8.0-beta1"))),
},
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
locals {
person = {
first_name = "John"
last_name = "Doe"
}
}
output "test" {
value = provider::assert::notnull(local.person)
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckOutput("test", "true"),
),
},
},
})
}

func TestExampleFunction_null(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(version.Must(version.NewVersion("1.8.0-beta1"))),
},
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
locals {
person = null
}
output "test" {
value = provider::assert::notnull(local.person)
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckOutput("test", "false"),
),
},
},
})
}
2 changes: 1 addition & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (p *AssertProvider) DataSources(ctx context.Context) []func() datasource.Da

func (p *AssertProvider) Functions(ctx context.Context) []func() function.Function {
return []func() function.Function{
NewExampleFunction,
NewNotNullFunction,
}
}

Expand Down
8 changes: 0 additions & 8 deletions internal/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package provider

import (
"testing"

"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
)
Expand All @@ -17,9 +15,3 @@ import (
var testAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){
"assert": providerserver.NewProtocol6WithError(New("test")()),
}

func testAccPreCheck(t *testing.T) {
// You can add code here to run prior to any test case execution, for example assertions
// about the appropriate environment variables being set are common to see in a pre-check
// function.
}

0 comments on commit 3c17d37

Please sign in to comment.