diff --git a/gojay/codegen/generator.go b/gojay/codegen/generator.go index dd43ae5..b5fab9a 100644 --- a/gojay/codegen/generator.go +++ b/gojay/codegen/generator.go @@ -7,6 +7,7 @@ import ( "os" "path/filepath" "strings" + "github.com/viant/toolbox" ) @@ -118,10 +119,13 @@ func (g *Generator) writeCode() error { return err } - code, err := format.Source([]byte(expandedCode)) - - if err != nil { - return err + code := []byte(expandedCode) + if g.options.GoFmt { + codefmt, err := format.Source(code) + if err != nil { + return err + } + code = codefmt } // code destination is empty, we just print to stdout diff --git a/gojay/codegen/options.go b/gojay/codegen/options.go index 5d400cd..3f5f750 100644 --- a/gojay/codegen/options.go +++ b/gojay/codegen/options.go @@ -16,6 +16,7 @@ type Options struct { PoolObjects bool TagName string Pkg string + GoFmt bool } func (o *Options) Validate() error { @@ -35,6 +36,7 @@ const ( optionKeyTagName = "a" optionKeyPoolObjects = "p" optionKeyPkg = "pkg" + optionGoFmt = "gofmt" ) //NewOptionsWithFlagSet creates a new options for the supplide flagset @@ -48,6 +50,7 @@ func NewOptionsWithFlagSet(set *flag.FlagSet) *Options { result.TagName = set.Lookup(optionKeyTagName).Value.String() result.Types = strings.Split(set.Lookup(optionKeyTypes).Value.String(), ",") result.Pkg = set.Lookup(optionKeyPkg).Value.String() + result.GoFmt = toolbox.AsBoolean(set.Lookup(optionGoFmt).Value.String()) if result.Source == "" { result.Source = url.NewResource(".").ParsedURL.Path } diff --git a/gojay/gojay.go b/gojay/gojay.go index e9358cb..7457b48 100644 --- a/gojay/gojay.go +++ b/gojay/gojay.go @@ -2,8 +2,9 @@ package main import ( "flag" - "github.com/francoispqt/gojay/gojay/codegen" "log" + + "github.com/francoispqt/gojay/gojay/codegen" ) var pkg = flag.String("pkg", "", "the package name of the generated file") @@ -12,6 +13,7 @@ var src = flag.String("s", "", "source dir or file (absolute or relative path)") var types = flag.String("t", "", "types to generate") var annotation = flag.String("a", "json", "annotation tag (default json)") var poolObjects = flag.String("p", "", "generate code to reuse objects using sync.Pool") +var gofmt = flag.String("gofmt", "true", "format the generated code") func main() { flag.Parse()