Skip to content

Commit

Permalink
Merge pull request #22 from link-duan/main
Browse files Browse the repository at this point in the history
bugfix
  • Loading branch information
link-duan authored Dec 1, 2022
2 parents 670bc56 + 6652e56 commit 3d8dc06
Show file tree
Hide file tree
Showing 14 changed files with 320 additions and 376 deletions.
7 changes: 5 additions & 2 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"github.com/gotomicro/ego-gen-api/spec"
"github.com/knadh/koanf"
"github.com/samber/lo"
"golang.org/x/mod/modfile"
"golang.org/x/tools/go/ast/inspector"
Expand All @@ -22,17 +23,19 @@ type Analyzer struct {
plugins []Plugin
definitions Definitions
depends []string
k *koanf.Koanf

doc *spec.T
packages []*packages.Package
}

func NewAnalyzer() *Analyzer {
func NewAnalyzer(k *koanf.Koanf) *Analyzer {
a := &Analyzer{
routes: make(APIs, 0),
globalEnv: NewEnvironment(nil),
plugins: make([]Plugin, 0),
definitions: make(Definitions),
k: k,
}

components := spec.NewComponents()
Expand All @@ -50,7 +53,7 @@ func NewAnalyzer() *Analyzer {

func (a *Analyzer) Plugin(plugins ...Plugin) *Analyzer {
for _, plugin := range plugins {
err := plugin.Mount()
err := plugin.Mount(a.k)
if err != nil {
panic(fmt.Sprintf("mount plugin '%s' failed. error: %s", plugin.Name(), err.Error()))
}
Expand Down
14 changes: 8 additions & 6 deletions entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (

_ "github.com/gotomicro/ego-gen-api/generators/ts"
_ "github.com/gotomicro/ego-gen-api/generators/umi"
"github.com/spf13/viper"
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/file"
"github.com/urfave/cli/v2"
)

Expand All @@ -34,13 +36,14 @@ type GeneratorConfig struct {
}

type Entrypoint struct {
k *koanf.Koanf
plugins []Plugin

cfg Config
}

func NewEntrypoint(plugins ...Plugin) *Entrypoint {
return &Entrypoint{plugins: plugins}
return &Entrypoint{plugins: plugins, k: koanf.New(".")}
}

const usageText = `Generate Doc:
Expand Down Expand Up @@ -111,7 +114,7 @@ func (e *Entrypoint) before(c *cli.Context) error {
return err
}

err = viper.Unmarshal(&e.cfg)
err = e.k.Unmarshal("", &e.cfg)
if err != nil {
return err
}
Expand All @@ -131,8 +134,7 @@ func (e *Entrypoint) before(c *cli.Context) error {
}

func (e *Entrypoint) loadConfig(cfg string) error {
viper.SetConfigFile(cfg)
return viper.ReadInConfig()
return e.k.Load(file.Provider(cfg), yaml.Parser())
}

func (e *Entrypoint) run(c *cli.Context) error {
Expand Down Expand Up @@ -161,7 +163,7 @@ func (e *Entrypoint) run(c *cli.Context) error {
return err
}

a := NewAnalyzer().Plugin(plugin).Depends(e.cfg.Depends...)
a := NewAnalyzer(e.k).Plugin(plugin).Depends(e.cfg.Depends...)
doc := a.Process(e.cfg.Dir).Doc()
if e.cfg.OpenAPI.Version != "" {
doc.OpenAPI = e.cfg.OpenAPI.Version
Expand Down
2 changes: 1 addition & 1 deletion generators/ts/ts.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,5 @@ func (p *Printer) printBasicType(t string) f.Doc {
case "file":
return f.Content("File")
}
return f.Content("unknown")
return f.Content("any")
}
44 changes: 20 additions & 24 deletions generators/umi/umi.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,16 @@ func (p *Printer) request(path string, method string, item *spec.Operation) f.Do
if item.RequestBody != nil {
_, mediaType := p.getRequestMediaType(item)
if mediaType != nil {
s := spec.Unref(p.schema, mediaType.Schema)
p.importTypes = append(p.importTypes, s.Value.Title)
params = append(params, f.Content("data: "+s.Value.Title))
if mediaType.Schema.Ref != "" {
s := spec.Unref(p.schema, mediaType.Schema)
p.importType(s.Value.Title)
params = append(params, f.Content("data: "+s.Value.Title))
} else {
params = append(params, f.Group(
f.Content("data: "),
ts.NewPrinter(p.schema).PrintType(mediaType.Schema),
))
}
}
}

Expand Down Expand Up @@ -207,7 +214,7 @@ func (p *Printer) requestFunctionBody(pathName string, method string, queryParam
f.Content("let formData = new FormData();"), f.LineBreak(),
f.Content("for (const key in data) {"), f.LineBreak(),
f.Indent(f.Group(
f.Content("formData.append(key, data[key as keyof typeof data] as string);"), f.LineBreak(),
f.Content("formData.append(key, data[key as keyof typeof data] as string | Blob);"), f.LineBreak(),
)),
f.Content("}"),
f.LineBreak(),
Expand Down Expand Up @@ -249,25 +256,10 @@ func (p *Printer) toLowerCamelCase(id string) string {
func (p *Printer) paramsType(params []*spec.ParameterRef) f.Doc {
var fields []f.Doc
for _, param := range params {
typeName := param.Value.Schema.Value.Type
switch typeName {
case "integer":
typeName = "number"
case "array":
var itemType string
items := param.Value.Schema.Value.Items
if items != nil && items.Value != nil {
itemType = items.Value.Type
}
if itemType == "" {
itemType = "string"
}
typeName = itemType + "[]"

case "":
typeName = "string"
}
fields = append(fields, f.Content(param.Value.Name+": "+typeName))
fields = append(fields, f.Group(
f.Content(param.Value.Name+"?: "),
ts.NewPrinter(p.schema).PrintType(param.Value.Schema),
))
}

return f.Group(
Expand Down Expand Up @@ -338,7 +330,7 @@ func (p *Printer) responseType(res *spec.Response) f.Doc {
tsPrinter := ts.NewPrinter(p.schema)
tsPrinter.TypeFieldsInLine = true
ret := tsPrinter.PrintType(schema)
p.importTypes = append(p.importTypes, tsPrinter.ReferencedTypes...)
p.importType(tsPrinter.ReferencedTypes...)
return ret
}

Expand All @@ -357,3 +349,7 @@ func (p *Printer) requestFnName(item *spec.Operation) string {
}
return res
}

func (p *Printer) importType(types ...string) {
p.importTypes = append(p.importTypes, types...)
}
15 changes: 4 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ require (
github.com/go-openapi/jsonpointer v0.19.5
github.com/iancoleman/strcase v0.2.0
github.com/invopop/yaml v0.1.0
github.com/knadh/koanf v1.4.4
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
github.com/robertkrimen/otto v0.0.0-20221025135307-511d75fba9f8
github.com/samber/lo v1.28.2
github.com/spf13/viper v1.14.0
github.com/stretchr/testify v1.8.1
github.com/urfave/cli/v2 v2.23.4
golang.org/x/mod v0.7.0
Expand All @@ -23,26 +23,19 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-openapi/swag v0.19.15 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
golang.org/x/sys v0.2.0 // indirect
golang.org/x/text v0.4.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/sourcemap.v1 v1.0.5 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading

0 comments on commit 3d8dc06

Please sign in to comment.