Skip to content

Commit

Permalink
feat: support for axios generator
Browse files Browse the repository at this point in the history
  • Loading branch information
link-duan committed Feb 20, 2023
1 parent bb09810 commit 3a6864c
Show file tree
Hide file tree
Showing 6 changed files with 405 additions and 10 deletions.
22 changes: 20 additions & 2 deletions entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
"path/filepath"
"runtime/debug"

_ "github.com/gotomicro/eapi/generators/axios"
_ "github.com/gotomicro/eapi/generators/ts"
_ "github.com/gotomicro/eapi/generators/umi"
"github.com/gotomicro/eapi/spec"
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/file"
"github.com/mitchellh/mapstructure"
"github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -230,8 +232,24 @@ func (e *Entrypoint) run(c *cli.Context) error {
}

// execute generators
for _, item := range e.cfg.Generators {
err = newGeneratorExecutor(item, doc).execute()
for idx, item := range e.cfg.Generators {
err = newGeneratorExecutor(
item,
doc,
func(value interface{}) error {
raw := e.k.Raw()["generators"].([]interface{})[idx]
d, _ := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
DecodeHook: mapstructure.ComposeDecodeHookFunc(
mapstructure.StringToTimeDurationHookFunc(),
mapstructure.StringToSliceHookFunc(","),
mapstructure.TextUnmarshallerHookFunc()),
Metadata: nil,
Result: value,
WeaklyTypedInput: true,
})
return d.Decode(raw)
},
).execute()
if err != nil {
return err
}
Expand Down
11 changes: 6 additions & 5 deletions generator_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import (
)

type generatorExecutor struct {
cfg *GeneratorConfig
doc *spec.T
cfg *GeneratorConfig
doc *spec.T
configUnmarshaller func(value interface{}) error
}

func newGeneratorExecutor(cfg *GeneratorConfig, doc *spec.T) *generatorExecutor {
return &generatorExecutor{cfg: cfg, doc: doc}
func newGeneratorExecutor(cfg *GeneratorConfig, doc *spec.T, configUnmarshaller func(value interface{}) error) *generatorExecutor {
return &generatorExecutor{cfg: cfg, doc: doc, configUnmarshaller: configUnmarshaller}
}

func (r *generatorExecutor) execute() (err error) {
Expand Down Expand Up @@ -47,7 +48,7 @@ func (r *generatorExecutor) executeItem(t *generators.Item) error {
}
defer file.Close()

content := t.Print(r.doc)
content := t.Print(r.doc, &generators.PrintOptions{ConfigUnmarshaller: r.configUnmarshaller})
_, err = file.WriteString(content)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 3a6864c

Please sign in to comment.