Skip to content

Commit

Permalink
Replace deprecated 'TypeWithValidate' interface (#237)
Browse files Browse the repository at this point in the history
Fixes #231
  • Loading branch information
mitchnielsen authored Jul 17, 2024
1 parent af790d3 commit f83d831
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 70 deletions.
35 changes: 0 additions & 35 deletions internal/provider/customtypes/timestamp_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@ package customtypes
import (
"context"
"fmt"
"time"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/attr/xattr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

var (
_ = basetypes.StringTypable(&TimestampType{})
_ = xattr.TypeWithValidate(&TimestampType{})
_ = fmt.Stringer(&TimestampType{})
)

Expand Down Expand Up @@ -79,34 +75,3 @@ func (t TimestampType) ValueFromTerraform(ctx context.Context, in tftypes.Value)
func (t TimestampType) ValueType(_ context.Context) attr.Value {
return TimestampValue{}
}

// Validate ensures that the string can be converted to a TimestampValue.
func (t TimestampType) Validate(_ context.Context, value tftypes.Value, valuePath path.Path) diag.Diagnostics {
if value.IsNull() || !value.IsKnown() {
return nil
}

var diags diag.Diagnostics
var timestampStr string
if err := value.As(&timestampStr); err != nil {
diags.AddAttributeError(
valuePath,
"Invalid Terraform Value",
fmt.Sprintf("Failed to convert %T to string: %s. Please report this issue to the provider developers.", value, err.Error()),
)

return diags
}

if _, err := time.Parse(time.RFC3339, timestampStr); err != nil {
diags.AddAttributeError(
valuePath,
"Invalid RFC 3339 String Value",
fmt.Sprintf("Failed to parse string %q as RFC 3339 timestamp (YYYY-MM-DDTHH:MM:SSZ): %s", timestampStr, err.Error()),
)

return diags
}

return diags
}
19 changes: 19 additions & 0 deletions internal/provider/customtypes/timestamp_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"time"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/attr/xattr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)

var (
_ = basetypes.StringValuable(&TimestampValue{})
_ = basetypes.StringValuableWithSemanticEquals(&TimestampValue{})
_ = xattr.ValidateableAttribute(&TimestampValue{})
_ = fmt.Stringer(&TimestampValue{})
)

Expand Down Expand Up @@ -123,3 +125,20 @@ func (v TimestampValue) ValueTimePointer() *time.Time {

return &value
}

// ValidateAttribute ensures that the string can be converted to a TimestampValue.
//
//nolint:ireturn // required to implement ValidateAttribute
func (v TimestampValue) ValidateAttribute(_ context.Context, req xattr.ValidateAttributeRequest, resp *xattr.ValidateAttributeResponse) {
if v.IsNull() || v.IsUnknown() {
return
}

if _, err := time.Parse(time.RFC3339, v.ValueString()); err != nil {
resp.Diagnostics.AddAttributeError(
req.Path,
"Invalid RFC 3339 String Value",
fmt.Sprintf("Failed to parse string %q as RFC 3339 timestamp (YYYY-MM-DDTHH:MM:SSZ): %s", v.ValueString(), err.Error()),
)
}
}
35 changes: 0 additions & 35 deletions internal/provider/customtypes/uuid_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@ import (
"context"
"fmt"

"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/attr/xattr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

var (
_ = basetypes.StringTypable(&UUIDType{})
_ = xattr.TypeWithValidate(&UUIDType{})
_ = fmt.Stringer(&UUIDType{})
)

Expand Down Expand Up @@ -79,34 +75,3 @@ func (t UUIDType) ValueFromTerraform(ctx context.Context, in tftypes.Value) (att
func (t UUIDType) ValueType(_ context.Context) attr.Value {
return UUIDValue{}
}

// Validate ensures that the string can be converted to a UUIDValue.
func (t UUIDType) Validate(_ context.Context, value tftypes.Value, valuePath path.Path) diag.Diagnostics {
if value.IsNull() || !value.IsKnown() {
return nil
}

var diags diag.Diagnostics
var uuidStr string
if err := value.As(&uuidStr); err != nil {
diags.AddAttributeError(
valuePath,
"Invalid Terraform Value",
fmt.Sprintf("Failed to convert %T to string: %s. Please report this issue to the provider developers.", value, err.Error()),
)

return diags
}

if _, err := uuid.Parse(uuidStr); err != nil {
diags.AddAttributeError(
valuePath,
"Invalid UUID String Value",
fmt.Sprintf("Failed to parse string %q as a UUID: %s", uuidStr, err.Error()),
)

return diags
}

return diags
}
21 changes: 21 additions & 0 deletions internal/provider/customtypes/uuid_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/attr/xattr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)
Expand All @@ -14,6 +15,7 @@ var (
_ = basetypes.StringValuable(&UUIDValue{})
_ = basetypes.StringValuableWithSemanticEquals(&UUIDValue{})
_ = fmt.Stringer(&UUIDValue{})
_ = xattr.ValidateableAttribute(&UUIDValue{})
)

// UUIDValue implements a custom Terraform value that represents
Expand Down Expand Up @@ -123,3 +125,22 @@ func (v UUIDValue) ValueUUIDPointer() *uuid.UUID {

return &value
}

// ValidateAttribute ensures that the string can be converted to a UUIDValue.
//
//nolint:ireturn // required to implement ValidateAttribute
func (v UUIDValue) ValidateAttribute(_ context.Context, req xattr.ValidateAttributeRequest, resp *xattr.ValidateAttributeResponse) {
if v.IsNull() || v.IsUnknown() {
return
}

if _, err := uuid.Parse(v.ValueString()); err != nil {
resp.Diagnostics.AddAttributeError(
req.Path,
"Invalid UUID String Value",
fmt.Sprintf("Failed to parse string %q as a UUID: %s", v.ValueString(), err.Error()),
)

return
}
}

0 comments on commit f83d831

Please sign in to comment.