From 2b5ff1ac708eb6226490b4a8e9bfd387614d4163 Mon Sep 17 00:00:00 2001 From: Sebastian Czech Date: Fri, 22 Mar 2024 16:04:27 +0100 Subject: [PATCH] improve rendering different structs names for different versions --- pkg/generate/generator.go | 5 +- pkg/properties/normalized.go | 4 +- pkg/properties/normalized_test.go | 2 +- pkg/translate/structs.go | 13 +- pkg/translate/structs_test.go | 10 +- templates/sdk/config.tmpl | 256 +++++++++++++------------- templates/sdk/entry.tmpl | 296 +++++++++++++++--------------- templates/sdk/interfaces.tmpl | 2 +- templates/sdk/location.tmpl | 94 +++++----- 9 files changed, 346 insertions(+), 336 deletions(-) diff --git a/pkg/generate/generator.go b/pkg/generate/generator.go index d6c35bb1..80b1808e 100644 --- a/pkg/generate/generator.go +++ b/pkg/generate/generator.go @@ -132,8 +132,9 @@ func (c *Creator) parseTemplate(templateName string) (*template.Template, error) "subtract": func(a, b int) int { return a - b }, - "generateEntryXpath": translate.GenerateEntryXpathForLocation, - "nestedSpecs": translate.NestedSpecs, + "generateEntryXpath": translate.GenerateEntryXpathForLocation, + "nestedSpecs": translate.NestedSpecs, + "createGoSuffixFromVersion": translate.CreateGoSuffixFromVersion, } return template.New(templateName).Funcs(funcMap).ParseFiles(templatePath) } diff --git a/pkg/properties/normalized.go b/pkg/properties/normalized.go index 371dd19a..db18c8e6 100644 --- a/pkg/properties/normalized.go +++ b/pkg/properties/normalized.go @@ -296,7 +296,7 @@ func (spec *Normalization) Validate() []error { return checks } -// SupportedVersions provides list of all supported versions in format _MAJOR_MINOR_PATCH +// SupportedVersions provides list of all supported versions in format MAJOR.MINOR.PATCH func (spec *Normalization) SupportedVersions() []string { if spec.Spec != nil { versions := supportedVersions(spec.Spec.Params, []string{""}) @@ -317,7 +317,7 @@ func supportedVersions(params map[string]*SpecParam, versions []string) []string } } if notExist { - versions = append(versions, fmt.Sprintf("_%s", strings.ReplaceAll(profile.FromVersion, ".", "_"))) + versions = append(versions, profile.FromVersion) } } } diff --git a/pkg/properties/normalized_test.go b/pkg/properties/normalized_test.go index 63455763..f5a1091e 100644 --- a/pkg/properties/normalized_test.go +++ b/pkg/properties/normalized_test.go @@ -424,5 +424,5 @@ func TestNamesOfStructsForVersioning(t *testing.T) { // then assert.NotNilf(t, yamlParsedData, "Unmarshalled data cannot be nil") - assert.Contains(t, versions, "_10_1_1") + assert.Contains(t, versions, "10.1.1") } diff --git a/pkg/translate/structs.go b/pkg/translate/structs.go index 76ad7cc7..e1bad63b 100644 --- a/pkg/translate/structs.go +++ b/pkg/translate/structs.go @@ -46,7 +46,7 @@ func updateNestedSpecs(parent []string, param *properties.SpecParam, nestedSpecs } // SpecParamType return param type (it can be nested spec) (for struct based on spec from YAML files). -func SpecParamType(parent string, param *properties.SpecParam, version string) string { +func SpecParamType(parent string, param *properties.SpecParam) string { prefix := determinePrefix(param, false) calculatedType := "" @@ -62,7 +62,7 @@ func SpecParamType(parent string, param *properties.SpecParam, version string) s } // XmlParamType return param type (it can be nested spec) (for struct based on spec from YAML files). -func XmlParamType(parent string, param *properties.SpecParam, version string) string { +func XmlParamType(parent string, param *properties.SpecParam) string { prefix := determinePrefix(param, true) calculatedType := "" @@ -123,3 +123,12 @@ func OmitEmpty(location *properties.Location) string { return "" } } + +// CreateGoSuffixFromVersion convert version into Go suffix e.g. 10.1.1 into _10_1_1 +func CreateGoSuffixFromVersion(version string) string { + if len(version) > 0 { + return fmt.Sprintf("_%s", strings.ReplaceAll(version, ".", "_")) + } else { + return version + } +} diff --git a/pkg/translate/structs_test.go b/pkg/translate/structs_test.go index 78a51945..415be26d 100644 --- a/pkg/translate/structs_test.go +++ b/pkg/translate/structs_test.go @@ -85,9 +85,9 @@ func TestSpecParamType(t *testing.T) { } // when - calculatedTypeRequiredString := SpecParamType("", ¶mTypeRequiredString, "") - calculatedTypeListString := SpecParamType("", ¶mTypeListString, "") - calculatedTypeOptionalString := SpecParamType("", ¶mTypeOptionalString, "") + calculatedTypeRequiredString := SpecParamType("", ¶mTypeRequiredString) + calculatedTypeListString := SpecParamType("", ¶mTypeListString) + calculatedTypeOptionalString := SpecParamType("", ¶mTypeOptionalString) // then assert.Equal(t, "string", calculatedTypeRequiredString) @@ -139,8 +139,8 @@ func TestXmlParamType(t *testing.T) { } // when - calculatedTypeRequiredString := XmlParamType("", ¶mTypeRequiredString, "") - calculatedTypeListString := XmlParamType("", ¶mTypeListString, "") + calculatedTypeRequiredString := XmlParamType("", ¶mTypeRequiredString) + calculatedTypeListString := XmlParamType("", ¶mTypeListString) // then assert.Equal(t, "string", calculatedTypeRequiredString) diff --git a/templates/sdk/config.tmpl b/templates/sdk/config.tmpl index bf4a6c31..d15d7ae7 100644 --- a/templates/sdk/config.tmpl +++ b/templates/sdk/config.tmpl @@ -1,172 +1,172 @@ {{- if not .Entry}} -package {{packageName .GoSdkPath}} - -import ( - "encoding/xml" - "fmt" - - "github.com/PaloAltoNetworks/pango/filtering" - "github.com/PaloAltoNetworks/pango/generic" - "github.com/PaloAltoNetworks/pango/util" - "github.com/PaloAltoNetworks/pango/version" -) - -{{- range $version := .SupportedVersions }} -type Config struct { -{{- range $_, $param := $.Spec.Params}} - {{$param.Name.CamelCase}} {{specParamType "" $param $version}} - {{- end}} -{{- range $_, $param := $.Spec.OneOf}} - {{$param.Name.CamelCase}} {{specParamType "" $param $version}} + package {{packageName .GoSdkPath}} + + import ( + "encoding/xml" + "fmt" + + "github.com/PaloAltoNetworks/pango/filtering" + "github.com/PaloAltoNetworks/pango/generic" + "github.com/PaloAltoNetworks/pango/util" + "github.com/PaloAltoNetworks/pango/version" + ) + + {{- range $version := .SupportedVersions }} + type Config{{createGoSuffixFromVersion $version}} struct { + {{- range $_, $param := $.Spec.Params}} + {{$param.Name.CamelCase}} {{specParamType "" $param}} + {{- end}} + {{- range $_, $param := $.Spec.OneOf}} + {{$param.Name.CamelCase}} {{specParamType "" $param}} + {{- end}} + + Misc map[string][]generic.Xml + } {{- end}} - Misc map[string][]generic.Xml -} -{{- end}} + {{- range $version := .SupportedVersions }} + {{- range $name, $spec := nestedSpecs $.Spec }} + type Spec{{$name}}{{createGoSuffixFromVersion $version}} struct { + {{- range $_, $param := $spec.Params}} + {{$param.Name.CamelCase}} {{specParamType $name $param}} + {{- end}} + {{- range $_, $param := $spec.OneOf}} + {{$param.Name.CamelCase}} {{specParamType $name $param}} + {{- end}} -{{- range $version := .SupportedVersions }} - {{- range $name, $spec := nestedSpecs $.Spec }} -type Spec{{$name}} struct { - {{- range $_, $param := $spec.Params}} - {{$param.Name.CamelCase}} {{specParamType $name $param $version}} - {{- end}} - {{- range $_, $param := $spec.OneOf}} - {{$param.Name.CamelCase}} {{specParamType $name $param $version}} + Misc map[string][]generic.Xml + } + {{- end}} {{- end}} - Misc map[string][]generic.Xml -} -{{- end}} -{{- end}} - -{{- range $version := .SupportedVersions }} - type configXmlContainer{{$version}} struct { - Answer []configXml{{$version}} `xml:"config"` -} -{{- end}} - -{{- range $version := .SupportedVersions }} - type configXml{{$version}} struct { - {{- range $_, $param := $.Spec.Params}} - {{$param.Name.CamelCase}} {{xmlParamType "" $param $version}} {{xmlTag $param}} - {{- end}} - {{- range $_, $param := $.Spec.OneOf}} - {{$param.Name.CamelCase}} {{xmlParamType "" $param $version}} {{xmlTag $param}} + {{- range $version := .SupportedVersions }} + type configXmlContainer{{createGoSuffixFromVersion $version}} struct { + Answer []configXml{{createGoSuffixFromVersion $version}} `xml:"config"` + } {{- end}} - Misc []generic.Xml `xml:",any"` -} -{{- end}} + {{- range $version := .SupportedVersions }} + type configXml{{createGoSuffixFromVersion $version}} struct { + {{- range $_, $param := $.Spec.Params}} + {{$param.Name.CamelCase}} {{xmlParamType "" $param}} {{xmlTag $param}} + {{- end}} + {{- range $_, $param := $.Spec.OneOf}} + {{$param.Name.CamelCase}} {{xmlParamType "" $param}} {{xmlTag $param}} + {{- end}} -{{- range $version := .SupportedVersions }} - {{- range $name, $spec := nestedSpecs $.Spec }} - type spec{{$name}}Xml{{$version}} struct { - {{- range $_, $param := $spec.Params}} - {{$param.Name.CamelCase}} {{xmlParamType $name $param $version}} {{xmlTag $param}} - {{- end}} - {{- range $_, $param := $spec.OneOf}} - {{$param.Name.CamelCase}} {{xmlParamType $name $param $version}} {{xmlTag $param}} + Misc []generic.Xml `xml:",any"` + } {{- end}} - Misc []generic.Xml `xml:",any"` -} -{{- end}} -{{- end}} + {{- range $version := .SupportedVersions }} + {{- range $name, $spec := nestedSpecs $.Spec }} + type spec{{$name}}Xml{{createGoSuffixFromVersion $version}} struct { + {{- range $_, $param := $spec.Params}} + {{$param.Name.CamelCase}} {{xmlParamType $name $param}} {{xmlTag $param}} + {{- end}} + {{- range $_, $param := $spec.OneOf}} + {{$param.Name.CamelCase}} {{xmlParamType $name $param}} {{xmlTag $param}} + {{- end}} -func (e *Config) CopyMiscFrom(v *Config) { - if v == nil || len(v.Misc) == 0 { - return - } + Misc []generic.Xml `xml:",any"` + } + {{- end}} + {{- end}} - e.Misc = make(map[string][]generic.Xml) - for key := range v.Misc { - e.Misc[key] = append([]generic.Xml(nil), v.Misc[key]...) - } -} + func (e *Config) CopyMiscFrom(v *Config) { + if v == nil || len(v.Misc) == 0 { + return + } -func (e *Config) Field(v string) (any, error) { - {{- range $_, $param := .Spec.Params}} - if v == "{{$param.Name.Underscore}}" || v == "{{$param.Name.CamelCase}}" { - return e.{{$param.Name.CamelCase}}, nil + e.Misc = make(map[string][]generic.Xml) + for key := range v.Misc { + e.Misc[key] = append([]generic.Xml(nil), v.Misc[key]...) } - {{- if eq $param.Type "list"}} - if v == "{{$param.Name.Underscore}}|LENGTH" || v == "{{$param.Name.CamelCase}}|LENGTH" { - return int64(len(e.{{$param.Name.CamelCase}})), nil } - {{- end}} + + func (e *Config) Field(v string) (any, error) { + {{- range $_, $param := .Spec.Params}} + if v == "{{$param.Name.Underscore}}" || v == "{{$param.Name.CamelCase}}" { + return e.{{$param.Name.CamelCase}}, nil + } + {{- if eq $param.Type "list"}} + if v == "{{$param.Name.Underscore}}|LENGTH" || v == "{{$param.Name.CamelCase}}|LENGTH" { + return int64(len(e.{{$param.Name.CamelCase}})), nil + } + {{- end}} {{- end}} {{- range $_, $param := .Spec.OneOf}} - if v == "{{$param.Name.Underscore}}" || v == "{{$param.Name.CamelCase}}" { + if v == "{{$param.Name.Underscore}}" || v == "{{$param.Name.CamelCase}}" { return e.{{$param.Name.CamelCase}}, nil - } + } {{- end}} - return nil, fmt.Errorf("unknown field") -} + return nil, fmt.Errorf("unknown field") + } -func Versioning(vn version.Number) (Specifier, Normalizer, error) { -return SpecifyConfig, &configXmlContainer{}, nil -} + func Versioning(vn version.Number) (Specifier, Normalizer, error) { + return SpecifyConfig, &configXmlContainer{}, nil + } -func SpecifyConfig(o Config) (any, error) { -config := configXml{} + func SpecifyConfig(o Config) (any, error) { + config := configXml{} {{- range $_, $param := .Spec.Params}} - {{specifyEntryAssignment "config" $param}} + {{specifyEntryAssignment "config" $param}} {{- end}} {{- range $_, $param := .Spec.OneOf}} - {{specifyEntryAssignment "config" $param}} + {{specifyEntryAssignment "config" $param}} {{- end}} - config.Misc = o.Misc["Config"] + config.Misc = o.Misc["Config"] - return config, nil -} + return config, nil + } -func (c *configXmlContainer) Normalize() ([]Config, error) { - configList := make([]Config, 0, len(c.Answer)) - for _, o := range c.Answer { - config := Config{ - Misc: make(map[string][]generic.Xml), - } - {{- range $_, $param := .Spec.Params}} + func (c *configXmlContainer) Normalize() ([]Config, error) { + configList := make([]Config, 0, len(c.Answer)) + for _, o := range c.Answer { + config := Config{ + Misc: make(map[string][]generic.Xml), + } + {{- range $_, $param := .Spec.Params}} {{normalizeAssignment "config" $param}} - {{- end}} - {{- range $_, $param := .Spec.OneOf}} + {{- end}} + {{- range $_, $param := .Spec.OneOf}} {{normalizeAssignment "config" $param}} - {{- end}} + {{- end}} - config.Misc["Config"] = o.Misc + config.Misc["Config"] = o.Misc - configList = append(configList, config) - } + configList = append(configList, config) + } - return configList, nil -} + return configList, nil + } -func SpecMatches(a, b *Config) bool { - if a == nil && b != nil || a != nil && b == nil { - return false - } else if a == nil && b == nil { - return true - } + func SpecMatches(a, b *Config) bool { + if a == nil && b != nil || a != nil && b == nil { + return false + } else if a == nil && b == nil { + return true + } - // Don't compare Name. + // Don't compare Name. {{- range $_, $param := .Spec.Params}} - {{- if or (eq $param.Type "list") (eq $param.Type "string")}} - if !util.{{specMatchesFunction $param}}(a.{{$param.Name.CamelCase}}, b.{{$param.Name.CamelCase}}) { - return false - } - {{- end}} + {{- if or (eq $param.Type "list") (eq $param.Type "string")}} + if !util.{{specMatchesFunction $param}}(a.{{$param.Name.CamelCase}}, b.{{$param.Name.CamelCase}}) { + return false + } + {{- end}} {{- end}} {{- range $_, $param := .Spec.OneOf}} - {{- if or (eq $param.Type "list") (eq $param.Type "string")}} - if !util.{{specMatchesFunction $param}}(a.{{$param.Name.CamelCase}}, b.{{$param.Name.CamelCase}}) { - return false - } - {{- end}} + {{- if or (eq $param.Type "list") (eq $param.Type "string")}} + if !util.{{specMatchesFunction $param}}(a.{{$param.Name.CamelCase}}, b.{{$param.Name.CamelCase}}) { + return false + } + {{- end}} {{- end}} - return true -} + return true + } {{- end}} \ No newline at end of file diff --git a/templates/sdk/entry.tmpl b/templates/sdk/entry.tmpl index 00c20b1f..9b4ed6e4 100644 --- a/templates/sdk/entry.tmpl +++ b/templates/sdk/entry.tmpl @@ -1,193 +1,193 @@ {{- if .Entry}} -package {{packageName .GoSdkPath}} - -import ( - "encoding/xml" - "fmt" - - "github.com/PaloAltoNetworks/pango/filtering" - "github.com/PaloAltoNetworks/pango/generic" - "github.com/PaloAltoNetworks/pango/util" - "github.com/PaloAltoNetworks/pango/version" -) - -var ( - _ filtering.Fielder = &Entry{} -) - -var ( - Suffix = []string{ - {{- $length := subtract (len .XpathSuffix) 1 }} - {{- range $index, $suffix := .XpathSuffix}}" - {{- $suffix}}"{{- if lt $index $length}},{{- end}} - {{- end}}} -) - -{{- range $version := .SupportedVersions }} - type Entry{{$version}} struct { - Name string - {{- range $_, $param := $.Spec.Params}} - {{$param.Name.CamelCase}} {{specParamType "" $param $version}} - {{- end}} - {{- range $_, $param := $.Spec.OneOf}} - {{$param.Name.CamelCase}} {{specParamType "" $param $version}} + package {{packageName .GoSdkPath}} + + import ( + "encoding/xml" + "fmt" + + "github.com/PaloAltoNetworks/pango/filtering" + "github.com/PaloAltoNetworks/pango/generic" + "github.com/PaloAltoNetworks/pango/util" + "github.com/PaloAltoNetworks/pango/version" + ) + + var ( + _ filtering.Fielder = &Entry{} + ) + + var ( + Suffix = []string{ + {{- $length := subtract (len .XpathSuffix) 1 }} + {{- range $index, $suffix := .XpathSuffix}}" + {{- $suffix}}"{{- if lt $index $length}},{{- end}} + {{- end}}} + ) + + {{- range $version := .SupportedVersions }} + type Entry{{createGoSuffixFromVersion $version}} struct { + Name string + {{- range $_, $param := $.Spec.Params}} + {{$param.Name.CamelCase}} {{specParamType "" $param}} + {{- end}} + {{- range $_, $param := $.Spec.OneOf}} + {{$param.Name.CamelCase}} {{specParamType "" $param}} + {{- end}} + + Misc map[string][]generic.Xml + } {{- end}} - Misc map[string][]generic.Xml -} -{{- end}} + {{- range $version := .SupportedVersions }} + {{- range $name, $spec := nestedSpecs $.Spec }} + type Spec{{$name}}{{createGoSuffixFromVersion $version}} struct { + {{- range $_, $param := $spec.Params}} + {{$param.Name.CamelCase}} {{specParamType $name $param}} + {{- end}} + {{- range $_, $param := $spec.OneOf}} + {{$param.Name.CamelCase}} {{specParamType $name $param}} + {{- end}} -{{- range $version := .SupportedVersions }} - {{- range $name, $spec := nestedSpecs $.Spec }} - type Spec{{$name}}{{$version}} struct { - {{- range $_, $param := $spec.Params}} - {{$param.Name.CamelCase}} {{specParamType $name $param $version}} - {{- end}} - {{- range $_, $param := $spec.OneOf}} - {{$param.Name.CamelCase}} {{specParamType $name $param $version}} + Misc map[string][]generic.Xml + } + {{- end}} {{- end}} - Misc map[string][]generic.Xml -} -{{- end}} -{{- end}} - -{{- range $version := .SupportedVersions }} - type entryXmlContainer{{$version}} struct { - Answer []entryXml{{$version}} `xml:"entry"` -} -{{- end}} - -{{- range $version := .SupportedVersions }} - type entryXml{{$version}} struct { - XMLName xml.Name `xml:"entry"` - Name string `xml:"name,attr"` - {{- range $_, $param := $.Spec.Params}} - {{$param.Name.CamelCase}} {{xmlParamType "" $param $version}} {{xmlTag $param}} - {{- end}} - {{- range $_, $param := $.Spec.OneOf}} - {{$param.Name.CamelCase}} {{xmlParamType "" $param $version}} {{xmlTag $param}} + {{- range $version := .SupportedVersions }} + type entryXmlContainer{{createGoSuffixFromVersion $version}} struct { + Answer []entryXml{{createGoSuffixFromVersion $version}} `xml:"entry"` + } {{- end}} - Misc []generic.Xml `xml:",any"` -} -{{- end}} + {{- range $version := .SupportedVersions }} + type entryXml{{createGoSuffixFromVersion $version}} struct { + XMLName xml.Name `xml:"entry"` + Name string `xml:"name,attr"` + {{- range $_, $param := $.Spec.Params}} + {{$param.Name.CamelCase}} {{xmlParamType "" $param}} {{xmlTag $param}} + {{- end}} + {{- range $_, $param := $.Spec.OneOf}} + {{$param.Name.CamelCase}} {{xmlParamType "" $param}} {{xmlTag $param}} + {{- end}} -{{- range $version := .SupportedVersions }} - {{- range $name, $spec := nestedSpecs $.Spec }} - type spec{{$name}}Xml{{$version}} struct { - {{- range $_, $param := $spec.Params}} - {{$param.Name.CamelCase}} {{xmlParamType $name $param $version}} {{xmlTag $param}} - {{- end}} - {{- range $_, $param := $spec.OneOf}} - {{$param.Name.CamelCase}} {{xmlParamType $name $param $version}} {{xmlTag $param}} + Misc []generic.Xml `xml:",any"` + } {{- end}} - Misc []generic.Xml `xml:",any"` -} -{{- end}} -{{- end}} + {{- range $version := .SupportedVersions }} + {{- range $name, $spec := nestedSpecs $.Spec }} + type spec{{$name}}Xml{{createGoSuffixFromVersion $version}} struct { + {{- range $_, $param := $spec.Params}} + {{$param.Name.CamelCase}} {{xmlParamType $name $param}} {{xmlTag $param}} + {{- end}} + {{- range $_, $param := $spec.OneOf}} + {{$param.Name.CamelCase}} {{xmlParamType $name $param}} {{xmlTag $param}} + {{- end}} -func (e *Entry) CopyMiscFrom(v *Entry) { - if v == nil || len(v.Misc) == 0 { - return - } + Misc []generic.Xml `xml:",any"` + } + {{- end}} + {{- end}} + + func (e *Entry) CopyMiscFrom(v *Entry) { + if v == nil || len(v.Misc) == 0 { + return + } - e.Misc = make(map[string][]generic.Xml) - for key := range v.Misc { - e.Misc[key] = append([]generic.Xml(nil), v.Misc[key]...) - } -} + e.Misc = make(map[string][]generic.Xml) + for key := range v.Misc { + e.Misc[key] = append([]generic.Xml(nil), v.Misc[key]...) + } + } -func (e *Entry) Field(v string) (any, error) { - if v == "name" || v == "Name" { - return e.Name, nil - } + func (e *Entry) Field(v string) (any, error) { + if v == "name" || v == "Name" { + return e.Name, nil + } {{- range $_, $param := .Spec.Params}} - if v == "{{$param.Name.Underscore}}" || v == "{{$param.Name.CamelCase}}" { + if v == "{{$param.Name.Underscore}}" || v == "{{$param.Name.CamelCase}}" { return e.{{$param.Name.CamelCase}}, nil - } - {{- if eq $param.Type "list"}} - if v == "{{$param.Name.Underscore}}|LENGTH" || v == "{{$param.Name.CamelCase}}|LENGTH" { - return int64(len(e.{{$param.Name.CamelCase}})), nil - } - {{- end}} + } + {{- if eq $param.Type "list"}} + if v == "{{$param.Name.Underscore}}|LENGTH" || v == "{{$param.Name.CamelCase}}|LENGTH" { + return int64(len(e.{{$param.Name.CamelCase}})), nil + } + {{- end}} {{- end}} {{- range $_, $param := .Spec.OneOf}} - if v == "{{$param.Name.Underscore}}" || v == "{{$param.Name.CamelCase}}" { + if v == "{{$param.Name.Underscore}}" || v == "{{$param.Name.CamelCase}}" { return e.{{$param.Name.CamelCase}}, nil - } + } {{- end}} - return nil, fmt.Errorf("unknown field") -} + return nil, fmt.Errorf("unknown field") + } -func Versioning(vn version.Number) (Specifier, Normalizer, error) { - return SpecifyEntry, &entryXmlContainer{}, nil -} + func Versioning(vn version.Number) (Specifier, Normalizer, error) { + return SpecifyEntry, &entryXmlContainer{}, nil + } -func SpecifyEntry(o Entry) (any, error) { - entry := entryXml{} + func SpecifyEntry(o Entry) (any, error) { + entry := entryXml{} - entry.Name = o.Name + entry.Name = o.Name {{- range $_, $param := .Spec.Params}} - {{specifyEntryAssignment "entry" $param}} + {{specifyEntryAssignment "entry" $param}} {{- end}} {{- range $_, $param := .Spec.OneOf}} - {{specifyEntryAssignment "entry" $param}} + {{specifyEntryAssignment "entry" $param}} {{- end}} - entry.Misc = o.Misc["Entry"] + entry.Misc = o.Misc["Entry"] - return entry, nil -} + return entry, nil + } -func (c *entryXmlContainer) Normalize() ([]Entry, error) { - entryList := make([]Entry, 0, len(c.Answer)) - for _, o := range c.Answer { - entry := Entry{ - Misc: make(map[string][]generic.Xml), - } - entry.Name = o.Name - {{- range $_, $param := .Spec.Params}} + func (c *entryXmlContainer) Normalize() ([]Entry, error) { + entryList := make([]Entry, 0, len(c.Answer)) + for _, o := range c.Answer { + entry := Entry{ + Misc: make(map[string][]generic.Xml), + } + entry.Name = o.Name + {{- range $_, $param := .Spec.Params}} {{normalizeAssignment "entry" $param}} - {{- end}} - {{- range $_, $param := .Spec.OneOf}} + {{- end}} + {{- range $_, $param := .Spec.OneOf}} {{normalizeAssignment "entry" $param}} - {{- end}} + {{- end}} - entry.Misc["Entry"] = o.Misc + entry.Misc["Entry"] = o.Misc - entryList = append(entryList, entry) - } + entryList = append(entryList, entry) + } - return entryList, nil -} + return entryList, nil + } -func SpecMatches(a, b *Entry) bool { - if a == nil && b != nil || a != nil && b == nil { - return false - } else if a == nil && b == nil { - return true - } + func SpecMatches(a, b *Entry) bool { + if a == nil && b != nil || a != nil && b == nil { + return false + } else if a == nil && b == nil { + return true + } - // Don't compare Name. + // Don't compare Name. {{- range $_, $param := .Spec.Params}} - {{- if or (eq $param.Type "list") (eq $param.Type "string")}} - if !util.{{specMatchesFunction $param}}(a.{{$param.Name.CamelCase}}, b.{{$param.Name.CamelCase}}) { - return false - } - {{- end}} + {{- if or (eq $param.Type "list") (eq $param.Type "string")}} + if !util.{{specMatchesFunction $param}}(a.{{$param.Name.CamelCase}}, b.{{$param.Name.CamelCase}}) { + return false + } + {{- end}} {{- end}} {{- range $_, $param := .Spec.OneOf}} - {{- if or (eq $param.Type "list") (eq $param.Type "string")}} - if !util.{{specMatchesFunction $param}}(a.{{$param.Name.CamelCase}}, b.{{$param.Name.CamelCase}}) { - return false - } - {{- end}} + {{- if or (eq $param.Type "list") (eq $param.Type "string")}} + if !util.{{specMatchesFunction $param}}(a.{{$param.Name.CamelCase}}, b.{{$param.Name.CamelCase}}) { + return false + } + {{- end}} {{- end}} - return true -} + return true + } {{- end}} \ No newline at end of file diff --git a/templates/sdk/interfaces.tmpl b/templates/sdk/interfaces.tmpl index c886baa1..cac86e15 100644 --- a/templates/sdk/interfaces.tmpl +++ b/templates/sdk/interfaces.tmpl @@ -3,5 +3,5 @@ package {{packageName .GoSdkPath}} type Specifier func(Entry) (any, error) type Normalizer interface { - Normalize() ([]Entry, error) +Normalize() ([]Entry, error) } \ No newline at end of file diff --git a/templates/sdk/location.tmpl b/templates/sdk/location.tmpl index 5d898d08..13c0e854 100644 --- a/templates/sdk/location.tmpl +++ b/templates/sdk/location.tmpl @@ -1,81 +1,81 @@ package {{packageName .GoSdkPath}} import ( - "fmt" +"fmt" - "github.com/PaloAltoNetworks/pango/errors" - "github.com/PaloAltoNetworks/pango/util" - "github.com/PaloAltoNetworks/pango/version" +"github.com/PaloAltoNetworks/pango/errors" +"github.com/PaloAltoNetworks/pango/util" +"github.com/PaloAltoNetworks/pango/version" ) type Location struct { - {{range $key, $location := .Locations}} +{{range $key, $location := .Locations}} {{- $location.Name.CamelCase }} {{locationType $location true}} `json:"{{$location.Name.Underscore}}{{omitEmpty $location}}"` - {{end}} +{{end}} } {{range $key, $location := .Locations}} -{{- if $location.Vars}} -type {{locationType $location false}} struct { -{{- range $key, $var := $location.Vars}} - {{$var.Name.CamelCase}} string `json:"{{$var.Name.Underscore}}"` -{{- end}} -} -{{end}} + {{- if $location.Vars}} + type {{locationType $location false}} struct { + {{- range $key, $var := $location.Vars}} + {{$var.Name.CamelCase}} string `json:"{{$var.Name.Underscore}}"` + {{- end}} + } + {{end}} {{- end}} func (o Location) IsValid() error { - count := 0 +count := 0 - switch { - {{- range $key, $location := .Locations}} +switch { +{{- range $key, $location := .Locations}} case o.{{- $location.Name.CamelCase}}{{if ne (locationType $location true) "bool"}} != nil{{end}}: - {{- range $name, $var := $location.Vars}} + {{- range $name, $var := $location.Vars}} if o.{{$location.Name.CamelCase}}.{{$var.Name.CamelCase}} == "" { - return fmt.Errorf("{{$var.Name.CamelCase}} is unspecified") + return fmt.Errorf("{{$var.Name.CamelCase}} is unspecified") } - {{- end}} - count++ {{- end}} - } + count++ +{{- end}} +} - if count == 0 { - return fmt.Errorf("no path specified") - } +if count == 0 { +return fmt.Errorf("no path specified") +} - if count > 1 { - return fmt.Errorf("multiple paths specified: only one should be specified") - } +if count > 1 { +return fmt.Errorf("multiple paths specified: only one should be specified") +} - return nil +return nil } func (o Location) Xpath(vn version.Number, name string) ([]string, error) { - var ans []string +var ans []string - switch { - {{- range $key, $location := .Locations}} +switch { +{{- range $key, $location := .Locations}} case o.{{- $location.Name.CamelCase}}{{if ne (locationType $location true) "bool"}} != nil{{end}}: - {{- range $name, $var := $location.Vars}} + {{- range $name, $var := $location.Vars}} if o.{{$location.Name.CamelCase}}.{{$var.Name.CamelCase}} == "" { - return nil, fmt.Errorf("{{$var.Name.CamelCase}} is unspecified") + return nil, fmt.Errorf("{{$var.Name.CamelCase}} is unspecified") } - {{- end}} - ans = []string{ - {{- range $name, $xpath := $location.Xpath}} - {{- if contains $xpath "Entry"}} + {{- end}} + ans = []string{ + {{- range $name, $xpath := $location.Xpath}} + {{- if contains $xpath "Entry"}} {{generateEntryXpath $location.Name.CamelCase $xpath}} - {{- else}} + {{- else}} "{{$xpath}}", - {{- end}} - {{- end}} - } + {{- end}} {{- end}} - default: - return nil, errors.NoLocationSpecifiedError - } + } +{{- end}} +default: +return nil, errors.NoLocationSpecifiedError +} - ans = append(ans, Suffix...) - ans = append(ans, util.AsEntryXpath([]string{name})) +ans = append(ans, Suffix...) +ans = append(ans, util.AsEntryXpath([]string{name})) - return ans, nil +return ans, nil } \ No newline at end of file