Skip to content

Commit

Permalink
set value for nested Misc
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianczech committed Mar 22, 2024
1 parent 0bddff2 commit e5bcd38
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
18 changes: 18 additions & 0 deletions pkg/translate/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@ func assignEmptyStructForNestedObject(parent []string, builder *strings.Builder,
builder.WriteString(fmt.Sprintf("nested%s = &Spec%s%s{}\n",
strings.Join(parent, "."),
strings.Join(parent, ""), suffix))

if suffix == "Xml" {
builder.WriteString(fmt.Sprintf("if o.%s.Misc != nil {\n", strings.Join(parent, ".")))
builder.WriteString(fmt.Sprintf("nested%s.Misc = o.%s.Misc[\"%s\"]\n",
strings.Join(parent, "."),
strings.Join(parent, "."),
strings.Join(parent, ""),
))
builder.WriteString("}\n")
} else {
builder.WriteString(fmt.Sprintf("if o.%s.Misc != nil {\n", strings.Join(parent, ".")))
builder.WriteString(fmt.Sprintf("nested%s.Misc[\"%s\"] = o.%s.Misc\n",
strings.Join(parent, "."),
strings.Join(parent, ""),
strings.Join(parent, "."),
))
builder.WriteString("}\n")
}
}
}

Expand Down
8 changes: 5 additions & 3 deletions templates/sdk/config.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type Spec{{$name}} struct {
{{- range $_, $param := $spec.OneOf}}
{{$param.Name.CamelCase}} {{specParamType $name $param}}
{{- end}}

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

Expand Down Expand Up @@ -57,7 +59,7 @@ type Spec{{$name}}Xml struct {
{{$param.Name.CamelCase}} {{xmlParamType $name $param}} {{xmlTag $param}}
{{- end}}

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

Expand Down Expand Up @@ -106,7 +108,7 @@ func SpecifyConfig(o Config) (any, error) {
{{specifyEntryAssignment "config" $param}}
{{- end}}

config.Misc = o.Misc[fmt.Sprintf("%s", "Config")]
config.Misc = o.Misc["Config"]

return config, nil
}
Expand All @@ -124,7 +126,7 @@ func (c *ConfigXmlContainer) Normalize() ([]Config, error) {
{{normalizeAssignment "config" $param}}
{{- end}}

config.Misc[fmt.Sprintf("%s", "Config")] = o.Misc
config.Misc["Config"] = o.Misc

configList = append(configList, config)
}
Expand Down
12 changes: 5 additions & 7 deletions templates/sdk/entry.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type Spec{{$name}} struct {
{{- range $_, $param := $spec.OneOf}}
{{$param.Name.CamelCase}} {{specParamType $name $param}}
{{- end}}

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

Expand Down Expand Up @@ -118,7 +120,7 @@ func Versioning(vn version.Number) (Specifier, Normalizer, error) {
// TODO: think if struct EntryXml1 / E_0_0 has to be public or not ???
// maybe it's good to make it hidden and do not have in public documentation ...

func SpecifyEntry(o Entry) (any,
func SpecifyEntry(o Entry) (any, error) {
entry := EntryXml{}

entry.Name = o.Name
Expand All @@ -129,9 +131,7 @@ func SpecifyEntry(o Entry) (any,
{{specifyEntryAssignment "entry" $param}}
{{- end}}

entry.Misc = o.Misc[fmt.Sprintf("%s\n%s", "Entry", o.Name)]

// TODO: we use name of the struct for single objects, but if we have a list, we combine name of the struct + index
entry.Misc = o.Misc["Entry"]

return entry, nil
}
Expand All @@ -150,9 +150,7 @@ func (c *EntryXmlContainer) Normalize() ([]Entry, error) {
{{normalizeAssignment "entry" $param}}
{{- end}}

entry.Misc[fmt.Sprintf("%s\n%s", "Entry", o.Name)] = o.Misc

// TODO: similary as for SpecifyEntry(), it has to be name of the struct e.g. EntryXml for address
entry.Misc["Entry"] = o.Misc

// TODO: start thinking about versioning e.g:
// 1 approach: EntryXml1, EntryXml2, EntryXml3, ...
Expand Down

0 comments on commit e5bcd38

Please sign in to comment.