Skip to content

Commit

Permalink
apply changes after review (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianczech committed Mar 14, 2024
1 parent a0edfa2 commit e55a5be
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 22 deletions.
4 changes: 2 additions & 2 deletions pkg/generate/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ func (c *Creator) parseTemplate(templateName string) (*template.Template, error)
"subtract": func(a, b int) int {
return a - b
},
"asEntryXpath": translate.AsEntryXpath,
"nestedSpecs": translate.NestedSpecs,
"generateEntryXpath": translate.GenerateEntryXpathForLocation,
"nestedSpecs": translate.NestedSpecs,
}
return template.New(templateName).Funcs(funcMap).ParseFiles(templatePath)
}
16 changes: 10 additions & 6 deletions pkg/translate/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import (
"strings"
)

// AsEntryXpath functions used in location.tmpl to generate XPath for location.
func AsEntryXpath(location, xpath string) (string, error) {
// GenerateEntryXpathForLocation functions used in location.tmpl to generate XPath for location.
func GenerateEntryXpathForLocation(location, xpath string) (string, error) {
if !strings.Contains(xpath, "$") || !strings.Contains(xpath, "}") {
return "", fmt.Errorf("xpath '%s' is missing '$' followed by '}'", xpath)
}
asEntryXpath := generateXpathForLocation(location, xpath)
asEntryXpath := generateEntryXpathForLocation(location, xpath)
return asEntryXpath, nil
}

func generateXpathForLocation(location string, xpath string) string {
func generateEntryXpathForLocation(location string, xpath string) string {
xpathPartWithoutDollar := strings.SplitAfter(xpath, "$")
xpathPartWithoutBrackets := strings.TrimSpace(strings.Trim(xpathPartWithoutDollar[1], "${}"))
xpathPartCamelCase := naming.CamelCase("", xpathPartWithoutBrackets, "", true)
Expand All @@ -43,7 +43,7 @@ func SpecifyEntryAssignment(param *properties.SpecParam) string {
func prepareAssignment(param *properties.SpecParam, listFunction, specSuffix string) string {
var builder strings.Builder

if param.Type == "list" && param.Profiles != nil && len(param.Profiles) > 0 && param.Profiles[0].Type == "member" {
if isParamListAndProfileTypeIsMember(param) {
builder.WriteString(fmt.Sprintf("entry.%s = %s(o.%s)", param.Name.CamelCase, listFunction, param.Name.CamelCase))
} else if param.Spec != nil {
for _, subParam := range param.Spec.Params {
Expand All @@ -67,6 +67,10 @@ func prepareAssignment(param *properties.SpecParam, listFunction, specSuffix str
return builder.String()
}

func isParamListAndProfileTypeIsMember(param *properties.SpecParam) bool {
return param.Type == "list" && param.Profiles != nil && len(param.Profiles) > 0 && param.Profiles[0].Type == "member"
}

func nestedObjectDeclaration(parent []string, param *properties.SpecParam) string {
var builder strings.Builder

Expand Down Expand Up @@ -101,7 +105,7 @@ func declareVariableForNestedObject(parent []string, param *properties.SpecParam
func nestedObjectAssignment(parent []string, suffix string, param *properties.SpecParam) string {
var builder strings.Builder

if param.Type == "list" && param.Profiles != nil && len(param.Profiles) > 0 && param.Profiles[0].Type == "member" {
if isParamListAndProfileTypeIsMember(param) {
builder.WriteString(fmt.Sprintf("%s : util.StrToMem(o.%s),\n",
param.Name.CamelCase, param.Name.CamelCase))
} else if param.Spec != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/translate/funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"testing"
)

func TestAsEntryXpath(t *testing.T) {
func TestGenerateEntryXpath(t *testing.T) {
// given

// when
asEntryXpath, _ := AsEntryXpath("DeviceGroup", "{{ Entry $panorama_device }}")
asEntryXpath, _ := GenerateEntryXpathForLocation("DeviceGroup", "{{ Entry $panorama_device }}")

// then
assert.Equal(t, "util.AsEntryXpath([]string{o.DeviceGroup.PanoramaDevice}),", asEntryXpath)
Expand Down
22 changes: 11 additions & 11 deletions pkg/translate/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func SpecParamType(parent string, param *properties.SpecParam) string {
calculatedType = param.Type
}

return prefix + calculatedType
return fmt.Sprintf("%s%s", prefix, calculatedType)
}

// XmlParamType return param type (it can be nested spec) (for struct based on spec from YAML files).
Expand All @@ -79,29 +79,29 @@ func XmlParamType(parent string, param *properties.SpecParam) string {
}

calculatedType := ""
if param.Type == "list" && param.Profiles != nil && len(param.Profiles) > 0 && param.Profiles[0].Type == "member" {
if isParamListAndProfileTypeIsMember(param) {
calculatedType = "util.MemberType"
} else if param.Spec != nil {
calculatedType = fmt.Sprintf("Spec%s%sXml", parent, naming.CamelCase("", param.Name.CamelCase, "", true))
} else {
calculatedType = param.Type
}

return prefix + calculatedType
return fmt.Sprintf("%s%s", prefix, calculatedType)
}

// XmlTag creates a string with xml tag (e.g. `xml:"description,omitempty"`).
func XmlTag(param *properties.SpecParam) string {
suffix := ""
if !param.Required {
suffix = ",omitempty"
}

calculatedTag := ""
if param.Profiles != nil && len(param.Profiles) > 0 {
calculatedTag = fmt.Sprintf("`xml:\"%s%s\"`", param.Profiles[0].Xpath[len(param.Profiles[0].Xpath)-1], suffix)
suffix := ""
if !param.Required {
suffix = ",omitempty"
}

return fmt.Sprintf("`xml:\"%s%s\"`", param.Profiles[0].Xpath[len(param.Profiles[0].Xpath)-1], suffix)
}
return calculatedTag

return ""
}

// OmitEmpty return omitempty in XML tag for location, if there are variables defined.
Expand Down
2 changes: 1 addition & 1 deletion templates/sdk/location.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (o Location) Xpath(vn version.Number, name string) ([]string, error) {
ans = []string{
{{- range $name, $xpath := $location.Xpath}}
{{- if contains $xpath "Entry"}}
{{asEntryXpath $location.Name.CamelCase $xpath}}
{{generateEntryXpath $location.Name.CamelCase $xpath}}
{{- else}}
"{{$xpath}}",
{{- end}}
Expand Down

0 comments on commit e55a5be

Please sign in to comment.