diff --git a/pkg/translate/structs.go b/pkg/translate/structs.go index f0774f4f..76ad7cc7 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) string { +func SpecParamType(parent string, param *properties.SpecParam, version string) string { prefix := determinePrefix(param, false) calculatedType := "" @@ -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 := "" diff --git a/pkg/translate/structs_test.go b/pkg/translate/structs_test.go index 415be26d..78a51945 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 32840482..bf4a6c31 100644 --- a/templates/sdk/config.tmpl +++ b/templates/sdk/config.tmpl @@ -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 { @@ -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}} @@ -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{ diff --git a/templates/sdk/entry.tmpl b/templates/sdk/entry.tmpl index 67e414e0..00c20b1f 100644 --- a/templates/sdk/entry.tmpl +++ b/templates/sdk/entry.tmpl @@ -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 {