From fa2422258c7bb324760e0e4f67ecac6a285f4a1f Mon Sep 17 00:00:00 2001 From: Sebastian Czech Date: Mon, 25 Mar 2024 20:50:14 +0100 Subject: [PATCH] Refactor code --- pkg/properties/normalized.go | 17 ++++++---- pkg/properties/normalized_test.go | 2 +- pkg/translate/funcs.go | 18 ++++------ pkg/translate/structs.go | 55 +++++++++++++++++++------------ 4 files changed, 51 insertions(+), 41 deletions(-) diff --git a/pkg/properties/normalized.go b/pkg/properties/normalized.go index db18c8e6..f6a0137c 100644 --- a/pkg/properties/normalized.go +++ b/pkg/properties/normalized.go @@ -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) } } @@ -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 +} diff --git a/pkg/properties/normalized_test.go b/pkg/properties/normalized_test.go index f5a1091e..bd0aeab8 100644 --- a/pkg/properties/normalized_test.go +++ b/pkg/properties/normalized_test.go @@ -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)) diff --git a/pkg/translate/funcs.go b/pkg/translate/funcs.go index a651118b..b370f51f 100644 --- a/pkg/translate/funcs.go +++ b/pkg/translate/funcs.go @@ -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") } } diff --git a/pkg/translate/structs.go b/pkg/translate/structs.go index 953cf950..df85ad52 100644 --- a/pkg/translate/structs.go +++ b/pkg/translate/structs.go @@ -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 {