Skip to content

Commit

Permalink
Fix codegen for specs with top-level entry lists
Browse files Browse the repository at this point in the history
  • Loading branch information
kklimonda-cl committed Nov 22, 2024
1 parent f948646 commit 9fc5ae4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
49 changes: 32 additions & 17 deletions pkg/translate/assignments.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,24 +133,27 @@ func defineNestedObject(parent []*properties.SpecParam, param, parentParam *prop
}
}

func startIfBlockForParamNotNil(parent []*properties.SpecParam, param *properties.SpecParam, parentParam *properties.SpecParam, builder *strings.Builder) {
if len(parent) == 2 && parent[0].Type == "list" && parent[0].Items.Type == "entry" {
func startIfBlockForParamNotNil(parents []*properties.SpecParam, param *properties.SpecParam, parentParam *properties.SpecParam, builder *strings.Builder) {
grandparent := parents[0]

if grandparent != param && grandparent.Type == "list" && grandparent.Items.Type == "entry" {
if isParamName(param) {
builder.WriteString(fmt.Sprintf("if o%s != \"\" {\n",
renderNestedVariableName(parent, true, true, false)))
renderNestedVariableName(parents, true, true, false)))
} else {
builder.WriteString(fmt.Sprintf("if o%s != nil {\n",
renderNestedVariableName(parent, true, true, false)))
renderNestedVariableName(parents, true, true, false)))
}
} else {
if isParamName(param) {
builder.WriteString(fmt.Sprintf("if o%s != \"\" {\n",
renderNestedVariableName(parent, true, true, true)))
renderNestedVariableName(parents, true, true, true)))
} else {
builder.WriteString(fmt.Sprintf("if o%s != nil {\n",
renderNestedVariableName(parent, true, true, true)))
renderNestedVariableName(parents, true, true, true)))
}
}

}

func finishNestedObjectIfBlock(parent []*properties.SpecParam, param *properties.SpecParam, builder *strings.Builder) {
Expand Down Expand Up @@ -193,8 +196,8 @@ func declareRootOfNestedObject(parent []*properties.SpecParam, builder *strings.

func assignEmptyStructForNestedObject(parent []*properties.SpecParam, builder *strings.Builder, param *properties.SpecParam, objectType, version, prefix, suffix string) {
if isParamListAndProfileTypeIsExtendedEntry(param) {
createListAndLoopForNestedEntry(parent, builder, prefix, suffix, version)
miscForUnknownXmlWithExtendedEntry(parent, builder, suffix)
createListAndLoopForNestedEntry(parent, param, builder, prefix, suffix, version)
miscForUnknownXmlWithExtendedEntry(parent, objectType, builder, suffix)
} else {
createStructForParamWithSpec(parent, builder, prefix, suffix, version)
miscForUnknownXmlWithSpec(parent, builder, suffix, objectType)
Expand All @@ -209,7 +212,7 @@ func createStructForParamWithSpec(parent []*properties.SpecParam, builder *strin
CreateGoSuffixFromVersion(version)))
}

func createListAndLoopForNestedEntry(parent []*properties.SpecParam, builder *strings.Builder, prefix string, suffix string, version string) {
func createListAndLoopForNestedEntry(parent []*properties.SpecParam, param *properties.SpecParam, builder *strings.Builder, prefix string, suffix string, version string) {
if len(parent) == 1 && parent[0].Type == "list" && parent[0].Items.Type == "entry" {
builder.WriteString(fmt.Sprintf("nested%sCol = []%s%s%s%s{}\n",
renderNestedVariableName(parent, true, true, false), prefix,
Expand All @@ -222,9 +225,14 @@ func createListAndLoopForNestedEntry(parent []*properties.SpecParam, builder *st
CreateGoSuffixFromVersion(version)))
}

builder.WriteString(fmt.Sprintf("for _, o%s := range o%s {\n",
startFromDot := true
if len(parent) == 2 && parent[0].Type == "list" && parent[0].Items.Type == "entry" {
startFromDot = false
}

builder.WriteString(fmt.Sprintf("for _, o%s := range o%s { \n",
renderNestedVariableName(parent, false, false, false),
renderNestedVariableName(parent, true, true, true)))
renderNestedVariableName(parent, true, true, startFromDot)))
builder.WriteString(fmt.Sprintf("nested%s := %s%s%s%s{}\n",
renderNestedVariableName(parent, false, false, false),
prefix, renderNestedVariableName(parent, false, false, false), suffix,
Expand All @@ -240,16 +248,21 @@ func miscForUnknownXmlWithSpec(parent []*properties.SpecParam, builder *strings.
renderNestedVariableName(parent, false, false, false),
))
} else {
startsWithDot := true
if parent[0].Type == "list" && parent[0].Items.Type == "entry" {
startsWithDot = false
}

builder.WriteString(fmt.Sprintf("if o%s.Misc != nil {\n",
renderNestedVariableName(parent, true, true, true)))
renderNestedVariableName(parent, true, true, startsWithDot)))
builder.WriteString(fmt.Sprintf("%s.Misc[\"%s\"] = o%s.Misc\n",
objectType, renderNestedVariableName(parent, false, false, false),
renderNestedVariableName(parent, true, true, true),
renderNestedVariableName(parent, true, true, startsWithDot),
))
}
}

func miscForUnknownXmlWithExtendedEntry(parent []*properties.SpecParam, builder *strings.Builder, suffix string) {
func miscForUnknownXmlWithExtendedEntry(parent []*properties.SpecParam, objectType string, builder *strings.Builder, suffix string) {
if suffix == "Xml" {
builder.WriteString(fmt.Sprintf("if _, ok := o.Misc[\"%s\"]; ok {\n",
renderNestedVariableName(parent, false, false, false)))
Expand All @@ -260,7 +273,8 @@ func miscForUnknownXmlWithExtendedEntry(parent []*properties.SpecParam, builder
} else {
builder.WriteString(fmt.Sprintf("if o%s.Misc != nil {\n",
renderNestedVariableName(parent, false, false, false)))
builder.WriteString(fmt.Sprintf("entry.Misc[\"%s\"] = o%s.Misc\n",
builder.WriteString(fmt.Sprintf("%s.Misc[\"%s\"] = o%s.Misc\n",
objectType,
renderNestedVariableName(parent, false, false, false),
renderNestedVariableName(parent, false, false, false),
))
Expand All @@ -270,7 +284,7 @@ func miscForUnknownXmlWithExtendedEntry(parent []*properties.SpecParam, builder
var _ = log.Printf

func assignValueForNestedObject(parent []*properties.SpecParam, builder *strings.Builder, param, parentParam *properties.SpecParam) {
if len(parent) == 2 && parent[0].Type == "list" && parent[0].Items.Type == "entry" {
if parent[0] != param && parent[0].Type == "list" && parent[0].Items.Type == "entry" {
builder.WriteString(fmt.Sprintf("nested%s = o%s\n",
renderNestedVariableName(parent, true, true, false),
renderNestedVariableName(parent, true, true, false)))
Expand All @@ -282,8 +296,9 @@ func assignValueForNestedObject(parent []*properties.SpecParam, builder *strings
}

func assignFunctionForNestedObject(parent []*properties.SpecParam, functionName, additionalArguments string, builder *strings.Builder, param, parentParam *properties.SpecParam) {

var startWithDot bool
if len(parent) == 2 && parent[0].Type == "list" && parent[0].Items.Type == "entry" {
if parent[0] != param && parent[0].Type == "list" && parent[0].Items.Type == "entry" {
startWithDot = false
} else {
startWithDot = true
Expand Down
5 changes: 4 additions & 1 deletion pkg/translate/terraform_provider/terraform_provider_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,10 @@ func conditionallyAddValidators(manager *imports.Manager, spec *properties.Norma
}

func conditionallyAddModifiers(manager *imports.Manager, spec *properties.Normalization) {
manager.AddHashicorpImport("github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier", "")
if len(spec.Locations) > 0 {
manager.AddHashicorpImport("github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier", "")
}

for _, loc := range spec.Locations {
if len(loc.Vars) == 0 {
manager.AddHashicorpImport("github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier", "")
Expand Down

0 comments on commit 9fc5ae4

Please sign in to comment.