Skip to content
This repository has been archived by the owner on Dec 30, 2024. It is now read-only.

Commit

Permalink
fix: equal funcs in nested types
Browse files Browse the repository at this point in the history
  • Loading branch information
azrod committed Oct 23, 2023
1 parent fb65141 commit 2b82d09
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 14 deletions.
23 changes: 22 additions & 1 deletion list_nested_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,28 @@ type ListNestedType struct {
}

func (t ListNestedType) String() string {
return "types.ListType[" + t.ElementType().String() + "]"
return "supertypes.ListType[" + t.ElementType().String() + "]"
}

func (t ListNestedType) Equal(o attr.Type) bool {
switch o.(type) {
case ListNestedType:
other, ok := o.(ListNestedType)
if !ok {
return false
}

return t.ListType.Equal(other.ListType)
case basetypes.ListType:
other, ok := o.(basetypes.ListType)
if !ok {
return false
}

return t.ListType.Equal(other)
default:
return false
}
}

func (t ListNestedType) ValueFromList(_ context.Context, in basetypes.ListValue) (basetypes.ListValuable, diag.Diagnostics) {
Expand Down
23 changes: 22 additions & 1 deletion map_nested_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,28 @@ type MapNestedType struct {
}

func (t MapNestedType) String() string {
return "types.MapType[" + t.ElementType().String() + "]"
return "supertypes.MapType[" + t.ElementType().String() + "]"
}

func (t MapNestedType) Equal(o attr.Type) bool {
switch o.(type) {
case MapNestedType:
other, ok := o.(MapNestedType)
if !ok {
return false
}

return t.MapType.Equal(other.MapType)
case basetypes.MapType:
other, ok := o.(basetypes.MapType)
if !ok {
return false
}

return t.MapType.Equal(other)
default:
return false
}
}

func (t MapNestedType) ValueFromMap(_ context.Context, in basetypes.MapValue) (basetypes.MapValuable, diag.Diagnostics) {
Expand Down
23 changes: 22 additions & 1 deletion set_nested_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,28 @@ type SetNestedType struct {
}

func (t SetNestedType) String() string {
return "types.SetType[" + t.ElementType().String() + "]"
return "supertypes.SetType[" + t.ElementType().String() + "]"
}

func (t SetNestedType) Equal(o attr.Type) bool {
switch o.(type) {
case SetNestedType:
other, ok := o.(SetNestedType)
if !ok {
return false
}

return t.SetType.Equal(other.SetType)
case basetypes.SetType:
other, ok := o.(basetypes.SetType)
if !ok {
return false
}

return t.SetType.Equal(other)
default:
return false
}
}

func (t SetNestedType) ValueFromSet(_ context.Context, in basetypes.SetValue) (basetypes.SetValuable, diag.Diagnostics) {
Expand Down
19 changes: 14 additions & 5 deletions single_nested_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ func (t SingleNestedType) Equal(o attr.Type) bool {
}

return t.ObjectType.Equal(other)
case basetypes.ListType:
other, ok := o.(basetypes.ListType)
if !ok {
return false
}

return t.ListType.Equal(other)

Check failure on line 47 in single_nested_type.go

View workflow job for this annotation

GitHub Actions / Lint

t.ListType undefined (type SingleNestedType has no field or method ListType)

Check failure on line 47 in single_nested_type.go

View workflow job for this annotation

GitHub Actions / coverage (1.20)

t.ListType undefined (type SingleNestedType has no field or method ListType)

Check failure on line 47 in single_nested_type.go

View workflow job for this annotation

GitHub Actions / call-workflow-tests / Lint

t.ListType undefined (type SingleNestedType has no field or method ListType)

Check failure on line 47 in single_nested_type.go

View workflow job for this annotation

GitHub Actions / call-workflow-tests / coverage (1.20)

t.ListType undefined (type SingleNestedType has no field or method ListType)
case basetypes.SetType:
other, ok := o.(basetypes.SetType)
if !ok {
return false
}

return t.SetType.Equal(other)

Check failure on line 54 in single_nested_type.go

View workflow job for this annotation

GitHub Actions / Lint

t.SetType undefined (type SingleNestedType has no field or method SetType) (typecheck)

Check failure on line 54 in single_nested_type.go

View workflow job for this annotation

GitHub Actions / coverage (1.20)

t.SetType undefined (type SingleNestedType has no field or method SetType)

Check failure on line 54 in single_nested_type.go

View workflow job for this annotation

GitHub Actions / call-workflow-tests / Lint

t.SetType undefined (type SingleNestedType has no field or method SetType) (typecheck)

Check failure on line 54 in single_nested_type.go

View workflow job for this annotation

GitHub Actions / call-workflow-tests / coverage (1.20)

t.SetType undefined (type SingleNestedType has no field or method SetType)
default:
return false
}
Expand Down Expand Up @@ -91,8 +105,3 @@ func (t SingleNestedType) ValueFromTerraform(ctx context.Context, in tftypes.Val

return value, nil
}

func (t SingleNestedType) ValueType(ctx context.Context) attr.Value {
// SingleNestedValue defined in the value type section
return SingleNestedValue{}
}
23 changes: 22 additions & 1 deletion templates/nested_type.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,28 @@ type {{ .TypeName }}NestedType struct {
}

func (t {{ .TypeName }}NestedType) String() string {
return "types.{{ .TypeName }}Type[" + t.ElementType().String() + "]"
return "supertypes.{{ .TypeName }}Type[" + t.ElementType().String() + "]"
}

func (t {{ .TypeName }}NestedType) Equal(o attr.Type) bool {
switch o.(type) {
case {{ .TypeName }}NestedType:
other, ok := o.({{ .TypeName }}NestedType)
if !ok {
return false
}

return t.{{ .TypeName }}Type.Equal(other.{{ .TypeName }}Type)
case basetypes.{{ .TypeName }}Type:
other, ok := o.(basetypes.{{ .TypeName }}Type)
if !ok {
return false
}

return t.{{ .TypeName }}Type.Equal(other)
default:
return false
}
}

func (t {{ .TypeName }}NestedType) ValueFrom{{ .TypeName }}(_ context.Context, in basetypes.{{ .TypeName }}Value) (basetypes.{{ .TypeName }}Valuable, diag.Diagnostics) {
Expand Down
35 changes: 30 additions & 5 deletions templates/single_nested_type.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,43 @@ type {{ .TypeName }}NestedType struct {
}

func (t {{ .TypeName }}NestedType) Equal(o attr.Type) bool {
other, ok := o.(basetypes.ObjectType)
switch o.(type) {
case SingleNestedType:
other, ok := o.(SingleNestedType)
if !ok {
return false
}

if !ok {
return t.ObjectType.Equal(other.ObjectType)
case basetypes.ObjectType:
other, ok := o.(basetypes.ObjectType)
if !ok {
return false
}

return t.ObjectType.Equal(other)
case basetypes.ListType:
other, ok := o.(basetypes.ListType)
if !ok {
return false
}

return t.ListType.Equal(other)
case basetypes.SetType:
other, ok := o.(basetypes.SetType)
if !ok {
return false
}

return t.SetType.Equal(other)
default:
return false
}

return t.ObjectType.Equal(other)
}

func (t {{ .TypeName }}NestedType) String() string {
var res strings.Builder
res.WriteString("types.ObjectType[")
res.WriteString("supertypes.SingleNestedType[")
keys := make([]string, 0, len(t.AttrTypes))
for k := range t.AttrTypes {
keys = append(keys, k)
Expand Down

0 comments on commit 2b82d09

Please sign in to comment.