-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: if possible assign directly to struct fields, slices, maps
- Loading branch information
Showing
47 changed files
with
367 additions
and
255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package builder | ||
|
||
import ( | ||
"github.com/dave/jennifer/jen" | ||
"github.com/jmattheis/goverter/xtype" | ||
) | ||
|
||
type AssignTo struct { | ||
Must bool | ||
Stmt *jen.Statement | ||
} | ||
|
||
func assignOf(s *jen.Statement) *AssignTo { | ||
return &AssignTo{Stmt: s} | ||
} | ||
|
||
func (a *AssignTo) WithIndex(s *jen.Statement) *AssignTo { | ||
return &AssignTo{ | ||
Stmt: a.Stmt.Clone().Index(s), | ||
} | ||
} | ||
|
||
func (a *AssignTo) WithMust() *AssignTo { | ||
a.Must = 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 { | ||
return nil, err | ||
} | ||
stmt = append(stmt, assignTo.Stmt.Clone().Op("=").Add(nextID.Code)) | ||
return stmt, nil | ||
} | ||
} | ||
|
||
func AssignByBuild(b Builder, gen Generator, ctx *MethodContext, assignTo *AssignTo, sourceID *xtype.JenID, source, target *xtype.Type, errPath ErrorPath) ([]jen.Code, *Error) { | ||
return ToAssignable(assignTo)(b.Build(gen, ctx, sourceID, source, target, errPath)) | ||
} | ||
|
||
func BuildByAssign(b Builder, gen Generator, ctx *MethodContext, sourceID *xtype.JenID, source, target *xtype.Type, path ErrorPath) ([]jen.Code, *xtype.JenID, *Error) { | ||
buildStmt, valueVar, err := buildTargetVar(gen, ctx, sourceID, source, target, path) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
stmt, err := b.Assign(gen, ctx, assignOf(valueVar), sourceID, source, target, path) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
buildStmt = append(buildStmt, stmt...) | ||
return buildStmt, xtype.VariableID(valueVar), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.