Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianczech committed Mar 25, 2024
1 parent e6c6700 commit fa24222
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 41 deletions.
17 changes: 10 additions & 7 deletions pkg/properties/normalized.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,7 @@ func supportedVersions(params map[string]*SpecParam, versions []string) []string
for _, param := range params {
for _, profile := range param.Profiles {
if profile.FromVersion != "" {
notExist := true
for _, version := range versions {
if version == profile.FromVersion {
notExist = false
}
}
if notExist {
if notExist := listContains(versions, profile.FromVersion); notExist {
versions = append(versions, profile.FromVersion)
}
}
Expand All @@ -328,3 +322,12 @@ func supportedVersions(params map[string]*SpecParam, versions []string) []string
}
return versions
}

func listContains(versions []string, checkedVersion string) bool {
for _, version := range versions {
if version == checkedVersion {
return false
}
}
return true
}
2 changes: 1 addition & 1 deletion pkg/properties/normalized_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ xpath_suffix:
assert.Len(t, problems, 2, "Not all expected validation checks failed")
}

func TestNamesOfStructsForVersioning(t *testing.T) {
func TestGettingListOfSupportedVersions(t *testing.T) {
// given
yamlParsedData, _ := ParseSpec([]byte(sampleSpec))

Expand Down
18 changes: 6 additions & 12 deletions pkg/translate/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,26 +96,20 @@ func declareRootOfNestedObject(parent []string, builder *strings.Builder, prefix
func assignEmptyStructForNestedObject(parent []string, builder *strings.Builder, 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))
strings.Join(parent, "."), prefix, strings.Join(parent, ""), suffix))

builder.WriteString(fmt.Sprintf("if o.%s.Misc != nil {\n",
strings.Join(parent, ".")))
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, ""),
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, "."),
strings.Join(parent, "."), strings.Join(parent, ""), strings.Join(parent, "."),
))
builder.WriteString("}\n")
}
builder.WriteString("}\n")
}
}

Expand Down
55 changes: 34 additions & 21 deletions pkg/translate/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,37 +138,50 @@ func CreateGoSuffixFromVersion(version string) string {
func ParamSupportedInVersion(param *properties.SpecParam, deviceVersionStr string) bool {
var supported []bool
if deviceVersionStr == "" {
for _, profile := range param.Profiles {
if profile.FromVersion != "" {
supported = append(supported, profile.NotPresent)
} else {
supported = append(supported, true)
}
}
supported = listOfProfileSupportForNotDefinedDeviceVersion(param, supported)
} else {
deviceVersion, err := version.New(deviceVersionStr)
if err != nil {
return false
}

for _, profile := range param.Profiles {
if profile.FromVersion != "" {
paramProfileVersion, err := version.New(profile.FromVersion)
if err != nil {
return false
}

if deviceVersion.Gte(paramProfileVersion) {
supported = append(supported, !profile.NotPresent)
} else {
supported = append(supported, profile.NotPresent)
}
} else {
supported, err = listOfProfileSupportForDefinedDeviceVersion(param, supported, deviceVersion)
if err != nil {
return false
}
}
return allTrue(supported)
}

func listOfProfileSupportForNotDefinedDeviceVersion(param *properties.SpecParam, supported []bool) []bool {
for _, profile := range param.Profiles {
if profile.FromVersion != "" {
supported = append(supported, profile.NotPresent)
} else {
supported = append(supported, true)
}
}
return supported
}

func listOfProfileSupportForDefinedDeviceVersion(param *properties.SpecParam, supported []bool, deviceVersion version.Version) ([]bool, error) {
for _, profile := range param.Profiles {
if profile.FromVersion != "" {
paramProfileVersion, err := version.New(profile.FromVersion)
if err != nil {
return nil, err
}

if deviceVersion.Gte(paramProfileVersion) {
supported = append(supported, !profile.NotPresent)
} else {
supported = append(supported, profile.NotPresent)
}
} else {
supported = append(supported, !profile.NotPresent)
}
}
return allTrue(supported)
return supported, nil
}

func allTrue(values []bool) bool {
Expand Down

0 comments on commit fa24222

Please sign in to comment.