Skip to content

Commit

Permalink
render 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 73717c2 commit 36db8b4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 49 deletions.
4 changes: 2 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) string {
func SpecParamType(parent string, param *properties.SpecParam, version string) string {
prefix := determinePrefix(param, false)

calculatedType := ""
Expand All @@ -62,7 +62,7 @@ func SpecParamType(parent string, param *properties.SpecParam) string {
}

// XmlParamType return param type (it can be nested spec) (for struct based on spec from YAML files).
func XmlParamType(parent string, param *properties.SpecParam) string {
func XmlParamType(parent string, param *properties.SpecParam, version string) string {
prefix := determinePrefix(param, true)

calculatedType := ""
Expand Down
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
52 changes: 31 additions & 21 deletions templates/sdk/config.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,57 +11,67 @@ import (
"github.com/PaloAltoNetworks/pango/version"
)

{{- range $version := .SupportedVersions }}
type Config struct {
{{- range $_, $param := .Spec.Params}}
{{$param.Name.CamelCase}} {{specParamType "" $param}}
{{- range $_, $param := $.Spec.Params}}
{{$param.Name.CamelCase}} {{specParamType "" $param $version}}
{{- end}}
{{- range $_, $param := .Spec.OneOf}}
{{$param.Name.CamelCase}} {{specParamType "" $param}}
{{- range $_, $param := $.Spec.OneOf}}
{{$param.Name.CamelCase}} {{specParamType "" $param $version}}
{{- end}}

Misc map[string][]generic.Xml
}
{{- end}}

{{- range $name, $spec := nestedSpecs .Spec }}
{{- range $version := .SupportedVersions }}
{{- range $name, $spec := nestedSpecs $.Spec }}
type Spec{{$name}} struct {
{{- range $_, $param := $spec.Params}}
{{$param.Name.CamelCase}} {{specParamType $name $param}}
{{$param.Name.CamelCase}} {{specParamType $name $param $version}}
{{- end}}
{{- range $_, $param := $spec.OneOf}}
{{$param.Name.CamelCase}} {{specParamType $name $param}}
{{$param.Name.CamelCase}} {{specParamType $name $param $version}}
{{- end}}

Misc map[string][]generic.Xml
}
{{- end}}
{{- end}}

type ConfigXmlContainer struct {
Answer []ConfigXml `xml:"config"`
{{- range $version := .SupportedVersions }}
type configXmlContainer{{$version}} struct {
Answer []configXml{{$version}} `xml:"config"`
}
{{- end}}

type ConfigXml struct {
{{- range $_, $param := .Spec.Params}}
{{$param.Name.CamelCase}} {{xmlParamType "" $param}} {{xmlTag $param}}
{{- 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}} {{xmlTag $param}}
{{- range $_, $param := $.Spec.OneOf}}
{{$param.Name.CamelCase}} {{xmlParamType "" $param $version}} {{xmlTag $param}}
{{- end}}

Misc []generic.Xml `xml:",any"`
}
{{- end}}

{{- range $name, $spec := nestedSpecs .Spec }}
type spec{{$name}}Xml struct {
{{- range $version := .SupportedVersions }}
{{- range $name, $spec := nestedSpecs $.Spec }}
type spec{{$name}}Xml{{$version}} struct {
{{- range $_, $param := $spec.Params}}
{{$param.Name.CamelCase}} {{xmlParamType $name $param}} {{xmlTag $param}}
{{$param.Name.CamelCase}} {{xmlParamType $name $param $version}} {{xmlTag $param}}
{{- end}}
{{- range $_, $param := $spec.OneOf}}
{{$param.Name.CamelCase}} {{xmlParamType $name $param}} {{xmlTag $param}}
{{$param.Name.CamelCase}} {{xmlParamType $name $param $version}} {{xmlTag $param}}
{{- end}}

Misc []generic.Xml `xml:",any"`
}
{{- end}}
{{- end}}

func (e *Config) CopyMiscFrom(v *Config) {
if v == nil || len(v.Misc) == 0 {
Expand Down Expand Up @@ -95,11 +105,11 @@ func (e *Config) Field(v string) (any, error) {
}

func Versioning(vn version.Number) (Specifier, Normalizer, error) {
return SpecifyConfig, &ConfigXmlContainer{}, nil
return SpecifyConfig, &configXmlContainer{}, nil
}

func SpecifyConfig(o Config) (any, error) {
config := ConfigXml{}
config := configXml{}

{{- range $_, $param := .Spec.Params}}
{{specifyEntryAssignment "config" $param}}
Expand All @@ -113,7 +123,7 @@ func SpecifyConfig(o Config) (any, error) {
return config, nil
}

func (c *ConfigXmlContainer) Normalize() ([]Config, error) {
func (c *configXmlContainer) Normalize() ([]Config, error) {
configList := make([]Config, 0, len(c.Answer))
for _, o := range c.Answer {
config := Config{
Expand Down
52 changes: 31 additions & 21 deletions templates/sdk/entry.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,60 +23,70 @@ var (
{{- end}}}
)

type Entry struct {
{{- range $version := .SupportedVersions }}
type Entry{{$version}} struct {
Name string
{{- range $_, $param := .Spec.Params}}
{{$param.Name.CamelCase}} {{specParamType "" $param}}
{{- range $_, $param := $.Spec.Params}}
{{$param.Name.CamelCase}} {{specParamType "" $param $version}}
{{- end}}
{{- range $_, $param := .Spec.OneOf}}
{{$param.Name.CamelCase}} {{specParamType "" $param}}
{{- range $_, $param := $.Spec.OneOf}}
{{$param.Name.CamelCase}} {{specParamType "" $param $version}}
{{- end}}

Misc map[string][]generic.Xml
Misc map[string][]generic.Xml
}
{{- end}}

{{- range $name, $spec := nestedSpecs .Spec }}
type Spec{{$name}} struct {
{{- range $version := .SupportedVersions }}
{{- range $name, $spec := nestedSpecs $.Spec }}
type Spec{{$name}}{{$version}} struct {
{{- range $_, $param := $spec.Params}}
{{$param.Name.CamelCase}} {{specParamType $name $param}}
{{$param.Name.CamelCase}} {{specParamType $name $param $version}}
{{- end}}
{{- range $_, $param := $spec.OneOf}}
{{$param.Name.CamelCase}} {{specParamType $name $param}}
{{$param.Name.CamelCase}} {{specParamType $name $param $version}}
{{- end}}

Misc map[string][]generic.Xml
}
{{- end}}
{{- end}}

type entryXmlContainer struct {
Answer []entryXml `xml:"entry"`
{{- range $version := .SupportedVersions }}
type entryXmlContainer{{$version}} struct {
Answer []entryXml{{$version}} `xml:"entry"`
}
{{- end}}

type entryXml struct {
{{- 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}} {{xmlTag $param}}
{{- range $_, $param := $.Spec.Params}}
{{$param.Name.CamelCase}} {{xmlParamType "" $param $version}} {{xmlTag $param}}
{{- end}}
{{- range $_, $param := .Spec.OneOf}}
{{$param.Name.CamelCase}} {{xmlParamType "" $param}} {{xmlTag $param}}
{{- range $_, $param := $.Spec.OneOf}}
{{$param.Name.CamelCase}} {{xmlParamType "" $param $version}} {{xmlTag $param}}
{{- end}}

Misc []generic.Xml `xml:",any"`
}
{{- end}}

{{- range $name, $spec := nestedSpecs .Spec }}
type spec{{$name}}Xml struct {
{{- range $version := .SupportedVersions }}
{{- range $name, $spec := nestedSpecs $.Spec }}
type spec{{$name}}Xml{{$version}} struct {
{{- range $_, $param := $spec.Params}}
{{$param.Name.CamelCase}} {{xmlParamType $name $param}} {{xmlTag $param}}
{{$param.Name.CamelCase}} {{xmlParamType $name $param $version}} {{xmlTag $param}}
{{- end}}
{{- range $_, $param := $spec.OneOf}}
{{$param.Name.CamelCase}} {{xmlParamType $name $param}} {{xmlTag $param}}
{{$param.Name.CamelCase}} {{xmlParamType $name $param $version}} {{xmlTag $param}}
{{- end}}

Misc []generic.Xml `xml:",any"`
}
{{- end}}
{{- end}}

func (e *Entry) CopyMiscFrom(v *Entry) {
if v == nil || len(v.Misc) == 0 {
Expand Down

0 comments on commit 36db8b4

Please sign in to comment.