From 6517e11490f3579ec4488a3a9d551b43c3fbc9a6 Mon Sep 17 00:00:00 2001 From: Jannis Mattheis Date: Fri, 13 Dec 2024 15:05:55 +0100 Subject: [PATCH] feat: `update:ignoreZeroValueField` affects `default:update` --- builder/assignto.go | 10 +++++++-- builder/pointer.go | 6 +++--- builder/struct.go | 8 +++---- ...efault_on_source_struct_target_pointer.yml | 21 +++++++++++++++++++ 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/builder/assignto.go b/builder/assignto.go index a27f08e..55bf52d 100644 --- a/builder/assignto.go +++ b/builder/assignto.go @@ -6,8 +6,9 @@ import ( ) type AssignTo struct { - Must bool - Stmt *jen.Statement + Stmt *jen.Statement + Must bool + Update bool } func AssignOf(s *jen.Statement) *AssignTo { @@ -25,6 +26,11 @@ func (a *AssignTo) MustAssign() *AssignTo { return a } +func (a *AssignTo) IsUpdate() *AssignTo { + a.Update = true + return a +} + func ToAssignable(assignTo *AssignTo) 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 { diff --git a/builder/pointer.go b/builder/pointer.go index bde0d77..eb9eb7c 100644 --- a/builder/pointer.go +++ b/builder/pointer.go @@ -22,7 +22,7 @@ func (p *Pointer) Build(gen Generator, ctx *MethodContext, sourceID *xtype.JenID return nil, nil, err } - stmt, err := gen.Assign(ctx, AssignOf(jen.Parens(jen.Op("*").Add(valueVar))), sourceID.Deref(source), source.PointerInner, target.PointerInner, errPath) + stmt, err := gen.Assign(ctx, AssignOf(jen.Parens(jen.Op("*").Add(valueVar))).IsUpdate(), sourceID.Deref(source), source.PointerInner, target.PointerInner, errPath) if err != nil { return nil, nil, err.Lift(&Path{ SourceID: "*", @@ -81,7 +81,7 @@ func (s *SourcePointer) Build(gen Generator, ctx *MethodContext, sourceID *xtype return nil, nil, err } - stmt, err := gen.Assign(ctx, AssignOf(valueVar), sourceID.Deref(source), source.PointerInner, target, path) + stmt, err := gen.Assign(ctx, AssignOf(valueVar).IsUpdate(), sourceID.Deref(source), source.PointerInner, target, path) if err != nil { return nil, nil, err.Lift(&Path{ SourceID: "*", @@ -133,7 +133,7 @@ func (*TargetPointer) Build(gen Generator, ctx *MethodContext, sourceID *xtype.J return nil, nil, err } - stmt, err := gen.Assign(ctx, AssignOf(jen.Parens(jen.Op("*").Add(valueVar))), sourceID, source, target.PointerInner, path) + stmt, err := gen.Assign(ctx, AssignOf(jen.Parens(jen.Op("*").Add(valueVar))).IsUpdate(), sourceID, source, target.PointerInner, path) if err != nil { return nil, nil, err.Lift(&Path{ TargetID: "*", diff --git a/builder/struct.go b/builder/struct.go index f55d369..0c60f78 100644 --- a/builder/struct.go +++ b/builder/struct.go @@ -79,7 +79,7 @@ func (s *Struct) Assign(gen Generator, ctx *MethodContext, assignTo *AssignTo, s if err != nil { return nil, err.Lift(lift...) } - if shouldCheckAgainstZero(ctx, nextSource, targetFieldType, false) { + if shouldCheckAgainstZero(ctx, nextSource, targetFieldType, assignTo.Update, false) { stmt = append(stmt, jen.If(nextID.Code.Clone().Op("!=").Add(xtype.ZeroValue(nextSource.T))).Block(fieldStmt...)) } else { stmt = append(stmt, fieldStmt...) @@ -121,7 +121,7 @@ func (s *Struct) Assign(gen Generator, ctx *MethodContext, assignTo *AssignTo, s } callStmt = append(callStmt, assignTo.Stmt.Clone().Dot(targetField.Name()).Op("=").Add(callReturnID.Code)) - if shouldCheckAgainstZero(ctx, functionCallSourceType, targetFieldType, true) { + if shouldCheckAgainstZero(ctx, functionCallSourceType, targetFieldType, assignTo.Update, true) { stmt = append(stmt, jen.If(functionCallSourceID.Code.Clone().Op("!=").Add(xtype.ZeroValue(functionCallSourceType.T))).Block(callStmt...)) } else { stmt = append(stmt, callStmt...) @@ -143,9 +143,9 @@ func (s *Struct) Assign(gen Generator, ctx *MethodContext, assignTo *AssignTo, s return stmt, nil } -func shouldCheckAgainstZero(ctx *MethodContext, s, t *xtype.Type, call bool) bool { +func shouldCheckAgainstZero(ctx *MethodContext, s, t *xtype.Type, isUpdate, call bool) bool { switch { - case !ctx.Conf.UpdateTarget: + case !ctx.Conf.UpdateTarget && !isUpdate: return false case s.Struct && ctx.Conf.IgnoreStructZeroValueField: return true diff --git a/scenario/default_on_source_struct_target_pointer.yml b/scenario/default_on_source_struct_target_pointer.yml index bd924b4..a5e6404 100644 --- a/scenario/default_on_source_struct_target_pointer.yml +++ b/scenario/default_on_source_struct_target_pointer.yml @@ -16,6 +16,15 @@ input: // goverter:useZeroValueOnPointerInconsistency Update(source *Input) (Output, error) } + + // goverter:converter + type UpdateUpdate interface { + // goverter:default NewOutputWithDefaults + // goverter:default:update + // goverter:update:ignoreZeroValueField + // goverter:useZeroValueOnPointerInconsistency + Update(source *Input) (Output, error) + } type Input struct { Name string } type Output struct { Name string } @@ -51,3 +60,15 @@ success: } return executionOutput, nil } + + type UpdateUpdateImpl struct{} + + func (c *UpdateUpdateImpl) Update(source *execution.Input) (execution.Output, error) { + executionOutput := execution.NewOutputWithDefaults() + if source != nil { + if (*source).Name != "" { + executionOutput.Name = (*source).Name + } + } + return executionOutput, nil + }