Skip to content

Commit

Permalink
Fix config template after tests for DNS
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianczech committed Mar 26, 2024
1 parent d534d1c commit b0ac86c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
1 change: 1 addition & 0 deletions pkg/generate/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func (c *Creator) parseTemplate(templateName string) (*template.Template, error)
"locationType": translate.LocationType,
"specParamType": translate.SpecParamType,
"xmlParamType": translate.XmlParamType,
"xmlName": translate.XmlName,
"xmlTag": translate.XmlTag,
"specifyEntryAssignment": translate.SpecifyEntryAssignment,
"normalizeAssignment": translate.NormalizeAssignment,
Expand Down
11 changes: 10 additions & 1 deletion pkg/translate/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ func calculateNestedXmlSpecType(parent string, param *properties.SpecParam) stri
return fmt.Sprintf("spec%s%sXml", parent, naming.CamelCase("", param.Name.CamelCase, "", true))
}

// XmlName creates a string with xml name (e.g. `description`).
func XmlName(param *properties.SpecParam) string {
if param.Profiles != nil && len(param.Profiles) > 0 {
return param.Profiles[0].Xpath[len(param.Profiles[0].Xpath)-1]
}

return ""
}

// XmlTag creates a string with xml tag (e.g. `xml:"description,omitempty"`).
func XmlTag(param *properties.SpecParam) string {
if param.Profiles != nil && len(param.Profiles) > 0 {
Expand All @@ -110,7 +119,7 @@ func XmlTag(param *properties.SpecParam) string {
suffix = ",omitempty"
}

return fmt.Sprintf("`xml:\"%s%s\"`", param.Profiles[0].Xpath[len(param.Profiles[0].Xpath)-1], suffix)
return fmt.Sprintf("`xml:\"%s%s\"`", XmlName(param), suffix)
}

return ""
Expand Down
21 changes: 9 additions & 12 deletions specs/device/dns.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,12 @@ spec:
-
xpath: ["servers"]
spec:
properties:
params:
primary:
description: 'Primary DNS server IP address'
profiles:
-
xpath: ["primary"]
secondary:
description: 'Secondary DNS server IP address'
profiles:
-
xpath: ["secondary"]
params:
primary:
description: 'Primary DNS server IP address'
profiles:
- xpath: [ "primary" ]
secondary:
description: 'Secondary DNS server IP address'
profiles:
- xpath: [ "secondary" ]
22 changes: 16 additions & 6 deletions templates/sdk/config.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,22 @@
{{- end}}

func (c configXml) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if c.NtpServers != nil {
start.Name = xml.Name{Local: "ntp-servers"}
if err := e.EncodeElement(c.NtpServers, start); err != nil {
return err
}
}
{{- range $_, $param := $.Spec.Params}}
if c.{{$param.Name.CamelCase}} != nil {
start.Name = xml.Name{Local: "{{xmlName $param}}"}
if err := e.EncodeElement(c.{{$param.Name.CamelCase}}, start); err != nil {
return err
}
}
{{- end}}
{{- range $_, $param := $.Spec.OneOf}}
if c.{{$param.Name.CamelCase}} != nil {
start.Name = xml.Name{Local: "{{xmlName $param}}"}
if err := e.EncodeElement(c.{{$param.Name.CamelCase}}, start); err != nil {
return err
}
}
{{- end}}

for _, v := range c.Misc {
if err := e.Encode(v); err != nil {
Expand Down

0 comments on commit b0ac86c

Please sign in to comment.