Skip to content

Commit 8e91241

Browse files
authored
Allow commandline directives (#401)
Make it possible to add directives as commandline parameter. Alternatively to `//msgp:tag json` directives can also be specified using the `-d` flag. Example: `msgp -d "tag json"`. Multiple flags can be added. Directives in files take precedence over commandline. Bonus: Print info on all directives.
1 parent 60fdfdb commit 8e91241

File tree

5 files changed

+32
-10
lines changed

5 files changed

+32
-10
lines changed

_generated/compactfloats.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
package _generated
22

3-
//go:generate msgp
4-
5-
//msgp:compactfloats
3+
//go:generate msgp -d "msgp:compactfloats" -d "replace F64 with:float64" -v
64

75
//msgp:ignore F64
86
type F64 float64
97

10-
//msgp:replace F64 with:float64
11-
128
type Floats struct {
139
A float64
1410
B float32

_generated/custom_tag.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package _generated
22

3-
//go:generate msgp
4-
//msgp:tag mytag
3+
//go:generate msgp -d "tag mytag"
54

65
type CustomTag struct {
76
Foo string `mytag:"foo_custom_name"`

main.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ var (
4141
tests = flag.Bool("tests", true, "create tests and benchmarks")
4242
unexported = flag.Bool("unexported", false, "also process unexported types")
4343
verbose = flag.Bool("v", false, "verbose diagnostics")
44+
directives = stringArrFlags{}
4445
)
4546

4647
func diagf(f string, args ...interface{}) {
@@ -59,12 +60,17 @@ func exitln(res string) {
5960
}
6061

6162
func main() {
63+
flag.Var(&directives, "d", "apply directive to all files. Multiple -d flags allowed. 'msgp:' can be omitted")
6264
flag.Parse()
6365

6466
if *verbose {
6567
printer.Logf = diagf
6668
parse.Logf = diagf
6769
}
70+
for i, v := range directives {
71+
// Trim prefix and whitespace, if any.
72+
directives[i] = strings.TrimPrefix(strings.TrimSpace(v), "msgp:")
73+
}
6874

6975
// GOFILE is set by go generate
7076
if *file == "" {
@@ -102,7 +108,7 @@ func Run(gofile string, mode gen.Method, unexported bool) error {
102108
return nil
103109
}
104110
diagf("Input: \"%s\"\n", gofile)
105-
fs, err := parse.File(gofile, unexported)
111+
fs, err := parse.File(gofile, unexported, directives)
106112
if err != nil {
107113
return err
108114
}
@@ -130,3 +136,17 @@ func newFilename(old string, pkg string) string {
130136
// new file name is old file name + _gen.go
131137
return strings.TrimSuffix(old, ".go") + "_gen.go"
132138
}
139+
140+
// stringArrFlags is a flag.Value that accepts multiple values
141+
type stringArrFlags []string
142+
143+
// String is an implementation of the flag.Value interface
144+
func (i *stringArrFlags) String() string {
145+
return fmt.Sprintf("%v", *i)
146+
}
147+
148+
// Set is an implementation of the flag.Value interface
149+
func (i *stringArrFlags) Set(value string) error {
150+
*i = append(*i, value)
151+
return nil
152+
}

parse/directives.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,29 +219,34 @@ func tag(text []string, f *FileSet) error {
219219
return nil
220220
}
221221
f.tagName = strings.TrimSpace(text[1])
222+
infof("using field tag %q\n", f.tagName)
222223
return nil
223224
}
224225

225226
//msgp:pointer
226227
func pointer(text []string, f *FileSet) error {
228+
infof("using pointer receiver\n")
227229
f.pointerRcv = true
228230
return nil
229231
}
230232

231233
//msgp:compactfloats
232234
func compactfloats(text []string, f *FileSet) error {
235+
infof("using compact floats\n")
233236
f.CompactFloats = true
234237
return nil
235238
}
236239

237240
//msgp:clearomitted
238241
func clearomitted(text []string, f *FileSet) error {
242+
infof("clearing omitted fields\n")
239243
f.ClearOmitted = true
240244
return nil
241245
}
242246

243247
//msgp:newtime
244248
func newtime(text []string, f *FileSet) error {
249+
infof("using new time encoding\n")
245250
f.NewTime = true
246251
return nil
247252
}
@@ -307,5 +312,6 @@ func newtimezone(text []string, f *FileSet) error {
307312
default:
308313
return fmt.Errorf("timezone directive should be either 'local' or 'utc'; found %q", text[1])
309314
}
315+
infof("using timezone %q\n", text[1])
310316
return nil
311317
}

parse/getast.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ type FileSet struct {
3939
// directory will be parsed.
4040
// If unexport is false, only exported identifiers are included in the FileSet.
4141
// If the resulting FileSet would be empty, an error is returned.
42-
func File(name string, unexported bool) (*FileSet, error) {
42+
func File(name string, unexported bool, directives []string) (*FileSet, error) {
4343
pushstate(name)
4444
defer popstate()
4545
fs := &FileSet{
4646
Specs: make(map[string]ast.Expr),
4747
Identities: make(map[string]gen.Elem),
48+
Directives: append([]string{}, directives...),
4849
}
4950

5051
fset := token.NewFileSet()
@@ -81,7 +82,7 @@ func File(name string, unexported bool) (*FileSet, error) {
8182
return nil, err
8283
}
8384
fs.Package = f.Name.Name
84-
fs.Directives = yieldComments(f.Comments)
85+
fs.Directives = append(fs.Directives, yieldComments(f.Comments)...)
8586
if !unexported {
8687
ast.FileExports(f)
8788
}

0 commit comments

Comments
 (0)