Skip to content

Commit

Permalink
replace all string concatenation using "+" by function fmt.Sprintf()
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianczech committed Mar 13, 2024
1 parent 917a301 commit 2939d86
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 34 deletions.
3 changes: 2 additions & 1 deletion pkg/generate/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package generate

import (
"bytes"
"fmt"
"github.com/paloaltonetworks/pan-os-codegen/pkg/properties"
"io"
"io/fs"
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/generate/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 22 additions & 28 deletions pkg/translate/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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))
}
Expand All @@ -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()
Expand All @@ -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))
}
Expand All @@ -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()
Expand All @@ -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 ""
}
Expand All @@ -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))
}
Expand All @@ -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()
Expand Down
9 changes: 5 additions & 4 deletions pkg/translate/structs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package translate

import (
"fmt"
"github.com/paloaltonetworks/pan-os-codegen/pkg/naming"
"github.com/paloaltonetworks/pan-os-codegen/pkg/properties"
)
Expand All @@ -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"
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit 2939d86

Please sign in to comment.