Skip to content

Commit 3a6864c

Browse files
committed
feat: support for axios generator
1 parent bb09810 commit 3a6864c

File tree

6 files changed

+405
-10
lines changed

6 files changed

+405
-10
lines changed

entrypoint.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import (
88
"path/filepath"
99
"runtime/debug"
1010

11+
_ "github.com/gotomicro/eapi/generators/axios"
1112
_ "github.com/gotomicro/eapi/generators/ts"
1213
_ "github.com/gotomicro/eapi/generators/umi"
1314
"github.com/gotomicro/eapi/spec"
1415
"github.com/knadh/koanf"
1516
"github.com/knadh/koanf/parsers/yaml"
1617
"github.com/knadh/koanf/providers/file"
18+
"github.com/mitchellh/mapstructure"
1719
"github.com/urfave/cli/v2"
1820
)
1921

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

232234
// execute generators
233-
for _, item := range e.cfg.Generators {
234-
err = newGeneratorExecutor(item, doc).execute()
235+
for idx, item := range e.cfg.Generators {
236+
err = newGeneratorExecutor(
237+
item,
238+
doc,
239+
func(value interface{}) error {
240+
raw := e.k.Raw()["generators"].([]interface{})[idx]
241+
d, _ := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
242+
DecodeHook: mapstructure.ComposeDecodeHookFunc(
243+
mapstructure.StringToTimeDurationHookFunc(),
244+
mapstructure.StringToSliceHookFunc(","),
245+
mapstructure.TextUnmarshallerHookFunc()),
246+
Metadata: nil,
247+
Result: value,
248+
WeaklyTypedInput: true,
249+
})
250+
return d.Decode(raw)
251+
},
252+
).execute()
235253
if err != nil {
236254
return err
237255
}

generator_executor.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ import (
1010
)
1111

1212
type generatorExecutor struct {
13-
cfg *GeneratorConfig
14-
doc *spec.T
13+
cfg *GeneratorConfig
14+
doc *spec.T
15+
configUnmarshaller func(value interface{}) error
1516
}
1617

17-
func newGeneratorExecutor(cfg *GeneratorConfig, doc *spec.T) *generatorExecutor {
18-
return &generatorExecutor{cfg: cfg, doc: doc}
18+
func newGeneratorExecutor(cfg *GeneratorConfig, doc *spec.T, configUnmarshaller func(value interface{}) error) *generatorExecutor {
19+
return &generatorExecutor{cfg: cfg, doc: doc, configUnmarshaller: configUnmarshaller}
1920
}
2021

2122
func (r *generatorExecutor) execute() (err error) {
@@ -47,7 +48,7 @@ func (r *generatorExecutor) executeItem(t *generators.Item) error {
4748
}
4849
defer file.Close()
4950

50-
content := t.Print(r.doc)
51+
content := t.Print(r.doc, &generators.PrintOptions{ConfigUnmarshaller: r.configUnmarshaller})
5152
_, err = file.WriteString(content)
5253
if err != nil {
5354
return err

0 commit comments

Comments
 (0)