Skip to content

Commit

Permalink
generate content of function Normalize for specification with nested …
Browse files Browse the repository at this point in the history
…specs
  • Loading branch information
sebastianczech committed Mar 12, 2024
1 parent 07ccc14 commit 60af14e
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 14 deletions.
1 change: 1 addition & 0 deletions pkg/generate/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func (c *Creator) parseTemplate(templateName string) (*template.Template, error)
"xmlParamType": translate.XmlParamType,
"xmlTag": translate.XmlTag,
"specifyEntryAssignment": translate.SpecifyEntryAssignment,
"normalizeAssignment": translate.NormalizeAssignment,
"specMatchesFunction": translate.SpecMatchesFunction,
"omitEmpty": translate.OmitEmpty,
"contains": func(full, part string) bool {
Expand Down
36 changes: 30 additions & 6 deletions pkg/translate/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ func SpecifyEntryAssignment(param *properties.SpecParam) string {
}
builder.WriteString("entry." + param.Name.CamelCase + " = &Spec" + param.Name.CamelCase + "Xml{\n")
for _, subParam := range param.Spec.Params {
builder.WriteString(nestedObjectAssignment([]string{param.Name.CamelCase}, subParam))
builder.WriteString(nestedObjectAssignment([]string{param.Name.CamelCase}, "Xml", subParam))
}
for _, subParam := range param.Spec.OneOf {
builder.WriteString(nestedObjectAssignment([]string{param.Name.CamelCase}, subParam))
builder.WriteString(nestedObjectAssignment([]string{param.Name.CamelCase}, "Xml", subParam))
}
builder.WriteString("}\n")
} else {
Expand Down Expand Up @@ -73,7 +73,7 @@ func declareVariableForNestedObject(parent []string, param *properties.SpecParam
}
}

func nestedObjectAssignment(parent []string, param *properties.SpecParam) string {
func nestedObjectAssignment(parent []string, suffix string, param *properties.SpecParam) string {
var builder strings.Builder

if param.Type == "list" && param.Profiles != nil && len(param.Profiles) > 0 && param.Profiles[0].Type == "member" {
Expand All @@ -85,12 +85,12 @@ func nestedObjectAssignment(parent []string, param *properties.SpecParam) string
builder.WriteString(param.Name.CamelCase +
" : &Spec" +
param.Name.CamelCase +
"Xml{\n")
suffix + "{\n")
for _, subParam := range param.Spec.Params {
builder.WriteString(nestedObjectAssignment(append(parent, param.Name.CamelCase), subParam))
builder.WriteString(nestedObjectAssignment(append(parent, param.Name.CamelCase), suffix, subParam))
}
for _, subParam := range param.Spec.OneOf {
builder.WriteString(nestedObjectAssignment(append(parent, param.Name.CamelCase), subParam))
builder.WriteString(nestedObjectAssignment(append(parent, param.Name.CamelCase), suffix, subParam))
}
builder.WriteString("},\n")
} else {
Expand All @@ -103,6 +103,30 @@ func nestedObjectAssignment(parent []string, param *properties.SpecParam) string
return builder.String()
}

func NormalizeAssignment(param *properties.SpecParam) string {
var builder strings.Builder

if param.Type == "list" && param.Profiles != nil && len(param.Profiles) > 0 && param.Profiles[0].Type == "member" {
builder.WriteString("entry." + param.Name.CamelCase + " = entryXml." + param.Name.CamelCase)
} else if param.Spec != nil {
for _, subParam := range param.Spec.Params {
builder.WriteString(nestedObjectDeclaration([]string{param.Name.CamelCase}, subParam))
}
builder.WriteString("entry." + param.Name.CamelCase + " = &Spec" + param.Name.CamelCase + "{\n")
for _, subParam := range param.Spec.Params {
builder.WriteString(nestedObjectAssignment([]string{param.Name.CamelCase}, "", subParam))
}
for _, subParam := range param.Spec.OneOf {
builder.WriteString(nestedObjectAssignment([]string{param.Name.CamelCase}, "", subParam))
}
builder.WriteString("}\n")
} else {
builder.WriteString("entry." + param.Name.CamelCase + " = entryXml." + param.Name.CamelCase)
}

return builder.String()
}

func SpecMatchesFunction(param *properties.SpecParam) string {
calculatedFunction := "OptionalStringsMatch"
if param.Type == "list" {
Expand Down
46 changes: 46 additions & 0 deletions pkg/translate/funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,52 @@ C : nestedABC,
assert.Equal(t, expectedAssignmentStreing, calculatedAssignmentString)
}

func TestNormalizeAssignmentForNestedObject(t *testing.T) {
// given
spec := properties.Spec{
Params: map[string]*properties.SpecParam{
"a": {
Name: &properties.NameVariant{
Underscore: "a",
CamelCase: "A",
},
Spec: &properties.Spec{
Params: map[string]*properties.SpecParam{
"b": {
Name: &properties.NameVariant{
Underscore: "b",
CamelCase: "B",
},
Spec: &properties.Spec{
Params: map[string]*properties.SpecParam{
"c": {
Name: &properties.NameVariant{
Underscore: "c",
CamelCase: "C",
},
},
},
},
},
},
},
},
},
}
expectedAssignmentStreing := `nestedABC := o.A.B.C
entry.A = &SpecA{
B : &SpecB{
C : nestedABC,
},
}
`
// when
calculatedAssignmentString := NormalizeAssignment(spec.Params["a"])

// then
assert.Equal(t, expectedAssignmentStreing, calculatedAssignmentString)
}

func TestSpecMatchesFunction(t *testing.T) {
// given
paramTypeString := properties.SpecParam{
Expand Down
6 changes: 3 additions & 3 deletions pkg/translate/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,21 +188,21 @@ func TestNestedSpecs(t *testing.T) {
"a": {
Name: &properties.NameVariant{
Underscore: "a",
CamelCase: "a",
CamelCase: "A",
},
Spec: &properties.Spec{
Params: map[string]*properties.SpecParam{
"b": {
Name: &properties.NameVariant{
Underscore: "b",
CamelCase: "b",
CamelCase: "B",
},
Spec: &properties.Spec{
Params: map[string]*properties.SpecParam{
"c": {
Name: &properties.NameVariant{
Underscore: "c",
CamelCase: "c",
CamelCase: "C",
},
},
},
Expand Down
10 changes: 5 additions & 5 deletions templates/sdk/entry.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,19 @@ func SpecifyEntry(o Entry) (any, error) {

func (c *EntryXmlContainer) Normalize() ([]Entry, error) {
entryList := make([]Entry, 0, len(c.Answer))
for _, entryXml := range c.Answer {
for _, o := range c.Answer {
entry := Entry{
Misc: make(map[string][]generic.Xml),
}
entry.Name = entryXml.Name
entry.Name = o.Name
{{- range $_, $param := .Spec.Params}}
entry.{{$param.Name.CamelCase}} = entryXml.{{$param.Name.CamelCase}}
{{normalizeAssignment $param}}
{{- end}}
{{- range $_, $param := .Spec.OneOf}}
entry.{{$param.Name.CamelCase}} = entryXml.{{$param.Name.CamelCase}}
{{normalizeAssignment $param}}
{{- end}}

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

entryList = append(entryList, entry)
}
Expand Down

0 comments on commit 60af14e

Please sign in to comment.