Skip to content

Commit

Permalink
feat: update:ignoreZeroValueField affects default:update
Browse files Browse the repository at this point in the history
  • Loading branch information
jmattheis committed Dec 13, 2024
1 parent a8a6bdc commit 6517e11
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
10 changes: 8 additions & 2 deletions builder/assignto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions builder/pointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "*",
Expand Down Expand Up @@ -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: "*",
Expand Down Expand Up @@ -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: "*",
Expand Down
8 changes: 4 additions & 4 deletions builder/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down Expand Up @@ -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...)
Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions scenario/default_on_source_struct_target_pointer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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
}

0 comments on commit 6517e11

Please sign in to comment.