|
| 1 | +package customtypes |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" |
| 10 | + "github.com/hashicorp/terraform-plugin-go/tftypes" |
| 11 | +) |
| 12 | + |
| 13 | +/* |
| 14 | + Custom List type used in auto-generated code to enable the generic marshal/unmarshal operations to access the elements' type during conversion. |
| 15 | + Custom types docs: https://developer.hashicorp.com/terraform/plugin/framework/handling-data/types/custom |
| 16 | +
|
| 17 | + Usage: |
| 18 | + - Schema definition: |
| 19 | + "sample_string_list": schema.ListAttribute{ |
| 20 | + ... |
| 21 | + CustomType: customtypes.NewListType[basetypes.StringValue](ctx), |
| 22 | + ElementType: types.StringType, |
| 23 | + } |
| 24 | +
|
| 25 | + - TF Models: |
| 26 | + type TFModel struct { |
| 27 | + SampleStringList customtypes.ListValue[basetypes.StringValue] `tfsdk:"sample_string_list"` |
| 28 | + ... |
| 29 | + } |
| 30 | +*/ |
| 31 | + |
| 32 | +var ( |
| 33 | + _ basetypes.ListTypable = ListType[basetypes.StringValue]{} |
| 34 | + _ basetypes.ListValuable = ListValue[basetypes.StringValue]{} |
| 35 | + _ ListValueInterface = ListValue[basetypes.StringValue]{} |
| 36 | +) |
| 37 | + |
| 38 | +type ListType[T attr.Value] struct { |
| 39 | + basetypes.ListType |
| 40 | +} |
| 41 | + |
| 42 | +func NewListType[T attr.Value](ctx context.Context) ListType[T] { |
| 43 | + elemType := getElemType[T](ctx) |
| 44 | + return ListType[T]{ |
| 45 | + ListType: basetypes.ListType{ElemType: elemType}, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func (t ListType[T]) Equal(o attr.Type) bool { |
| 50 | + other, ok := o.(ListType[T]) |
| 51 | + if !ok { |
| 52 | + return false |
| 53 | + } |
| 54 | + return t.ListType.Equal(other.ListType) |
| 55 | +} |
| 56 | + |
| 57 | +func (ListType[T]) String() string { |
| 58 | + var t T |
| 59 | + return fmt.Sprintf("ListType[%T]", t) |
| 60 | +} |
| 61 | + |
| 62 | +func (t ListType[T]) ValueFromList(ctx context.Context, in basetypes.ListValue) (basetypes.ListValuable, diag.Diagnostics) { |
| 63 | + if in.IsNull() { |
| 64 | + return NewListValueNull[T](ctx), nil |
| 65 | + } |
| 66 | + |
| 67 | + if in.IsUnknown() { |
| 68 | + return NewListValueUnknown[T](ctx), nil |
| 69 | + } |
| 70 | + |
| 71 | + elemType := getElemType[T](ctx) |
| 72 | + baseListValue, diags := basetypes.NewListValue(elemType, in.Elements()) |
| 73 | + if diags.HasError() { |
| 74 | + return nil, diags |
| 75 | + } |
| 76 | + |
| 77 | + return ListValue[T]{ListValue: baseListValue}, nil |
| 78 | +} |
| 79 | + |
| 80 | +func (t ListType[T]) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) { |
| 81 | + attrValue, err := t.ListType.ValueFromTerraform(ctx, in) |
| 82 | + |
| 83 | + if err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + |
| 87 | + listValue, ok := attrValue.(basetypes.ListValue) |
| 88 | + if !ok { |
| 89 | + return nil, fmt.Errorf("unexpected value type of %T", attrValue) |
| 90 | + } |
| 91 | + |
| 92 | + listValuable, diags := t.ValueFromList(ctx, listValue) |
| 93 | + if diags.HasError() { |
| 94 | + return nil, fmt.Errorf("unexpected error converting ListValue to ListValuable: %v", diags) |
| 95 | + } |
| 96 | + |
| 97 | + return listValuable, nil |
| 98 | +} |
| 99 | + |
| 100 | +func (t ListType[T]) ValueType(_ context.Context) attr.Value { |
| 101 | + return ListValue[T]{} |
| 102 | +} |
| 103 | + |
| 104 | +type ListValue[T attr.Value] struct { |
| 105 | + basetypes.ListValue |
| 106 | +} |
| 107 | + |
| 108 | +type ListValueInterface interface { |
| 109 | + basetypes.ListValuable |
| 110 | + NewListValue(ctx context.Context, value []attr.Value) ListValueInterface |
| 111 | + NewListValueNull(ctx context.Context) ListValueInterface |
| 112 | + ElementType(ctx context.Context) attr.Type |
| 113 | + Elements() []attr.Value |
| 114 | +} |
| 115 | + |
| 116 | +func (v ListValue[T]) NewListValue(ctx context.Context, value []attr.Value) ListValueInterface { |
| 117 | + return NewListValue[T](ctx, value) |
| 118 | +} |
| 119 | + |
| 120 | +func NewListValue[T attr.Value](ctx context.Context, value []attr.Value) ListValue[T] { |
| 121 | + elemType := getElemType[T](ctx) |
| 122 | + |
| 123 | + listValue, diags := basetypes.NewListValue(elemType, value) |
| 124 | + if diags.HasError() { |
| 125 | + return NewListValueUnknown[T](ctx) |
| 126 | + } |
| 127 | + |
| 128 | + return ListValue[T]{ListValue: listValue} |
| 129 | +} |
| 130 | + |
| 131 | +func (v ListValue[T]) NewListValueNull(ctx context.Context) ListValueInterface { |
| 132 | + return NewListValueNull[T](ctx) |
| 133 | +} |
| 134 | + |
| 135 | +func NewListValueNull[T attr.Value](ctx context.Context) ListValue[T] { |
| 136 | + elemType := getElemType[T](ctx) |
| 137 | + return ListValue[T]{ListValue: basetypes.NewListNull(elemType)} |
| 138 | +} |
| 139 | + |
| 140 | +func NewListValueUnknown[T attr.Value](ctx context.Context) ListValue[T] { |
| 141 | + elemType := getElemType[T](ctx) |
| 142 | + return ListValue[T]{ListValue: basetypes.NewListUnknown(elemType)} |
| 143 | +} |
| 144 | + |
| 145 | +func (v ListValue[T]) Equal(o attr.Value) bool { |
| 146 | + other, ok := o.(ListValue[T]) |
| 147 | + if !ok { |
| 148 | + return false |
| 149 | + } |
| 150 | + return v.ListValue.Equal(other.ListValue) |
| 151 | +} |
| 152 | + |
| 153 | +func (v ListValue[T]) Type(ctx context.Context) attr.Type { |
| 154 | + return NewListType[T](ctx) |
| 155 | +} |
| 156 | + |
| 157 | +func (v ListValue[T]) ElementType(ctx context.Context) attr.Type { |
| 158 | + return getElemType[T](ctx) |
| 159 | +} |
| 160 | + |
| 161 | +func (v ListValue[T]) Elements() []attr.Value { |
| 162 | + return v.ListValue.Elements() |
| 163 | +} |
| 164 | + |
| 165 | +func getElemType[T attr.Value](ctx context.Context) attr.Type { |
| 166 | + var t T |
| 167 | + return t.Type(ctx) |
| 168 | +} |
0 commit comments