Skip to content

Commit

Permalink
Refactor code to render SpecMatches()
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianczech committed Mar 27, 2024
1 parent 3dac047 commit a361d44
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 31 deletions.
62 changes: 31 additions & 31 deletions pkg/translate/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func specMatchFunctionName(parent []string, param *properties.SpecParam) string
}
}

// NestedSpecMatchesFunction ..........
// NestedSpecMatchesFunction return a string with body of specMach* functions required for nested params
func NestedSpecMatchesFunction(spec *properties.Spec) string {
var builder strings.Builder

Expand All @@ -157,50 +157,50 @@ func defineSpecMatchesFunction(parent []string, params map[string]*properties.Sp
defineSpecMatchesFunction(append(parent, param.Name.CamelCase), param.Spec.Params, builder)
defineSpecMatchesFunction(append(parent, param.Name.CamelCase), param.Spec.OneOf, builder)

builder.WriteString(fmt.Sprintf("func specMatch%s%s(a *%s, b *%s) bool {",
strings.Join(parent, ""), param.Name.CamelCase,
argumentTypeForSpecMatchesFunction(parent, param),
argumentTypeForSpecMatchesFunction(parent, param)))
renderSpecMatchesFunctionNameWithArguments(parent, builder, param)
checkInSpecMatchesFunctionIfVariablesAreNil(builder)

builder.WriteString("if a == nil && b != nil || a != nil && b == nil {\n")
builder.WriteString(" return false\n")
builder.WriteString("} else if a == nil && b == nil {\n")
builder.WriteString(" return true\n")
builder.WriteString("}\n")

for _, subparam := range param.Spec.Params {
builder.WriteString(fmt.Sprintf("if !%s(a.%s, b.%s) {\n",
specMatchFunctionName(append(parent, param.Name.CamelCase), subparam), subparam.Name.CamelCase, subparam.Name.CamelCase))
builder.WriteString(" return false\n")
builder.WriteString("}\n")
for _, subParam := range param.Spec.Params {
renderInSpecMatchesFunctionIfToCheckIfVariablesMatches(parent, builder, param, subParam)
}
for _, subparam := range param.Spec.OneOf {
builder.WriteString(fmt.Sprintf("if !%s(a.%s, b.%s) {\n",
specMatchFunctionName(append(parent, param.Name.CamelCase), subparam), subparam.Name.CamelCase, subparam.Name.CamelCase))
builder.WriteString(" return false\n")
builder.WriteString("}\n")
for _, subParam := range param.Spec.OneOf {
renderInSpecMatchesFunctionIfToCheckIfVariablesMatches(parent, builder, param, subParam)
}

builder.WriteString("return true\n")
builder.WriteString("}\n")
} else if param.Type != "list" && param.Type != "string" {
builder.WriteString(fmt.Sprintf("func specMatch%s%s(a *%s, b *%s) bool {",
strings.Join(parent, ""), param.Name.CamelCase,
argumentTypeForSpecMatchesFunction(parent, param),
argumentTypeForSpecMatchesFunction(parent, param)))

builder.WriteString("if a == nil && b != nil || a != nil && b == nil {\n")
builder.WriteString(" return false\n")
builder.WriteString("} else if a == nil && b == nil {\n")
builder.WriteString(" return true\n")
builder.WriteString("}\n")
renderSpecMatchesFunctionNameWithArguments(parent, builder, param)
checkInSpecMatchesFunctionIfVariablesAreNil(builder)

builder.WriteString("return *a == *b\n")
builder.WriteString("}\n")
}
}
}

func renderSpecMatchesFunctionNameWithArguments(parent []string, builder *strings.Builder, param *properties.SpecParam) {
builder.WriteString(fmt.Sprintf("func specMatch%s%s(a *%s, b *%s) bool {",
strings.Join(parent, ""), param.Name.CamelCase,
argumentTypeForSpecMatchesFunction(parent, param),
argumentTypeForSpecMatchesFunction(parent, param)))
}

func checkInSpecMatchesFunctionIfVariablesAreNil(builder *strings.Builder) {
builder.WriteString("if a == nil && b != nil || a != nil && b == nil {\n")
builder.WriteString(" return false\n")
builder.WriteString("} else if a == nil && b == nil {\n")
builder.WriteString(" return true\n")
builder.WriteString("}\n")
}

func renderInSpecMatchesFunctionIfToCheckIfVariablesMatches(parent []string, builder *strings.Builder, param *properties.SpecParam, subparam *properties.SpecParam) {
builder.WriteString(fmt.Sprintf("if !%s(a.%s, b.%s) {\n",
specMatchFunctionName(append(parent, param.Name.CamelCase), subparam), subparam.Name.CamelCase, subparam.Name.CamelCase))
builder.WriteString(" return false\n")
builder.WriteString("}\n")
}

func argumentTypeForSpecMatchesFunction(parent []string, param *properties.SpecParam) string {
if param.Type == "bool" {
return "bool"
Expand Down
68 changes: 68 additions & 0 deletions pkg/translate/funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,71 @@ func TestSpecMatchesFunction(t *testing.T) {
assert.Equal(t, "util.OptionalStringsMatch", calculatedSpecMatchFunctionString)
assert.Equal(t, "util.OrderedListsMatch", calculatedSpecMatchFunctionListString)
}

func TestNestedSpecMatchesFunction(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",
},
},
},
},
},
},
},
},
},
}
expectedNestedSpec := `func specMatchABC(a *SpecABC, b *SpecABC) bool {if a == nil && b != nil || a != nil && b == nil {
return false
} else if a == nil && b == nil {
return true
}
return *a == *b
}
func specMatchAB(a *SpecAB, b *SpecAB) bool {if a == nil && b != nil || a != nil && b == nil {
return false
} else if a == nil && b == nil {
return true
}
if !specMatchABC(a.C, b.C) {
return false
}
return true
}
func specMatchA(a *SpecA, b *SpecA) bool {if a == nil && b != nil || a != nil && b == nil {
return false
} else if a == nil && b == nil {
return true
}
if !specMatchAB(a.B, b.B) {
return false
}
return true
}
`

// when
renderedNestedSpecMatches := NestedSpecMatchesFunction(&spec)

// then
assert.Equal(t, expectedNestedSpec, renderedNestedSpecMatches)
}

0 comments on commit a361d44

Please sign in to comment.