Skip to content

Commit

Permalink
optimize mir's exported interface and engine's usage
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed Mar 27, 2020
1 parent 88b258c commit 6536696
Show file tree
Hide file tree
Showing 21 changed files with 255 additions and 203 deletions.
165 changes: 127 additions & 38 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@ import (

const (
// run mode list
InSerialMode RunMode = iota
InSerialMode runMode = iota
InConcurrentMode
InSerialDebugMode
InConcurrentDebugMode

// options key list
OptSinkPath = "sinkPath"
OptDefaultTag = "defaultTag"

// generator Names
GeneratorGin = "gin"
GeneratorChi = "chi"
Expand All @@ -40,44 +36,83 @@ var (
// parsers parser list
parsers = make(map[string]Parser, 1)

// InDebug whether in debug mode
InDebug bool
// inDebug whether in debug mode
inDebug bool
)

// RunMode indicate process mode (InSerialMode | InSerialDebugMode | InConcurrentMode | InConcurrentDebugMode)
type RunMode uint8

// Opts use for generator or parser init
type InitOpts = map[string]string
// runMode indicate process mode (InSerialMode | InSerialDebugMode | InConcurrentMode | InConcurrentDebugMode)
type runMode uint8

// Options generator options
type Options struct {
// RunMode set run mode (InSerialMode Or InConcurrentMode).
// Default is InSerialMode if not set explicit.
RunMode RunMode
// InitOpts use for generator or parser init
type InitOpts struct {
RunMode runMode
GeneratorName string
ParserName string
GeneratorOpts InitOpts
ParserOpts InitOpts
SinkPath string
DefaultTag string
}

// Crate component common info
type Crate interface {
Name() string
Init(opts InitOpts) error
// ParserOpts used for initial parser
type ParserOpts struct {
DefaultTag string
}

// GeneratorOpts used for initial generator
type GeneratorOpts struct {
SinkPath string
}

// Option pass option to custom run behavior
type Option interface {
apply(opts *InitOpts)
}

// Options generator options
type Options []Option

// InitOpts return an initOpts instance
func (opts Options) InitOpts() *InitOpts {
res := defaultInitOpts()
for _, opt := range opts {
opt.apply(res)
}
return res
}

// ParserOpts return a ParserOpts instance
func (opts *InitOpts) ParserOpts() *ParserOpts {
return &ParserOpts{
DefaultTag: opts.DefaultTag,
}
}

// GeneratorOpts return a GeneratorOpts
func (opts *InitOpts) GeneratorOpts() *GeneratorOpts {
return &GeneratorOpts{
SinkPath: opts.SinkPath,
}
}

// optFunc used for convert function to Option interface
type optFunc func(opts *InitOpts)

func (f optFunc) apply(opts *InitOpts) {
f(opts)
}

// Parser parse entries
type Parser interface {
Crate
Name() string
Init(opts *ParserOpts) error
Parse(entries []interface{}) (Descriptors, error)
ParseContext(ctx MirCtx, entries []interface{})
Clone() Parser
}

// Generator generate interface code for engine
type Generator interface {
Crate
Name() string
Init(opts *GeneratorOpts) error
Generate(Descriptors) error
GenerateContext(ctx MirCtx)
Clone() Generator
Expand All @@ -94,7 +129,8 @@ type MirCtx interface {
Pipe() (<-chan *IfaceDescriptor, chan<- *IfaceDescriptor)
}

func (m RunMode) String() string {
// String runMode describe
func (m runMode) String() string {
res := "not support mode"
switch m {
case InSerialMode:
Expand All @@ -109,6 +145,41 @@ func (m RunMode) String() string {
return res
}

// RunMode set run mode option
func RunMode(mode runMode) Option {
return optFunc(func(opts *InitOpts) {
opts.RunMode = mode
})
}

// GeneratorName set generator name option
func GeneratorName(name string) Option {
return optFunc(func(opts *InitOpts) {
opts.GeneratorName = name
})
}

// ParserName set parser name option
func ParserName(name string) Option {
return optFunc(func(opts *InitOpts) {
opts.ParserName = name
})
}

// SinkPath set generated code out directory
func SinkPath(path string) Option {
return optFunc(func(opts *InitOpts) {
opts.SinkPath = path
})
}

// DefaultTag set parser's default struct field tag string key
func DefaultTag(tag string) Option {
return optFunc(func(opts *InitOpts) {
opts.DefaultTag = tag
})
}

// RegisterGenerators register generators
func RegisterGenerators(gs ...Generator) {
for _, g := range gs {
Expand All @@ -127,17 +198,6 @@ func RegisterParsers(ps ...Parser) {
}
}

// DefaultOptions get a default options
func DefaultOptions() *Options {
return &Options{
GeneratorName: GeneratorGin,
ParserName: ParserStructTag,
GeneratorOpts: InitOpts{
OptSinkPath: "./gen",
},
}
}

// GeneratorByName get a generator by name
func GeneratorByName(name string) Generator {
return generators[name]
Expand All @@ -160,7 +220,36 @@ func DefaultParser() Parser {

// Logus print log info
func Logus(format string, v ...interface{}) {
if InDebug {
if inDebug {
log.Printf("[mir] "+format, v...)
}
}

// Init initial from Options and return an InitOpts instance
func Init(opts Options) *InitOpts {
var initOpts *InitOpts
if opts == nil {
initOpts = defaultInitOpts()
} else {
initOpts = opts.InitOpts()
}

switch initOpts.RunMode {
case InSerialDebugMode, InConcurrentDebugMode:
inDebug = true
default:
inDebug = false
}

return initOpts
}

func defaultInitOpts() *InitOpts {
return &InitOpts{
RunMode: InSerialMode,
GeneratorName: GeneratorGin,
ParserName: ParserStructTag,
SinkPath: ".gen",
DefaultTag: "mir",
}
}
32 changes: 11 additions & 21 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,48 +36,38 @@ func AddEntries(entries ...interface{}) {
}

// Generate generate interface code from mir's iface entry
func Generate(opts *core.Options, entries ...interface{}) (err error) {
func Generate(opts core.Options, entries ...interface{}) (err error) {
mu.Lock()
defer mu.Unlock()

if opts == nil {
return errors.New("options is nil")
}

addEntries(entries...)
if len(mirEntries) == 0 {
return errors.New("mir entries is empty maybe need add entries first")
}

p := core.ParserByName(opts.ParserName)
initOpts := core.Init(opts)
p := core.ParserByName(initOpts.ParserName)
// use default parser when not set parser name from options
if p == nil {
p = core.DefaultParser()
}
if err = p.Init(opts.ParserOpts); err != nil {
if err = p.Init(initOpts.ParserOpts()); err != nil {
return
}

g := core.GeneratorByName(opts.GeneratorName)
g := core.GeneratorByName(initOpts.GeneratorName)
if g == nil {
return fmt.Errorf("unknow generators that name %s", opts.GeneratorName)
return fmt.Errorf("unknow generators that name %s", initOpts.GeneratorName)
}
if err = g.Init(opts.GeneratorOpts); err != nil {
if err = g.Init(initOpts.GeneratorOpts()); err != nil {
return
}

switch opts.RunMode {
case core.InSerialDebugMode:
core.InDebug = true
core.Logus("run in %s", opts.RunMode)
fallthrough
case core.InSerialMode:
core.Logus("run in %s", initOpts.RunMode)
switch initOpts.RunMode {
case core.InSerialDebugMode, core.InSerialMode:
err = doInSerial(p, g, mirEntries)
case core.InConcurrentDebugMode:
core.InDebug = true
core.Logus("run in %s", opts.RunMode)
fallthrough
case core.InConcurrentMode:
case core.InConcurrentDebugMode, core.InConcurrentMode:
err = doInConcurrent(p, g, mirEntries)
}
return err
Expand Down
10 changes: 1 addition & 9 deletions engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,10 @@ package engine

import (
"testing"

"github.com/alimy/mir/v2/core"
)

func TestGenerate(t *testing.T) {
if err := Generate(nil); err == nil {
t.Error("want an error but not")
}
if err := Generate(core.DefaultOptions()); err == nil {
t.Error("want an error but not")
}
if err := Generate(core.DefaultOptions(), nil); err != nil {
if err := Generate(nil, nil); err != nil {
t.Error("don't want an error but not")
}
AddEntry(nil)
Expand Down
16 changes: 5 additions & 11 deletions internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ import (
"github.com/alimy/mir/v2/internal/container"
)

var (
errNotExistSinkPath = errors.New("not exist output path")
)

func init() {
core.RegisterGenerators(
&mirGenerator{name: core.GeneratorGin},
Expand Down Expand Up @@ -63,14 +59,12 @@ func (g *mirGenerator) Name() string {
}

// Init init generator
func (g *mirGenerator) Init(opts core.InitOpts) (err error) {
if len(opts) != 0 {
if sp, exist := opts[core.OptSinkPath]; exist {
g.sinkPath, err = evalSinkPath(sp)
return
}
func (g *mirGenerator) Init(opts *core.GeneratorOpts) (err error) {
if opts == nil {
return errors.New("init opts is nil")
}
return errNotExistSinkPath
g.sinkPath, err = evalSinkPath(opts.SinkPath)
return
}

// Generate serial generate interface code
Expand Down
7 changes: 4 additions & 3 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ func (p *mirParser) Name() string {
}

// Init init parser
func (p *mirParser) Init(opts core.InitOpts) error {
if len(opts) != 0 {
p.tagName = opts[core.OptDefaultTag]
func (p *mirParser) Init(opts *core.ParserOpts) error {
if opts == nil {
return errors.New("init opts is nil")
}
p.tagName = opts.DefaultTag
if p.tagName == "" {
p.tagName = defaultTag
}
Expand Down
2 changes: 1 addition & 1 deletion mirc/cmd/templates/chi_go_mod.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module {{ .PkgName }}
go 1.12

require (
github.com/alimy/mir/v2 v2.2.0
github.com/alimy/mir/v2 v2.3.0
github.com/go-chi/chi v4.0.3+incompatible
)
10 changes: 4 additions & 6 deletions mirc/cmd/templates/chi_mirc_main.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ import (
//go:generate go run main.go
func main() {
log.Println("generate code start")
opts := &core.Options{
RunMode: core.InSerialMode,
GeneratorName: core.GeneratorChi,
GeneratorOpts: core.InitOpts{
core.OptSinkPath: "./gen",
},
opts := core.Options{
core.RunMode(core.InSerialMode),
core.GeneratorName(core.GeneratorChi),
core.SinkPath("./gen"),
}
if err := engine.Generate(opts); err != nil {
log.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion mirc/cmd/templates/echo_go_mod.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module {{ .PkgName }}
go 1.12

require (
github.com/alimy/mir/v2 v2.2.0
github.com/alimy/mir/v2 v2.3.0
github.com/labstack/echo/v4 v4.1.15
)
Loading

0 comments on commit 6536696

Please sign in to comment.