From e55a5be5270c0dbce7c62c54f6deb417a5a2a2d7 Mon Sep 17 00:00:00 2001 From: Sebastian Czech Date: Thu, 14 Mar 2024 13:42:47 +0100 Subject: [PATCH] apply changes after review (#2) --- pkg/generate/generator.go | 4 ++-- pkg/translate/funcs.go | 16 ++++++++++------ pkg/translate/funcs_test.go | 4 ++-- pkg/translate/structs.go | 22 +++++++++++----------- templates/sdk/location.tmpl | 2 +- 5 files changed, 26 insertions(+), 22 deletions(-) diff --git a/pkg/generate/generator.go b/pkg/generate/generator.go index 6e671302..d6c35bb1 100644 --- a/pkg/generate/generator.go +++ b/pkg/generate/generator.go @@ -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) } diff --git a/pkg/translate/funcs.go b/pkg/translate/funcs.go index 1328d8fe..dc9cf49f 100644 --- a/pkg/translate/funcs.go +++ b/pkg/translate/funcs.go @@ -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) @@ -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 { @@ -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 @@ -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 { diff --git a/pkg/translate/funcs_test.go b/pkg/translate/funcs_test.go index e04fc087..0c048edf 100644 --- a/pkg/translate/funcs_test.go +++ b/pkg/translate/funcs_test.go @@ -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) diff --git a/pkg/translate/structs.go b/pkg/translate/structs.go index cfc15670..6bdbb13e 100644 --- a/pkg/translate/structs.go +++ b/pkg/translate/structs.go @@ -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). @@ -79,7 +79,7 @@ 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)) @@ -87,21 +87,21 @@ func XmlParamType(parent string, param *properties.SpecParam) string { 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. diff --git a/templates/sdk/location.tmpl b/templates/sdk/location.tmpl index cdc57075..121125cf 100644 --- a/templates/sdk/location.tmpl +++ b/templates/sdk/location.tmpl @@ -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}}