Skip to content

Commit

Permalink
Merge branch 'master' into comment_lits
Browse files Browse the repository at this point in the history
  • Loading branch information
p4l1ly authored Sep 25, 2020
2 parents 0cca585 + 3e22f1a commit 72164b8
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Flags:
-in="": file to parse instead of stdin
-out="": file to save output to instead of stdout
-pkg="": package name for generated files
-tag="": build tag that is stripped from output
```

* Comma separated type lists will generate code for each type
Expand Down
11 changes: 6 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func main() {
in = flag.String("in", "", "file to parse instead of stdin")
out = flag.String("out", "", "file to save output to instead of stdout")
pkgName = flag.String("pkg", "", "package name for generated files")
genTag = flag.String("tag", "", "build tag that is stripped from output")
prefix = "https://github.com/metabition/gennylib/raw/master/"
)
flag.Parse()
Expand Down Expand Up @@ -83,23 +84,23 @@ func main() {
}
r.Body.Close()
br := bytes.NewReader(b)
err = gen(*in, outputFilename, *pkgName, br, typeSets, outWriter)
err = gen(*in, outputFilename, *pkgName, *genTag, br, typeSets, outWriter)
} else if len(*in) > 0 {
var file *os.File
file, err = os.Open(*in)
if err != nil {
fatal(exitcodeSourceFileInvalid, err)
}
defer file.Close()
err = gen(*in, outputFilename, *pkgName, file, typeSets, outWriter)
err = gen(*in, outputFilename, *pkgName, *genTag, file, typeSets, outWriter)
} else {
var source []byte
source, err = ioutil.ReadAll(os.Stdin)
if err != nil {
fatal(exitcodeStdinFailed, err)
}
reader := bytes.NewReader(source)
err = gen("stdin", outputFilename, *pkgName, reader, typeSets, outWriter)
err = gen("stdin", outputFilename, *pkgName, *genTag, reader, typeSets, outWriter)
}

// do the work
Expand Down Expand Up @@ -143,12 +144,12 @@ func fatal(code int, a ...interface{}) {
}

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

var output []byte
var err error

output, err = parse.Generics(filename, outputFilename, pkgName, in, typesets)
output, err = parse.Generics(filename, outputFilename, pkgName, tag, in, typesets)
if err != nil {
return err
}
Expand Down
12 changes: 9 additions & 3 deletions parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,13 @@ func generateSpecific(filename string, in io.ReadSeeker, typeSet map[string]stri

// Generics parses the source file and generates the bytes replacing the
// generic types for the keys map with the specific types (its value).
func Generics(filename, outputFilename, pkgName string, in io.ReadSeeker, typeSets []map[string]string) ([]byte, error) {
func Generics(filename, outputFilename, pkgName, tag string, in io.ReadSeeker, typeSets []map[string]string) ([]byte, error) {
var localUnwantedLinePrefixes [][]byte
localUnwantedLinePrefixes = append(localUnwantedLinePrefixes, unwantedLinePrefixes...)

if tag != "" {
localUnwantedLinePrefixes = append(localUnwantedLinePrefixes, []byte(fmt.Sprintf("// +build %s", tag)))
}

totalOutput := header

Expand Down Expand Up @@ -216,9 +222,9 @@ func Generics(filename, outputFilename, pkgName string, in io.ReadSeeker, typeSe
continue
}

// check all unwantedLinePrefixes - and skip them
// check all localUnwantedLinePrefixes - and skip them
skipline := false
for _, prefix := range unwantedLinePrefixes {
for _, prefix := range localUnwantedLinePrefixes {
if bytes.HasPrefix(scanner.Bytes(), prefix) {
skipline = true
continue
Expand Down
17 changes: 16 additions & 1 deletion parse/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var tests = []struct {
outputFilename string
pkgName string
in string
tag string
types []map[string]string

// expectations
Expand Down Expand Up @@ -115,6 +116,20 @@ var tests = []struct {
in: `test/bugreports/comment_generic.go`,
types: []map[string]string{{"SomeThing": "int"}},
expectedOut: `test/bugreports/comment_int.go`,
},
{
filename: "buildtags.go",
in: `test/buildtags/buildtags.go`,
types: []map[string]string{{"_t_": "int"}},
expectedOut: `test/buildtags/buildtags_expected.go`,
tag: "genny",
},
{
filename: "buildtags.go",
in: `test/buildtags/buildtags.go`,
types: []map[string]string{{"_t_": "string"}},
expectedOut: `test/buildtags/buildtags_expected_nostrip.go`,
tag: "",
},
}

Expand All @@ -125,7 +140,7 @@ func TestParse(t *testing.T) {
test.in = contents(test.in)
test.expectedOut = contents(test.expectedOut)

bytes, err := parse.Generics(test.filename, test.outputFilename, test.pkgName, strings.NewReader(test.in), test.types)
bytes, err := parse.Generics(test.filename, test.outputFilename, test.pkgName, test.tag, strings.NewReader(test.in), test.types)

// check the error
if test.expectedErr == nil {
Expand Down
16 changes: 16 additions & 0 deletions parse/test/buildtags/buildtags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// +build x,y z
// +build genny

package buildtags

import (
"fmt"

"github.com/cheekybits/genny/generic"
)

type _t_ generic.Type

func _t_Print(t _t_) {
fmt.Println(t)
}
13 changes: 13 additions & 0 deletions parse/test/buildtags/buildtags_expected.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// This file was automatically generated by genny.
// Any changes will be lost if this file is regenerated.
// see https://github.com/cheekybits/genny

// +build x,y z

package buildtags

import "fmt"

func intPrint(t int) {
fmt.Println(t)
}
14 changes: 14 additions & 0 deletions parse/test/buildtags/buildtags_expected_nostrip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// This file was automatically generated by genny.
// Any changes will be lost if this file is regenerated.
// see https://github.com/cheekybits/genny

// +build x,y z
// +build genny

package buildtags

import "fmt"

func stringPrint(t string) {
fmt.Println(t)
}

0 comments on commit 72164b8

Please sign in to comment.