|
| 1 | +package customtype |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "reflect" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" |
| 11 | + "github.com/hashicorp/terraform-plugin-go/tftypes" |
| 12 | +) |
| 13 | + |
| 14 | +/* |
| 15 | + Custom Nested List type used in auto-generated code to enable the generic marshal/unmarshal operations to access nested attribute struct tags during conversion. |
| 16 | + Custom types docs: https://developer.hashicorp.com/terraform/plugin/framework/handling-data/types/custom |
| 17 | +
|
| 18 | + Usage: |
| 19 | + - Schema definition: |
| 20 | + "sample_nested_object_list": schema.ListNestedAttribute{ |
| 21 | + ... |
| 22 | + CustomType: customtype.NewNestedListType[TFSampleNestedObjectModel](ctx), |
| 23 | + NestedObject: schema.NestedAttributeObject{ |
| 24 | + Attributes: map[string]schema.Attribute{ |
| 25 | + "string_attribute": schema.StringAttribute{...}, |
| 26 | + }, |
| 27 | + }, |
| 28 | + } |
| 29 | +
|
| 30 | + - TF Models: |
| 31 | + type TFModel struct { |
| 32 | + SampleNestedObjectList customtype.NestedListValue[TFSampleNestedObjectModel] `tfsdk:"sample_nested_object_list"` |
| 33 | + ... |
| 34 | + } |
| 35 | +
|
| 36 | + type TFSampleNestedObjectModel struct { |
| 37 | + StringAttribute types.String `tfsdk:"string_attribute"` |
| 38 | + ... |
| 39 | + } |
| 40 | +*/ |
| 41 | + |
| 42 | +var ( |
| 43 | + _ basetypes.ListTypable = NestedListType[struct{}]{} |
| 44 | + _ basetypes.ListValuable = NestedListValue[struct{}]{} |
| 45 | + _ NestedListValueInterface = NestedListValue[struct{}]{} |
| 46 | +) |
| 47 | + |
| 48 | +type NestedListType[T any] struct { |
| 49 | + basetypes.ListType |
| 50 | +} |
| 51 | + |
| 52 | +func NewNestedListType[T any](ctx context.Context) NestedListType[T] { |
| 53 | + elemType, diags := getElementType[T](ctx) |
| 54 | + if diags.HasError() { |
| 55 | + panic(fmt.Errorf("error creating NestedListType: %v", diags)) |
| 56 | + } |
| 57 | + |
| 58 | + result := NestedListType[T]{ |
| 59 | + ListType: basetypes.ListType{ElemType: elemType}, |
| 60 | + } |
| 61 | + return result |
| 62 | +} |
| 63 | + |
| 64 | +func (t NestedListType[T]) Equal(o attr.Type) bool { |
| 65 | + other, ok := o.(NestedListType[T]) |
| 66 | + if !ok { |
| 67 | + return false |
| 68 | + } |
| 69 | + return t.ListType.Equal(other.ListType) |
| 70 | +} |
| 71 | + |
| 72 | +func (NestedListType[T]) String() string { |
| 73 | + var t T |
| 74 | + return fmt.Sprintf("NestedListType[%T]", t) |
| 75 | +} |
| 76 | + |
| 77 | +func (t NestedListType[T]) ValueFromList(ctx context.Context, in basetypes.ListValue) (basetypes.ListValuable, diag.Diagnostics) { |
| 78 | + if in.IsNull() { |
| 79 | + return NewNestedListValueNull[T](ctx), nil |
| 80 | + } |
| 81 | + |
| 82 | + if in.IsUnknown() { |
| 83 | + return NewNestedListValueUnknown[T](ctx), nil |
| 84 | + } |
| 85 | + |
| 86 | + elemType, diags := getElementType[T](ctx) |
| 87 | + if diags.HasError() { |
| 88 | + return nil, diags |
| 89 | + } |
| 90 | + |
| 91 | + baseListValue, diags := basetypes.NewListValue(elemType, in.Elements()) |
| 92 | + if diags.HasError() { |
| 93 | + return nil, diags |
| 94 | + } |
| 95 | + |
| 96 | + return NestedListValue[T]{ListValue: baseListValue}, nil |
| 97 | +} |
| 98 | + |
| 99 | +func (t NestedListType[T]) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) { |
| 100 | + attrValue, err := t.ListType.ValueFromTerraform(ctx, in) |
| 101 | + |
| 102 | + if err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + |
| 106 | + listValue, ok := attrValue.(basetypes.ListValue) |
| 107 | + if !ok { |
| 108 | + return nil, fmt.Errorf("unexpected value type of %T", attrValue) |
| 109 | + } |
| 110 | + |
| 111 | + listValuable, diags := t.ValueFromList(ctx, listValue) |
| 112 | + if diags.HasError() { |
| 113 | + return nil, fmt.Errorf("unexpected error converting ListValue to ListValuable: %v", diags) |
| 114 | + } |
| 115 | + |
| 116 | + return listValuable, nil |
| 117 | +} |
| 118 | + |
| 119 | +func (t NestedListType[T]) ValueType(_ context.Context) attr.Value { |
| 120 | + return NestedListValue[T]{} |
| 121 | +} |
| 122 | + |
| 123 | +type NestedListValue[T any] struct { |
| 124 | + basetypes.ListValue |
| 125 | +} |
| 126 | + |
| 127 | +type NestedListValueInterface interface { |
| 128 | + basetypes.ListValuable |
| 129 | + NewNestedListValue(ctx context.Context, value any) NestedListValueInterface |
| 130 | + NewNestedListValueNull(ctx context.Context) NestedListValueInterface |
| 131 | + SlicePtrAsAny(ctx context.Context) (any, diag.Diagnostics) |
| 132 | + NewEmptySlicePtr() any |
| 133 | + Len() int |
| 134 | +} |
| 135 | + |
| 136 | +func (v NestedListValue[T]) NewNestedListValue(ctx context.Context, value any) NestedListValueInterface { |
| 137 | + return NewNestedListValue[T](ctx, value) |
| 138 | +} |
| 139 | + |
| 140 | +func NewNestedListValue[T any](ctx context.Context, value any) NestedListValue[T] { |
| 141 | + elemType, diags := getElementType[T](ctx) |
| 142 | + if diags.HasError() { |
| 143 | + panic(fmt.Errorf("error creating NestedListValue: %v", diags)) |
| 144 | + } |
| 145 | + |
| 146 | + newValue, diags := basetypes.NewListValueFrom(ctx, elemType, value) |
| 147 | + if diags.HasError() { |
| 148 | + return NewNestedListValueUnknown[T](ctx) |
| 149 | + } |
| 150 | + |
| 151 | + return NestedListValue[T]{ListValue: newValue} |
| 152 | +} |
| 153 | + |
| 154 | +func (v NestedListValue[T]) NewNestedListValueNull(ctx context.Context) NestedListValueInterface { |
| 155 | + return NewNestedListValueNull[T](ctx) |
| 156 | +} |
| 157 | + |
| 158 | +func NewNestedListValueNull[T any](ctx context.Context) NestedListValue[T] { |
| 159 | + elemType, diags := getElementType[T](ctx) |
| 160 | + if diags.HasError() { |
| 161 | + panic(fmt.Errorf("error creating null NestedListValue: %v", diags)) |
| 162 | + } |
| 163 | + return NestedListValue[T]{ListValue: basetypes.NewListNull(elemType)} |
| 164 | +} |
| 165 | + |
| 166 | +func NewNestedListValueUnknown[T any](ctx context.Context) NestedListValue[T] { |
| 167 | + elemType, diags := getElementType[T](ctx) |
| 168 | + if diags.HasError() { |
| 169 | + panic(fmt.Errorf("error creating unknown NestedListValue: %v", diags)) |
| 170 | + } |
| 171 | + return NestedListValue[T]{ListValue: basetypes.NewListUnknown(elemType)} |
| 172 | +} |
| 173 | + |
| 174 | +func (v NestedListValue[T]) Equal(o attr.Value) bool { |
| 175 | + other, ok := o.(NestedListValue[T]) |
| 176 | + if !ok { |
| 177 | + return false |
| 178 | + } |
| 179 | + return v.ListValue.Equal(other.ListValue) |
| 180 | +} |
| 181 | + |
| 182 | +func (v NestedListValue[T]) Type(ctx context.Context) attr.Type { |
| 183 | + return NewNestedListType[T](ctx) |
| 184 | +} |
| 185 | + |
| 186 | +func (v NestedListValue[T]) SlicePtrAsAny(ctx context.Context) (any, diag.Diagnostics) { |
| 187 | + valuePtr := new([]T) |
| 188 | + |
| 189 | + if v.IsNull() || v.IsUnknown() { |
| 190 | + return valuePtr, nil |
| 191 | + } |
| 192 | + |
| 193 | + diags := v.ElementsAs(ctx, valuePtr, false) |
| 194 | + if diags.HasError() { |
| 195 | + return nil, diags |
| 196 | + } |
| 197 | + |
| 198 | + return valuePtr, diags |
| 199 | +} |
| 200 | + |
| 201 | +func (v NestedListValue[T]) NewEmptySlicePtr() any { |
| 202 | + return new([]T) |
| 203 | +} |
| 204 | + |
| 205 | +func (v NestedListValue[T]) Len() int { |
| 206 | + return len(v.Elements()) |
| 207 | +} |
| 208 | + |
| 209 | +func getElementType[T any](ctx context.Context) (attr.Type, diag.Diagnostics) { |
| 210 | + var t T |
| 211 | + attrTypes, diags := valueToAttributeTypes(ctx, reflect.ValueOf(t)) |
| 212 | + if diags.HasError() { |
| 213 | + return nil, diags |
| 214 | + } |
| 215 | + |
| 216 | + return basetypes.ObjectType{AttrTypes: attrTypes}, nil |
| 217 | +} |
0 commit comments