Skip to content

Commit

Permalink
improve rendering different structs names for different versions
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianczech committed Mar 22, 2024
1 parent 36db8b4 commit 2b5ff1a
Show file tree
Hide file tree
Showing 9 changed files with 346 additions and 336 deletions.
5 changes: 3 additions & 2 deletions pkg/generate/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions pkg/properties/normalized.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{""})
Expand All @@ -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)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/properties/normalized_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
13 changes: 11 additions & 2 deletions pkg/translate/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := ""
Expand All @@ -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 := ""
Expand Down Expand Up @@ -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
}
}
10 changes: 5 additions & 5 deletions pkg/translate/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ func TestSpecParamType(t *testing.T) {
}

// when
calculatedTypeRequiredString := SpecParamType("", &paramTypeRequiredString, "")
calculatedTypeListString := SpecParamType("", &paramTypeListString, "")
calculatedTypeOptionalString := SpecParamType("", &paramTypeOptionalString, "")
calculatedTypeRequiredString := SpecParamType("", &paramTypeRequiredString)
calculatedTypeListString := SpecParamType("", &paramTypeListString)
calculatedTypeOptionalString := SpecParamType("", &paramTypeOptionalString)

// then
assert.Equal(t, "string", calculatedTypeRequiredString)
Expand Down Expand Up @@ -139,8 +139,8 @@ func TestXmlParamType(t *testing.T) {
}

// when
calculatedTypeRequiredString := XmlParamType("", &paramTypeRequiredString, "")
calculatedTypeListString := XmlParamType("", &paramTypeListString, "")
calculatedTypeRequiredString := XmlParamType("", &paramTypeRequiredString)
calculatedTypeListString := XmlParamType("", &paramTypeListString)

// then
assert.Equal(t, "string", calculatedTypeRequiredString)
Expand Down
256 changes: 128 additions & 128 deletions templates/sdk/config.tmpl
Original file line number Diff line number Diff line change
@@ -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}}
Loading

0 comments on commit 2b5ff1a

Please sign in to comment.