diff --git a/builder/basic.go b/builder/basic.go index 4386494..826ca0b 100644 --- a/builder/basic.go +++ b/builder/basic.go @@ -22,6 +22,10 @@ func (*Basic) Build(_ Generator, _ *MethodContext, sourceID *xtype.JenID, source return nil, sourceID, nil } +func (b *Basic) Assign(gen Generator, ctx *MethodContext, assignTo *jen.Statement, sourceID *xtype.JenID, source, target *xtype.Type, errPath ErrorPath) ([]jen.Code, *Error) { + return ToAssignable(assignTo)(b.Build(gen, ctx, sourceID, source, target, errPath)) +} + // BasicTargetPointerRule handles edge conditions if the target type is a pointer. type BasicTargetPointerRule struct{} @@ -49,3 +53,7 @@ func (*BasicTargetPointerRule) Build(gen Generator, ctx *MethodContext, sourceID return stmt, xtype.OtherID(newID), err } + +func (b *BasicTargetPointerRule) Assign(gen Generator, ctx *MethodContext, assignTo *jen.Statement, sourceID *xtype.JenID, source, target *xtype.Type, errPath ErrorPath) ([]jen.Code, *Error) { + return ToAssignable(assignTo)(b.Build(gen, ctx, sourceID, source, target, errPath)) +} diff --git a/builder/builder.go b/builder/builder.go index 749e8ce..6d66316 100644 --- a/builder/builder.go +++ b/builder/builder.go @@ -20,6 +20,13 @@ type Builder interface { sourceID *xtype.JenID, source, target *xtype.Type, path ErrorPath) ([]jen.Code, *xtype.JenID, *Error) + + Assign(gen Generator, + ctx *MethodContext, + assignTo *jen.Statement, + sourceID *xtype.JenID, + source, target *xtype.Type, + path ErrorPath) ([]jen.Code, *Error) } // Generator checks all existing builders if they can create a conversion implementations for the given source and target type @@ -31,6 +38,12 @@ type Generator interface { source, target *xtype.Type, path ErrorPath) ([]jen.Code, *xtype.JenID, *Error) + Assign(ctx *MethodContext, + assignTo *jen.Statement, + sourceID *xtype.JenID, + source, target *xtype.Type, + path ErrorPath) ([]jen.Code, *Error) + CallMethod( ctx *MethodContext, method *method.Definition, @@ -43,6 +56,16 @@ type Generator interface { id *jen.Statement) (jen.Code, bool) } +func ToAssignable(assignTo *jen.Statement) func(stmt []jen.Code, nextID *xtype.JenID, err *Error) ([]jen.Code, *Error) { + return func(stmt []jen.Code, nextID *xtype.JenID, err *Error) ([]jen.Code, *Error) { + if err != nil { + return nil, err + } + stmt = append(stmt, assignTo.Clone().Op("=").Add(nextID.Code)) + return stmt, nil + } +} + // MethodContext exposes information for the current method. type MethodContext struct { *namer.Namer diff --git a/builder/default.go b/builder/default.go index af1599e..cb803e2 100644 --- a/builder/default.go +++ b/builder/default.go @@ -41,3 +41,30 @@ func buildTargetVar(gen Generator, ctx *MethodContext, sourceID *xtype.JenID, so ctx.SetErrorTargetVar(jen.Id(name)) return stmt, jen.Id(name), nil } + +func assignTargetVar(gen Generator, ctx *MethodContext, assignTo *jen.Statement, sourceID *xtype.JenID, source, target *xtype.Type, errPath ErrorPath) ([]jen.Code, *Error) { + if ctx.Conf.Constructor == nil || + ctx.Conf.Source.String != source.String || + ctx.Conf.Target.String != target.String { + return []jen.Code{}, nil + } + + callTarget := target + toPointer := target.Pointer && !ctx.Conf.Constructor.Target.Pointer + if toPointer { + callTarget = target.PointerInner + } + + stmt, nextID, err := gen.CallMethod(ctx, ctx.Conf.Constructor, sourceID, source, callTarget, errPath) + if err != nil { + return nil, err + } + + if toPointer { + pstmt, pointerID := nextID.Pointer(callTarget, ctx.Name) + stmt = append(stmt, pstmt...) + nextID = pointerID + } + + return []jen.Code{assignTo.Clone().Op("=").Add(nextID.Code)}, nil +} diff --git a/builder/enum.go b/builder/enum.go index b291910..3ff8add 100644 --- a/builder/enum.go +++ b/builder/enum.go @@ -98,6 +98,10 @@ func (*Enum) Build(gen Generator, ctx *MethodContext, sourceID *xtype.JenID, sou return stmt, xtype.VariableID(nameVar), nil } +func (s *Enum) Assign(gen Generator, ctx *MethodContext, assignTo *jen.Statement, sourceID *xtype.JenID, source, target *xtype.Type, path ErrorPath) ([]jen.Code, *Error) { + return ToAssignable(assignTo)(s.Build(gen, ctx, sourceID, source, target, path)) +} + func caseAction(gen Generator, ctx *MethodContext, nameVar *jen.Statement, target *xtype.Type, targetEnum *xtype.Enum, targetName string, sourceID *xtype.JenID, errPath ErrorPath) (jen.Code, *Error) { if config.IsEnumAction(targetName) { switch targetName { diff --git a/builder/list.go b/builder/list.go index ed8220e..53824fd 100644 --- a/builder/list.go +++ b/builder/list.go @@ -14,7 +14,7 @@ func (*List) Matches(_ *MethodContext, source, target *xtype.Type) bool { } // Build creates conversion source code for the given source and target type. -func (*List) Build(gen Generator, ctx *MethodContext, sourceID *xtype.JenID, source, target *xtype.Type, path ErrorPath) ([]jen.Code, *xtype.JenID, *Error) { +func (l *List) Build(gen Generator, ctx *MethodContext, sourceID *xtype.JenID, source, target *xtype.Type, path ErrorPath) ([]jen.Code, *xtype.JenID, *Error) { ctx.SetErrorTargetVar(jen.Nil()) targetSlice := ctx.Name(target.ID()) index := ctx.Index() @@ -52,3 +52,31 @@ func (*List) Build(gen Generator, ctx *MethodContext, sourceID *xtype.JenID, sou return stmt, xtype.VariableID(jen.Id(targetSlice)), nil } +func (*List) Assign(gen Generator, ctx *MethodContext, assignTo *jen.Statement, sourceID *xtype.JenID, source, target *xtype.Type, path ErrorPath) ([]jen.Code, *Error) { + ctx.SetErrorTargetVar(jen.Nil()) + index := ctx.Index() + + indexedSource := xtype.VariableID(sourceID.Code.Clone().Index(jen.Id(index))) + + forBlock, err := gen.Assign(ctx, assignTo.Clone().Index(jen.Id(index)), indexedSource, source.ListInner, target.ListInner, path.Index(jen.Id(index))) + if err != nil { + return nil, err.Lift(&Path{ + SourceID: "[]", + SourceType: source.ListInner.String, + TargetID: "[]", + TargetType: target.ListInner.String, + }) + } + forStmt := jen.For(jen.Id(index).Op(":=").Lit(0), jen.Id(index).Op("<").Len(sourceID.Code.Clone()), jen.Id(index).Op("++")). + Block(forBlock...) + + if source.ListFixed { + return []jen.Code{forStmt}, nil + } + return []jen.Code{ + jen.If(sourceID.Code.Clone().Op("!=").Nil()).Block( + assignTo.Clone().Op("=").Make(target.TypeAsJen(), jen.Len(sourceID.Code.Clone())), + forStmt, + ), + }, nil +} diff --git a/builder/map.go b/builder/map.go index 49ddf84..b67ce00 100644 --- a/builder/map.go +++ b/builder/map.go @@ -14,26 +14,38 @@ func (*Map) Matches(_ *MethodContext, source, target *xtype.Type) bool { } // Build creates conversion source code for the given source and target type. -func (*Map) Build(gen Generator, ctx *MethodContext, sourceID *xtype.JenID, source, target *xtype.Type, errPath ErrorPath) ([]jen.Code, *xtype.JenID, *Error) { +func (m *Map) Build(gen Generator, ctx *MethodContext, sourceID *xtype.JenID, source, target *xtype.Type, errPath ErrorPath) ([]jen.Code, *xtype.JenID, *Error) { ctx.SetErrorTargetVar(jen.Nil()) targetMap := ctx.Name(target.ID()) + + stmt, err := m.Assign(gen, ctx, jen.Id(targetMap), sourceID, source, target, errPath) + if err != nil { + return nil, nil, err + } + + stmt = append([]jen.Code{jen.Var().Add(jen.Id(targetMap), target.TypeAsJen())}, stmt...) + return stmt, xtype.VariableID(jen.Id(targetMap)), nil +} + +func (*Map) Assign(gen Generator, ctx *MethodContext, assignTo *jen.Statement, sourceID *xtype.JenID, source, target *xtype.Type, errPath ErrorPath) ([]jen.Code, *Error) { + ctx.SetErrorTargetVar(jen.Nil()) key, value := ctx.Map() errPath = errPath.Key(jen.Id(key)) - block, newKey, err := gen.Build(ctx, xtype.VariableID(jen.Id(key)), source.MapKey, target.MapKey, errPath) + block, keyID, err := gen.Build(ctx, xtype.VariableID(jen.Id(key)), source.MapKey, target.MapKey, errPath) if err != nil { - return nil, nil, err.Lift(&Path{ + return nil, err.Lift(&Path{ SourceID: "[]", SourceType: " " + source.MapKey.String, TargetID: "[]", TargetType: " " + target.MapKey.String, }) } - valueStmt, valueKey, err := gen.Build( - ctx, xtype.VariableID(jen.Id(value)), source.MapValue, target.MapValue, errPath) + valueStmt, err := gen.Assign( + ctx, assignTo.Clone().Index(keyID.Code), xtype.VariableID(jen.Id(value)), source.MapValue, target.MapValue, errPath) if err != nil { - return nil, nil, err.Lift(&Path{ + return nil, err.Lift(&Path{ SourceID: "[]", SourceType: " " + source.MapValue.String, TargetID: "[]", @@ -41,16 +53,14 @@ func (*Map) Build(gen Generator, ctx *MethodContext, sourceID *xtype.JenID, sour }) } block = append(block, valueStmt...) - block = append(block, jen.Id(targetMap).Index(newKey.Code).Op("=").Add(valueKey.Code)) stmt := []jen.Code{ - jen.Var().Add(jen.Id(targetMap), target.TypeAsJen()), jen.If(sourceID.Code.Clone().Op("!=").Nil()).Block( - jen.Id(targetMap).Op("=").Make(target.TypeAsJen(), jen.Len(sourceID.Code.Clone())), + assignTo.Clone().Op("=").Make(target.TypeAsJen(), jen.Len(sourceID.Code.Clone())), jen.For(jen.List(jen.Id(key), jen.Id(value)).Op(":=").Range().Add(sourceID.Code)). Block(block...), ), } - return stmt, xtype.VariableID(jen.Id(targetMap)), nil + return stmt, nil } diff --git a/builder/pointer.go b/builder/pointer.go index 1e6f15b..fa89691 100644 --- a/builder/pointer.go +++ b/builder/pointer.go @@ -52,6 +52,44 @@ func (*Pointer) Build(gen Generator, ctx *MethodContext, sourceID *xtype.JenID, return stmt, xtype.VariableID(outerVar), err } +func (*Pointer) Assign(gen Generator, ctx *MethodContext, assignTo *jen.Statement, sourceID *xtype.JenID, source, target *xtype.Type, errPath ErrorPath) ([]jen.Code, *Error) { + ctx.SetErrorTargetVar(jen.Nil()) + + stmt, err := assignTargetVar(gen, ctx, assignTo, sourceID, source, target, errPath) + if err != nil { + return nil, err + } + + valueSourceID := jen.Op("*").Add(sourceID.Code.Clone()) + if !source.PointerInner.Basic { + valueSourceID = jen.Parens(valueSourceID) + } + + innerID := xtype.OtherID(valueSourceID) + innerID.ParentPointer = sourceID + nextBlock, id, err := gen.Build( + ctx, innerID, source.PointerInner, target.PointerInner, errPath) + if err != nil { + return nil, err.Lift(&Path{ + SourceID: "*", + SourceType: source.PointerInner.String, + TargetID: "*", + TargetType: target.PointerInner.String, + }) + } + + pstmt, tmpID := id.Pointer(target.PointerInner, ctx.Name) + + ifBlock := append(nextBlock, pstmt...) + ifBlock = append(ifBlock, assignTo.Clone().Op("=").Add(tmpID.Code)) + + stmt = append(stmt, + jen.If(sourceID.Code.Clone().Op("!=").Nil()).Block(ifBlock...), + ) + + return stmt, err +} + // SourcePointer handles type were only the source is a pointer. type SourcePointer struct{} @@ -94,6 +132,37 @@ func (*SourcePointer) Build(gen Generator, ctx *MethodContext, sourceID *xtype.J return stmt, xtype.VariableID(valueVar), nil } +func (*SourcePointer) Assign(gen Generator, ctx *MethodContext, assignTo *jen.Statement, sourceID *xtype.JenID, source, target *xtype.Type, path ErrorPath) ([]jen.Code, *Error) { + valueSourceID := jen.Op("*").Add(sourceID.Code.Clone()) + if !source.PointerInner.Basic { + valueSourceID = jen.Parens(valueSourceID) + } + + innerID := xtype.OtherID(valueSourceID) + innerID.ParentPointer = sourceID + + stmt, err := assignTargetVar(gen, ctx, assignTo, sourceID, source, target, path) + if err != nil { + return nil, err + } + + nextInner, nextID, err := gen.Build(ctx, innerID, source.PointerInner, target, path) + if err != nil { + return nil, err.Lift(&Path{ + SourceID: "*", + SourceType: source.PointerInner.String, + }) + } + + stmt = append(stmt, + jen.If(sourceID.Code.Clone().Op("!=").Nil()).Block( + append(nextInner, assignTo.Clone().Op("=").Add(nextID.Code))..., + ), + ) + + return stmt, nil +} + // TargetPointer handles type were only the target is a pointer. type TargetPointer struct{} @@ -119,3 +188,21 @@ func (*TargetPointer) Build(gen Generator, ctx *MethodContext, sourceID *xtype.J stmt = append(stmt, pstmt...) return stmt, nextID, nil } + +func (*TargetPointer) Assign(gen Generator, ctx *MethodContext, assignTo *jen.Statement, sourceID *xtype.JenID, source, target *xtype.Type, path ErrorPath) ([]jen.Code, *Error) { + ctx.SetErrorTargetVar(jen.Nil()) + stmt, id, err := gen.Build(ctx, sourceID, source, target.PointerInner, path) + if err != nil { + return nil, err.Lift(&Path{ + SourceID: "*", + SourceType: source.String, + TargetID: "*", + TargetType: target.PointerInner.String, + }) + } + + pstmt, nextID := id.Pointer(target.PointerInner, ctx.Name) + stmt = append(stmt, pstmt...) + stmt = append(stmt, assignTo.Clone().Op("=").Add(nextID.Code)) + return stmt, nil +} diff --git a/builder/skipcopy.go b/builder/skipcopy.go index 5b2e326..6a9cfd3 100644 --- a/builder/skipcopy.go +++ b/builder/skipcopy.go @@ -17,3 +17,7 @@ func (*SkipCopy) Matches(ctx *MethodContext, source, target *xtype.Type) bool { func (*SkipCopy) Build(_ Generator, _ *MethodContext, sourceID *xtype.JenID, _, _ *xtype.Type, _ ErrorPath) ([]jen.Code, *xtype.JenID, *Error) { return nil, sourceID, nil } + +func (*SkipCopy) Assign(_ Generator, _ *MethodContext, assignTo *jen.Statement, sourceID *xtype.JenID, _, _ *xtype.Type, _ ErrorPath) ([]jen.Code, *Error) { + return []jen.Code{assignTo.Clone().Op("=").Add(sourceID.Code)}, nil +} diff --git a/builder/struct.go b/builder/struct.go index 8d35362..e40c738 100644 --- a/builder/struct.go +++ b/builder/struct.go @@ -74,12 +74,11 @@ func (*Struct) Build(gen Generator, ctx *MethodContext, sourceID *xtype.JenID, s } stmt = append(stmt, mapStmt...) - fieldStmt, fieldID, err := gen.Build(ctx, nextID, nextSource, targetFieldType, targetFieldPath) + fieldStmt, err := gen.Assign(ctx, nameVar.Clone().Dot(targetField.Name()), nextID, nextSource, targetFieldType, targetFieldPath) if err != nil { return nil, nil, err.Lift(lift...) } stmt = append(stmt, fieldStmt...) - stmt = append(stmt, nameVar.Clone().Dot(targetField.Name()).Op("=").Add(fieldID.Code)) } else { def := fieldMapping.Function @@ -134,6 +133,10 @@ func (*Struct) Build(gen Generator, ctx *MethodContext, sourceID *xtype.JenID, s return stmt, xtype.VariableID(nameVar), nil } +func (s *Struct) Assign(gen Generator, ctx *MethodContext, assignTo *jen.Statement, sourceID *xtype.JenID, source, target *xtype.Type, path ErrorPath) ([]jen.Code, *Error) { + return ToAssignable(assignTo)(s.Build(gen, ctx, sourceID, source, target, path)) +} + func mapField( gen Generator, ctx *MethodContext, diff --git a/builder/underlying.go b/builder/underlying.go index c02e192..042dcf7 100644 --- a/builder/underlying.go +++ b/builder/underlying.go @@ -62,6 +62,10 @@ You have to disable enum or useUnderlyingTypeMethods to resolve the setting conf return stmt, id, err } +func (u *UseUnderlyingTypeMethods) Assign(gen Generator, ctx *MethodContext, assignTo *jen.Statement, sourceID *xtype.JenID, source, target *xtype.Type, errPath ErrorPath) ([]jen.Code, *Error) { + return ToAssignable(assignTo)(u.Build(gen, ctx, sourceID, source, target, errPath)) +} + func findUnderlyingExtendMapping(ctx *MethodContext, source, target *xtype.Type) (underlyingSource, underlyingTarget bool) { if source.Named { if ctx.HasMethod(source.NamedType.Underlying(), target.NamedType) { diff --git a/generator/generator.go b/generator/generator.go index 5c8e17c..766d390 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -173,6 +173,35 @@ You can define a custom conversion method with extend: https://goverter.jmattheis.de/reference/extend`, source.T, target.T)) } + +func (g *generator) assignNoLookup(ctx *builder.MethodContext, assignTo *jen.Statement, sourceID *xtype.JenID, source, target *xtype.Type, errPath builder.ErrorPath) ([]jen.Code, *builder.Error) { + if err := g.getOverlappingStructDefinition(ctx, source, target); err != nil { + return nil, err + } + + for _, rule := range BuildSteps { + if rule.Matches(ctx, source, target) { + return rule.Assign(g, ctx, assignTo, sourceID, source, target, errPath) + } + } + + if source.Pointer && !target.Pointer { + return nil, builder.NewError(fmt.Sprintf(`TypeMismatch: Cannot convert %s to %s +It is unclear how nil should be handled in the pointer to non pointer conversion. + +You can enable useZeroValueOnPointerInconsistency to instruct goverter to use the zero value if source is nil +https://goverter.jmattheis.de/reference/useZeroValueOnPointerInconsistency + +or you can define a custom conversion method with extend: +https://goverter.jmattheis.de/reference/extend`, source.T, target.T)) + } + + return nil, builder.NewError(fmt.Sprintf(`TypeMismatch: Cannot convert %s to %s + +You can define a custom conversion method with extend: +https://goverter.jmattheis.de/reference/extend`, source.T, target.T)) +} + func (g *generator) CallMethod( ctx *builder.MethodContext, definition *method.Definition, @@ -298,6 +327,37 @@ func (g *generator) Build( return g.CallMethod(ctx, genMethod.Definition, sourceID, source, target, errPath) } + if g.shouldCreateSubMethod(ctx, source, target) { + return g.createSubMethod(ctx, sourceID, source, target, errPath) + } + + return g.buildNoLookup(ctx, sourceID, source, target, errPath) +} + +// Build builds an implementation for the given source and target type, or uses an existing method for it. +func (g *generator) Assign( + ctx *builder.MethodContext, + assignTo *jen.Statement, + sourceID *xtype.JenID, + source, target *xtype.Type, + errPath builder.ErrorPath, +) ([]jen.Code, *builder.Error) { + signature := xtype.SignatureOf(source, target) + if def, ok := g.extend[signature]; ok { + return builder.ToAssignable(assignTo)(g.CallMethod(ctx, def, sourceID, source, target, errPath)) + } + if genMethod, ok := g.lookup[signature]; ok { + return builder.ToAssignable(assignTo)(g.CallMethod(ctx, genMethod.Definition, sourceID, source, target, errPath)) + } + + if g.shouldCreateSubMethod(ctx, source, target) { + return builder.ToAssignable(assignTo)(g.createSubMethod(ctx, sourceID, source, target, errPath)) + } + + return g.assignNoLookup(ctx, assignTo, sourceID, source, target, errPath) +} + +func (g *generator) shouldCreateSubMethod(ctx *builder.MethodContext, source, target *xtype.Type) bool { isCurrentPointerStructMethod := false if source.Struct && target.Struct { // This checks if we are currently inside the generation of one of the following combinations. @@ -330,11 +390,7 @@ func (g *generator) Build( } ctx.MarkSeen(source) - if createSubMethod { - return g.createSubMethod(ctx, sourceID, source, target, errPath) - } - - return g.buildNoLookup(ctx, sourceID, source, target, errPath) + return createSubMethod } func (g *generator) createSubMethod(ctx *builder.MethodContext, sourceID *xtype.JenID, source, target *xtype.Type, errPAth builder.ErrorPath) ([]jen.Code, *xtype.JenID, *builder.Error) { diff --git a/scenario/auto_map_pointer.yml b/scenario/auto_map_pointer.yml index 2f1407e..84d301e 100644 --- a/scenario/auto_map_pointer.yml +++ b/scenario/auto_map_pointer.yml @@ -39,11 +39,9 @@ success: if source.Address != nil { pString = &source.Address.Street } - var pString2 *string if pString != nil { xstring := *pString - pString2 = &xstring + exampleFlatPerson.Street = &xstring } - exampleFlatPerson.Street = pString2 return exampleFlatPerson } diff --git a/scenario/gomap_nested_struct.yml b/scenario/gomap_nested_struct.yml index 6ff2288..4b4f704 100644 --- a/scenario/gomap_nested_struct.yml +++ b/scenario/gomap_nested_struct.yml @@ -19,21 +19,17 @@ success: if source != nil { mapStringMapUintMapBoolString = make(map[string]map[uint]map[bool]string, len(source)) for key, value := range source { - var mapUintMapBoolString map[uint]map[bool]string if value != nil { - mapUintMapBoolString = make(map[uint]map[bool]string, len(value)) + mapStringMapUintMapBoolString[key] = make(map[uint]map[bool]string, len(value)) for key2, value2 := range value { - var mapBoolString map[bool]string if value2 != nil { - mapBoolString = make(map[bool]string, len(value2)) + mapStringMapUintMapBoolString[key][key2] = make(map[bool]string, len(value2)) for key3, value3 := range value2 { - mapBoolString[key3] = value3 + mapStringMapUintMapBoolString[key][key2][key3] = value3 } } - mapUintMapBoolString[key2] = mapBoolString } } - mapStringMapUintMapBoolString[key] = mapUintMapBoolString } } return mapStringMapUintMapBoolString diff --git a/scenario/gomap_primitive_pointer.yml b/scenario/gomap_primitive_pointer.yml index a2da34d..f0216b1 100644 --- a/scenario/gomap_primitive_pointer.yml +++ b/scenario/gomap_primitive_pointer.yml @@ -31,12 +31,10 @@ success: if source != nil { mapStringPInt = make(map[string]*int, len(source)) for key, value := range source { - var pInt *int if value != nil { xint := *value - pInt = &xint + mapStringPInt[key] = &xint } - mapStringPInt[key] = pInt } } return mapStringPInt diff --git a/scenario/map_nested_complex.yml b/scenario/map_nested_complex.yml index b5d0dd7..a139095 100644 --- a/scenario/map_nested_complex.yml +++ b/scenario/map_nested_complex.yml @@ -39,11 +39,9 @@ success: if source.Nested != nil && source.Nested.Inner.Inner2 != nil { pString = source.Nested.Inner.Inner2.Name } - var pString2 *string if pString != nil { xstring := *pString - pString2 = &xstring + structsOutput.Name = &xstring } - structsOutput.Name = pString2 return structsOutput } diff --git a/scenario/map_nested_inner_pointer.yml b/scenario/map_nested_inner_pointer.yml index f87cb72..c2ebbed 100644 --- a/scenario/map_nested_inner_pointer.yml +++ b/scenario/map_nested_inner_pointer.yml @@ -29,11 +29,9 @@ success: func (c *ConverterImpl) Convert(source execution.Input) execution.Output { var structsOutput execution.Output - var pString *string if source.Nested.Name != nil { xstring := *source.Nested.Name - pString = &xstring + structsOutput.Name = &xstring } - structsOutput.Name = pString return structsOutput } diff --git a/scenario/map_nested_pointer.yml b/scenario/map_nested_pointer.yml index 8e98d8b..91e0fb2 100644 --- a/scenario/map_nested_pointer.yml +++ b/scenario/map_nested_pointer.yml @@ -33,11 +33,9 @@ success: if source.Nested != nil { pString = source.Nested.Name } - var pString2 *string if pString != nil { xstring := *pString - pString2 = &xstring + structsOutput.Name = &xstring } - structsOutput.Name = pString2 return structsOutput } diff --git a/scenario/map_nested_structpointer.yml b/scenario/map_nested_structpointer.yml index 5610305..d71a935 100644 --- a/scenario/map_nested_structpointer.yml +++ b/scenario/map_nested_structpointer.yml @@ -33,11 +33,9 @@ success: if source.Nested != nil { pString = &source.Nested.Name } - var pString2 *string if pString != nil { xstring := *pString - pString2 = &xstring + structsOutput.Name = &xstring } - structsOutput.Name = pString2 return structsOutput } diff --git a/scenario/recursive4.yml b/scenario/recursive4.yml index b3106a8..73031e2 100644 --- a/scenario/recursive4.yml +++ b/scenario/recursive4.yml @@ -37,13 +37,11 @@ success: func (c *ConverterImpl) structsInputToStructsOutput(source execution.Input) execution.Output { var structsOutput execution.Output structsOutput.Name = source.Name - var structsOutputList []execution.Output if source.Values != nil { - structsOutputList = make([]execution.Output, len(source.Values)) + structsOutput.Values = make([]execution.Output, len(source.Values)) for i := 0; i < len(source.Values); i++ { - structsOutputList[i] = c.structsInputToStructsOutput(source.Values[i]) + structsOutput.Values[i] = c.structsInputToStructsOutput(source.Values[i]) } } - structsOutput.Values = structsOutputList return structsOutput } diff --git a/scenario/skipcopy_inner.yml b/scenario/skipcopy_inner.yml index e57446e..032180d 100644 --- a/scenario/skipcopy_inner.yml +++ b/scenario/skipcopy_inner.yml @@ -43,14 +43,12 @@ success: var skipOutput execution.Output skipOutput.ID = source.ID skipOutput.Map = source.Map - var mapStringSkipID map[string]execution.ID if source.MapDifferentType != nil { - mapStringSkipID = make(map[string]execution.ID, len(source.MapDifferentType)) + skipOutput.MapDifferentType = make(map[string]execution.ID, len(source.MapDifferentType)) for key, value := range source.MapDifferentType { - mapStringSkipID[key] = execution.ID(value) + skipOutput.MapDifferentType[key] = execution.ID(value) } } - skipOutput.MapDifferentType = mapStringSkipID skipOutput.CreatedAt = c.timeTimeToPTimeTime(source.CreatedAt) skipOutput.Unnamed = source.Unnamed return skipOutput diff --git a/scenario/slice_pointer.yml b/scenario/slice_pointer.yml index ad5658d..48ec035 100644 --- a/scenario/slice_pointer.yml +++ b/scenario/slice_pointer.yml @@ -26,7 +26,6 @@ success: func (c *ConverterImpl) ConvertHouse(source execution.DBHouseNames) execution.APIHouseNames { var slices_arraysAPIHouseNames execution.APIHouseNames - var pStringList *[]string if source.Names != nil { var stringList []string if (*source.Names) != nil { @@ -35,8 +34,7 @@ success: stringList[i] = (*source.Names)[i] } } - pStringList = &stringList + slices_arraysAPIHouseNames.Names = &stringList } - slices_arraysAPIHouseNames.Names = pStringList return slices_arraysAPIHouseNames } diff --git a/scenario/slice_pointer3.yml b/scenario/slice_pointer3.yml index 1913352..3f7817f 100644 --- a/scenario/slice_pointer3.yml +++ b/scenario/slice_pointer3.yml @@ -26,7 +26,6 @@ success: func (c *ConverterImpl) ConvertHouse(source execution.DBHouseNames) execution.APIHouseNames { var slices_arraysAPIHouseNames execution.APIHouseNames - var pPPStringList ***[]string if source.Names != nil { var pPStringList **[]string if (*source.Names) != nil { @@ -43,8 +42,7 @@ success: } pPStringList = &pStringList } - pPPStringList = &pPStringList + slices_arraysAPIHouseNames.Names = &pPStringList } - slices_arraysAPIHouseNames.Names = pPPStringList return slices_arraysAPIHouseNames } diff --git a/scenario/struct_method_nested.yml b/scenario/struct_method_nested.yml index ac23b11..78b40d4 100644 --- a/scenario/struct_method_nested.yml +++ b/scenario/struct_method_nested.yml @@ -37,11 +37,9 @@ success: xint := source.Nested.Age() pInt = &xint } - var pInt2 *int if pInt != nil { xint2 := *pInt - pInt2 = &xint2 + structsOutput.Age = &xint2 } - structsOutput.Age = pInt2 return structsOutput } diff --git a/scenario/struct_method_nested_with_error.yml b/scenario/struct_method_nested_with_error.yml index 1a4ceb3..564fe54 100644 --- a/scenario/struct_method_nested_with_error.yml +++ b/scenario/struct_method_nested_with_error.yml @@ -40,11 +40,9 @@ success: } pInt = &xint } - var pInt2 *int if pInt != nil { xint2 := *pInt - pInt2 = &xint2 + structsOutput.Age = &xint2 } - structsOutput.Age = pInt2 return structsOutput, nil } diff --git a/scenario/struct_pointer.yml b/scenario/struct_pointer.yml index a22d372..1b80ce8 100644 --- a/scenario/struct_pointer.yml +++ b/scenario/struct_pointer.yml @@ -35,12 +35,10 @@ success: func (c *ConverterImpl) Convert(source execution.Input) execution.Output { var structsOutput execution.Output - var pPStructsX **execution.X if source.X != nil { pStructsX := c.pStructsXToPStructsX((*source.X)) - pPStructsX = &pStructsX + structsOutput.X = &pStructsX } - structsOutput.X = pPStructsX return structsOutput } func (c *ConverterImpl) pStructsXToPStructsX(source *execution.X) *execution.X { diff --git a/scenario/struct_primitive_pointer.yml b/scenario/struct_primitive_pointer.yml index 4c5ad32..ff5c331 100644 --- a/scenario/struct_primitive_pointer.yml +++ b/scenario/struct_primitive_pointer.yml @@ -35,11 +35,9 @@ success: structsOutput.ID = &pStructsID pString := source.Name structsOutput.Name = &pString - var pInt *int if source.Age != nil { xint := *source.Age - pInt = &xint + structsOutput.Age = &xint } - structsOutput.Age = pInt return structsOutput } diff --git a/scenario/struct_unnamed_pointer.yml b/scenario/struct_unnamed_pointer.yml index 9b6d51c..b206bfc 100644 --- a/scenario/struct_unnamed_pointer.yml +++ b/scenario/struct_unnamed_pointer.yml @@ -34,30 +34,20 @@ success: func (c *ConverterImpl) ConvertHouse(source execution.Input) execution.Output { var structsOutput execution.Output - var pUnnamed *struct { - Inner *struct { - Name string - } - } if source.House != nil { var unnamed struct { Inner *struct { Name string } } - var pUnnamed2 *struct { - Name string - } if (*source.House).Inner != nil { var unnamed2 struct { Name string } unnamed2.Name = (*(*source.House).Inner).Name - pUnnamed2 = &unnamed2 + unnamed.Inner = &unnamed2 } - unnamed.Inner = pUnnamed2 - pUnnamed = &unnamed + structsOutput.House = &unnamed } - structsOutput.House = pUnnamed return structsOutput } diff --git a/scenario/struct_unnamed_slice.yml b/scenario/struct_unnamed_slice.yml index d074fe8..fae915d 100644 --- a/scenario/struct_unnamed_slice.yml +++ b/scenario/struct_unnamed_slice.yml @@ -30,11 +30,8 @@ success: func (c *ConverterImpl) ConvertHouse(source execution.Input) execution.Output { var structsOutput execution.Output - var unnamedList []struct { - Name string - } if source.House != nil { - unnamedList = make([]struct { + structsOutput.House = make([]struct { Name string }, len(source.House)) for i := 0; i < len(source.House); i++ { @@ -42,9 +39,8 @@ success: Name string } unnamed.Name = source.House[i].Name - unnamedList[i] = unnamed + structsOutput.House[i] = unnamed } } - structsOutput.House = unnamedList return structsOutput } diff --git a/scenario/struct_unnamed_slice_nested.yml b/scenario/struct_unnamed_slice_nested.yml index d62d337..e6b9613 100644 --- a/scenario/struct_unnamed_slice_nested.yml +++ b/scenario/struct_unnamed_slice_nested.yml @@ -30,39 +30,29 @@ success: func (c *ConverterImpl) ConvertHouse(source execution.Input) execution.Output { var structsOutput execution.Output - var unnamedListList [][]struct { - Names []string - } if source.House != nil { - unnamedListList = make([][]struct { + structsOutput.House = make([][]struct { Names []string }, len(source.House)) for i := 0; i < len(source.House); i++ { - var unnamedList []struct { - Names []string - } if source.House[i] != nil { - unnamedList = make([]struct { + structsOutput.House[i] = make([]struct { Names []string }, len(source.House[i])) for j := 0; j < len(source.House[i]); j++ { var unnamed struct { Names []string } - var stringList []string if source.House[i][j].Names != nil { - stringList = make([]string, len(source.House[i][j].Names)) + unnamed.Names = make([]string, len(source.House[i][j].Names)) for k := 0; k < len(source.House[i][j].Names); k++ { - stringList[k] = source.House[i][j].Names[k] + unnamed.Names[k] = source.House[i][j].Names[k] } } - unnamed.Names = stringList - unnamedList[j] = unnamed + structsOutput.House[i][j] = unnamed } } - unnamedListList[i] = unnamedList } } - structsOutput.House = unnamedListList return structsOutput } diff --git a/scenario/use_zerovalue_on_pointer_inconsistency_map_nested.yml b/scenario/use_zerovalue_on_pointer_inconsistency_map_nested.yml index b3f0b42..3f8377d 100644 --- a/scenario/use_zerovalue_on_pointer_inconsistency_map_nested.yml +++ b/scenario/use_zerovalue_on_pointer_inconsistency_map_nested.yml @@ -31,10 +31,8 @@ success: func (c *ConverterImpl) ConvertPerson(source execution.Person) execution.APIPerson { var structsAPIPerson execution.APIPerson - var xstring string if source.Inner.Name != nil { - xstring = *source.Inner.Name + structsAPIPerson.Name = *source.Inner.Name } - structsAPIPerson.Name = xstring return structsAPIPerson } diff --git a/scenario/use_zerovalue_on_pointer_inconsistency_map_nested2.yml b/scenario/use_zerovalue_on_pointer_inconsistency_map_nested2.yml index eaa2da3..f2c518f 100644 --- a/scenario/use_zerovalue_on_pointer_inconsistency_map_nested2.yml +++ b/scenario/use_zerovalue_on_pointer_inconsistency_map_nested2.yml @@ -35,10 +35,8 @@ success: if source.Inner != nil { pString = source.Inner.Name } - var xstring string if pString != nil { - xstring = *pString + structsAPIPerson.Name = *pString } - structsAPIPerson.Name = xstring return structsAPIPerson } diff --git a/scenario/use_zerovalue_on_pointer_inconsistency_map_nested3.yml b/scenario/use_zerovalue_on_pointer_inconsistency_map_nested3.yml index 0286588..de5b803 100644 --- a/scenario/use_zerovalue_on_pointer_inconsistency_map_nested3.yml +++ b/scenario/use_zerovalue_on_pointer_inconsistency_map_nested3.yml @@ -35,10 +35,8 @@ success: if source.Inner != nil { pString = &source.Inner.Name } - var xstring string if pString != nil { - xstring = *pString + structsAPIPerson.Name = *pString } - structsAPIPerson.Name = xstring return structsAPIPerson } diff --git a/scenario/use_zerovalue_on_pointer_inconsistency_multi_ptr.yml b/scenario/use_zerovalue_on_pointer_inconsistency_multi_ptr.yml index 9da3c9a..f48bd4b 100644 --- a/scenario/use_zerovalue_on_pointer_inconsistency_multi_ptr.yml +++ b/scenario/use_zerovalue_on_pointer_inconsistency_multi_ptr.yml @@ -27,14 +27,12 @@ success: func (c *ConverterImpl) ConvertHouse(source execution.DBHouseNames) execution.APIHouseNames { var slices_arraysAPIHouseNames execution.APIHouseNames - var xstring string if source.Name != nil { - var xstring2 string + var xstring string if (*source.Name) != nil { - xstring2 = *(*source.Name) + xstring = *(*source.Name) } - xstring = xstring2 + slices_arraysAPIHouseNames.Name = xstring } - slices_arraysAPIHouseNames.Name = xstring return slices_arraysAPIHouseNames } diff --git a/scenario/use_zerovalue_on_pointer_inconsistency_primitive.yml b/scenario/use_zerovalue_on_pointer_inconsistency_primitive.yml index 9544f2c..198eec7 100644 --- a/scenario/use_zerovalue_on_pointer_inconsistency_primitive.yml +++ b/scenario/use_zerovalue_on_pointer_inconsistency_primitive.yml @@ -27,10 +27,8 @@ success: func (c *ConverterImpl) ConvertPerson(source execution.Person) execution.APIPerson { var structsAPIPerson execution.APIPerson - var xstring string if source.FirstName != nil { - xstring = *source.FirstName + structsAPIPerson.FirstName = *source.FirstName } - structsAPIPerson.FirstName = xstring return structsAPIPerson } diff --git a/scenario/use_zerovalue_on_pointer_inconsistency_slice.yml b/scenario/use_zerovalue_on_pointer_inconsistency_slice.yml index a0c5068..78bcca0 100644 --- a/scenario/use_zerovalue_on_pointer_inconsistency_slice.yml +++ b/scenario/use_zerovalue_on_pointer_inconsistency_slice.yml @@ -27,17 +27,15 @@ success: func (c *ConverterImpl) ConvertHouse(source execution.DBHouseNames) execution.APIHouseNames { var slices_arraysAPIHouseNames execution.APIHouseNames - var stringList []string if source.Names != nil { - var stringList2 []string + var stringList []string if (*source.Names) != nil { - stringList2 = make([]string, len((*source.Names))) + stringList = make([]string, len((*source.Names))) for i := 0; i < len((*source.Names)); i++ { - stringList2[i] = (*source.Names)[i] + stringList[i] = (*source.Names)[i] } } - stringList = stringList2 + slices_arraysAPIHouseNames.Names = stringList } - slices_arraysAPIHouseNames.Names = stringList return slices_arraysAPIHouseNames } diff --git a/scenario/use_zerovalue_on_pointer_inconsistency_struct.yml b/scenario/use_zerovalue_on_pointer_inconsistency_struct.yml index 5f60a18..ee90a46 100644 --- a/scenario/use_zerovalue_on_pointer_inconsistency_struct.yml +++ b/scenario/use_zerovalue_on_pointer_inconsistency_struct.yml @@ -42,11 +42,9 @@ success: var structsAPIPersonInfo execution.APIPersonInfo if source != nil { var structsAPIPersonInfo2 execution.APIPersonInfo - var xstring string if (*source).FirstName != nil { - xstring = *(*source).FirstName + structsAPIPersonInfo2.FirstName = *(*source).FirstName } - structsAPIPersonInfo2.FirstName = xstring structsAPIPersonInfo = structsAPIPersonInfo2 } return structsAPIPersonInfo diff --git a/scenario/use_zerovalue_on_pointer_inconsistency_unnamed.yml b/scenario/use_zerovalue_on_pointer_inconsistency_unnamed.yml index 46a080a..46caa4e 100644 --- a/scenario/use_zerovalue_on_pointer_inconsistency_unnamed.yml +++ b/scenario/use_zerovalue_on_pointer_inconsistency_unnamed.yml @@ -27,16 +27,12 @@ success: func (c *ConverterImpl) ConvertPerson(source execution.Input) execution.Output { var structsOutput execution.Output - var unnamed struct { - Name string - } if source.Thing != nil { - var unnamed2 struct { + var unnamed struct { Name string } - unnamed2.Name = (*source.Thing).Name - unnamed = unnamed2 + unnamed.Name = (*source.Thing).Name + structsOutput.Thing = unnamed } - structsOutput.Thing = unnamed return structsOutput } diff --git a/scenario/wrap_errors_using_nested.yml b/scenario/wrap_errors_using_nested.yml index 77ac9c3..04385ce 100644 --- a/scenario/wrap_errors_using_nested.yml +++ b/scenario/wrap_errors_using_nested.yml @@ -40,19 +40,13 @@ success: func (c *ConverterImpl) Convert(source execution.Input) (execution.Output, error) { var structsOutput execution.Output - var mapStringMapIntUnnamed map[string]map[int]struct { - Age int - } if source.Values != nil { - mapStringMapIntUnnamed = make(map[string]map[int]struct { + structsOutput.Values = make(map[string]map[int]struct { Age int }, len(source.Values)) for key, value := range source.Values { - var mapIntUnnamed map[int]struct { - Age int - } if value != nil { - mapIntUnnamed = make(map[int]struct { + structsOutput.Values[key] = make(map[int]struct { Age int }, len(value)) for key2, value2 := range value { @@ -64,35 +58,29 @@ success: return structsOutput, patherr.Wrap(err, patherr.Field("Values"), patherr.Key(key), patherr.Key(key2), patherr.Field("Age")) } unnamed.Age = xint - mapIntUnnamed[key2] = unnamed + structsOutput.Values[key][key2] = unnamed } } - mapStringMapIntUnnamed[key] = mapIntUnnamed } } - structsOutput.Values = mapStringMapIntUnnamed var unnamed2 struct { Names []map[bool]int } - var mapBoolIntList []map[bool]int if source.S.Names != nil { - mapBoolIntList = make([]map[bool]int, len(source.S.Names)) + unnamed2.Names = make([]map[bool]int, len(source.S.Names)) for i := 0; i < len(source.S.Names); i++ { - var mapBoolInt map[bool]int if source.S.Names[i] != nil { - mapBoolInt = make(map[bool]int, len(source.S.Names[i])) + unnamed2.Names[i] = make(map[bool]int, len(source.S.Names[i])) for key3, value3 := range source.S.Names[i] { xint2, err := strconv.Atoi(value3) if err != nil { return structsOutput, patherr.Wrap(err, patherr.Field("S"), patherr.Field("Names"), patherr.Index(i), patherr.Key(key3)) } - mapBoolInt[key3] = xint2 + unnamed2.Names[i][key3] = xint2 } } - mapBoolIntList[i] = mapBoolInt } } - unnamed2.Names = mapBoolIntList structsOutput.S = unnamed2 return structsOutput, nil }