Skip to content

Commit

Permalink
refactor structs.go
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianczech committed Mar 15, 2024
1 parent 4df52fa commit 0ff2d59
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
49 changes: 31 additions & 18 deletions pkg/translate/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,13 @@ 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 {
prefix := ""
if !param.Required {
prefix = "*"
}
if param.Type == "list" {
prefix = "[]"
}
prefix := determinePrefix(param)

calculatedType := ""
if param.Type == "list" && param.Items != nil {
if param.Items.Type == "object" && param.Items.Ref != nil {
calculatedType = "string"
} else {
calculatedType = param.Items.Type
}
calculatedType = determineListType(param)
} else if param.Spec != nil {
calculatedType = fmt.Sprintf("Spec%s%s", parent, naming.CamelCase("", param.Name.CamelCase, "", true))
calculatedType = calculateNestedSpecType(parent, param)
} else {
calculatedType = param.Type
}
Expand All @@ -73,23 +63,46 @@ 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 {
prefix := ""
if !param.Required {
prefix = "*"
}
prefix := determinePrefix(param)

calculatedType := ""
if isParamListAndProfileTypeIsMember(param) {
calculatedType = "util.MemberType"
} else if param.Spec != nil {
calculatedType = fmt.Sprintf("Spec%s%sXml", parent, naming.CamelCase("", param.Name.CamelCase, "", true))
calculatedType = calculateNestedXmlSpecType(parent, param)
} else {
calculatedType = param.Type
}

return fmt.Sprintf("%s%s", prefix, calculatedType)
}

func determinePrefix(param *properties.SpecParam) string {
prefix := ""
if param.Type == "list" {
prefix = prefix + "[]"
}
if !param.Required {
prefix = prefix + "*"
}
return prefix
}

func determineListType(param *properties.SpecParam) string {
if param.Items.Type == "object" && param.Items.Ref != nil {
return "string"
}
return param.Items.Type
}

func calculateNestedSpecType(parent string, param *properties.SpecParam) string {
return fmt.Sprintf("Spec%s%s", parent, naming.CamelCase("", param.Name.CamelCase, "", true))
}

func calculateNestedXmlSpecType(parent string, param *properties.SpecParam) string {
return fmt.Sprintf("Spec%s%sXml", parent, naming.CamelCase("", param.Name.CamelCase, "", true))
}

// 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 Down
4 changes: 2 additions & 2 deletions pkg/translate/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestSpecParamType(t *testing.T) {

// then
assert.Equal(t, "string", calculatedTypeRequiredString)
assert.Equal(t, "[]string", calculatedTypeListString)
assert.Equal(t, "[]*string", calculatedTypeListString)
assert.Equal(t, "*string", calculatedTypeOptionalString)
}

Expand Down Expand Up @@ -144,7 +144,7 @@ func TestXmlParamType(t *testing.T) {

// then
assert.Equal(t, "string", calculatedTypeRequiredString)
assert.Equal(t, "*util.MemberType", calculatedTypeListString)
assert.Equal(t, "[]*util.MemberType", calculatedTypeListString)
}

func TestXmlTag(t *testing.T) {
Expand Down

0 comments on commit 0ff2d59

Please sign in to comment.