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.
This commit is contained in:
parent
e9a38094fb
commit
02f6b2afc6
@ -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
|
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,
|
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.
|
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.
|
This is not an official Google product (experimental or otherwise), it is just code that happens to be owned by Google.
|
||||||
|
13
jsonenums.go
13
jsonenums.go
@ -58,9 +58,10 @@
|
|||||||
// or a set of Go source files that represent a single Go package.
|
// 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
|
// 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,
|
// generate methods for multiple types. The default output file is
|
||||||
// where t is the lower-cased name of the first type listed. The suffix can be
|
// t_jsonenums.go, where t is the lower-cased name of the first type listed.
|
||||||
// overridden with the -suffix flag.
|
// The suffix can be overridden with the -suffix flag and a prefix may be added
|
||||||
|
// with the -prefix flag.
|
||||||
//
|
//
|
||||||
package main
|
package main
|
||||||
|
|
||||||
@ -79,6 +80,7 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
typeNames = flag.String("type", "", "comma-separated list of type names; must be set")
|
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")
|
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")
|
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 {
|
if err != nil {
|
||||||
log.Fatalf("parsing package: %v", err)
|
log.Fatalf("parsing package: %v", err)
|
||||||
}
|
}
|
||||||
@ -134,7 +136,8 @@ func main() {
|
|||||||
src = buf.Bytes()
|
src = buf.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
output := strings.ToLower(typeName + *outputSuffix + ".go")
|
output := strings.ToLower(*outputPrefix + typeName +
|
||||||
|
*outputSuffix + ".go")
|
||||||
outputPath := filepath.Join(dir, output)
|
outputPath := filepath.Join(dir, output)
|
||||||
if err := ioutil.WriteFile(outputPath, src, 0644); err != nil {
|
if err := ioutil.WriteFile(outputPath, src, 0644); err != nil {
|
||||||
log.Fatalf("writing output: %s", err)
|
log.Fatalf("writing output: %s", err)
|
||||||
|
@ -30,7 +30,7 @@ type Package struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ParsePackage parses the package in the given directory and returns it.
|
// 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)
|
pkgDir, err := build.Default.ImportDir(directory, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot process directory %s: %s", directory, err)
|
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()
|
fs := token.NewFileSet()
|
||||||
for _, name := range pkgDir.GoFiles {
|
for _, name := range pkgDir.GoFiles {
|
||||||
if !strings.HasSuffix(name, ".go") ||
|
if !strings.HasSuffix(name, ".go") ||
|
||||||
(skipSuffix != "" && strings.HasSuffix(name, skipSuffix)) {
|
(skipSuffix != "" && strings.HasPrefix(name, skipPrefix) &&
|
||||||
|
strings.HasSuffix(name, skipSuffix)) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if directory != "." {
|
if directory != "." {
|
||||||
|
Loading…
Reference in New Issue
Block a user