From 5cf8bd124a4343f9daf339cf104429188f5f766a Mon Sep 17 00:00:00 2001 From: Alan LAM Date: Thu, 22 Aug 2019 11:22:27 +0800 Subject: [PATCH 1/2] Add external import paths option to print external imports to generated file. --- gojay/codegen/generator.go | 17 ++++++++++++++++- gojay/codegen/options.go | 22 ++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/gojay/codegen/generator.go b/gojay/codegen/generator.go index dd43ae5..0cabf79 100644 --- a/gojay/codegen/generator.go +++ b/gojay/codegen/generator.go @@ -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" @@ -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{} @@ -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 diff --git a/gojay/codegen/options.go b/gojay/codegen/options.go index 5d400cd..7fc673d 100644 --- a/gojay/codegen/options.go +++ b/gojay/codegen/options.go @@ -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 { @@ -35,6 +41,7 @@ const ( optionKeyTagName = "a" optionKeyPoolObjects = "p" optionKeyPkg = "pkg" + optionKeyExtImports = "imp" ) //NewOptionsWithFlagSet creates a new options for the supplide flagset @@ -51,5 +58,20 @@ func NewOptionsWithFlagSet(set *flag.FlagSet) *Options { if result.Source == "" { result.Source = url.NewResource(".").ParsedURL.Path } + var extImportSlice = strings.Split(set.Lookup(optionKeyExtImports).Value.String(), ",") + result.ExtImports = make([]ExtImport, len(extImportSlice)) + 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 } From 8173d07253dd91970149b3750cac6d86c8011ae0 Mon Sep 17 00:00:00 2001 From: Alan LAM Date: Thu, 22 Aug 2019 14:06:24 +0800 Subject: [PATCH 2/2] Add external import paths option to print external imports to generated file. --- gojay/codegen/options.go | 27 +++++++++++++++------------ gojay/gojay.go | 1 + 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/gojay/codegen/options.go b/gojay/codegen/options.go index 7fc673d..50bb39e 100644 --- a/gojay/codegen/options.go +++ b/gojay/codegen/options.go @@ -58,20 +58,23 @@ func NewOptionsWithFlagSet(set *flag.FlagSet) *Options { if result.Source == "" { result.Source = url.NewResource(".").ParsedURL.Path } - var extImportSlice = strings.Split(set.Lookup(optionKeyExtImports).Value.String(), ",") - result.ExtImports = make([]ExtImport, len(extImportSlice)) - 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], + 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) } - result.ExtImports = append(result.ExtImports, extImp) } return result } diff --git a/gojay/gojay.go b/gojay/gojay.go index e9358cb..4f15646 100644 --- a/gojay/gojay.go +++ b/gojay/gojay.go @@ -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")