|
| 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 Nested Set type used in auto-generated code to enable the generic marshal/unmarshal operations to access nested attribute struct tags 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_nested_object_set": schema.SetNestedAttribute{ |
| 20 | + ... |
| 21 | + CustomType: customtypes.NewNestedSetType[TFSampleNestedObjectModel](ctx), |
| 22 | + NestedObject: schema.NestedAttributeObject{ |
| 23 | + Attributes: map[string]schema.Attribute{ |
| 24 | + "string_attribute": schema.StringAttribute{...}, |
| 25 | + }, |
| 26 | + }, |
| 27 | + } |
| 28 | +
|
| 29 | + - TF Models: |
| 30 | + type TFModel struct { |
| 31 | + SampleNestedObjectSet customtypes.NestedSetValue[TFSampleNestedObjectModel] `tfsdk:"sample_nested_object_set"` |
| 32 | + ... |
| 33 | + } |
| 34 | +
|
| 35 | + type TFSampleNestedObjectModel struct { |
| 36 | + StringAttribute types.String `tfsdk:"string_attribute"` |
| 37 | + ... |
| 38 | + } |
| 39 | +*/ |
| 40 | + |
| 41 | +var ( |
| 42 | + _ basetypes.SetTypable = NestedSetType[struct{}]{} |
| 43 | + _ basetypes.SetValuable = NestedSetValue[struct{}]{} |
| 44 | + _ NestedSetValueInterface = NestedSetValue[struct{}]{} |
| 45 | +) |
| 46 | + |
| 47 | +type NestedSetType[T any] struct { |
| 48 | + basetypes.SetType |
| 49 | +} |
| 50 | + |
| 51 | +func NewNestedSetType[T any](ctx context.Context) NestedSetType[T] { |
| 52 | + elemType, diags := getElementType[T](ctx) |
| 53 | + if diags.HasError() { |
| 54 | + panic(fmt.Errorf("error creating NestedSetType: %v", diags)) |
| 55 | + } |
| 56 | + |
| 57 | + result := NestedSetType[T]{ |
| 58 | + SetType: basetypes.SetType{ElemType: elemType}, |
| 59 | + } |
| 60 | + return result |
| 61 | +} |
| 62 | + |
| 63 | +func (t NestedSetType[T]) Equal(o attr.Type) bool { |
| 64 | + other, ok := o.(NestedSetType[T]) |
| 65 | + if !ok { |
| 66 | + return false |
| 67 | + } |
| 68 | + return t.SetType.Equal(other.SetType) |
| 69 | +} |
| 70 | + |
| 71 | +func (NestedSetType[T]) String() string { |
| 72 | + var t T |
| 73 | + return fmt.Sprintf("NestedSetType[%T]", t) |
| 74 | +} |
| 75 | + |
| 76 | +func (t NestedSetType[T]) ValueFromSet(ctx context.Context, in basetypes.SetValue) (basetypes.SetValuable, diag.Diagnostics) { |
| 77 | + if in.IsNull() { |
| 78 | + return NewNestedSetValueNull[T](ctx), nil |
| 79 | + } |
| 80 | + |
| 81 | + if in.IsUnknown() { |
| 82 | + return NewNestedSetValueUnknown[T](ctx), nil |
| 83 | + } |
| 84 | + |
| 85 | + elemType, diags := getElementType[T](ctx) |
| 86 | + if diags.HasError() { |
| 87 | + return nil, diags |
| 88 | + } |
| 89 | + |
| 90 | + baseSetValue, diags := basetypes.NewSetValue(elemType, in.Elements()) |
| 91 | + if diags.HasError() { |
| 92 | + return nil, diags |
| 93 | + } |
| 94 | + |
| 95 | + return NestedSetValue[T]{SetValue: baseSetValue}, nil |
| 96 | +} |
| 97 | + |
| 98 | +func (t NestedSetType[T]) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) { |
| 99 | + attrValue, err := t.SetType.ValueFromTerraform(ctx, in) |
| 100 | + |
| 101 | + if err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + |
| 105 | + setValue, ok := attrValue.(basetypes.SetValue) |
| 106 | + if !ok { |
| 107 | + return nil, fmt.Errorf("unexpected value type of %T", attrValue) |
| 108 | + } |
| 109 | + |
| 110 | + setValuable, diags := t.ValueFromSet(ctx, setValue) |
| 111 | + if diags.HasError() { |
| 112 | + return nil, fmt.Errorf("unexpected error converting SetValue to SetValuable: %v", diags) |
| 113 | + } |
| 114 | + |
| 115 | + return setValuable, nil |
| 116 | +} |
| 117 | + |
| 118 | +func (t NestedSetType[T]) ValueType(_ context.Context) attr.Value { |
| 119 | + return NestedSetValue[T]{} |
| 120 | +} |
| 121 | + |
| 122 | +type NestedSetValue[T any] struct { |
| 123 | + basetypes.SetValue |
| 124 | +} |
| 125 | + |
| 126 | +type NestedSetValueInterface interface { |
| 127 | + basetypes.SetValuable |
| 128 | + NewNestedSetValue(ctx context.Context, value any) NestedSetValueInterface |
| 129 | + NewNestedSetValueNull(ctx context.Context) NestedSetValueInterface |
| 130 | + SlicePtrAsAny(ctx context.Context) (any, diag.Diagnostics) |
| 131 | + NewEmptySlicePtr() any |
| 132 | + Len() int |
| 133 | +} |
| 134 | + |
| 135 | +func (v NestedSetValue[T]) NewNestedSetValue(ctx context.Context, value any) NestedSetValueInterface { |
| 136 | + return NewNestedSetValue[T](ctx, value) |
| 137 | +} |
| 138 | + |
| 139 | +func NewNestedSetValue[T any](ctx context.Context, value any) NestedSetValue[T] { |
| 140 | + elemType, diags := getElementType[T](ctx) |
| 141 | + if diags.HasError() { |
| 142 | + panic(fmt.Errorf("error creating NestedSetValue: %v", diags)) |
| 143 | + } |
| 144 | + |
| 145 | + newValue, diags := basetypes.NewSetValueFrom(ctx, elemType, value) |
| 146 | + if diags.HasError() { |
| 147 | + return NewNestedSetValueUnknown[T](ctx) |
| 148 | + } |
| 149 | + |
| 150 | + return NestedSetValue[T]{SetValue: newValue} |
| 151 | +} |
| 152 | + |
| 153 | +func (v NestedSetValue[T]) NewNestedSetValueNull(ctx context.Context) NestedSetValueInterface { |
| 154 | + return NewNestedSetValueNull[T](ctx) |
| 155 | +} |
| 156 | + |
| 157 | +func NewNestedSetValueNull[T any](ctx context.Context) NestedSetValue[T] { |
| 158 | + elemType, diags := getElementType[T](ctx) |
| 159 | + if diags.HasError() { |
| 160 | + panic(fmt.Errorf("error creating null NestedSetValue: %v", diags)) |
| 161 | + } |
| 162 | + return NestedSetValue[T]{SetValue: basetypes.NewSetNull(elemType)} |
| 163 | +} |
| 164 | + |
| 165 | +func NewNestedSetValueUnknown[T any](ctx context.Context) NestedSetValue[T] { |
| 166 | + elemType, diags := getElementType[T](ctx) |
| 167 | + if diags.HasError() { |
| 168 | + panic(fmt.Errorf("error creating unknown NestedSetValue: %v", diags)) |
| 169 | + } |
| 170 | + return NestedSetValue[T]{SetValue: basetypes.NewSetUnknown(elemType)} |
| 171 | +} |
| 172 | + |
| 173 | +func (v NestedSetValue[T]) Equal(o attr.Value) bool { |
| 174 | + other, ok := o.(NestedSetValue[T]) |
| 175 | + if !ok { |
| 176 | + return false |
| 177 | + } |
| 178 | + return v.SetValue.Equal(other.SetValue) |
| 179 | +} |
| 180 | + |
| 181 | +func (v NestedSetValue[T]) Type(ctx context.Context) attr.Type { |
| 182 | + return NewNestedSetType[T](ctx) |
| 183 | +} |
| 184 | + |
| 185 | +func (v NestedSetValue[T]) SlicePtrAsAny(ctx context.Context) (any, diag.Diagnostics) { |
| 186 | + valuePtr := new([]T) |
| 187 | + |
| 188 | + if v.IsNull() || v.IsUnknown() { |
| 189 | + return valuePtr, nil |
| 190 | + } |
| 191 | + |
| 192 | + diags := v.ElementsAs(ctx, valuePtr, false) |
| 193 | + if diags.HasError() { |
| 194 | + return nil, diags |
| 195 | + } |
| 196 | + |
| 197 | + return valuePtr, diags |
| 198 | +} |
| 199 | + |
| 200 | +func (v NestedSetValue[T]) NewEmptySlicePtr() any { |
| 201 | + return new([]T) |
| 202 | +} |
| 203 | + |
| 204 | +func (v NestedSetValue[T]) Len() int { |
| 205 | + return len(v.Elements()) |
| 206 | +} |
0 commit comments