-
Notifications
You must be signed in to change notification settings - Fork 0
/
funcs.go
146 lines (121 loc) · 2.93 KB
/
funcs.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package gogen
import (
"bytes"
"context"
"fmt"
"strings"
)
type FuncBuilder struct {
receiver string
sig string
vars []string
blocks []Code
funcSig *FuncSigBuilder
}
func Func(sig string, vars ...string) (r *FuncBuilder) {
r = &FuncBuilder{}
r.sig = sig
r.vars = vars
return
}
func (b *FuncBuilder) BodySnippet(template string, vars ...string) (r *FuncBuilder) {
b.Body(Snippet(template, vars...))
return b
}
func (b *FuncBuilder) Receiver(varName string, typ string) (r *FuncBuilder) {
b.receiver = fmt.Sprintf("func (%s %s)", varName, typ)
return b
}
func (b *FuncBuilder) Sig(sig *FuncSigBuilder) (r *FuncBuilder) {
b.funcSig = sig
return b
}
func (b *FuncBuilder) Body(blocks ...Code) (r *FuncBuilder) {
b.blocks = append(b.blocks, blocks...)
return b
}
func (b *FuncBuilder) MarshalCode(ctx context.Context) (r []byte, err error) {
buf := bytes.NewBuffer(nil)
if b.funcSig != nil {
err = Fprint(buf, b.funcSig, ctx)
if err != nil {
return
}
} else {
if strings.Index(b.sig, "func") != 0 {
if len(b.receiver) > 0 {
b.sig = fmt.Sprintf("%s %s", b.receiver, b.sig)
} else {
b.sig = fmt.Sprintf("func %s", b.sig)
}
}
err = Fprint(buf, Snippet(b.sig, b.vars...), ctx)
if err != nil {
return
}
}
buf.WriteString(" {\n")
err = Fprint(buf, Snippets(b.blocks...), ctx)
if err != nil {
return
}
buf.WriteString("}\n\n")
r = buf.Bytes()
return
}
type FuncSigBuilder struct {
name string
receiverVar string
receiverTyp string
parameters []string
results []string
}
func FuncSig(name string) (r *FuncSigBuilder) {
r = &FuncSigBuilder{}
r.name = name
return
}
func (b *FuncSigBuilder) Parameters(vs ...string) (r *FuncSigBuilder) {
b.parameters = append(b.parameters, vs...)
return b
}
func (b *FuncSigBuilder) Results(vs ...string) (r *FuncSigBuilder) {
b.results = append(b.results, vs...)
return b
}
func (b *FuncSigBuilder) Receiver(varName string, typ string) (r *FuncSigBuilder) {
b.receiverVar = varName
b.receiverTyp = typ
return b
}
func (b *FuncSigBuilder) MarshalCode(ctx context.Context) (r []byte, err error) {
buf := bytes.NewBuffer(nil)
buf.WriteString("func ")
if len(b.receiverTyp) > 0 {
if len(b.receiverVar) == 0 {
b.receiverVar = "this"
}
buf.WriteString(fmt.Sprintf("(%s %s) ", b.receiverVar, b.receiverTyp))
}
if len(b.parameters)%2 != 0 {
b.parameters = append(b.parameters, "interface{}")
}
params := []string{}
for i := 0; i < len(b.parameters); i = i + 2 {
params = append(params, fmt.Sprintf("%s %s", b.parameters[i], b.parameters[i+1]))
}
buf.WriteString(b.name + " (")
buf.WriteString(strings.Join(params, ", "))
buf.WriteString(")")
if len(b.results) > 0 {
buf.WriteString("(")
rs := []string{}
for i := 0; i < len(b.results); i = i + 2 {
rs = append(rs, fmt.Sprintf("%s %s", b.results[i], b.results[i+1]))
}
buf.WriteString(strings.Join(rs, ", "))
buf.WriteString(")")
}
r = buf.Bytes()
return
}