Skip to content

Commit

Permalink
Fix versioning after more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianczech committed Mar 27, 2024
1 parent a361d44 commit accd7db
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
38 changes: 22 additions & 16 deletions pkg/translate/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ func prepareAssignment(objectType string, param *properties.SpecParam, listFunct

if ParamSupportedInVersion(param, version) {
if param.Spec != nil {
appendSpecObjectAssignment(param, objectType, specPrefix, specSuffix, &builder)
if specSuffix == "Xml" {
appendSpecObjectAssignment(param, objectType, version, specPrefix, specSuffix, &builder)
} else {
appendSpecObjectAssignment(param, objectType, "", specPrefix, specSuffix, &builder)
}
} else if isParamListAndProfileTypeIsMember(param) {
appendListFunctionAssignment(param, objectType, listFunction, &builder)
} else {
Expand All @@ -66,37 +70,39 @@ func appendListFunctionAssignment(param *properties.SpecParam, objectType string
builder.WriteString(fmt.Sprintf("%s.%s = %s(o.%s)", objectType, param.Name.CamelCase, listFunction, param.Name.CamelCase))
}

func appendSpecObjectAssignment(param *properties.SpecParam, objectType string, prefix, suffix string, builder *strings.Builder) {
defineNestedObject([]string{param.Name.CamelCase}, param, objectType, prefix, suffix, builder)
func appendSpecObjectAssignment(param *properties.SpecParam, objectType string, version, prefix, suffix string, builder *strings.Builder) {
defineNestedObject([]string{param.Name.CamelCase}, param, objectType, version, prefix, suffix, builder)
builder.WriteString(fmt.Sprintf("%s.%s = nested%s\n", objectType, param.Name.CamelCase, param.Name.CamelCase))
}

func defineNestedObject(parent []string, param *properties.SpecParam, objectType string, prefix, suffix string, builder *strings.Builder) {
declareRootOfNestedObject(parent, builder, prefix, suffix)
func defineNestedObject(parent []string, param *properties.SpecParam, objectType string, version, prefix, suffix string, builder *strings.Builder) {
declareRootOfNestedObject(parent, builder, version, prefix, suffix)

builder.WriteString(fmt.Sprintf("if o.%s != nil {\n", strings.Join(parent, ".")))
if param.Spec != nil {
assignEmptyStructForNestedObject(parent, builder, prefix, suffix)
defineNestedObjectForChildParams(parent, param.Spec.Params, objectType, prefix, suffix, builder)
defineNestedObjectForChildParams(parent, param.Spec.OneOf, objectType, prefix, suffix, builder)
assignEmptyStructForNestedObject(parent, builder, version, prefix, suffix)
defineNestedObjectForChildParams(parent, param.Spec.Params, objectType, version, prefix, suffix, builder)
defineNestedObjectForChildParams(parent, param.Spec.OneOf, objectType, version, prefix, suffix, builder)
} else {
assignValueForNestedObject(parent, builder)
}
builder.WriteString("}\n")
}

func declareRootOfNestedObject(parent []string, builder *strings.Builder, prefix, suffix string) {
func declareRootOfNestedObject(parent []string, builder *strings.Builder, version, prefix, suffix string) {
if len(parent) == 1 {
builder.WriteString(fmt.Sprintf("nested%s := &%s%s%s{}\n",
builder.WriteString(fmt.Sprintf("nested%s := &%s%s%s%s{}\n",
strings.Join(parent, "."), prefix,
strings.Join(parent, ""), suffix))
strings.Join(parent, ""), suffix,
CreateGoSuffixFromVersion(version)))
}
}

func assignEmptyStructForNestedObject(parent []string, builder *strings.Builder, prefix, suffix string) {
func assignEmptyStructForNestedObject(parent []string, builder *strings.Builder, version, prefix, suffix string) {
if len(parent) > 1 {
builder.WriteString(fmt.Sprintf("nested%s = &%s%s%s{}\n",
strings.Join(parent, "."), prefix, strings.Join(parent, ""), suffix))
builder.WriteString(fmt.Sprintf("nested%s = &%s%s%s%s{}\n",
strings.Join(parent, "."), prefix, strings.Join(parent, ""), suffix,
CreateGoSuffixFromVersion(version)))

builder.WriteString(fmt.Sprintf("if o.%s.Misc != nil {\n",
strings.Join(parent, ".")))
Expand All @@ -119,9 +125,9 @@ func assignValueForNestedObject(parent []string, builder *strings.Builder) {
strings.Join(parent, ".")))
}

func defineNestedObjectForChildParams(parent []string, params map[string]*properties.SpecParam, objectType string, prefix, suffix string, builder *strings.Builder) {
func defineNestedObjectForChildParams(parent []string, params map[string]*properties.SpecParam, objectType string, version, prefix, suffix string, builder *strings.Builder) {
for _, param := range params {
defineNestedObject(append(parent, param.Name.CamelCase), param, objectType, prefix, suffix, builder)
defineNestedObject(append(parent, param.Name.CamelCase), param, objectType, version, prefix, suffix, builder)
}
}

Expand Down
24 changes: 20 additions & 4 deletions templates/sdk/entry.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,20 @@
Name string `xml:"name,attr"`
{{- range $_, $param := $.Spec.Params}}
{{- if paramSupportedInVersion $param $version}}
{{$param.Name.CamelCase}} {{xmlParamType "" $param}} {{xmlTag $param}}
{{- if $param.Spec}}
{{$param.Name.CamelCase}} {{xmlParamType "" $param}}{{createGoSuffixFromVersion $version}} {{xmlTag $param}}
{{- else}}
{{$param.Name.CamelCase}} {{xmlParamType "" $param}} {{xmlTag $param}}
{{- end}}
{{- end}}
{{- end}}
{{- range $_, $param := $.Spec.OneOf}}
{{- if paramSupportedInVersion $param $version}}
{{$param.Name.CamelCase}} {{xmlParamType "" $param}} {{xmlTag $param}}
{{- if $param.Spec}}
{{$param.Name.CamelCase}} {{xmlParamType "" $param}}{{createGoSuffixFromVersion $version}} {{xmlTag $param}}
{{- else}}
{{$param.Name.CamelCase}} {{xmlParamType "" $param}} {{xmlTag $param}}
{{- end}}
{{- end}}
{{- end}}

Expand All @@ -78,12 +86,20 @@
type spec{{$name}}Xml{{createGoSuffixFromVersion $version}} struct {
{{- range $_, $param := $spec.Params}}
{{- if paramSupportedInVersion $param $version}}
{{$param.Name.CamelCase}} {{xmlParamType $name $param}} {{xmlTag $param}}
{{- if $param.Spec}}
{{$param.Name.CamelCase}} {{xmlParamType $name $param}}{{createGoSuffixFromVersion $version}} {{xmlTag $param}}
{{- else}}
{{$param.Name.CamelCase}} {{xmlParamType $name $param}} {{xmlTag $param}}
{{- end}}
{{- end}}
{{- end}}
{{- range $_, $param := $spec.OneOf}}
{{- if paramSupportedInVersion $param $version}}
{{$param.Name.CamelCase}} {{xmlParamType $name $param}} {{xmlTag $param}}
{{- if $param.Spec}}
{{$param.Name.CamelCase}} {{xmlParamType $name $param}}{{createGoSuffixFromVersion $version}} {{xmlTag $param}}
{{- else}}
{{$param.Name.CamelCase}} {{xmlParamType $name $param}} {{xmlTag $param}}
{{- end}}
{{- end}}
{{- end}}

Expand Down

0 comments on commit accd7db

Please sign in to comment.