-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgoaccessor.go
287 lines (269 loc) · 8.19 KB
/
goaccessor.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Goaccessor provides a Go tool designed to automate the generation of getter and setter boilerplate
// code for your data types and variables.
//
// Usage:
//
// go install github.com/yujiachen-y/goaccessor@latest
//
// After installing goaccessor, you can use it in the command line interface (CLI) or with //go:generate directives.
//
// Examples:
//
// Consider the file book.go:
//
// package main
//
// //go:generate goaccessor --target Book --getter --setter
// type Book struct {
// Title string
// Author string
// }
//
// When we run go generate, it creates a new file book_goaccessor.go:
//
// package main
//
// func (b *Book) GetTitle() string {
// return b.Title
// }
//
// func (b *Book) SetTitle(title string) {
// b.Title = title
// }
//
// func (b *Book) GetAuthor() string {
// return b.Author
// }
//
// func (b *Book) SetAuthor(author string) {
// b.Author = author
// }
//
// goaccessor isn't just for struct types; it can also handle top-level constants and variables. For instance:
//
// //go:generate goaccessor --target books --getter --setter
// var books = map[string]*Book {
// ...
// }
//
// After executing go generate, we get:
//
// func GetBooks() map[string]*Book {
// return books
// }
//
// func SetBooks(newBooks map[string]*Book) {
// books = newBooks
// }
//
// In certain cases, you might want to export specific fields of a top-level variable with a prefix:
//
// //go:generate goaccessor --target bestSellingBook --field --getter --include Author --prefix BestSelling
// var bestSellingBook = &Book{ ... }
//
// This directive will generate:
//
// func GetBestSellingAuthor() string {
// return bestSellingBook.Author
// }
//
// Options:
//
// Here are the available options for goaccessor:
//
// --target | -t: Specify the target to be handled.
// --getter | -g: Generate getter for the target.
// --setter | -s: Generate setter for the target.
// --accessor | -a: Generate both getter and setter for the target.
// --prefix | -p: Add a prefix to the generated methods/functions.
// --field | -f: Apply the flag (getter, setter, accessor) to each field of the target (only applicable for struct type variables).
// --include | -i: Generate methods only for the specified fields (fields should be comma-separated).
// --exclude | -e: Exclude specified fields from method generation (fields should be comma-separated).
//
// Dependency Management:
//
// If you do not want to install goaccessor and want to use it as a dependency for your project, follow these steps:
//
// 1. Go to your project directory and add the goaccessor dependency via go mod:
//
// go get github.com/yujiachen-y/goaccessor@latest
//
// 2. Create a new file named tools.go (or any other name you like) with the following code:
//
// //go:build tools
//
// package main
//
// import (
// _ "github.com/yujiachen-y/goaccessor"
// )
//
// 3. If you want to use the goaccessor CLI command, use go run github.com/yujiachen-y/goaccessor@latest instead.
//
// 4. If you use go:generate directives to generate code, change the directives like this:
//
// //go:generate go run github.com/yujiachen-y/goaccessor@latest -target book
// var book Book
//
// 5. Note: even though goaccessor is a dependency of your project, it will not—and should not—be a part of your project's build result.
// We use a build flag in tools.go to ensure goaccessor is ignored during build.
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)
var debug *log.Logger
func setupLogger() {
if os.Getenv("DEBUG") != "" {
debug = log.New(os.Stderr, "DEBUG: ", log.Ldate|log.Ltime|log.Lshortfile)
} else {
debug = log.New(ioutil.Discard, "", 0)
}
log.SetFlags(0)
log.SetPrefix("goaccessor: ")
}
var (
flagTargets []string
flagGetter bool
flagSetter bool
flagPureGetter bool
flagField bool
flagPrefix string
flagIncludes []string
flagExcludes []string
argDir string
)
func parseFlags() {
t := flag.String("t", "", "")
target := flag.String("target", "", "")
g := flag.Bool("g", false, "")
getter := flag.Bool("getter", false, "")
s := flag.Bool("s", false, "")
setter := flag.Bool("setter", false, "")
a := flag.Bool("a", false, "")
accessor := flag.Bool("accessor", false, "")
pg := flag.Bool("pg", false, "")
pureGetter := flag.Bool("pure-getter", false, "")
f := flag.Bool("f", false, "")
field := flag.Bool("field", false, "")
p := flag.String("p", "", "")
prefix := flag.String("prefix", "", "")
i := flag.String("i", "", "")
include := flag.String("include", "", "")
e := flag.String("e", "", "")
exclude := flag.String("exclude", "", "")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of goaccessor:\n")
fmt.Fprintf(os.Stderr, "\t--target -t string\n")
fmt.Fprintf(os.Stderr, "\t\tSpecify the target to be handled.\n")
fmt.Fprintf(os.Stderr, "\t--getter -g getter\n")
fmt.Fprintf(os.Stderr, "\t\tGenerate `getter` for the target.\n")
fmt.Fprintf(os.Stderr, "\t--setter -s getter\n")
fmt.Fprintf(os.Stderr, "\t\tGenerate `setter` for the target.\n")
fmt.Fprintf(os.Stderr, "\t--accessor -a getter\n")
fmt.Fprintf(os.Stderr, "\t\tGenerate `accessor` for the target.\n")
fmt.Fprintf(os.Stderr, "\t--pure-getter -pg getter\n")
fmt.Fprintf(os.Stderr, "\t\tGenerate `getter` without 'Get' prefix for the target.\n")
fmt.Fprintf(os.Stderr, "\t--field -f getter\n")
fmt.Fprintf(os.Stderr, "\t\tApply the command (`getter`, `setter`, `accessor`) to each field of the target (only works for struct type variables).\n")
fmt.Fprintf(os.Stderr, "\t--prefix -p string\n")
fmt.Fprintf(os.Stderr, "\t\tAdd a prefix to the generated methods/functions.\n")
fmt.Fprintf(os.Stderr, "\t--include -i string\n")
fmt.Fprintf(os.Stderr, "\t\tGenerate methods only for the specified fields (fields should be comma-separated).\n")
fmt.Fprintf(os.Stderr, "\t--exclude -e string\n")
fmt.Fprintf(os.Stderr, "\t\tExclude specified fields from method generation (fields should be comma-separated).\n")
fmt.Fprintf(os.Stderr, "For more information, see:\n")
fmt.Fprintf(os.Stderr, "\thttps://www.github.com/yujiachen-y/goaccessor\n")
}
flag.Parse()
if len(*t) != 0 {
flagTargets = strings.Split(*t, ",")
} else if len(*target) != 0 {
flagTargets = strings.Split(*target, ",")
}
if len(flagTargets) == 0 {
flag.Usage()
os.Exit(2)
}
flagGetter = *g || *getter
flagSetter = *s || *setter
if *a || *accessor {
flagGetter = true
flagSetter = true
}
if *pg || *pureGetter {
flagGetter = true
flagPureGetter = true
}
if !flagGetter && !flagSetter {
flag.Usage()
os.Exit(2)
}
flagField = *f || *field
if *p != "" {
flagPrefix = *p
} else if *prefix != "" {
flagPrefix = *prefix
}
if len(*i) != 0 {
flagIncludes = strings.Split(*i, ",")
} else if len(*include) != 0 {
flagIncludes = strings.Split(*include, ",")
}
if len(*e) != 0 {
flagExcludes = strings.Split(*e, ",")
} else if len(*exclude) != 0 {
flagExcludes = strings.Split(*exclude, ",")
}
args := flag.Args()
if len(args) == 0 {
args = []string{"."}
}
path := args[0]
pathInfo, err := os.Stat(path)
if err != nil {
log.Fatal(err)
}
if pathInfo.IsDir() {
argDir = path
} else {
argDir = filepath.Dir(path)
}
}
func main() {
setupLogger()
parseFlags()
debug.Printf("Received arguments:\n")
debug.Printf("\t\tflagTargets %s\n", flagTargets)
debug.Printf("\t\tflagGetter %t\n", flagGetter)
debug.Printf("\t\tflagSetter %t\n", flagSetter)
debug.Printf("\t\tflagPureGetter %t\n", flagPureGetter)
debug.Printf("\t\tflagField %t\n", flagField)
debug.Printf("\t\tflagPrefix %s\n", flagPrefix)
debug.Printf("\t\tflagIncludes %s\n", flagIncludes)
debug.Printf("\t\tflagExcludes %s\n", flagExcludes)
debug.Printf("\t\targDir %s\n", argDir)
generators, err := NewGenerators(flagTargets, argDir, flagField)
if err != nil {
log.Fatalf("Failed to create generators, error: %s", err.Error())
}
for _, generator := range generators {
log.Printf("generate %s ...\n", generator.Name)
err := generator.Generate(
WithGetter(flagGetter),
WithSetter(flagSetter),
WithPureGetter(flagPureGetter),
WithPrefix(flagPrefix),
WithIncludes(flagIncludes),
WithExcludes(flagExcludes),
)
if err != nil {
log.Fatalf("Failed to generate, error: %s", err.Error())
}
}
}