-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.go
77 lines (72 loc) · 1.77 KB
/
template.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
var BuildVarTemplate = `
// With$Var method fills the $Var field of $NewObj object.
func ($obj *$newobj) With$Var($var $iType) *$NewObj {
$obj.$var = $var
return $obj
}
`
var BuildTemplate = `
// New$Struct returns new instance of object $Struct
func New$Struct() *$NewObj {
return &$Struct{}
}
// WithCheck add given predicate to predicate list
func ($obj *$newobj) WithCheck(pred ...PredicateFunc) *$NewObj {
$obj.predicatelist = append($obj.predicatelist, pred...)
return $obj
}
`
var ValidateTemplate = `
// Validate is to validate generated $NewObj object by builder
func ($obj *$newobj) Validate() *$NewObj {
if len($obj.predicatelist) != 0 {
for _, pred := range $obj.predicatelist {
if !pred($obj) {
errors.Wrapf($obj.err, "Failed to run predicate {%v}", pred)
}
}
}
return $obj
}
`
var BuildFuncTemplate = `
// Build returns the $NewObj object generated by builder
func ($obj *$newobj) Build() (*$NewObj, error) {
$obj = $obj.Validate()
return $obj, $obj.err
}
`
var SetVarTemplate = `
// Set$Var method set the $Var field of $NewObj object.
func ($obj *$newobj) Set$Var($var $iType) {
$obj.$var = $var
}
`
var GetVarTemplate = `
// Get$Var method get the $Var field of $NewObj object.
func ($obj *$newobj) Get$Var() $iType {
return $obj.$var
}
`
var PredicateVarTemplate = `
// IsSet$Var method check if the $Var field of $NewObj object is set.
func Is$VarSet() PredicateFunc {
return func($obj *$newobj) bool {
return $cond
}
}
`
var PredicateTemplate = `
// PredicateFunc defines data-type for validation function
type PredicateFunc func(*$newobj) bool
`
var BuilderFieldTemplate = `
// predicatelist is list of predicate function used for validating object
predicatelist []PredicateFunc
// error
err error
`
var pkgTemplate = `
package $package
`