-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathassignto.go
61 lines (50 loc) · 1.6 KB
/
assignto.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package builder
import (
"github.com/dave/jennifer/jen"
"github.com/jmattheis/goverter/xtype"
)
type AssignTo struct {
Stmt *jen.Statement
Must bool
Update bool
}
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) MustAssign() *AssignTo {
a.Must = true
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 {
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
}