Skip to content

Commit

Permalink
fix: update values from default
Browse files Browse the repository at this point in the history
  • Loading branch information
jmattheis committed Dec 13, 2024
1 parent 31bc2ee commit dbbdc48
Show file tree
Hide file tree
Showing 21 changed files with 376 additions and 55 deletions.
1 change: 1 addition & 0 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type MethodContext struct {
Conf *config.Method
FieldsTarget string
OutputPackagePath string
UseConstructor bool
Signature xtype.Signature
TargetType *xtype.Type
HasMethod func(*MethodContext, types.Type, types.Type) bool
Expand Down
3 changes: 2 additions & 1 deletion builder/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
)

func buildTargetVar(gen Generator, ctx *MethodContext, sourceID *xtype.JenID, source, target *xtype.Type, errPath ErrorPath) ([]jen.Code, *jen.Statement, *Error) {
if ctx.Conf.Constructor == nil ||
if !ctx.UseConstructor ||
!types.Identical(ctx.Conf.Source.T, source.T) ||
!types.Identical(ctx.Conf.Target.T, target.T) {
name := ctx.Name(target.ID())
variable := jen.Var().Id(name).Add(target.TypeAsJen())
ctx.SetErrorTargetVar(jen.Id(name))
return []jen.Code{variable}, jen.Id(name), nil
}
ctx.UseConstructor = false

callTarget := target
toPointer := target.Pointer && !ctx.Conf.Constructor.Target.Pointer
Expand Down
84 changes: 64 additions & 20 deletions builder/pointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,34 @@ func (*Pointer) Matches(_ *MethodContext, source, target *xtype.Type) bool {
// Build creates conversion source code for the given source and target type.
func (p *Pointer) Build(gen Generator, ctx *MethodContext, sourceID *xtype.JenID, source, target *xtype.Type, errPath ErrorPath) ([]jen.Code, *xtype.JenID, *Error) {
ctx.SetErrorTargetVar(jen.Nil())
if ctx.UseConstructor {
buildStmt, valueVar, err := buildTargetVar(gen, ctx, sourceID, source, target, errPath)
if err != nil {
return nil, nil, err
}

stmt, err := gen.Assign(ctx, AssignOf(jen.Parens(jen.Op("*").Add(valueVar))), sourceID.Deref(source), source.PointerInner, target.PointerInner, errPath)
if err != nil {
return nil, nil, err.Lift(&Path{
SourceID: "*",
SourceType: source.PointerInner.String,
TargetID: "*",
TargetType: target.PointerInner.String,
})
}

buildStmt = append(buildStmt, jen.If(sourceID.Code.Clone().Op("!=").Nil()).Block(stmt...))

return buildStmt, xtype.VariableID(valueVar), nil
}

return BuildByAssign(p, gen, ctx, sourceID, source, target, errPath)
}

func (*Pointer) Assign(gen Generator, ctx *MethodContext, assignTo *AssignTo, sourceID *xtype.JenID, source, target *xtype.Type, errPath ErrorPath) ([]jen.Code, *Error) {
ctx.SetErrorTargetVar(jen.Nil())

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)
nextBlock, id, err := gen.Build(ctx, sourceID.Deref(source), source.PointerInner, target.PointerInner, errPath)
if err != nil {
return nil, err.Lift(&Path{
SourceID: "*",
Expand Down Expand Up @@ -62,19 +75,31 @@ func (*SourcePointer) Matches(ctx *MethodContext, source, target *xtype.Type) bo

// Build creates conversion source code for the given source and target type.
func (s *SourcePointer) Build(gen Generator, ctx *MethodContext, sourceID *xtype.JenID, source, target *xtype.Type, path ErrorPath) ([]jen.Code, *xtype.JenID, *Error) {

Check failure on line 77 in builder/pointer.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary leading newline (whitespace)
return BuildByAssign(s, gen, ctx, sourceID, source, target, path)
}

Check failure on line 78 in builder/pointer.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
func (*SourcePointer) Assign(gen Generator, ctx *MethodContext, assignTo *AssignTo, 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)
if ctx.UseConstructor {
buildStmt, valueVar, err := buildTargetVar(gen, ctx, sourceID, source, target, path)
if err != nil {
return nil, nil, err
}

stmt, err := gen.Assign(ctx, AssignOf(valueVar), sourceID.Deref(source), source.PointerInner, target, path)
if err != nil {
return nil, nil, err.Lift(&Path{
SourceID: "*",
SourceType: source.PointerInner.String,
})
}

buildStmt = append(buildStmt, jen.If(sourceID.Code.Clone().Op("!=").Nil()).Block(stmt...))

return buildStmt, xtype.VariableID(valueVar), nil
}

innerID := xtype.OtherID(valueSourceID)
innerID.ParentPointer = sourceID
return BuildByAssign(s, gen, ctx, sourceID, source, target, path)
}

nextInner, nextID, err := gen.Build(ctx, innerID, source.PointerInner, target, path)
func (*SourcePointer) Assign(gen Generator, ctx *MethodContext, assignTo *AssignTo, sourceID *xtype.JenID, source, target *xtype.Type, path ErrorPath) ([]jen.Code, *Error) {
nextInner, nextID, err := gen.Build(ctx, sourceID.Deref(source), source.PointerInner, target, path)
if err != nil {
return nil, err.Lift(&Path{
SourceID: "*",
Expand Down Expand Up @@ -102,18 +127,37 @@ func (*TargetPointer) Matches(_ *MethodContext, source, target *xtype.Type) bool
// Build creates conversion source code for the given source and target type.
func (*TargetPointer) Build(gen Generator, ctx *MethodContext, sourceID *xtype.JenID, source, target *xtype.Type, path ErrorPath) ([]jen.Code, *xtype.JenID, *Error) {
ctx.SetErrorTargetVar(jen.Nil())

if ctx.UseConstructor {
buildStmt, valueVar, err := buildTargetVar(gen, ctx, sourceID, source, target, path)
if err != nil {
return nil, nil, err
}

stmt, err := gen.Assign(ctx, AssignOf(jen.Parens(jen.Op("*").Add(valueVar))), sourceID, source, target.PointerInner, path)
if err != nil {
return nil, nil, err.Lift(&Path{
TargetID: "*",
TargetType: target.PointerInner.String,
})
}

buildStmt = append(buildStmt, stmt...)

return buildStmt, xtype.VariableID(valueVar), nil
}

stmt, id, err := gen.Build(ctx, sourceID, source, target.PointerInner, path)
if err != nil {
return nil, 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...)

return stmt, nextID, nil
}

Expand Down
6 changes: 2 additions & 4 deletions example/default/generated/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func (g *generator) buildMethod(genMethod *generatedMethod, context map[string]*
Signature: genMethod.Signature,
HasMethod: g.hasMethod,
OutputPackagePath: g.conf.OutputPackagePath,
UseConstructor: genMethod.Constructor != nil,
}

var targetAssign *jen.Statement
Expand Down
4 changes: 1 addition & 3 deletions scenario/default_on_pointer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ success:
func (c *ConverterImpl) Convert(source *execution.Input) (*execution.Output, error) {
pExecutionOutput := execution.NewOutputWithDefaults()
if source != nil {
var executionOutput execution.Output
executionOutput.Name = (*source).Name
pExecutionOutput = &executionOutput
(*pExecutionOutput).Name = (*source).Name
}
return pExecutionOutput, nil
}
47 changes: 47 additions & 0 deletions scenario/default_on_pointer_error.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
input:
input.go: |
package execution
// goverter:converter
type Converter interface {
// goverter:default NewOutputWithDefaults
Convert(source *Input) (*Output, error)
}
type Input struct {
Name int
}
type Output struct {
Name string
}
func NewOutputWithDefaults() *Output {
return &Output{
Name: "string",
}
}
error: |-
Error while creating converter method:
@workdir/input.go:6
func (github.com/jmattheis/goverter/execution.Converter).Convert(source *github.com/jmattheis/goverter/execution.Input) (*github.com/jmattheis/goverter/execution.Output, error)
[source] *github.com/jmattheis/goverter/execution.Input
[target] *github.com/jmattheis/goverter/execution.Output
| *github.com/jmattheis/goverter/execution.Input
|
| | github.com/jmattheis/goverter/execution.Input
| |
| | | int
| | |
source*.Name
target*.Name
| | |
| | | string
| |
| | github.com/jmattheis/goverter/execution.Output
|
| *github.com/jmattheis/goverter/execution.Output
TypeMismatch: Cannot convert int to string
You can define a custom conversion method with extend:
https://goverter.jmattheis.de/reference/extend
4 changes: 1 addition & 3 deletions scenario/default_on_pointer_with_error.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ success:
return nil, err
}
if source != nil {
var executionOutput execution.Output
executionOutput.ID = (*source).ID
pExecutionOutput = &executionOutput
(*pExecutionOutput).ID = (*source).ID
}
return pExecutionOutput, nil
}
4 changes: 1 addition & 3 deletions scenario/default_on_pointer_with_non_pointer_method.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ success:
executionOutput := execution.NewOutput()
pExecutionOutput := &executionOutput
if source != nil {
var executionOutput2 execution.Output
executionOutput2.Name = (*source).Name
pExecutionOutput = &executionOutput2
(*pExecutionOutput).Name = (*source).Name
}
return pExecutionOutput
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ success:
}
pExecutionOutput := &executionOutput
if source != nil {
var executionOutput2 execution.Output
executionOutput2.Name = (*source).Name
pExecutionOutput = &executionOutput2
(*pExecutionOutput).Name = (*source).Name
}
return pExecutionOutput, nil
}
36 changes: 36 additions & 0 deletions scenario/default_on_source_pointer_struct_target_struct.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
input:
input.go: |
package execution
// goverter:converter
type Converter interface {
// goverter:default NewOutputWithDefaults
Convert(source Input) (*Output, error)
}
type Input struct {
Name string
}
type Output struct {
Name string
}
func NewOutputWithDefaults() *Output {
return &Output{
Name: "string",
}
}
success:
- generated/generated.go: |
// Code generated by github.com/jmattheis/goverter, DO NOT EDIT.
package generated
import execution "github.com/jmattheis/goverter/execution"
type ConverterImpl struct{}
func (c *ConverterImpl) Convert(source execution.Input) (*execution.Output, error) {
pExecutionOutput := execution.NewOutputWithDefaults()
(*pExecutionOutput).Name = source.Name
return pExecutionOutput, nil
}
45 changes: 45 additions & 0 deletions scenario/default_on_source_pointer_struct_target_struct_error.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
input:
input.go: |
package execution
// goverter:converter
type Converter interface {
// goverter:default NewOutputWithDefaults
Convert(source Input) (*Output, error)
}
type Input struct {
Name int
}
type Output struct {
Name string
}
func NewOutputWithDefaults() *Output {
return &Output{
Name: "string",
}
}
error: |-
Error while creating converter method:
@workdir/input.go:6
func (github.com/jmattheis/goverter/execution.Converter).Convert(source github.com/jmattheis/goverter/execution.Input) (*github.com/jmattheis/goverter/execution.Output, error)
[source] github.com/jmattheis/goverter/execution.Input
[target] *github.com/jmattheis/goverter/execution.Output
| github.com/jmattheis/goverter/execution.Input
|
| | int
| |
source .Name
target*.Name
| | |
| | | string
| |
| | github.com/jmattheis/goverter/execution.Output
|
| *github.com/jmattheis/goverter/execution.Output
TypeMismatch: Cannot convert int to string
You can define a custom conversion method with extend:
https://goverter.jmattheis.de/reference/extend
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
input:
input.go: |
package execution
// goverter:converter
type Converter interface {
// goverter:default NewOutputWithDefaults
Convert(source Input) (*Output, error)
}
type Input struct {
Name string
}
type Output struct {
Name string
}
func NewOutputWithDefaults(string) *Output {
return &Output{
Name: "string",
}
}
error: |-
Error while creating converter method:
@workdir/input.go:6
func (github.com/jmattheis/goverter/execution.Converter).Convert(source github.com/jmattheis/goverter/execution.Input) (*github.com/jmattheis/goverter/execution.Output, error)
[source] github.com/jmattheis/goverter/execution.Input
[target] *github.com/jmattheis/goverter/execution.Output
| github.com/jmattheis/goverter/execution.Input
|
source
target
|
| *github.com/jmattheis/goverter/execution.Output
Error using method:
func github.com/jmattheis/goverter/execution.NewOutputWithDefaults(string) *github.com/jmattheis/goverter/execution.Output
[source] string
[target] *github.com/jmattheis/goverter/execution.Output
Method source type mismatches with conversion source: string != github.com/jmattheis/goverter/execution.Input
Loading

0 comments on commit dbbdc48

Please sign in to comment.