Skip to content
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

Add custom_types config parameter #36

Merged
merged 3 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v2.0.2
v2.1.0
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ type Config struct {
// should be used for the given package names, it can be used when the
// correct import path is not found automatically.
ImportPathOverrides map[string]string `yaml:"import_path_overrides,omitempty"`
// CustomTypes fields marks fields as custom types like the
// "gogoproto.customtype" tag would do.
CustomTypes map[string]string `yaml:"custom_types,omitempty"`

// params represents CLI params passed from the plugin
params map[string]string `yaml:"-"`
Expand Down
7 changes: 6 additions & 1 deletion field_build_context.go
marcoandredinis marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,17 @@ func (c *FieldBuildContext) IsMessage() bool {
}

// IsCustomType returns true if fields has gogo.custom_type flag
// or if there's a custom type override in configuration.
func (c *FieldBuildContext) IsCustomType() bool {
return c.field.IsCustomType()
_, ok := c.config.CustomTypes[c.GetPath()]
return c.field.IsCustomType() || ok
}

// GetCustomType returns true if fields has gogo.custom_type flag
func (c *FieldBuildContext) GetCustomType() string {
if customType, ok := c.config.CustomTypes[c.GetPath()]; ok {
return customType
}
return c.field.GetCustomType()
}

Expand Down
3 changes: 3 additions & 0 deletions test/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,6 @@ plan_modifiers:
validators:
"Test.Str":
- UseMockValidator()

custom_types:
"Test.StringOverride": StringCustom
9 changes: 9 additions & 0 deletions test/copy_from_terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,12 @@ func TestCopyFromNullableEmbeddedFieldWithoutValue(t *testing.T) {
// should be nil
require.Nil(t, target.MaxAgeDuration)
}

func TestCopyFromStringOverride(t *testing.T) {
obj := copyFromTerraformObject(t)

target := Test{}
require.False(t, CopyTestFromTerraform(context.Background(), obj, &target).HasError())

require.Equal(t, "a/b/c", target.StringOverride)
}
63 changes: 63 additions & 0 deletions test/custom_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package test
import (
"context"
fmt "fmt"
"strings"
time "time"

"github.com/hashicorp/terraform-plugin-framework/attr"
Expand Down Expand Up @@ -78,3 +79,65 @@ func CopyToBoolSpecial(diags diag.Diagnostics, obj []BoolCustom, t attr.Type, v

return value
}

// StringCustom is a custom type that maps a Terraform List of string, onto a
// single go string by joining all elements with "/".

// GenSchemaStringCustom returns the StringCustom schema.
func GenSchemaStringCustom(_ context.Context) tfsdk.Attribute {
return tfsdk.Attribute{
Type: types.ListType{
ElemType: types.StringType,
},
}
}

// CopyFromStringCustom copies the value from Terraform (a list of strings) into
// the source (a single string) by joining all elements with "/".
func CopyFromStringCustom(diags diag.Diagnostics, tf attr.Value, obj *string) {
v, ok := tf.(types.List)
if !ok {
diags.AddError("Error reading value from Terraform", fmt.Sprintf("Failed to cast %T to types.List", tf))
return
}

items := make([]string, 0)
for _, raw := range v.Elems {
el, ok := raw.(types.String)
if !ok {
diags.AddError("Error reading value from Terraform", fmt.Sprintf("Failed to cast %T to types.Bool", raw))
return
}

if !el.Null && !el.Unknown {
items = append(items, el.Value)
}
}

*obj = strings.Join(items, "/")
}

// CopyToStringCustom copies a source value (single string) into the Terraform
// value (a list of strings) by splitting the string on every "/".
func CopyToStringCustom(diags diag.Diagnostics, obj string, t attr.Type, v attr.Value) attr.Value {
value, ok := v.(types.List)
if !ok {
value = types.List{
Null: true,
Unknown: false,
ElemType: types.StringType,
}
}

if len(obj) > 0 {
if value.Elems == nil {
value.Elems = make([]attr.Value, len(obj))
}

for i, b := range strings.Split(obj, "/") {
value.Elems[i] = types.String{Null: false, Unknown: false, Value: b}
}
}

return value
}
5 changes: 5 additions & 0 deletions test/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,11 @@ func copyFromTerraformObject(t *testing.T) types.Object {
},
},
"max_age": DurationValue{Value: duration},
"string_override": types.List{Elems: []attr.Value{
types.String{Value: "a"},
types.String{Value: "b"},
types.String{Value: "c"},
}},
},
AttrTypes: obj.AttrTypes,
}
Expand Down
148 changes: 76 additions & 72 deletions test/test.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion test/test.proto
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@ message Test {
(gogoproto.jsontag) = "",
(gogoproto.embed) = true
];

// StringOverride is represented by a single string in the go struct, but by
// a list of strings in the Terraform Schema. The plugin's configuration
// specifies a custom_type (StringCustom), the generator should use the
// functions "GenSchemaStringCustom", "CopyFromStringCustom",
// "CopyToStringCustom" instead of attempting to generate them.
string StringOverride = 40;
}

message MaxAgeDuration {
Expand Down Expand Up @@ -223,4 +230,3 @@ message EmbeddedNestedField {
// EmbeddedNestedString string field
string EmbeddedNestedString = 1 [(gogoproto.jsontag) = "embedded_nested_string"];
}

Loading