|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package types |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" |
| 13 | + "github.com/hashicorp/terraform-plugin-go/tftypes" |
| 14 | + "github.com/hashicorp/terraform-provider-aws/internal/errs/fwdiag" |
| 15 | +) |
| 16 | + |
| 17 | +// setTypeOf is the attribute type of a SetValueOf. |
| 18 | +type setTypeOf[T attr.Value] struct { |
| 19 | + basetypes.SetType |
| 20 | +} |
| 21 | + |
| 22 | +var ( |
| 23 | + SetOfStringType = setTypeOf[basetypes.StringValue]{basetypes.SetType{ElemType: basetypes.StringType{}}} |
| 24 | +) |
| 25 | + |
| 26 | +var ( |
| 27 | + _ basetypes.SetTypable = (*setTypeOf[basetypes.StringValue])(nil) |
| 28 | + _ basetypes.SetValuable = (*SetValueOf[basetypes.StringValue])(nil) |
| 29 | +) |
| 30 | + |
| 31 | +func newAttrTypeOf[T attr.Value](ctx context.Context) attr.Type { |
| 32 | + var zero T |
| 33 | + return zero.Type(ctx) |
| 34 | +} |
| 35 | + |
| 36 | +func NewSetTypeOf[T attr.Value](ctx context.Context) setTypeOf[T] { |
| 37 | + return setTypeOf[T]{basetypes.SetType{ElemType: newAttrTypeOf[T](ctx)}} |
| 38 | +} |
| 39 | + |
| 40 | +func (t setTypeOf[T]) Equal(o attr.Type) bool { |
| 41 | + other, ok := o.(setTypeOf[T]) |
| 42 | + |
| 43 | + if !ok { |
| 44 | + return false |
| 45 | + } |
| 46 | + |
| 47 | + return t.SetType.Equal(other.SetType) |
| 48 | +} |
| 49 | + |
| 50 | +func (t setTypeOf[T]) String() string { |
| 51 | + var zero T |
| 52 | + return fmt.Sprintf("SetTypeOf[%T]", zero) |
| 53 | +} |
| 54 | + |
| 55 | +func (t setTypeOf[T]) ValueFromSet(ctx context.Context, in basetypes.SetValue) (basetypes.SetValuable, diag.Diagnostics) { |
| 56 | + var diags diag.Diagnostics |
| 57 | + |
| 58 | + if in.IsNull() { |
| 59 | + return NewSetValueOfNull[T](ctx), diags |
| 60 | + } |
| 61 | + if in.IsUnknown() { |
| 62 | + return NewSetValueOfUnknown[T](ctx), diags |
| 63 | + } |
| 64 | + |
| 65 | + setValue, d := basetypes.NewSetValue(newAttrTypeOf[T](ctx), in.Elements()) |
| 66 | + diags.Append(d...) |
| 67 | + if diags.HasError() { |
| 68 | + return NewSetValueOfUnknown[T](ctx), diags |
| 69 | + } |
| 70 | + |
| 71 | + value := SetValueOf[T]{ |
| 72 | + SetValue: setValue, |
| 73 | + } |
| 74 | + |
| 75 | + return value, diags |
| 76 | +} |
| 77 | + |
| 78 | +func (t setTypeOf[T]) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) { |
| 79 | + attrValue, err := t.SetType.ValueFromTerraform(ctx, in) |
| 80 | + |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + |
| 85 | + setValue, ok := attrValue.(basetypes.SetValue) |
| 86 | + |
| 87 | + if !ok { |
| 88 | + return nil, fmt.Errorf("unexpected value type of %T", attrValue) |
| 89 | + } |
| 90 | + |
| 91 | + setValuable, diags := t.ValueFromSet(ctx, setValue) |
| 92 | + |
| 93 | + if diags.HasError() { |
| 94 | + return nil, fmt.Errorf("unexpected error converting SetValue to SetValuable: %v", diags) |
| 95 | + } |
| 96 | + |
| 97 | + return setValuable, nil |
| 98 | +} |
| 99 | + |
| 100 | +func (t setTypeOf[T]) ValueType(ctx context.Context) attr.Value { |
| 101 | + return SetValueOf[T]{} |
| 102 | +} |
| 103 | + |
| 104 | +// SetValueOf represents a Terraform Plugin Framework Set value whose elements are of type `T`. |
| 105 | +type SetValueOf[T attr.Value] struct { |
| 106 | + basetypes.SetValue |
| 107 | +} |
| 108 | + |
| 109 | +func (v SetValueOf[T]) Equal(o attr.Value) bool { |
| 110 | + other, ok := o.(SetValueOf[T]) |
| 111 | + |
| 112 | + if !ok { |
| 113 | + return false |
| 114 | + } |
| 115 | + |
| 116 | + return v.SetValue.Equal(other.SetValue) |
| 117 | +} |
| 118 | + |
| 119 | +func (v SetValueOf[T]) Type(ctx context.Context) attr.Type { |
| 120 | + return NewSetTypeOf[T](ctx) |
| 121 | +} |
| 122 | + |
| 123 | +func NewSetValueOfNull[T attr.Value](ctx context.Context) SetValueOf[T] { |
| 124 | + return SetValueOf[T]{SetValue: basetypes.NewSetNull(newAttrTypeOf[T](ctx))} |
| 125 | +} |
| 126 | + |
| 127 | +func NewSetValueOfUnknown[T attr.Value](ctx context.Context) SetValueOf[T] { |
| 128 | + return SetValueOf[T]{SetValue: basetypes.NewSetUnknown(newAttrTypeOf[T](ctx))} |
| 129 | +} |
| 130 | + |
| 131 | +func NewSetValueOf[T attr.Value](ctx context.Context, elements []attr.Value) (SetValueOf[T], diag.Diagnostics) { |
| 132 | + v, diags := basetypes.NewSetValue(newAttrTypeOf[T](ctx), elements) |
| 133 | + if diags.HasError() { |
| 134 | + return NewSetValueOfUnknown[T](ctx), diags |
| 135 | + } |
| 136 | + |
| 137 | + return SetValueOf[T]{SetValue: v}, diags |
| 138 | +} |
| 139 | + |
| 140 | +func NewSetValueOfMust[T attr.Value](ctx context.Context, elements []attr.Value) SetValueOf[T] { |
| 141 | + return fwdiag.Must(NewSetValueOf[T](ctx, elements)) |
| 142 | +} |
0 commit comments