gojay

high performance JSON encoder/decoder with stream API for Golang
git clone git://git.lair.cx/gojay
Log | Files | Refs | README | LICENSE

commit 57fd99d065deccaa976ff5ccc4bc03e121cd8a08
parent 7785f3c8e3257d4aa2884bfba702d1f52c13835c
Author: francoispqt <francois@parquet.ninja>
Date:   Wed, 23 Jan 2019 01:22:31 +0800

update generator add gofmt and generator readme

Diffstat:
Mgojay/codegen/README.md | 8++++----
Mgojay/codegen/generator.go | 13+++++++++++--
Mgojay/codegen/options.go | 3++-
Mgojay/codegen/template.go | 2+-
4 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/gojay/codegen/README.md b/gojay/codegen/README.md @@ -1,5 +1,5 @@ # Gojay code generator -This package provides a command line tool to generate gojay's marshaling and unmarshing interface implementation for custom type(s) +This package provides a command line tool to generate gojay's marshaling and unmarshing interface implementation for custom struct type(s) ## Get started @@ -13,10 +13,10 @@ go install github.com/francoispqt/gojay/gojaygen ### Basic command The basic command is straightforward and easy to use: ```sh -cd $GOPATH/src/mycoany/myproject -gojaygen -p myproject +cd $GOPATH/src/github.com/user/project +gojaygen -s . -p true -t MyType -o output.go ``` -If you just want to the output to stdout, omit the third parameter. +If you just want to the output to stdout, omit the -o flag. ### Using flags - s file/dir path, can be a relative or absolute path diff --git a/gojay/codegen/generator.go b/gojay/codegen/generator.go @@ -2,6 +2,7 @@ package codegen import ( "fmt" + "go/format" "io/ioutil" "os" "path/filepath" @@ -92,11 +93,19 @@ func (g *Generator) writeCode() error { if err != nil { return err } + + code, err := format.Source([]byte(expandedCode)) + + if err != nil { + return err + } + if g.options.Dest == "" { - fmt.Print(expandedCode) + fmt.Print(string(code)) return nil } - return ioutil.WriteFile(g.options.Dest, []byte(expandedCode), 0644) + + return ioutil.WriteFile(g.options.Dest, code, 0644) } func (g *Generator) generatePrimitiveArray(field *Field) error { diff --git a/gojay/codegen/options.go b/gojay/codegen/options.go @@ -2,10 +2,11 @@ package codegen import ( "flag" + "strings" + "github.com/go-errors/errors" "github.com/viant/toolbox" "github.com/viant/toolbox/url" - "strings" ) type Options struct { diff --git a/gojay/codegen/template.go b/gojay/codegen/template.go @@ -106,7 +106,7 @@ var fieldTemplate = map[int]string{ }{{else}} enc.TimeKey("{{.Key}}", {{.PointerModifier}}{{.Accessor}}, {{.TimeLayout}}){{end}}`, decodeSQLNull: ` case "{{.Key}}": var value = {{.Init}} - err := dec.DecodeSQLNull{{.NullType}}({{.PointerModifier}}value, format) + err := dec.DecodeSQLNull{{.NullType}}({{.PointerModifier}}value) if err == nil { {{.Mutator}} = value }