diff --git a/pkg/generate/assets.go b/pkg/generate/assets.go index 7e20fa66..9cabbb4b 100644 --- a/pkg/generate/assets.go +++ b/pkg/generate/assets.go @@ -2,6 +2,7 @@ package generate import ( "bytes" + "fmt" "github.com/paloaltonetworks/pan-os-codegen/pkg/properties" "io" "io/fs" @@ -56,7 +57,7 @@ func listAssets(asset *properties.Asset) ([]string, error) { // copyAsset copy single asset, which may contain multiple files func copyAsset(target string, asset *properties.Asset, files []string) error { // Prepare destination path - destinationDir := target + "/" + asset.Destination + destinationDir := fmt.Sprintf("%s/%s", target, asset.Destination) // Create the destination directory if it doesn't exist if err := os.MkdirAll(destinationDir, os.ModePerm); err != nil { diff --git a/pkg/generate/generator.go b/pkg/generate/generator.go index d401ddc4..ca3dbf8d 100644 --- a/pkg/generate/generator.go +++ b/pkg/generate/generator.go @@ -76,7 +76,7 @@ func (c *Creator) RenderTemplate() error { // createFullFilePath returns a full path for output file generated from template passed as argument to function func (c *Creator) createFullFilePath(templateName string) string { fileBaseName := strings.TrimSuffix(templateName, filepath.Ext(templateName)) - return filepath.Join(c.GoOutputDir, filepath.Join(c.Spec.GoSdkPath...), fileBaseName+".go") + return filepath.Join(c.GoOutputDir, filepath.Join(c.Spec.GoSdkPath...), fmt.Sprintf("%s.go", fileBaseName)) } // listOfTemplates return list of templates defined in TemplatesDir diff --git a/pkg/translate/funcs.go b/pkg/translate/funcs.go index 7087af7d..1fa3e50f 100644 --- a/pkg/translate/funcs.go +++ b/pkg/translate/funcs.go @@ -2,6 +2,7 @@ package translate import ( "errors" + "fmt" "github.com/paloaltonetworks/pan-os-codegen/pkg/naming" "github.com/paloaltonetworks/pan-os-codegen/pkg/properties" "strings" @@ -14,7 +15,8 @@ func AsEntryXpath(location, xpath string) (string, error) { } xpath = strings.TrimSpace(strings.Split(strings.Split(xpath, "$")[1], "}")[0]) xpath = naming.CamelCase("", xpath, "", true) - return "util.AsEntryXpath([]string{o." + location + "." + xpath + "}),", nil + asEntryXpath := fmt.Sprintf("util.AsEntryXpath([]string{o.%s.%s}),", location, xpath) + return asEntryXpath, nil } // NormalizeAssignment generates a string, which contains entry assignment in Normalize() function @@ -25,12 +27,12 @@ func NormalizeAssignment(param *properties.SpecParam) string { var builder strings.Builder if param.Type == "list" && param.Profiles != nil && len(param.Profiles) > 0 && param.Profiles[0].Type == "member" { - builder.WriteString("entry." + param.Name.CamelCase + " = entryXml." + param.Name.CamelCase) + builder.WriteString(fmt.Sprintf("entry.%s = entryXml.%s", param.Name.CamelCase, param.Name.CamelCase)) } else if param.Spec != nil { for _, subParam := range param.Spec.Params { builder.WriteString(nestedObjectDeclaration([]string{param.Name.CamelCase}, subParam)) } - builder.WriteString("entry." + param.Name.CamelCase + " = &Spec" + param.Name.CamelCase + "{\n") + builder.WriteString(fmt.Sprintf("entry.%s = &Spec%s{\n", param.Name.CamelCase, param.Name.CamelCase)) for _, subParam := range param.Spec.Params { builder.WriteString(nestedObjectAssignment([]string{param.Name.CamelCase}, "", subParam)) } @@ -39,7 +41,7 @@ func NormalizeAssignment(param *properties.SpecParam) string { } builder.WriteString("}\n") } else { - builder.WriteString("entry." + param.Name.CamelCase + " = entryXml." + param.Name.CamelCase) + builder.WriteString(fmt.Sprintf("entry.%s = entryXml.%s", param.Name.CamelCase, param.Name.CamelCase)) } return builder.String() @@ -53,12 +55,12 @@ func SpecifyEntryAssignment(param *properties.SpecParam) string { var builder strings.Builder if param.Type == "list" && param.Profiles != nil && len(param.Profiles) > 0 && param.Profiles[0].Type == "member" { - builder.WriteString("entry." + param.Name.CamelCase + " = util.StrToMem(o." + param.Name.CamelCase + ")") + builder.WriteString(fmt.Sprintf("entry.%s = util.StrToMem(o.%s)", param.Name.CamelCase, param.Name.CamelCase)) } else if param.Spec != nil { for _, subParam := range param.Spec.Params { builder.WriteString(nestedObjectDeclaration([]string{param.Name.CamelCase}, subParam)) } - builder.WriteString("entry." + param.Name.CamelCase + " = &Spec" + param.Name.CamelCase + "Xml{\n") + builder.WriteString(fmt.Sprintf("entry.%s = &Spec%sXml{\n", param.Name.CamelCase, param.Name.CamelCase)) for _, subParam := range param.Spec.Params { builder.WriteString(nestedObjectAssignment([]string{param.Name.CamelCase}, "Xml", subParam)) } @@ -67,7 +69,7 @@ func SpecifyEntryAssignment(param *properties.SpecParam) string { } builder.WriteString("}\n") } else { - builder.WriteString("entry." + param.Name.CamelCase + " = o." + param.Name.CamelCase) + builder.WriteString(fmt.Sprintf("entry.%s = o.%s", param.Name.CamelCase, param.Name.CamelCase)) } return builder.String() @@ -92,15 +94,13 @@ func nestedObjectDeclaration(parent []string, param *properties.SpecParam) strin func declareVariableForNestedObject(parent []string, param *properties.SpecParam, subParam *properties.SpecParam) string { if subParam.Spec == nil && parent != nil { - return "nested" + - strings.Join(parent, "") + - param.Name.CamelCase + - subParam.Name.CamelCase + - " := o." + - strings.Join(parent, ".") + - "." + param.Name.CamelCase + - "." + subParam.Name.CamelCase + - "\n" + return fmt.Sprintf("nested%s%s%s := o.%s.%s.%s\n", + strings.Join(parent, ""), + param.Name.CamelCase, + subParam.Name.CamelCase, + strings.Join(parent, "."), + param.Name.CamelCase, + subParam.Name.CamelCase) } else { return "" } @@ -110,15 +110,11 @@ func nestedObjectAssignment(parent []string, suffix string, param *properties.Sp var builder strings.Builder if param.Type == "list" && param.Profiles != nil && len(param.Profiles) > 0 && param.Profiles[0].Type == "member" { - builder.WriteString(param.Name.CamelCase + - " : util.StrToMem(o." + - param.Name.CamelCase + - "),\n") + builder.WriteString(fmt.Sprintf("%s : util.StrToMem(o.%s),\n", + param.Name.CamelCase, param.Name.CamelCase)) } else if param.Spec != nil { - builder.WriteString(param.Name.CamelCase + - " : &Spec" + - param.Name.CamelCase + - suffix + "{\n") + builder.WriteString(fmt.Sprintf("%s : &Spec%s%s{\n", + param.Name.CamelCase, param.Name.CamelCase, suffix)) for _, subParam := range param.Spec.Params { builder.WriteString(nestedObjectAssignment(append(parent, param.Name.CamelCase), suffix, subParam)) } @@ -127,10 +123,8 @@ func nestedObjectAssignment(parent []string, suffix string, param *properties.Sp } builder.WriteString("},\n") } else { - builder.WriteString(param.Name.CamelCase + - " : nested" + - strings.Join(parent, "") + - param.Name.CamelCase + ",\n") + builder.WriteString(fmt.Sprintf("%s : nested%s%s,\n", + param.Name.CamelCase, strings.Join(parent, ""), param.Name.CamelCase)) } return builder.String() diff --git a/pkg/translate/structs.go b/pkg/translate/structs.go index 14d2958b..bdf99de8 100644 --- a/pkg/translate/structs.go +++ b/pkg/translate/structs.go @@ -1,6 +1,7 @@ package translate import ( + "fmt" "github.com/paloaltonetworks/pan-os-codegen/pkg/naming" "github.com/paloaltonetworks/pan-os-codegen/pkg/properties" ) @@ -12,7 +13,7 @@ func LocationType(location *properties.Location, pointer bool) string { prefix = "*" } if location.Vars != nil { - return prefix + location.Name.CamelCase + "Location" + return fmt.Sprintf("%s%sLocation", prefix, location.Name.CamelCase) } else { return "bool" } @@ -57,7 +58,7 @@ func SpecParamType(param *properties.SpecParam) string { if param.Type == "list" && param.Items != nil { calculatedType = param.Items.Type } else if param.Spec != nil { - calculatedType = "Spec" + naming.CamelCase("", param.Name.CamelCase, "", true) + calculatedType = fmt.Sprintf("Spec%s", naming.CamelCase("", param.Name.CamelCase, "", true)) } else { calculatedType = param.Type } @@ -76,7 +77,7 @@ func XmlParamType(param *properties.SpecParam) string { if param.Type == "list" && param.Profiles != nil && len(param.Profiles) > 0 && param.Profiles[0].Type == "member" { calculatedType = "util.MemberType" } else if param.Spec != nil { - calculatedType = "Spec" + naming.CamelCase("", param.Name.CamelCase, "", true) + "Xml" + calculatedType = fmt.Sprintf("Spec%sXml", naming.CamelCase("", param.Name.CamelCase, "", true)) } else { calculatedType = param.Type } @@ -93,7 +94,7 @@ func XmlTag(param *properties.SpecParam) string { calculatedTag := "" if param.Profiles != nil && len(param.Profiles) > 0 { - calculatedTag = "`xml:\"" + param.Profiles[0].Xpath[len(param.Profiles[0].Xpath)-1] + suffix + "\"`" + calculatedTag = fmt.Sprintf("`xml:\"%s%s\"`", param.Profiles[0].Xpath[len(param.Profiles[0].Xpath)-1], suffix) } return calculatedTag }