Skip to content

Commit a9a6a04

Browse files
p4l1lyp4l1ly
authored andcommitted
Merge branch 'master' into comment_lits
2 parents 0cca585 + 3e22f1a commit a9a6a04

File tree

7 files changed

+75
-9
lines changed

7 files changed

+75
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Flags:
4949
-in="": file to parse instead of stdin
5050
-out="": file to save output to instead of stdout
5151
-pkg="": package name for generated files
52+
-tag="": build tag that is stripped from output
5253
```
5354

5455
* Comma separated type lists will generate code for each type

main.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func main() {
3636
in = flag.String("in", "", "file to parse instead of stdin")
3737
out = flag.String("out", "", "file to save output to instead of stdout")
3838
pkgName = flag.String("pkg", "", "package name for generated files")
39+
genTag = flag.String("tag", "", "build tag that is stripped from output")
3940
prefix = "https://github.com/metabition/gennylib/raw/master/"
4041
)
4142
flag.Parse()
@@ -83,23 +84,23 @@ func main() {
8384
}
8485
r.Body.Close()
8586
br := bytes.NewReader(b)
86-
err = gen(*in, outputFilename, *pkgName, br, typeSets, outWriter)
87+
err = gen(*in, outputFilename, *pkgName, *genTag, br, typeSets, outWriter)
8788
} else if len(*in) > 0 {
8889
var file *os.File
8990
file, err = os.Open(*in)
9091
if err != nil {
9192
fatal(exitcodeSourceFileInvalid, err)
9293
}
9394
defer file.Close()
94-
err = gen(*in, outputFilename, *pkgName, file, typeSets, outWriter)
95+
err = gen(*in, outputFilename, *pkgName, *genTag, file, typeSets, outWriter)
9596
} else {
9697
var source []byte
9798
source, err = ioutil.ReadAll(os.Stdin)
9899
if err != nil {
99100
fatal(exitcodeStdinFailed, err)
100101
}
101102
reader := bytes.NewReader(source)
102-
err = gen("stdin", outputFilename, *pkgName, reader, typeSets, outWriter)
103+
err = gen("stdin", outputFilename, *pkgName, *genTag, reader, typeSets, outWriter)
103104
}
104105

105106
// do the work
@@ -143,12 +144,12 @@ func fatal(code int, a ...interface{}) {
143144
}
144145

145146
// gen performs the generic generation.
146-
func gen(filename, outputFilename, pkgName string, in io.ReadSeeker, typesets []map[string]string, out io.Writer) error {
147+
func gen(filename, outputFilename, pkgName, tag string, in io.ReadSeeker, typesets []map[string]string, out io.Writer) error {
147148

148149
var output []byte
149150
var err error
150151

151-
output, err = parse.Generics(filename, outputFilename, pkgName, in, typesets)
152+
output, err = parse.Generics(filename, outputFilename, pkgName, tag, in, typesets)
152153
if err != nil {
153154
return err
154155
}

parse/parse.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,13 @@ func generateSpecific(filename string, in io.ReadSeeker, typeSet map[string]stri
172172

173173
// Generics parses the source file and generates the bytes replacing the
174174
// generic types for the keys map with the specific types (its value).
175-
func Generics(filename, outputFilename, pkgName string, in io.ReadSeeker, typeSets []map[string]string) ([]byte, error) {
175+
func Generics(filename, outputFilename, pkgName, tag string, in io.ReadSeeker, typeSets []map[string]string) ([]byte, error) {
176+
var localUnwantedLinePrefixes [][]byte
177+
localUnwantedLinePrefixes = append(localUnwantedLinePrefixes, unwantedLinePrefixes...)
178+
179+
if tag != "" {
180+
localUnwantedLinePrefixes = append(localUnwantedLinePrefixes, []byte(fmt.Sprintf("// +build %s", tag)))
181+
}
176182

177183
totalOutput := header
178184

@@ -216,9 +222,9 @@ func Generics(filename, outputFilename, pkgName string, in io.ReadSeeker, typeSe
216222
continue
217223
}
218224

219-
// check all unwantedLinePrefixes - and skip them
225+
// check all localUnwantedLinePrefixes - and skip them
220226
skipline := false
221-
for _, prefix := range unwantedLinePrefixes {
227+
for _, prefix := range localUnwantedLinePrefixes {
222228
if bytes.HasPrefix(scanner.Bytes(), prefix) {
223229
skipline = true
224230
continue

parse/parse_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var tests = []struct {
1616
outputFilename string
1717
pkgName string
1818
in string
19+
tag string
1920
types []map[string]string
2021

2122
// expectations
@@ -116,6 +117,20 @@ var tests = []struct {
116117
types: []map[string]string{{"SomeThing": "int"}},
117118
expectedOut: `test/bugreports/comment_int.go`,
118119
},
120+
{
121+
filename: "buildtags.go",
122+
in: `test/buildtags/buildtags.go`,
123+
types: []map[string]string{{"_t_": "int"}},
124+
expectedOut: `test/buildtags/buildtags_expected.go`,
125+
tag: "genny",
126+
},
127+
{
128+
filename: "buildtags.go",
129+
in: `test/buildtags/buildtags.go`,
130+
types: []map[string]string{{"_t_": "string"}},
131+
expectedOut: `test/buildtags/buildtags_expected_nostrip.go`,
132+
tag: "",
133+
},
119134
}
120135

121136
func TestParse(t *testing.T) {
@@ -125,7 +140,7 @@ func TestParse(t *testing.T) {
125140
test.in = contents(test.in)
126141
test.expectedOut = contents(test.expectedOut)
127142

128-
bytes, err := parse.Generics(test.filename, test.outputFilename, test.pkgName, strings.NewReader(test.in), test.types)
143+
bytes, err := parse.Generics(test.filename, test.outputFilename, test.pkgName, test.tag, strings.NewReader(test.in), test.types)
129144

130145
// check the error
131146
if test.expectedErr == nil {

parse/test/buildtags/buildtags.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// +build x,y z
2+
// +build genny
3+
4+
package buildtags
5+
6+
import (
7+
"fmt"
8+
9+
"github.com/cheekybits/genny/generic"
10+
)
11+
12+
type _t_ generic.Type
13+
14+
func _t_Print(t _t_) {
15+
fmt.Println(t)
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// This file was automatically generated by genny.
2+
// Any changes will be lost if this file is regenerated.
3+
// see https://github.com/cheekybits/genny
4+
5+
// +build x,y z
6+
7+
package buildtags
8+
9+
import "fmt"
10+
11+
func intPrint(t int) {
12+
fmt.Println(t)
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// This file was automatically generated by genny.
2+
// Any changes will be lost if this file is regenerated.
3+
// see https://github.com/cheekybits/genny
4+
5+
// +build x,y z
6+
// +build genny
7+
8+
package buildtags
9+
10+
import "fmt"
11+
12+
func stringPrint(t string) {
13+
fmt.Println(t)
14+
}

0 commit comments

Comments
 (0)