From a3cb65548057ce2a991e02ccd346a14e3d73dbeb Mon Sep 17 00:00:00 2001 From: Daved Date: Mon, 6 Apr 2015 20:38:10 -0700 Subject: [PATCH 1/6] Corrected minor typo in second to last paragraph of readme. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9c827cf..230411e 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,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 +where t is the lower-cased name of the first type listed. The suffix can be overridden with the `-suffix` flag. This is not an official Google product (experimental or otherwise), it is just code that happens to be owned by Google. From 362f6fe4ecd0f96a9778a43f0d45b963ab14b6b5 Mon Sep 17 00:00:00 2001 From: Daved Date: Mon, 6 Apr 2015 20:47:20 -0700 Subject: [PATCH 2/6] Added MarshalJSON and UnmarshalJSON comments to template to prevent golint from complaining. --- template.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/template.go b/template.go index f937a6f..8280536 100644 --- a/template.go +++ b/template.go @@ -42,6 +42,7 @@ func init() { } } +// MarshalJSON returns *r as the JSON encoding of r. func (r {{$typename}}) MarshalJSON() ([]byte, error) { if s, ok := interface{}(r).(fmt.Stringer); ok { return json.Marshal(s.String()) @@ -53,6 +54,7 @@ func (r {{$typename}}) MarshalJSON() ([]byte, error) { return json.Marshal(s) } +// UnmarshalJSON sets *r to a copy of data. func (r *{{$typename}}) UnmarshalJSON(data []byte) error { var s string if err := json.Unmarshal(data, &s); err != nil { From e9a38094fb1e46963e6b3475202490a6824fa32a Mon Sep 17 00:00:00 2001 From: Daved Date: Mon, 6 Apr 2015 20:56:34 -0700 Subject: [PATCH 3/6] Corrected minor typo in last paragraph of package main comment. --- jsonenums.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsonenums.go b/jsonenums.go index d4bd574..8b06cb7 100644 --- a/jsonenums.go +++ b/jsonenums.go @@ -59,7 +59,7 @@ // // 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 +// where t is the lower-cased name of the first type listed. The suffix can be // overridden with the -suffix flag. // package main From 02f6b2afc6d9971e04c97497eb538c5fdabe2760 Mon Sep 17 00:00:00 2001 From: Daved Date: Mon, 6 Apr 2015 21:33:43 -0700 Subject: [PATCH 4/6] Added prefix flag, and changed file name output accordingly. Also changed parser package ParsePackage() to take prefix and updated skip conditional as needed. Last paragraph of main package comment was updated, along with the second to last paragraph of readme. Due to confusion about import paths, the parser package import path was left pointing to original repo with the hope that the author will pull these changes. Otherwise, the import path will need to be changed back to the forked repo to work properly. --- README.md | 3 ++- jsonenums.go | 13 ++++++++----- parser/parser.go | 5 +++-- 3 files changed, 13 insertions(+), 8 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 != "." { From 827bbf1143e43a6951c79d7ecd5763949f483e5c Mon Sep 17 00:00:00 2001 From: Daved Date: Mon, 6 Apr 2015 21:52:29 -0700 Subject: [PATCH 5/6] Fixed arguments of ParsePackage() in server to coincide with changes made to parser. This was missed due to the previously mentioned disparity in import paths not causing install to break as would be expected. --- server/server.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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) } From b02f877c48b8afa28f6b40a6b8c89f003e8bb204 Mon Sep 17 00:00:00 2001 From: Daved Date: Tue, 7 Apr 2015 10:40:45 -0700 Subject: [PATCH 6/6] Fixed comments for MarshalJSON() and UnmarshalJSON() in template. --- template.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/template.go b/template.go index 8280536..118fa3d 100644 --- a/template.go +++ b/template.go @@ -42,7 +42,7 @@ func init() { } } -// MarshalJSON returns *r as the JSON encoding of r. +// MarshalJSON is generated; DO NOT EDIT func (r {{$typename}}) MarshalJSON() ([]byte, error) { if s, ok := interface{}(r).(fmt.Stringer); ok { return json.Marshal(s.String()) @@ -54,7 +54,7 @@ func (r {{$typename}}) MarshalJSON() ([]byte, error) { return json.Marshal(s) } -// UnmarshalJSON sets *r to a copy of data. +// UnmarshalJSON is generated; DO NOT EDIT func (r *{{$typename}}) UnmarshalJSON(data []byte) error { var s string if err := json.Unmarshal(data, &s); err != nil {