Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -gofmt option for debugging output #140

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions gojay/codegen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"strings"

"github.com/viant/toolbox"
)

Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions gojay/codegen/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Options struct {
PoolObjects bool
TagName string
Pkg string
GoFmt bool
}

func (o *Options) Validate() error {
Expand All @@ -35,6 +36,7 @@ const (
optionKeyTagName = "a"
optionKeyPoolObjects = "p"
optionKeyPkg = "pkg"
optionGoFmt = "gofmt"
)

//NewOptionsWithFlagSet creates a new options for the supplide flagset
Expand All @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion gojay/gojay.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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()
Expand Down