-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroup.go
More file actions
181 lines (150 loc) · 2.97 KB
/
Copy pathgroup.go
File metadata and controls
181 lines (150 loc) · 2.97 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package gg
import (
"fmt"
"io"
"os"
)
func NewGroup() *group {
return newGroup("", "", "\n")
}
func newGroup(open, close, sep string) *group {
return &group{
open: open,
close: close,
separator: sep,
}
}
type group struct {
items []Node
open string
close string
separator string
// NewIf this result is true, we will omit the wrap like `()`, `{}`.
omitWrapIf func() bool
}
func (g *group) length() int {
return len(g.items)
}
func (g *group) shouldOmitWrap() bool {
if g.omitWrapIf == nil {
return false
}
return g.omitWrapIf()
}
func (g *group) append(node ...interface{}) *group {
if len(node) == 0 {
return g
}
g.items = append(g.items, parseNodes(node)...)
return g
}
func (g *group) render(w io.Writer) {
if g.open != "" && !g.shouldOmitWrap() {
writeString(w, g.open)
}
isfirst := true
for _, node := range g.items {
if !isfirst {
writeString(w, g.separator)
}
node.render(w)
isfirst = false
}
if g.close != "" && !g.shouldOmitWrap() {
writeString(w, g.close)
}
}
func (g *group) Write(w io.Writer) {
g.render(w)
}
func (g *group) WriteFile(path string) error {
file, err := os.Create(path)
if err != nil {
return fmt.Errorf("create file %s: %s", path, err)
}
g.render(file)
return nil
}
func (g *group) AppendFile(path string) error {
file, err := os.OpenFile(path, os.O_APPEND|os.O_RDWR, 0644)
if err != nil {
return fmt.Errorf("create file %s: %s", path, err)
}
g.render(file)
return nil
}
func (g *group) String() string {
buf := pool.Get()
defer buf.Free()
g.render(buf)
return buf.String()
}
func (g *group) AddLineComment(content string, args ...interface{}) *group {
g.append(LineComment(content, args...))
return g
}
func (g *group) AddPackage(name string) *group {
g.append(Package(name))
return g
}
func (g *group) AddLine() *group {
g.append(Line())
return g
}
func (g *group) AddString(content string, args ...interface{}) *group {
g.append(S(content, args...))
return g
}
func (g *group) AddType(name string, typ interface{}) *group {
g.append(Type(name, typ))
return g
}
func (g *group) AddTypeAlias(name string, typ interface{}) *group {
g.append(TypeAlias(name, typ))
return g
}
func (g *group) NewImport() *iimport {
i := Import()
g.append(i)
return i
}
func (g *group) NewIf(judge interface{}) *iif {
i := If(judge)
g.append(i)
return i
}
func (g *group) NewFor(judge interface{}) *ifor {
i := For(judge)
g.append(i)
return i
}
func (g *group) NewSwitch(judge interface{}) *iswitch {
i := Switch(judge)
g.append(i)
return i
}
func (g *group) NewVar() *ivar {
i := Var()
g.append(i)
return i
}
func (g *group) NewConst() *iconst {
i := Const()
g.append(i)
return i
}
func (g *group) NewFunction(name string) *ifunction {
f := Function(name)
g.append(f)
return f
}
func (g *group) NewStruct(name string) *istruct {
i := Struct(name)
g.append(i)
return i
}
func (g *group) NewInterface(name string) *iinterface {
i := Interface(name)
g.append(i)
return i
}