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

Feature/print external imports #126

Open
wants to merge 2 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion gojay/codegen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package codegen

import (
"fmt"
"github.com/viant/toolbox"
"go/format"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/viant/toolbox"
)

const gojayPackage = "github.com/francoispqt/gojay"
Expand Down Expand Up @@ -39,6 +39,15 @@ func (g *Generator) addImport(pkg string) {
g.imports[`"`+pkg+`"`] = true
}

// addExtImport adds an external import package to be printed on the generated code
func (g *Generator) addExtImport(pkg ExtImport) {
var identifier string
if pkg.Identifier != "" {
identifier = pkg.Identifier + " "
}
g.imports[identifier+`"`+pkg.Path+`"`] = true
}

// we initiate the variables containing the code to be generated
func (g *Generator) init() {
g.filedInit = []string{}
Expand All @@ -52,6 +61,12 @@ func (g *Generator) init() {
if g.options.PoolObjects {
g.addImport("sync")
}
if g.options.ExtImports != nil {
for _, extImp := range g.options.ExtImports {
g.addExtImport(extImp)
}
}

}

// NewGenerator creates a new generator with the given options
Expand Down
25 changes: 25 additions & 0 deletions gojay/codegen/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ type Options struct {
PoolObjects bool
TagName string
Pkg string
ExtImports []ExtImport
}

type ExtImport struct {
Identifier string
Path string
}

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

//NewOptionsWithFlagSet creates a new options for the supplide flagset
Expand All @@ -51,5 +58,23 @@ func NewOptionsWithFlagSet(set *flag.FlagSet) *Options {
if result.Source == "" {
result.Source = url.NewResource(".").ParsedURL.Path
}
var extImport = set.Lookup(optionKeyExtImports).Value.String()
if extImport != "" {
var extImportSlice = strings.Split(set.Lookup(optionKeyExtImports).Value.String(), ",")
result.ExtImports = []ExtImport{}
for _, impStr := range extImportSlice {
var imp = strings.Split(impStr, ":")
var extImp = ExtImport{
Path: imp[0],
}
if len(imp) > 1 {
extImp = ExtImport{
Identifier: imp[0],
Path: imp[1],
}
}
result.ExtImports = append(result.ExtImports, extImp)
}
}
return result
}
1 change: 1 addition & 0 deletions gojay/gojay.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

var pkg = flag.String("pkg", "", "the package name of the generated file")
var imp = flag.String("imp", "", "the external package name and identifier to add into generated file")
var dst = flag.String("o", "", "destination file to output generated code")
var src = flag.String("s", "", "source dir or file (absolute or relative path)")
var types = flag.String("t", "", "types to generate")
Expand Down