-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ignore] Add a new template and Modify generator to generate custom s…
…tring types for attributes that need sementic equality integration for their valid values.
- Loading branch information
Showing
16 changed files
with
1,637 additions
and
48 deletions.
There are no files selected for viewing
This file contains 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 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,152 @@ | ||
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" | ||
) | ||
|
||
// {{.ResourceClassName}}{{.PropertyName}} custom string type. | ||
|
||
var _ basetypes.StringTypable = {{.ResourceClassName}}{{.PropertyName}}StringType{} | ||
|
||
type {{.ResourceClassName}}{{.PropertyName}}StringType struct { | ||
basetypes.StringType | ||
} | ||
|
||
func (t {{.ResourceClassName}}{{.PropertyName}}StringType) Equal(o attr.Type) bool { | ||
other, ok := o.({{.ResourceClassName}}{{.PropertyName}}StringType) | ||
|
||
if !ok { | ||
return false | ||
} | ||
|
||
return t.StringType.Equal(other.StringType) | ||
} | ||
|
||
func (t {{.ResourceClassName}}{{.PropertyName}}StringType) String() string { | ||
return "{{.ResourceClassName}}{{.PropertyName}}StringType" | ||
} | ||
|
||
func (t {{.ResourceClassName}}{{.PropertyName}}StringType) ValueFromString(ctx context.Context, in basetypes.StringValue) (basetypes.StringValuable, diag.Diagnostics) { | ||
value := {{.ResourceClassName}}{{.PropertyName}}StringValue{ | ||
StringValue: in, | ||
} | ||
|
||
return value, nil | ||
} | ||
|
||
func (t {{.ResourceClassName}}{{.PropertyName}}StringType) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) { | ||
attrValue, err := t.StringType.ValueFromTerraform(ctx, in) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
stringValue, ok := attrValue.(basetypes.StringValue) | ||
|
||
if !ok { | ||
return nil, fmt.Errorf("unexpected value type of %T", attrValue) | ||
} | ||
|
||
stringValuable, diags := t.ValueFromString(ctx, stringValue) | ||
|
||
if diags.HasError() { | ||
return nil, fmt.Errorf("unexpected error converting StringValue to StringValuable: %v", diags) | ||
} | ||
|
||
return stringValuable, nil | ||
} | ||
|
||
func (t {{.ResourceClassName}}{{.PropertyName}}StringType) ValueType(ctx context.Context) attr.Value { | ||
return {{.ResourceClassName}}{{.PropertyName}}StringValue{} | ||
} | ||
|
||
// {{.ResourceClassName}}{{.PropertyName}} custom string value. | ||
|
||
var _ basetypes.StringValuableWithSemanticEquals = {{.ResourceClassName}}{{.PropertyName}}StringValue{} | ||
|
||
type {{.ResourceClassName}}{{.PropertyName}}StringValue struct { | ||
basetypes.StringValue | ||
} | ||
|
||
func (v {{.ResourceClassName}}{{.PropertyName}}StringValue) Equal(o attr.Value) bool { | ||
other, ok := o.({{.ResourceClassName}}{{.PropertyName}}StringValue) | ||
|
||
if !ok { | ||
return false | ||
} | ||
|
||
return v.StringValue.Equal(other.StringValue) | ||
} | ||
|
||
func (v {{.ResourceClassName}}{{.PropertyName}}StringValue) Type(ctx context.Context) attr.Type { | ||
return {{.ResourceClassName}}{{.PropertyName}}StringType{} | ||
} | ||
|
||
func (v {{.ResourceClassName}}{{.PropertyName}}StringValue) StringSemanticEquals(ctx context.Context, newValuable basetypes.StringValuable) (bool, diag.Diagnostics) { | ||
var diags diag.Diagnostics | ||
|
||
newValue, ok := newValuable.({{.ResourceClassName}}{{.PropertyName}}StringValue) | ||
|
||
if !ok { | ||
diags.AddError( | ||
"Semantic Equality Check Error", | ||
"An unexpected value type was received while performing semantic equality checks. "+ | ||
"Please report this to the provider developers.\n\n"+ | ||
"Expected Value Type: "+fmt.Sprintf("%T", v)+"\n"+ | ||
"Got Value Type: "+fmt.Sprintf("%T", newValuable), | ||
) | ||
|
||
return false, diags | ||
} | ||
|
||
priorMappedValue := {{.ResourceClassName}}{{.PropertyName}}ValueMap(v.StringValue) | ||
|
||
newMappedValue := {{.ResourceClassName}}{{.PropertyName}}ValueMap(newValue.StringValue) | ||
|
||
return priorMappedValue.Equal(newMappedValue), diags | ||
} | ||
|
||
func {{.ResourceClassName}}{{.PropertyName}}ValueMap(value basetypes.StringValue) basetypes.StringValue { | ||
{{- $ValidValuesMap := validValuesToMap .ValidValues}} | ||
matchMap := map[string]string{ | ||
{{- range $key, $value := $ValidValuesMap}} | ||
"{{$key}}": "{{$value}}", | ||
{{- end}} | ||
} | ||
|
||
if val, ok := matchMap[value.ValueString()]; ok { | ||
return basetypes.NewStringValue(val) | ||
} | ||
|
||
return value | ||
} | ||
|
||
func New{{.ResourceClassName}}{{.PropertyName}}StringNull() {{.ResourceClassName}}{{.PropertyName}}StringValue { | ||
return {{.ResourceClassName}}{{.PropertyName}}StringValue{ | ||
StringValue: basetypes.NewStringNull(), | ||
} | ||
} | ||
|
||
func New{{.ResourceClassName}}{{.PropertyName}}StringUnknown() {{.ResourceClassName}}{{.PropertyName}}StringValue { | ||
return {{.ResourceClassName}}{{.PropertyName}}StringValue{ | ||
StringValue: basetypes.NewStringUnknown(), | ||
} | ||
} | ||
|
||
func New{{.ResourceClassName}}{{.PropertyName}}StringValue(value string) {{.ResourceClassName}}{{.PropertyName}}StringValue { | ||
return {{.ResourceClassName}}{{.PropertyName}}StringValue{ | ||
StringValue: basetypes.NewStringValue(value), | ||
} | ||
} | ||
|
||
func New{{.ResourceClassName}}{{.PropertyName}}StringPointerValue(value *string) {{.ResourceClassName}}{{.PropertyName}}StringValue { | ||
return {{.ResourceClassName}}{{.PropertyName}}StringValue{ | ||
StringValue: basetypes.NewStringPointerValue(value), | ||
} | ||
} |
This file contains 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 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 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.