From c55a706513b8d9c96486d6db7f634a8163348cc3 Mon Sep 17 00:00:00 2001 From: Daved Date: Thu, 23 Apr 2015 09:50:48 -0700 Subject: [PATCH] Added prefix flag, and updated file name output. Changed parser package's ParsePackage to take prefix and updated related skip conditional. Also updated args in call to ParsePackage within server. Last paragraph of main package comment was updated, along with the second to last paragraph of readme. --- README.md | 3 ++- jsonenums.go | 13 ++++++++----- parser/parser.go | 5 +++-- server/server.go | 2 +- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 230411e..712ae30 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,7 @@ source files that represent a single Go package. The `-type` flag accepts a comma-separated list of types so a single run can generate methods for multiple types. The default output file is t_jsonenums.go, where t is the lower-cased name of the first type listed. The suffix can be -overridden with the `-suffix` flag. +overridden with the `-suffix` flag and a prefix may be added with the `-prefix` +flag. This is not an official Google product (experimental or otherwise), it is just code that happens to be owned by Google. diff --git a/jsonenums.go b/jsonenums.go index 8b06cb7..1b9c3bf 100644 --- a/jsonenums.go +++ b/jsonenums.go @@ -58,9 +58,10 @@ // or a set of Go source files that represent a single Go package. // // The -type flag accepts a comma-separated list of types so a single run can -// generate methods for multiple types. The default output file is t_string.go, -// where t is the lower-cased name of the first type listed. The suffix can be -// overridden with the -suffix flag. +// generate methods for multiple types. The default output file is +// t_jsonenums.go, where t is the lower-cased name of the first type listed. +// The suffix can be overridden with the -suffix flag and a prefix may be added +// with the -prefix flag. // package main @@ -79,6 +80,7 @@ import ( var ( typeNames = flag.String("type", "", "comma-separated list of type names; must be set") + outputPrefix = flag.String("prefix", "", "prefix to be added to the output file") outputSuffix = flag.String("suffix", "_jsonenums", "suffix to be added to the output file") ) @@ -97,7 +99,7 @@ func main() { log.Fatalf("only one directory at a time") } - pkg, err := parser.ParsePackage(dir, *outputSuffix+".go") + pkg, err := parser.ParsePackage(dir, *outputPrefix, *outputSuffix+".go") if err != nil { log.Fatalf("parsing package: %v", err) } @@ -134,7 +136,8 @@ func main() { src = buf.Bytes() } - output := strings.ToLower(typeName + *outputSuffix + ".go") + output := strings.ToLower(*outputPrefix + typeName + + *outputSuffix + ".go") outputPath := filepath.Join(dir, output) if err := ioutil.WriteFile(outputPath, src, 0644); err != nil { log.Fatalf("writing output: %s", err) diff --git a/parser/parser.go b/parser/parser.go index b9c2dc3..f886826 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -30,7 +30,7 @@ type Package struct { } // ParsePackage parses the package in the given directory and returns it. -func ParsePackage(directory string, skipSuffix string) (*Package, error) { +func ParsePackage(directory, skipPrefix, skipSuffix string) (*Package, error) { pkgDir, err := build.Default.ImportDir(directory, 0) if err != nil { return nil, fmt.Errorf("cannot process directory %s: %s", directory, err) @@ -40,7 +40,8 @@ func ParsePackage(directory string, skipSuffix string) (*Package, error) { fs := token.NewFileSet() for _, name := range pkgDir.GoFiles { if !strings.HasSuffix(name, ".go") || - (skipSuffix != "" && strings.HasSuffix(name, skipSuffix)) { + (skipSuffix != "" && strings.HasPrefix(name, skipPrefix) && + strings.HasSuffix(name, skipSuffix)) { continue } if directory != "." { diff --git a/server/server.go b/server/server.go index 4ad8e16..20e5b8b 100644 --- a/server/server.go +++ b/server/server.go @@ -47,7 +47,7 @@ func generateHandler(w http.ResponseWriter, r *http.Request) error { } defer os.RemoveAll(dir) - pkg, err := parser.ParsePackage(dir, "") + pkg, err := parser.ParsePackage(dir, "", "") if err != nil { return fmt.Errorf("parse package: %v", err) }