-
Notifications
You must be signed in to change notification settings - Fork 208
chore: Add support for typed maps in autogen #3833
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| package customtypes | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-framework/attr" | ||
| "github.com/hashicorp/terraform-plugin-framework/diag" | ||
| "github.com/hashicorp/terraform-plugin-framework/types/basetypes" | ||
| "github.com/hashicorp/terraform-plugin-go/tftypes" | ||
| ) | ||
|
|
||
| /* | ||
| Custom Map type used in auto-generated code to enable the generic marshal/unmarshal operations to access the elements' type during conversion. | ||
| Custom types docs: https://developer.hashicorp.com/terraform/plugin/framework/handling-data/types/custom | ||
|
|
||
| Usage: | ||
| - Schema definition: | ||
| "sample_string_map": schema.MapAttribute{ | ||
| ... | ||
| CustomType: customtypes.NewMapType[basetypes.StringValue](ctx), | ||
| ElementType: types.StringType, | ||
| } | ||
|
|
||
| - TF Models: | ||
| type TFModel struct { | ||
| SampleStringMap customtypes.MapValue[basetypes.StringValue] `tfsdk:"sample_string_map"` | ||
| ... | ||
| } | ||
| */ | ||
|
|
||
| var ( | ||
| _ basetypes.MapTypable = MapType[basetypes.StringValue]{} | ||
| _ basetypes.MapValuable = MapValue[basetypes.StringValue]{} | ||
| _ MapValueInterface = MapValue[basetypes.StringValue]{} | ||
| ) | ||
|
|
||
| type MapType[T attr.Value] struct { | ||
| basetypes.MapType | ||
| } | ||
|
|
||
| func NewMapType[T attr.Value](ctx context.Context) MapType[T] { | ||
| elemType := getElemType[T](ctx) | ||
| return MapType[T]{ | ||
| MapType: basetypes.MapType{ElemType: elemType}, | ||
| } | ||
| } | ||
|
|
||
| func (t MapType[T]) Equal(o attr.Type) bool { | ||
| other, ok := o.(MapType[T]) | ||
| if !ok { | ||
| return false | ||
| } | ||
|
|
||
| return t.MapType.Equal(other.MapType) | ||
| } | ||
|
|
||
| func (MapType[T]) String() string { | ||
| var t T | ||
| return fmt.Sprintf("MapType[%T]", t) | ||
| } | ||
|
|
||
| func (t MapType[T]) ValueFromMap(ctx context.Context, in basetypes.MapValue) (basetypes.MapValuable, diag.Diagnostics) { | ||
| if in.IsNull() { | ||
| return NewMapValueNull[T](ctx), nil | ||
| } | ||
|
|
||
| if in.IsUnknown() { | ||
| return NewMapValueUnknown[T](ctx), nil | ||
| } | ||
|
|
||
| elemType := getElemType[T](ctx) | ||
| baseMapValue, diags := basetypes.NewMapValue(elemType, in.Elements()) | ||
| if diags.HasError() { | ||
| return nil, diags | ||
| } | ||
|
|
||
| return MapValue[T]{MapValue: baseMapValue}, nil | ||
| } | ||
|
|
||
| func (t MapType[T]) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) { | ||
| attrValue, err := t.MapType.ValueFromTerraform(ctx, in) | ||
|
|
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| mapValue, ok := attrValue.(basetypes.MapValue) | ||
| if !ok { | ||
| return nil, fmt.Errorf("unexpected value type of %T", attrValue) | ||
| } | ||
|
|
||
| mapValuable, diags := t.ValueFromMap(ctx, mapValue) | ||
| if diags.HasError() { | ||
| return nil, fmt.Errorf("unexpected error converting MapValue to MapValuable: %v", diags) | ||
| } | ||
|
|
||
| return mapValuable, nil | ||
| } | ||
|
|
||
| func (t MapType[T]) ValueType(_ context.Context) attr.Value { | ||
| return MapValue[T]{} | ||
| } | ||
|
|
||
| type MapValue[T attr.Value] struct { | ||
| basetypes.MapValue | ||
| } | ||
|
|
||
| type MapValueInterface interface { | ||
| basetypes.MapValuable | ||
| NewMapValue(ctx context.Context, value map[string]attr.Value) MapValueInterface | ||
| NewMapValueNull(ctx context.Context) MapValueInterface | ||
| ElementType(ctx context.Context) attr.Type | ||
| Elements() map[string]attr.Value | ||
| } | ||
|
|
||
| func (v MapValue[T]) ToTerraformValue(ctx context.Context) (tftypes.Value, error) { | ||
| if v.MapValue.ElementType(ctx) == nil { | ||
| // MapValue created as a zero value (not explicitly initialized), initialize now so conversion does not panic. | ||
| v.MapValue = NewMapValueNull[T](ctx).MapValue | ||
| } | ||
| return v.MapValue.ToTerraformValue(ctx) | ||
| } | ||
|
|
||
| func (v MapValue[T]) NewMapValue(ctx context.Context, value map[string]attr.Value) MapValueInterface { | ||
| return NewMapValue[T](ctx, value) | ||
| } | ||
|
|
||
| func NewMapValue[T attr.Value](ctx context.Context, value map[string]attr.Value) MapValue[T] { | ||
| elemType := getElemType[T](ctx) | ||
|
|
||
| mapValue, diags := basetypes.NewMapValue(elemType, value) | ||
| if diags.HasError() { | ||
| return NewMapValueUnknown[T](ctx) | ||
| } | ||
|
|
||
| return MapValue[T]{MapValue: mapValue} | ||
| } | ||
|
|
||
| func (v MapValue[T]) NewMapValueNull(ctx context.Context) MapValueInterface { | ||
| return NewMapValueNull[T](ctx) | ||
| } | ||
|
|
||
| func NewMapValueNull[T attr.Value](ctx context.Context) MapValue[T] { | ||
| elemType := getElemType[T](ctx) | ||
| return MapValue[T]{MapValue: basetypes.NewMapNull(elemType)} | ||
| } | ||
|
|
||
| func NewMapValueUnknown[T attr.Value](ctx context.Context) MapValue[T] { | ||
| elemType := getElemType[T](ctx) | ||
| return MapValue[T]{MapValue: basetypes.NewMapUnknown(elemType)} | ||
| } | ||
|
|
||
| func (v MapValue[T]) Equal(o attr.Value) bool { | ||
| other, ok := o.(MapValue[T]) | ||
| if !ok { | ||
| return false | ||
| } | ||
| return v.MapValue.Equal(other.MapValue) | ||
| } | ||
|
|
||
| func (v MapValue[T]) Type(ctx context.Context) attr.Type { | ||
| return NewMapType[T](ctx) | ||
| } | ||
|
|
||
| func (v MapValue[T]) ElementType(ctx context.Context) attr.Type { | ||
| return getElemType[T](ctx) | ||
| } | ||
|
|
||
| func (v MapValue[T]) Elements() map[string]attr.Value { | ||
| return v.MapValue.Elements() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
q: Is there a reason why this implementation deviates from other implementations which check for null?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unlike other types, which just return the underlying
elementType, thebasetypes.ObjectValue.AttributeTypes()implementation creates and returns a map:At which point, its better to compare against a zero-value.