Skip to content

Commit

Permalink
Rename CustomStringValue and CustomStringType to AAPCustomStringValue…
Browse files Browse the repository at this point in the history
… and AAPCustomStringType

Signed-off-by: Alina Buzachis <[email protected]>
  • Loading branch information
alinabuzachis committed Mar 11, 2024
1 parent 6e3256b commit b756aa4
Show file tree
Hide file tree
Showing 19 changed files with 208 additions and 211 deletions.
2 changes: 1 addition & 1 deletion examples/resources/group/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ output "group_abc" {

output "group_xyz" {
value = aap_group.sample_xyz
}
}
2 changes: 1 addition & 1 deletion examples/resources/host/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ output "host_abc" {
}
output "host_xyz" {
value = aap_host.sample_xyz
}
}
2 changes: 1 addition & 1 deletion examples/resources/inventory/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ output "inventory_abc" {

output "inventory_xyz" {
value = aap_inventory.sample_xyz
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ import (
)

var (
_ = basetypes.StringTypable(&CustomStringType{})
_ = xattr.TypeWithValidate(&CustomStringType{})
_ = basetypes.StringTypable(&AAPCustomStringType{})
_ = xattr.TypeWithValidate(&AAPCustomStringType{})
)

// CustomStringType implements a custom Terraform type.
type CustomStringType struct {
// AAPCustomStringType implements a custom Terraform type.
type AAPCustomStringType struct {
basetypes.StringType
}

// / Equal returns true if the given type is equivalent.
func (t CustomStringType) Equal(o attr.Type) bool {
other, ok := o.(CustomStringType)
func (t AAPCustomStringType) Equal(o attr.Type) bool {
other, ok := o.(AAPCustomStringType)
if !ok {
return false
}
Expand All @@ -33,21 +33,21 @@ func (t CustomStringType) Equal(o attr.Type) bool {
}

// String returns a human readable string of the type name.
func (t CustomStringType) String() string {
return "customtypes.CustomStringType"
func (t AAPCustomStringType) String() string {
return "customtypes.AAPCustomStringType"
}

// / ValueFromString returns a StringValuable type given a StringValue.
func (t CustomStringType) ValueFromString(_ context.Context, in basetypes.StringValue) (basetypes.StringValuable, diag.Diagnostics) {
value := CustomStringValue{
func (t AAPCustomStringType) ValueFromString(_ context.Context, in basetypes.StringValue) (basetypes.StringValuable, diag.Diagnostics) {
value := AAPCustomStringValue{
StringValue: in,
}

return value, nil
}

// ValueFromTerraform converts a Terraform value to a CustomStringValue.
func (t CustomStringType) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) {
// ValueFromTerraform converts a Terraform value to a AAPCustomStringValue.
func (t AAPCustomStringType) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) {
attrValue, err := t.StringType.ValueFromTerraform(ctx, in)
if err != nil {
return nil, fmt.Errorf("unexpected error converting value from Terraform: %w", err)
Expand All @@ -67,12 +67,12 @@ func (t CustomStringType) ValueFromTerraform(ctx context.Context, in tftypes.Val
}

// ValueType returns an instance of the value.
func (t CustomStringType) ValueType(_ context.Context) attr.Value {
return CustomStringValue{}
func (t AAPCustomStringType) ValueType(_ context.Context) attr.Value {
return AAPCustomStringValue{}
}

// Validate implements type validation. This type requires the value provided to be a String value.
func (t CustomStringType) Validate(_ context.Context, in tftypes.Value, path path.Path) diag.Diagnostics {
func (t AAPCustomStringType) Validate(_ context.Context, in tftypes.Value, path path.Path) diag.Diagnostics {
var diags diag.Diagnostics

if in.Type() == nil {
Expand All @@ -83,7 +83,7 @@ func (t CustomStringType) Validate(_ context.Context, in tftypes.Value, path pat
err := fmt.Errorf("expected String value, received %T with value: %v", in, in)
diags.AddAttributeError(
path,
"CustomString Type Validation Error",
"AAPCustomString Type Validation Error",
"An unexpected error was encountered trying to validate an attribute value. This is always an error in the provider. "+
"Please report the following to the provider developer:\n\n"+err.Error(),
)
Expand All @@ -99,7 +99,7 @@ func (t CustomStringType) Validate(_ context.Context, in tftypes.Value, path pat
if err := in.As(&valueString); err != nil {
diags.AddAttributeError(
path,
"CustomString Type Validation Error",
"AAPCustomString Type Validation Error",
"An unexpected error was encountered trying to validate an attribute value. This is always an error in the provider. "+
"Please report the following to the provider developer:\n\n"+err.Error(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

func TestCustomStringTypeValidate(t *testing.T) {
func TestAAPCustomStringTypeValidate(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
Expand Down Expand Up @@ -52,7 +52,7 @@ func TestCustomStringTypeValidate(t *testing.T) {
expectedDiags: diag.Diagnostics{
diag.NewAttributeErrorDiagnostic(
path.Root("test"),
"CustomString Type Validation Error",
"AAPCustomString Type Validation Error",
"An unexpected error was encountered trying to validate an attribute value. This is always "+
"an error in the provider. Please report the following to the provider developer:\n\n"+
"expected String value, received tftypes.Value with value: tftypes.Number<\"123\">",
Expand All @@ -65,7 +65,7 @@ func TestCustomStringTypeValidate(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()

diags := customtypes.CustomStringType{}.Validate(context.Background(), testCase.in, path.Root("test"))
diags := customtypes.AAPCustomStringType{}.Validate(context.Background(), testCase.in, path.Root("test"))

if diff := cmp.Diff(diags, testCase.expectedDiags); diff != "" {
t.Errorf("Unexpected diagnostics (-got, +expected): %s", diff)
Expand All @@ -74,7 +74,7 @@ func TestCustomStringTypeValidate(t *testing.T) {
}
}

func TestCustomStringTypeValueFromTerraform(t *testing.T) {
func TestAAPCustomStringTypeValueFromTerraform(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
Expand All @@ -84,15 +84,15 @@ func TestCustomStringTypeValueFromTerraform(t *testing.T) {
}{
"true": {
in: tftypes.NewValue(tftypes.String, `{"hello":"world"}`),
expectation: customtypes.NewCustomStringValue(`{"hello":"world"}`),
expectation: customtypes.NewAAPCustomStringValue(`{"hello":"world"}`),
},
"unknown": {
in: tftypes.NewValue(tftypes.String, tftypes.UnknownValue),
expectation: customtypes.NewCustomStringUnknown(),
expectation: customtypes.NewAAPCustomStringUnknown(),
},
"null": {
in: tftypes.NewValue(tftypes.String, nil),
expectation: customtypes.NewCustomStringNull(),
expectation: customtypes.NewAAPCustomStringNull(),
},
"wrongType": {
in: tftypes.NewValue(tftypes.Number, 123),
Expand All @@ -105,7 +105,7 @@ func TestCustomStringTypeValueFromTerraform(t *testing.T) {
t.Parallel()
ctx := context.Background()

got, err := customtypes.CustomStringType{}.ValueFromTerraform(ctx, testCase.in)
got, err := customtypes.AAPCustomStringType{}.ValueFromTerraform(ctx, testCase.in)
if err != nil {
if testCase.expectedErr == "" {
t.Fatalf("Unexpected error: %s", err)
Expand Down
93 changes: 93 additions & 0 deletions internal/provider/customtypes/aapcustomstring_value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package customtypes

import (
"context"
"fmt"
"strings"

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

var (
_ = basetypes.StringValuable(&AAPCustomStringValue{})
_ = basetypes.StringValuableWithSemanticEquals(&AAPCustomStringValue{})
)

// AAPCustomStringValue implements a custom Terraform value.
type AAPCustomStringValue struct {
basetypes.StringValue
}

// NewAAPCustomStringNull creates a AAPCustomStringValue with a null value. Determine
// whether the value is null via the AAPCustomStringValue type IsNull method.
func NewAAPCustomStringNull() AAPCustomStringValue {
return AAPCustomStringValue{
StringValue: basetypes.NewStringNull(),
}
}

// NewAAPCustomStringUnknown creates a AAPCustomStringValue with an unknown value.
func NewAAPCustomStringUnknown() AAPCustomStringValue {
return AAPCustomStringValue{
StringValue: basetypes.NewStringUnknown(),
}
}

// NewAAPCustomStringValue creates a AAPCustomStringValue with a known value.
func NewAAPCustomStringValue(value string) AAPCustomStringValue {
return AAPCustomStringValue{
StringValue: basetypes.NewStringValue(value),
}
}

// NewAAPCustomStringPointerValue creates a AAPCustomStringValue with a null value if
// nil or a known value.
func NewCustomStringPointerValue(value *string) AAPCustomStringValue {
if value == nil {
return NewAAPCustomStringNull()
}

return NewAAPCustomStringValue(*value)
}

// Equal returns true if the given value is equivalent.
func (v AAPCustomStringValue) Equal(o attr.Value) bool {
other, ok := o.(AAPCustomStringValue)
if !ok {
return false
}

return v.StringValue.Equal(other.StringValue)
}

// Type returns an instance of the type.
func (v AAPCustomStringValue) Type(_ context.Context) attr.Type {
return AAPCustomStringType{}
}

func (v AAPCustomStringValue) String() string {
return "AAPCustomStringValue"
}

// StringSemanticEquals checks if two AAPCustomStringValue objects have
// equivalent values, even if they are not equal.
func (v AAPCustomStringValue) StringSemanticEquals(_ context.Context, newValuable basetypes.StringValuable) (bool, diag.Diagnostics) {
var diags diag.Diagnostics

newValue, ok := newValuable.(AAPCustomStringValue)
if !ok {
diags.AddError(
"Semantic Equality Check Error",
fmt.Sprintf("Expected value type %T but got value type %T. Please report this to the provider developers.", v, newValuable),
)

return false, diags
}

priorValue := v.ValueString()
currentValue := strings.TrimSpace(newValue.ValueString())

return priorValue == currentValue, nil
}
96 changes: 0 additions & 96 deletions internal/provider/customtypes/customstring_value.go

This file was deleted.

Loading

0 comments on commit b756aa4

Please sign in to comment.