Skip to content

Commit

Permalink
passes the generated Attribute struct to custom type gen functions so…
Browse files Browse the repository at this point in the history
… things like Description can be captured (#44)
  • Loading branch information
eriktate authored Oct 4, 2024
1 parent 0876826 commit c91cc3e
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v2.2.0
v3.0.0
8 changes: 4 additions & 4 deletions gen_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,6 @@ func NewFieldSchemaGenerator(f *Field, i *Imports) *FieldSchemaGenerator {

// Generate returns field schema
func (f *FieldSchemaGenerator) Generate() *j.Statement {
if f.Kind == CustomKind {
return j.Id("GenSchema" + f.Suffix).Call(j.Id("ctx"))
}

d := j.Dict{
j.Id("Description"): j.Lit(f.Comment),
j.Id("Type"): f.schemaType(), // nils are automatically omitted
Expand Down Expand Up @@ -156,6 +152,10 @@ func (f *FieldSchemaGenerator) Generate() *j.Statement {
d[j.Id("PlanModifiers")] = generatePlanModifiers(f.i, f.PlanModifiers)
}

if f.Kind == CustomKind {
return j.Id("GenSchema"+f.Suffix).Call(j.Id("ctx"), j.Id(f.i.WithPackage(SDK, "Attribute")).Values(d))
}

return j.Values(d)
}

Expand Down
6 changes: 4 additions & 2 deletions test/custom_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ func (d Duration) String() string {
type BoolCustom bool

// GenSchemaBoolSpecial generates custom field schema (bool list)
func GenSchemaBoolSpecial(_ context.Context) tfsdk.Attribute {
func GenSchemaBoolSpecial(_ context.Context, attr tfsdk.Attribute) tfsdk.Attribute {
return tfsdk.Attribute{
Type: types.ListType{
ElemType: types.BoolType,
},
Description: attr.Description,
Optional: attr.Optional,
}
}

Expand Down Expand Up @@ -84,7 +86,7 @@ func CopyToBoolSpecial(diags diag.Diagnostics, obj []BoolCustom, t attr.Type, v
// single go string by joining all elements with "/".

// GenSchemaStringCustom returns the StringCustom schema.
func GenSchemaStringCustom(_ context.Context) tfsdk.Attribute {
func GenSchemaStringCustom(_ context.Context, _ tfsdk.Attribute) tfsdk.Attribute {
return tfsdk.Attribute{
Type: types.ListType{
ElemType: types.StringType,
Expand Down
2 changes: 2 additions & 0 deletions test/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ func TestSchema(t *testing.T) {
require.True(t, schema.Attributes["id"].Computed)
require.Len(t, schema.Attributes["str"].PlanModifiers, 1)
require.Len(t, schema.Attributes["str"].Validators, 1)
require.Equal(t, "BoolCustomList []bool field", schema.Attributes["bool_custom_list"].Description)
require.True(t, schema.Attributes["bool_custom_list"].Optional)
}
7 changes: 5 additions & 2 deletions test/test.pb.go

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

10 changes: 8 additions & 2 deletions test/test_terraform.go

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

0 comments on commit c91cc3e

Please sign in to comment.