Skip to content

Commit

Permalink
attr: Add attribute path parameter to (TypeWithValidate).Validate() m…
Browse files Browse the repository at this point in the history
…ethod

Reference: #127
  • Loading branch information
bflad committed Sep 7, 2021
1 parent 7055f97 commit 23bcbac
Show file tree
Hide file tree
Showing 22 changed files with 101 additions and 83 deletions.
2 changes: 1 addition & 1 deletion attr/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ type TypeWithValidate interface {
// being used to populate the Type. It is generally used to check the
// data format and ensure that it complies with the requirements of the
// Type.
Validate(context.Context, tftypes.Value) diag.Diagnostics
Validate(context.Context, tftypes.Value, *tftypes.AttributePath) diag.Diagnostics
}

// TypeWithPlaintextDescription extends the Type interface to include a
Expand Down
14 changes: 7 additions & 7 deletions internal/reflect/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func FromUnknownable(ctx context.Context, typ attr.Type, val Unknownable, path *
tfVal := tftypes.NewValue(typ.TerraformType(ctx), tftypes.UnknownValue)

if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfVal)...)
diags.Append(typeWithValidate.Validate(ctx, tfVal, path)...)

if diags.HasError() {
return nil, diags
Expand All @@ -91,7 +91,7 @@ func FromUnknownable(ctx context.Context, typ attr.Type, val Unknownable, path *
tfVal := tftypes.NewValue(typ.TerraformType(ctx), val.GetValue(ctx))

if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfVal)...)
diags.Append(typeWithValidate.Validate(ctx, tfVal, path)...)

if diags.HasError() {
return nil, diags
Expand Down Expand Up @@ -164,7 +164,7 @@ func FromNullable(ctx context.Context, typ attr.Type, val Nullable, path *tftype
tfVal := tftypes.NewValue(typ.TerraformType(ctx), nil)

if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfVal)...)
diags.Append(typeWithValidate.Validate(ctx, tfVal, path)...)

if diags.HasError() {
return nil, diags
Expand All @@ -185,7 +185,7 @@ func FromNullable(ctx context.Context, typ attr.Type, val Nullable, path *tftype
tfVal := tftypes.NewValue(typ.TerraformType(ctx), val.GetValue(ctx))

if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfVal)...)
diags.Append(typeWithValidate.Validate(ctx, tfVal, path)...)

if diags.HasError() {
return nil, diags
Expand Down Expand Up @@ -256,7 +256,7 @@ func FromValueCreator(ctx context.Context, typ attr.Type, val tftypes.ValueCreat
tfVal := tftypes.NewValue(typ.TerraformType(ctx), raw)

if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfVal)...)
diags.Append(typeWithValidate.Validate(ctx, tfVal, path)...)

if diags.HasError() {
return nil, diags
Expand All @@ -279,7 +279,7 @@ func NewAttributeValue(ctx context.Context, typ attr.Type, val tftypes.Value, ta
var diags diag.Diagnostics

if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, val)...)
diags.Append(typeWithValidate.Validate(ctx, val, path)...)

if diags.HasError() {
return target, diags
Expand Down Expand Up @@ -319,7 +319,7 @@ func FromAttributeValue(ctx context.Context, typ attr.Type, val attr.Value, path
return val, append(diags, toTerraformValueErrorDiag(err, path))
}

diags.Append(typeWithValidate.Validate(ctx, tftypes.NewValue(tfType, tfVal))...)
diags.Append(typeWithValidate.Validate(ctx, tftypes.NewValue(tfType, tfVal), path)...)

if diags.HasError() {
return val, diags
Expand Down
6 changes: 3 additions & 3 deletions internal/reflect/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func FromMap(ctx context.Context, typ attr.TypeWithElementType, val reflect.Valu
tfVal := tftypes.NewValue(tfType, nil)

if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfVal)...)
diags.Append(typeWithValidate.Validate(ctx, tfVal, path)...)

if diags.HasError() {
return nil, diags
Expand Down Expand Up @@ -156,7 +156,7 @@ func FromMap(ctx context.Context, typ attr.TypeWithElementType, val reflect.Valu
tfElemVal := tftypes.NewValue(tfElemType, tfVal)

if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfElemVal)...)
diags.Append(typeWithValidate.Validate(ctx, tfElemVal, path.WithElementKeyString(key.String()))...)

if diags.HasError() {
return nil, diags
Expand All @@ -174,7 +174,7 @@ func FromMap(ctx context.Context, typ attr.TypeWithElementType, val reflect.Valu
tfVal := tftypes.NewValue(tfType, tfElems)

if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfVal)...)
diags.Append(typeWithValidate.Validate(ctx, tfVal, path)...)

if diags.HasError() {
return nil, diags
Expand Down
10 changes: 5 additions & 5 deletions internal/reflect/number.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func FromInt(ctx context.Context, typ attr.Type, val int64, path *tftypes.Attrib
tfNum := tftypes.NewValue(tftypes.Number, val)

if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfNum)...)
diags.Append(typeWithValidate.Validate(ctx, tfNum, path)...)

if diags.HasError() {
return nil, diags
Expand All @@ -272,7 +272,7 @@ func FromUint(ctx context.Context, typ attr.Type, val uint64, path *tftypes.Attr
tfNum := tftypes.NewValue(tftypes.Number, val)

if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfNum)...)
diags.Append(typeWithValidate.Validate(ctx, tfNum, path)...)

if diags.HasError() {
return nil, diags
Expand All @@ -299,7 +299,7 @@ func FromFloat(ctx context.Context, typ attr.Type, val float64, path *tftypes.At
tfNum := tftypes.NewValue(tftypes.Number, val)

if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfNum)...)
diags.Append(typeWithValidate.Validate(ctx, tfNum, path)...)

if diags.HasError() {
return nil, diags
Expand All @@ -326,7 +326,7 @@ func FromBigFloat(ctx context.Context, typ attr.Type, val *big.Float, path *tfty
tfNum := tftypes.NewValue(tftypes.Number, val)

if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfNum)...)
diags.Append(typeWithValidate.Validate(ctx, tfNum, path)...)

if diags.HasError() {
return nil, diags
Expand Down Expand Up @@ -354,7 +354,7 @@ func FromBigInt(ctx context.Context, typ attr.Type, val *big.Int, path *tftypes.
tfNum := tftypes.NewValue(tftypes.Number, fl)

if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfNum)...)
diags.Append(typeWithValidate.Validate(ctx, tfNum, path)...)

if diags.HasError() {
return nil, diags
Expand Down
20 changes: 10 additions & 10 deletions internal/reflect/number_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1202,14 +1202,14 @@ func TestFromInt(t *testing.T) {
Value: big.NewFloat(1),
},
expectedDiags: diag.Diagnostics{
testtypes.TestWarningDiagnostic,
testtypes.TestWarningDiagnostic(tftypes.NewAttributePath()),
},
},
"WithValidateError": {
val: 1,
typ: testtypes.NumberTypeWithValidateError{},
expectedDiags: diag.Diagnostics{
testtypes.TestErrorDiagnostic,
testtypes.TestErrorDiagnostic(tftypes.NewAttributePath()),
},
},
}
Expand Down Expand Up @@ -1261,14 +1261,14 @@ func TestFromUint(t *testing.T) {
Value: big.NewFloat(1),
},
expectedDiags: diag.Diagnostics{
testtypes.TestWarningDiagnostic,
testtypes.TestWarningDiagnostic(tftypes.NewAttributePath()),
},
},
"WithValidateError": {
val: 1,
typ: testtypes.NumberTypeWithValidateError{},
expectedDiags: diag.Diagnostics{
testtypes.TestErrorDiagnostic,
testtypes.TestErrorDiagnostic(tftypes.NewAttributePath()),
},
},
}
Expand Down Expand Up @@ -1327,14 +1327,14 @@ func TestFromFloat(t *testing.T) {
Value: big.NewFloat(1),
},
expectedDiags: diag.Diagnostics{
testtypes.TestWarningDiagnostic,
testtypes.TestWarningDiagnostic(tftypes.NewAttributePath()),
},
},
"WithValidateError": {
val: 1,
typ: testtypes.NumberTypeWithValidateError{},
expectedDiags: diag.Diagnostics{
testtypes.TestErrorDiagnostic,
testtypes.TestErrorDiagnostic(tftypes.NewAttributePath()),
},
},
}
Expand Down Expand Up @@ -1393,14 +1393,14 @@ func TestFromBigFloat(t *testing.T) {
Value: big.NewFloat(1),
},
expectedDiags: diag.Diagnostics{
testtypes.TestWarningDiagnostic,
testtypes.TestWarningDiagnostic(tftypes.NewAttributePath()),
},
},
"WithValidateError": {
val: big.NewFloat(1),
typ: testtypes.NumberTypeWithValidateError{},
expectedDiags: diag.Diagnostics{
testtypes.TestErrorDiagnostic,
testtypes.TestErrorDiagnostic(tftypes.NewAttributePath()),
},
},
}
Expand Down Expand Up @@ -1452,14 +1452,14 @@ func TestFromBigInt(t *testing.T) {
Value: big.NewFloat(1),
},
expectedDiags: diag.Diagnostics{
testtypes.TestWarningDiagnostic,
testtypes.TestWarningDiagnostic(tftypes.NewAttributePath()),
},
},
"WithValidateError": {
val: big.NewInt(1),
typ: testtypes.NumberTypeWithValidateError{},
expectedDiags: diag.Diagnostics{
testtypes.TestErrorDiagnostic,
testtypes.TestErrorDiagnostic(tftypes.NewAttributePath()),
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/reflect/pointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func FromPointer(ctx context.Context, typ attr.Type, value reflect.Value, path *
tfVal := tftypes.NewValue(typ.TerraformType(ctx), nil)

if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfVal)...)
diags.Append(typeWithValidate.Validate(ctx, tfVal, path)...)

if diags.HasError() {
return nil, diags
Expand Down
4 changes: 2 additions & 2 deletions internal/reflect/pointer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestFromPointer(t *testing.T) {
typ: testtypes.StringTypeWithValidateError{},
val: reflect.ValueOf(strPtr("hello, world")),
expectedDiags: diag.Diagnostics{
testtypes.TestErrorDiagnostic,
testtypes.TestErrorDiagnostic(tftypes.NewAttributePath()),
},
},
"WithValidateWarning": {
Expand All @@ -119,7 +119,7 @@ func TestFromPointer(t *testing.T) {
Value: "hello, world",
},
expectedDiags: diag.Diagnostics{
testtypes.TestWarningDiagnostic,
testtypes.TestWarningDiagnostic(tftypes.NewAttributePath()),
},
},
}
Expand Down
4 changes: 2 additions & 2 deletions internal/reflect/primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func FromString(ctx context.Context, typ attr.Type, val string, path *tftypes.At
tfStr := tftypes.NewValue(tftypes.String, val)

if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfStr)...)
diags.Append(typeWithValidate.Validate(ctx, tfStr, path)...)

if diags.HasError() {
return nil, diags
Expand All @@ -92,7 +92,7 @@ func FromBool(ctx context.Context, typ attr.Type, val bool, path *tftypes.Attrib
tfBool := tftypes.NewValue(tftypes.Bool, val)

if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfBool)...)
diags.Append(typeWithValidate.Validate(ctx, tfBool, path)...)

if diags.HasError() {
return nil, diags
Expand Down
8 changes: 4 additions & 4 deletions internal/reflect/primitive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ func TestFromString(t *testing.T) {
Value: "mystring",
},
expectedDiags: diag.Diagnostics{
testtypes.TestWarningDiagnostic,
testtypes.TestWarningDiagnostic(tftypes.NewAttributePath()),
},
},
"WithValidateError": {
val: "mystring",
typ: testtypes.StringTypeWithValidateError{},
expectedDiags: diag.Diagnostics{
testtypes.TestErrorDiagnostic,
testtypes.TestErrorDiagnostic(tftypes.NewAttributePath()),
},
},
}
Expand Down Expand Up @@ -159,14 +159,14 @@ func TestFromBool(t *testing.T) {
Value: true,
},
expectedDiags: diag.Diagnostics{
testtypes.TestWarningDiagnostic,
testtypes.TestWarningDiagnostic(tftypes.NewAttributePath()),
},
},
"WithValidateError": {
val: true,
typ: testtypes.BoolTypeWithValidateError{},
expectedDiags: diag.Diagnostics{
testtypes.TestErrorDiagnostic,
testtypes.TestErrorDiagnostic(tftypes.NewAttributePath()),
},
},
}
Expand Down
6 changes: 3 additions & 3 deletions internal/reflect/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func FromSlice(ctx context.Context, typ attr.Type, val reflect.Value, path *tfty
tfVal := tftypes.NewValue(tfType, nil)

if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfVal)...)
diags.Append(typeWithValidate.Validate(ctx, tfVal, path)...)

if diags.HasError() {
return nil, diags
Expand Down Expand Up @@ -152,7 +152,7 @@ func FromSlice(ctx context.Context, typ attr.Type, val reflect.Value, path *tfty
tfElemVal := tftypes.NewValue(elemType.TerraformType(ctx), tfVal)

if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfElemVal)...)
diags.Append(typeWithValidate.Validate(ctx, tfElemVal, path.WithElementKeyInt(int64(i)))...)

if diags.HasError() {
return nil, diags
Expand All @@ -170,7 +170,7 @@ func FromSlice(ctx context.Context, typ attr.Type, val reflect.Value, path *tfty
tfVal := tftypes.NewValue(tfType, tfElems)

if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfVal)...)
diags.Append(typeWithValidate.Validate(ctx, tfVal, path)...)

if diags.HasError() {
return nil, diags
Expand Down
4 changes: 2 additions & 2 deletions internal/reflect/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func FromStruct(ctx context.Context, typ attr.TypeWithAttributeTypes, val reflec
tfObjVal := tftypes.NewValue(objTypes[name], tfVal)

if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfObjVal)...)
diags.Append(typeWithValidate.Validate(ctx, tfObjVal, path)...)

if diags.HasError() {
return nil, diags
Expand All @@ -219,7 +219,7 @@ func FromStruct(ctx context.Context, typ attr.TypeWithAttributeTypes, val reflec
}, objValues)

if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfVal)...)
diags.Append(typeWithValidate.Validate(ctx, tfVal, path)...)

if diags.HasError() {
return nil, diags
Expand Down
8 changes: 4 additions & 4 deletions internal/testing/types/boolwithvalidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ type BoolTypeWithValidateWarning struct {
BoolType
}

func (t BoolTypeWithValidateError) Validate(ctx context.Context, in tftypes.Value) diag.Diagnostics {
return diag.Diagnostics{TestErrorDiagnostic}
func (t BoolTypeWithValidateError) Validate(ctx context.Context, in tftypes.Value, path *tftypes.AttributePath) diag.Diagnostics {
return diag.Diagnostics{TestErrorDiagnostic(path)}
}

func (t BoolTypeWithValidateWarning) Validate(ctx context.Context, in tftypes.Value) diag.Diagnostics {
return diag.Diagnostics{TestWarningDiagnostic}
func (t BoolTypeWithValidateWarning) Validate(ctx context.Context, in tftypes.Value, path *tftypes.AttributePath) diag.Diagnostics {
return diag.Diagnostics{TestWarningDiagnostic(path)}
}
14 changes: 10 additions & 4 deletions internal/testing/types/diags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@ package types

import (
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

var (
TestErrorDiagnostic = diag.NewErrorDiagnostic(
func TestErrorDiagnostic(path *tftypes.AttributePath) diag.AttributeErrorDiagnostic {
return diag.NewAttributeErrorDiagnostic(
path,
"Error Diagnostic",
"This is an error.",
)
TestWarningDiagnostic = diag.NewWarningDiagnostic(
}

func TestWarningDiagnostic(path *tftypes.AttributePath) diag.AttributeWarningDiagnostic {
return diag.NewAttributeWarningDiagnostic(
path,
"Warning Diagnostic",
"This is a warning.",
)
)
}
Loading

0 comments on commit 23bcbac

Please sign in to comment.