From 68f35570b6efdfaf9b04d27088c9ff1853473619 Mon Sep 17 00:00:00 2001 From: Sebastian Czech Date: Mon, 18 Mar 2024 14:12:53 +0100 Subject: [PATCH] remove nested variables declaration, use the directly in assignments --- pkg/translate/funcs.go | 26 ++++---------------------- pkg/translate/funcs_test.go | 10 ++++------ 2 files changed, 8 insertions(+), 28 deletions(-) diff --git a/pkg/translate/funcs.go b/pkg/translate/funcs.go index be139fbc..9cd5bcf8 100644 --- a/pkg/translate/funcs.go +++ b/pkg/translate/funcs.go @@ -69,9 +69,6 @@ func appendListFunctionAssignment(param *properties.SpecParam, objectType string } func appendSpecObjectAssignment(param *properties.SpecParam, objectType string, suffix string, builder *strings.Builder) { - appendNestedObjectDeclaration([]string{param.Name.CamelCase}, param.Spec.Params, builder) - appendNestedObjectDeclaration([]string{param.Name.CamelCase}, param.Spec.OneOf, builder) - builder.WriteString(fmt.Sprintf("%s.%s = &Spec%s%s{\n", objectType, param.Name.CamelCase, param.Name.CamelCase, suffix)) appendNestedObjectAssignment([]string{param.Name.CamelCase}, param.Spec.Params, suffix, builder) @@ -80,24 +77,9 @@ func appendSpecObjectAssignment(param *properties.SpecParam, objectType string, builder.WriteString("}\n") } -func appendNestedObjectDeclaration(parent []string, params map[string]*properties.SpecParam, builder *strings.Builder) { - for _, subParam := range params { - appendDeclarationForNestedObject(parent, subParam, builder) - } -} - -func appendDeclarationForNestedObject(parent []string, param *properties.SpecParam, builder *strings.Builder) { - if param.Spec != nil { - appendNestedObjectDeclaration(append(parent, param.Name.CamelCase), param.Spec.Params, builder) - appendNestedObjectDeclaration(append(parent, param.Name.CamelCase), param.Spec.OneOf, builder) - } else { - builder.WriteString(fmt.Sprintf("nested%s%s := o.%s.%s\n", - strings.Join(parent, ""), param.Name.CamelCase, - strings.Join(parent, "."), param.Name.CamelCase)) - } -} - func appendNestedObjectAssignment(parent []string, params map[string]*properties.SpecParam, suffix string, builder *strings.Builder) { + // TODO: for one_of e.g. UDP or TCP, we need to generate additional if, because we cannot specify both TCP and UDP + // TODO: above applies not only for one, but also for any pointers to structs !!! for _, subParam := range params { appendAssignmentForNestedObject(parent, subParam, suffix, builder) } @@ -114,8 +96,8 @@ func appendAssignmentForNestedObject(parent []string, param *properties.SpecPara builder.WriteString(fmt.Sprintf("%s : util.StrToMem(o.%s),\n", param.Name.CamelCase, param.Name.CamelCase)) } else { - builder.WriteString(fmt.Sprintf("%s : nested%s%s,\n", - param.Name.CamelCase, strings.Join(parent, ""), param.Name.CamelCase)) + builder.WriteString(fmt.Sprintf("%s : o.%s.%s,\n", + param.Name.CamelCase, strings.Join(parent, "."), param.Name.CamelCase)) } } diff --git a/pkg/translate/funcs_test.go b/pkg/translate/funcs_test.go index 2c62b19a..d7de5583 100644 --- a/pkg/translate/funcs_test.go +++ b/pkg/translate/funcs_test.go @@ -85,10 +85,9 @@ func TestSpecifyEntryAssignmentForNestedObject(t *testing.T) { }, }, } - expectedAssignmentStreing := `nestedABC := o.A.B.C -entry.A = &SpecAXml{ + expectedAssignmentStreing := `entry.A = &SpecAXml{ B : &SpecABXml{ -C : nestedABC, +C : o.A.B.C, }, } ` @@ -131,10 +130,9 @@ func TestNormalizeAssignmentForNestedObject(t *testing.T) { }, }, } - expectedAssignmentStreing := `nestedABC := o.A.B.C -entry.A = &SpecA{ + expectedAssignmentStreing := `entry.A = &SpecA{ B : &SpecAB{ -C : nestedABC, +C : o.A.B.C, }, } `