Skip to content
Open
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
45 changes: 44 additions & 1 deletion internal/jennies/terraform/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,57 @@ func newAttributesGenerator(cfg Config, typeFormatter *typeFormatter, packageMap
}
}

func attributesVarName(name string) string {
return tools.UpperCamelCase(name) + "Attributes"
}

// generateForAllObjects generates a named `var XxxAttributes` for each struct object in the schema,
// skipping the object named skipObjectName (used to avoid duplicating the entry point).
func (a *attributes) generateForAllObjects(schema *ast.Schema, skipObjectName string) (string, error) {
var buffer strings.Builder

a.packageMapper("github.com/hashicorp/terraform-plugin-framework/resource/schema")

schema.Objects.Iterate(func(_ string, obj ast.Object) {
if obj.Name == skipObjectName {
return
}
if !obj.Type.IsStruct() {
return
}

buffer.WriteString(fmt.Sprintf("var %s = map[string]schema.Attribute{\n", attributesVarName(obj.Name)))
a.typeFormatter.structDepth[obj.Name]++
buffer.WriteString(a.typeFormatter.formatFieldAttributes(obj.Type.AsStruct().Fields))
a.typeFormatter.structDepth[obj.Name]--
buffer.WriteString("}\n\n")
})

return buffer.String(), nil
}

func (a *attributes) generateForSchema(schema *ast.Schema) (string, error) {
var buffer strings.Builder

a.packageMapper("github.com/hashicorp/terraform-plugin-framework/resource/schema")
buffer.WriteString("var SpecAttributes = map[string]schema.Attribute{\n")

schema.Objects.Iterate(func(_ string, obj ast.Object) {
if !obj.Type.IsAnyOf(ast.KindDisjunction, ast.KindRef, ast.KindConstantRef, ast.KindEnum, ast.KindIntersection) && !obj.Type.IsDisjunctionOfAnyKind() {
if obj.Type.IsAnyOf(ast.KindDisjunction, ast.KindRef, ast.KindConstantRef, ast.KindEnum, ast.KindIntersection) || obj.Type.IsDisjunctionOfAnyKind() {
return
}
if obj.Type.IsStruct() {
required := "Required: true,"
if obj.Type.Nullable {
required = "Optional: true,"
}
comments := ""
if c := formatComments(obj.Comments); c != "" {
comments = fmt.Sprintf("Description: %s,\n", c)
}
buffer.WriteString(fmt.Sprintf("\"%s\": schema.SingleNestedAttribute{\n%s\n%sAttributes: %s,\n},\n",
tools.SnakeCase(obj.Name), required, comments, attributesVarName(obj.Name)))
} else {
buffer.WriteString(a.typeFormatter.formatTypeAttributeForObject(obj, formatComments(obj.Comments)))
}
})
Expand Down
13 changes: 12 additions & 1 deletion internal/jennies/terraform/rawtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,19 @@ func (jenny RawTypes) generateSchema(context languages.Context, schema *ast.Sche
return nil, err
}

var attrs string
entryPointRef := schema.EntryPointType.Ref
skipObjectName := ""
if entryPointRef != nil {
skipObjectName = entryPointRef.ReferredType
}

allObjVars, err := attributesGenerator.generateForAllObjects(schema, skipObjectName)
if err != nil {
return nil, err
}
buffer.WriteString(allObjVars)

var attrs string
if entryPointRef != nil {
obj, ok := context.LocateObject(entryPointRef.ReferredPkg, entryPointRef.ReferredType)
if !ok {
Expand Down
114 changes: 79 additions & 35 deletions internal/jennies/terraform/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type typeFormatter struct {
imports *common.DirectImportMap
packageMapper func(pkg string) string
validators *validators
structDepth map[string]int
}

func defaultTypeFormatter(config Config, context languages.Context, imports *common.DirectImportMap, packageMapper func(pkg string) string) *typeFormatter {
Expand All @@ -24,6 +25,7 @@ func defaultTypeFormatter(config Config, context languages.Context, imports *com
context: context,
imports: imports,
packageMapper: packageMapper,
structDepth: make(map[string]int),
}

tf.validators = newValidators(tf)
Expand Down Expand Up @@ -215,7 +217,11 @@ func (formatter *typeFormatter) formatFieldAttributes(fields []ast.StructField)
if field.Type.IsIntersection() {
continue
}
buffer.WriteString(fmt.Sprintf("\"%s\": %s\n", tools.SnakeCase(field.Name), formatter.formatTypeAttribute(field.Type, "")))
attr := formatter.formatTypeAttribute(field.Type, "")
if attr == "" {
continue
}
buffer.WriteString(fmt.Sprintf("\"%s\": %s\n", tools.SnakeCase(field.Name), attr))
}

return buffer.String()
Expand All @@ -241,12 +247,6 @@ func (formatter *typeFormatter) formatArrayAttributes(def ast.Type) string {
return "unknown"
}

if !obj.Type.IsEnum() {
buffer.WriteString("schema.ListNestedAttribute{\n")
buffer.WriteString(fmt.Sprintf("NestedObject: schema.NestedAttributeObject {\n"))
buffer.WriteString("Attributes: map[string]schema.Attribute {\n")
}

switch obj.Type.Kind {
case ast.KindEnum:
buffer.WriteString("schema.ListAttribute{\n")
Expand All @@ -255,24 +255,41 @@ func (formatter *typeFormatter) formatArrayAttributes(def ast.Type) string {
enumType = "types.Int64Type"
}
buffer.WriteString(fmt.Sprintf("ElementType: %s,\n", enumType))
if defVal != "" {
buffer.WriteString(defVal)
}
if arrayValidator != "" {
buffer.WriteString(fmt.Sprintf("Validators: %s", arrayValidator))
} else if validator != "" {
buffer.WriteString(fmt.Sprintf("Validators: %s", validator))
}
case ast.KindStruct:
buffer.WriteString(formatter.formatFieldAttributes(obj.Type.AsStruct().Fields))
buffer.WriteString("schema.ListNestedAttribute{\n")
buffer.WriteString("NestedObject: schema.NestedAttributeObject{\n")
buffer.WriteString(fmt.Sprintf("Attributes: %s,\n", attributesVarName(obj.Name)))
buffer.WriteString("},\n")
if defVal != "" {
buffer.WriteString(defVal)
}
if arrayValidator != "" {
buffer.WriteString(fmt.Sprintf("Validators: %s", arrayValidator))
} else if validator != "" {
buffer.WriteString(fmt.Sprintf("Validators: %s", validator))
}
default:
buffer.WriteString("schema.ListNestedAttribute{\n")
buffer.WriteString("NestedObject: schema.NestedAttributeObject{\n")
buffer.WriteString("Attributes: map[string]schema.Attribute{\n")
buffer.WriteString(formatter.formatTypeAttributeForObject(obj, formatComments(obj.Comments)))
}

if defVal != "" {
buffer.WriteString(defVal)
}

if arrayValidator != "" {
buffer.WriteString(fmt.Sprintf("Validators: %s", arrayValidator))
} else if validator != "" {
buffer.WriteString(fmt.Sprintf("Validators: %s", validator))
}

if !obj.Type.IsEnum() {
buffer.WriteString("},\n},\n")
if defVal != "" {
buffer.WriteString(defVal)
}
if arrayValidator != "" {
buffer.WriteString(fmt.Sprintf("Validators: %s", arrayValidator))
} else if validator != "" {
buffer.WriteString(fmt.Sprintf("Validators: %s", validator))
}
}

default:
Expand Down Expand Up @@ -309,12 +326,6 @@ func (formatter *typeFormatter) formatMapAttributes(def ast.Type) string {
return "unknown"
}

if !obj.Type.IsEnum() {
buffer.WriteString("schema.MapNestedAttribute{\n")
buffer.WriteString(fmt.Sprintf("NestedObject: schema.NestedAttributeObject {\n"))
buffer.WriteString("Attributes: map[string]schema.Attribute {\n")
}

switch obj.Type.Kind {
case ast.KindEnum:
buffer.WriteString("schema.MapAttribute{\n")
Expand All @@ -323,18 +334,26 @@ func (formatter *typeFormatter) formatMapAttributes(def ast.Type) string {
enumType = "types.Int64Type"
}
buffer.WriteString(fmt.Sprintf("ElementType: %s,\n", enumType))
if defVal != "" {
buffer.WriteString(defVal)
}
case ast.KindStruct:
buffer.WriteString(formatter.formatFieldAttributes(obj.Type.AsStruct().Fields))
buffer.WriteString("schema.MapNestedAttribute{\n")
buffer.WriteString("NestedObject: schema.NestedAttributeObject{\n")
buffer.WriteString(fmt.Sprintf("Attributes: %s,\n", attributesVarName(obj.Name)))
buffer.WriteString("},\n")
if defVal != "" {
buffer.WriteString(defVal)
}
default:
buffer.WriteString("schema.MapNestedAttribute{\n")
buffer.WriteString("NestedObject: schema.NestedAttributeObject{\n")
buffer.WriteString("Attributes: map[string]schema.Attribute{\n")
buffer.WriteString(formatter.formatTypeAttributeForObject(obj, formatComments(obj.Comments)))
}

if defVal != "" {
buffer.WriteString(defVal)
}

if !obj.Type.IsEnum() {
buffer.WriteString("},\n},\n")
if defVal != "" {
buffer.WriteString(defVal)
}
}
default:
buffer.WriteString("schema.MapAttribute{\n ")
Expand Down Expand Up @@ -413,6 +432,31 @@ func (formatter *typeFormatter) formatReferenceAttribute(def ast.Type) string {
return "unknown"
}

if obj.Type.IsStruct() {
required := "Required: true,"
if def.Nullable {
required = "Optional: true,"
}

if formatter.structDepth[obj.Name] >= 2 {
// Already inlining this struct: break the cycle by returning empty.
return ""
}

if formatter.structDepth[obj.Name] == 1 {
// Self-referential: inline attributes instead of referencing the var
// to avoid a Go initialization cycle.
formatter.structDepth[obj.Name]++
attrs := formatter.formatFieldAttributes(obj.Type.AsStruct().Fields)
formatter.structDepth[obj.Name]--
return fmt.Sprintf("schema.SingleNestedAttribute{\n%s\nAttributes: map[string]schema.Attribute{\n%s},\n},\n",
required, attrs)
}

return fmt.Sprintf("schema.SingleNestedAttribute{\n%s\nAttributes: %s,\n},\n",
required, attributesVarName(obj.Name))
}

obj.Type.Default = def.Default
return formatter.formatTypeAttribute(obj.Type, formatComments(obj.Comments))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,24 @@ type ArrayOfRefs []SomeStruct

type ArrayOfArrayOfNumbers types.List

var SomeStructAttributes = map[string]schema.Attribute{
"field_any": schema.ObjectAttribute{
Required: true,
},

}

var SpecAttributes = map[string]schema.Attribute{
"array_of_strings": schema.ListAttribute{
ElementType: types.StringType,
},
"some_struct": schema.SingleNestedAttribute{
Required: true,
Attributes: map[string]schema.Attribute{
"field_any": schema.ObjectAttribute{
Required: true,
},

},
Attributes: SomeStructAttributes,
},
"array_of_refs": schema.ListNestedAttribute{
NestedObject: schema.NestedAttributeObject {
Attributes: map[string]schema.Attribute {
"field_any": schema.ObjectAttribute{
Required: true,
},

},
NestedObject: schema.NestedAttributeObject{
Attributes: SomeStructAttributes,
},
},
"array_of_array_of_numbers": schema.ListAttribute{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@ type MyStruct struct {
OptString types.String `tfsdk:"optString"`
}

var SpecAttributes = map[string]schema.Attribute{
"constant_ref_string": schema.StringAttribute{
Required: true,
Default: stringdefault.StaticString("AString"),
},
"my_struct": schema.SingleNestedAttribute{
Required: true,
Attributes: map[string]schema.Attribute{
var MyStructAttributes = map[string]schema.Attribute{
"a_string": schema.StringAttribute{
Required: true,
Default: stringdefault.StaticString("AString"),
Expand All @@ -31,6 +24,15 @@ Default: stringdefault.StaticString("AString"),
Default: stringdefault.StaticString("AString"),
},

}

var SpecAttributes = map[string]schema.Attribute{
"constant_ref_string": schema.StringAttribute{
Required: true,
Default: stringdefault.StaticString("AString"),
},
"my_struct": schema.SingleNestedAttribute{
Required: true,
Attributes: MyStructAttributes,
},
}
Loading
Loading