Skip to content

Move shared type validation functions to separate source file and add unit testing #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions float64validator/at_most.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/hashicorp/terraform-plugin-framework-validators/validatordiag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
)

var _ tfsdk.AttributeValidator = atMostValidator{}
Expand Down Expand Up @@ -57,22 +56,3 @@ func AtMost(max float64) tfsdk.AttributeValidator {
max: max,
}
}

// validateFloat ensures that the request contains a Float64 value.
func validateFloat(ctx context.Context, request tfsdk.ValidateAttributeRequest, response *tfsdk.ValidateAttributeResponse) (float64, bool) {
var n types.Float64

diags := tfsdk.ValueAs(ctx, request.AttributeConfig, &n)

if diags.HasError() {
response.Diagnostics = append(response.Diagnostics, diags...)

return 0, false
}

if n.Unknown || n.Null {
return 0, false
}

return n.Value, true
}
27 changes: 27 additions & 0 deletions float64validator/type_validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package float64validator

import (
"context"

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

// validateFloat ensures that the request contains a Float64 value.
func validateFloat(ctx context.Context, request tfsdk.ValidateAttributeRequest, response *tfsdk.ValidateAttributeResponse) (float64, bool) {
var n types.Float64

diags := tfsdk.ValueAs(ctx, request.AttributeConfig, &n)

if diags.HasError() {
response.Diagnostics = append(response.Diagnostics, diags...)

return 0, false
}

if n.Unknown || n.Null {
return 0, false
}

return n.Value, true
}
72 changes: 72 additions & 0 deletions float64validator/type_validation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package float64validator

import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

func TestValidateFloat(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
request tfsdk.ValidateAttributeRequest
expectedFloat64 float64
expectedOk bool
}{
"invalid-type": {
request: tfsdk.ValidateAttributeRequest{
AttributeConfig: types.Bool{Value: true},
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
},
expectedFloat64: 0.0,
expectedOk: false,
},
"float64-null": {
request: tfsdk.ValidateAttributeRequest{
AttributeConfig: types.Float64{Null: true},
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
},
expectedFloat64: 0.0,
expectedOk: false,
},
"float64-value": {
request: tfsdk.ValidateAttributeRequest{
AttributeConfig: types.Float64{Value: 1.2},
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
},
expectedFloat64: 1.2,
expectedOk: true,
},
"float64-unknown": {
request: tfsdk.ValidateAttributeRequest{
AttributeConfig: types.Float64{Unknown: true},
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
},
expectedFloat64: 0.0,
expectedOk: false,
},
}

for name, testCase := range testCases {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

gotFloat64, gotOk := validateFloat(context.Background(), testCase.request, &tfsdk.ValidateAttributeResponse{})

if diff := cmp.Diff(gotFloat64, testCase.expectedFloat64); diff != "" {
t.Errorf("unexpected float64 difference: %s", diff)
}

if diff := cmp.Diff(gotOk, testCase.expectedOk); diff != "" {
t.Errorf("unexpected ok difference: %s", diff)
}
})
}
}
20 changes: 0 additions & 20 deletions int64validator/at_most.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/hashicorp/terraform-plugin-framework-validators/validatordiag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
)

var _ tfsdk.AttributeValidator = atMostValidator{}
Expand Down Expand Up @@ -57,22 +56,3 @@ func AtMost(max int64) tfsdk.AttributeValidator {
max: max,
}
}

// validateInt ensures that the request contains an Int64 value.
func validateInt(ctx context.Context, request tfsdk.ValidateAttributeRequest, response *tfsdk.ValidateAttributeResponse) (int64, bool) {
var n types.Int64

diags := tfsdk.ValueAs(ctx, request.AttributeConfig, &n)

if diags.HasError() {
response.Diagnostics = append(response.Diagnostics, diags...)

return 0, false
}

if n.Unknown || n.Null {
return 0, false
}

return n.Value, true
}
27 changes: 27 additions & 0 deletions int64validator/type_validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package int64validator

import (
"context"

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

// validateInt ensures that the request contains an Int64 value.
func validateInt(ctx context.Context, request tfsdk.ValidateAttributeRequest, response *tfsdk.ValidateAttributeResponse) (int64, bool) {
var n types.Int64

diags := tfsdk.ValueAs(ctx, request.AttributeConfig, &n)

if diags.HasError() {
response.Diagnostics = append(response.Diagnostics, diags...)

return 0, false
}

if n.Unknown || n.Null {
return 0, false
}

return n.Value, true
}
72 changes: 72 additions & 0 deletions int64validator/type_validation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package int64validator

import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

func TestValidateInt(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
request tfsdk.ValidateAttributeRequest
expectedInt64 int64
expectedOk bool
}{
"invalid-type": {
request: tfsdk.ValidateAttributeRequest{
AttributeConfig: types.Bool{Value: true},
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
},
expectedInt64: 0.0,
expectedOk: false,
},
"int64-null": {
request: tfsdk.ValidateAttributeRequest{
AttributeConfig: types.Int64{Null: true},
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
},
expectedInt64: 0.0,
expectedOk: false,
},
"int64-value": {
request: tfsdk.ValidateAttributeRequest{
AttributeConfig: types.Int64{Value: 123},
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
},
expectedInt64: 123,
expectedOk: true,
},
"int64-unknown": {
request: tfsdk.ValidateAttributeRequest{
AttributeConfig: types.Int64{Unknown: true},
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
},
expectedInt64: 0.0,
expectedOk: false,
},
}

for name, testCase := range testCases {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

gotInt64, gotOk := validateInt(context.Background(), testCase.request, &tfsdk.ValidateAttributeResponse{})

if diff := cmp.Diff(gotInt64, testCase.expectedInt64); diff != "" {
t.Errorf("unexpected float64 difference: %s", diff)
}

if diff := cmp.Diff(gotOk, testCase.expectedOk); diff != "" {
t.Errorf("unexpected ok difference: %s", diff)
}
})
}
}
20 changes: 0 additions & 20 deletions stringvalidator/length_at_most.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/hashicorp/terraform-plugin-framework-validators/validatordiag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
)

var _ tfsdk.AttributeValidator = lengthAtMostValidator{}
Expand Down Expand Up @@ -61,22 +60,3 @@ func LengthAtMost(maxLength int) tfsdk.AttributeValidator {
maxLength: maxLength,
}
}

// validateString ensures that the request contains a String value.
func validateString(ctx context.Context, request tfsdk.ValidateAttributeRequest, response *tfsdk.ValidateAttributeResponse) (string, bool) {
var s types.String

diags := tfsdk.ValueAs(ctx, request.AttributeConfig, &s)

if diags.HasError() {
response.Diagnostics = append(response.Diagnostics, diags...)

return "", false
}

if s.Unknown || s.Null {
return "", false
}

return s.Value, true
}
27 changes: 27 additions & 0 deletions stringvalidator/type_validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package stringvalidator

import (
"context"

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

// validateString ensures that the request contains a String value.
func validateString(ctx context.Context, request tfsdk.ValidateAttributeRequest, response *tfsdk.ValidateAttributeResponse) (string, bool) {
var s types.String

diags := tfsdk.ValueAs(ctx, request.AttributeConfig, &s)

if diags.HasError() {
response.Diagnostics = append(response.Diagnostics, diags...)

return "", false
}

if s.Unknown || s.Null {
return "", false
}

return s.Value, true
}
72 changes: 72 additions & 0 deletions stringvalidator/type_validation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package stringvalidator

import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

func TestValidateString(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
request tfsdk.ValidateAttributeRequest
expectedString string
expectedOk bool
}{
"invalid-type": {
request: tfsdk.ValidateAttributeRequest{
AttributeConfig: types.Bool{Value: true},
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
},
expectedString: "",
expectedOk: false,
},
"string-null": {
request: tfsdk.ValidateAttributeRequest{
AttributeConfig: types.Int64{Null: true},
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
},
expectedString: "",
expectedOk: false,
},
"string-value": {
request: tfsdk.ValidateAttributeRequest{
AttributeConfig: types.String{Value: "test-value"},
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
},
expectedString: "test-value",
expectedOk: true,
},
"string-unknown": {
request: tfsdk.ValidateAttributeRequest{
AttributeConfig: types.Int64{Unknown: true},
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
},
expectedString: "",
expectedOk: false,
},
}

for name, testCase := range testCases {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

gotInt64, gotOk := validateString(context.Background(), testCase.request, &tfsdk.ValidateAttributeResponse{})

if diff := cmp.Diff(gotInt64, testCase.expectedString); diff != "" {
t.Errorf("unexpected float64 difference: %s", diff)
}

if diff := cmp.Diff(gotOk, testCase.expectedOk); diff != "" {
t.Errorf("unexpected ok difference: %s", diff)
}
})
}
}