Skip to content

Commit

Permalink
mir:optimize engine's generate logic and add entry's method
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed Mar 25, 2020
1 parent 4df141c commit 4350308
Show file tree
Hide file tree
Showing 43 changed files with 375 additions and 293 deletions.
40 changes: 22 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Mir is used for register handler to http router(eg: [Gin](https://github.com/gin
## Usage

* Generate a simple template project

```
% go get github.com/alimy/mir/mirc/v2@latest
% mirc new -d mir-examples
Expand All @@ -33,12 +34,20 @@ mir-examples
```

* Custom route info just use struct tag. eg:

```go
// file: mirc/routes/site.go

package routes

import "github.com/alimy/mir/v2"
import (
"github.com/alimy/mir/v2"
"github.com/alimy/mir/v2/engine"
)

func init() {
engine.AddEntry(new(Site))
}

// Site mir's struct tag define
type Site struct {
Expand All @@ -49,6 +58,7 @@ type Site struct {
```

* Invoke mir's generator to generate interface. eg:

```
% cat mirc/main.go
package main
Expand All @@ -59,38 +69,30 @@ import (
"github.com/alimy/mir/v2/core"
"github.com/alimy/mir/v2/engine"
routes "github.com/alimy/mir/v2/examples/mirc/routes"
v1 "github.com/alimy/mir/v2/examples/mirc/routes/v1"
v2 "github.com/alimy/mir/v2/examples/mirc/routes/v2"
_ "github.com/alimy/mir/v2/examples/mirc/routes"
_ "github.com/alimy/mir/v2/examples/mirc/routes/v1"
_ "github.com/alimy/mir/v2/examples/mirc/routes/v2"
)
//go:generate go run main.go
func main() {
log.Println("generate code start")
entries := mirEntries()
opts := &core.Options{
RunMode: core.InSerialMode,
GeneratorName: core.GeneratorGin,
GeneratorOpts: core.InitOpts{
core.OptSinkPath: "./gen",
},
}
if err := engine.Generate(entries, opts); err != nil {
if err := engine.Generate(opts); err != nil {
log.Fatal(err)
}
log.Println("generate code finish")
}
func mirEntries() []interface{} {
return []interface{}{
new(routes.Site),
new(v1.Site),
new(v2.Site),
}
}
```

* Then generate interface from routes info defined above

```go
% make generate
% cat mirc/gen/api/site.go
Expand Down Expand Up @@ -124,6 +126,7 @@ func RegisterSiteServant(e *gin.Engine, s Site) {
```

* Register interface to router

```go
package main

Expand All @@ -132,9 +135,9 @@ import (

"github.com/gin-gonic/gin"

"github.com/alimy/mir/v2/examples/mirc/gen/api"
"github.com/alimy/mir/v2/examples/mirc/gen/api/v1"
"github.com/alimy/mir/v2/examples/mirc/gen/api/v2"
api "github.com/alimy/mir/v2/examples/mirc/gen/api"
v1 "github.com/alimy/mir/v2/examples/mirc/gen/api/v1"
v2 "github.com/alimy/mir/v2/examples/mirc/gen/api/v2"
"github.com/alimy/mir/v2/examples/servants"
)

Expand Down Expand Up @@ -167,4 +170,5 @@ func registerServants(e *gin.Engine) {
```shell
% make run
```
**Please look at [examples](examples) project for more detail.Have an enjoy in your heart.**

**Please look at [examples](examples) project for more details.Have an enjoy in your heart.**
42 changes: 36 additions & 6 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,45 @@ import (
"errors"
"fmt"
"runtime"
"sync"

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

// Generate generate interface code
func Generate(entries []interface{}, opts *core.Options) (err error) {
var (
mu = &sync.Mutex{}
mirEntries = make([]interface{}, 0, 8)
)

// AddEntry add mir's entry
func AddEntry(entry interface{}) {
mu.Lock()
defer mu.Unlock()

mirEntries = append(mirEntries, entry)
}

// AddEntries add mir's entry list
func AddEntries(entries ...interface{}) {
mu.Lock()
defer mu.Unlock()

addEntries(entries...)
}

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

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

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

p := core.ParserByName(opts.ParserName)
Expand All @@ -46,17 +72,21 @@ func Generate(entries []interface{}, opts *core.Options) (err error) {
core.Logus("run in %s", opts.RunMode)
fallthrough
case core.InSerialMode:
err = doInSerial(p, g, entries)
err = doInSerial(p, g, mirEntries)
case core.InConcurrentDebugMode:
core.InDebug = true
core.Logus("run in %s", opts.RunMode)
fallthrough
case core.InConcurrentMode:
err = doInConcurrent(p, g, entries)
err = doInConcurrent(p, g, mirEntries)
}
return err
}

func addEntries(entries ...interface{}) {
mirEntries = append(mirEntries, entries...)
}

func doInSerial(p core.Parser, g core.Generator, entries []interface{}) error {
descriptors, err := p.Parse(entries)
if err == nil {
Expand Down
12 changes: 10 additions & 2 deletions engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ import (
)

func TestGenerate(t *testing.T) {
if err := Generate(nil, nil); err == nil {
if err := Generate(nil); err == nil {
t.Error("want an error but not")
}
if err := Generate(nil, core.DefaultOptions()); err == nil {
if err := Generate(core.DefaultOptions()); err == nil {
t.Error("want an error but not")
}
if err := Generate(core.DefaultOptions(), nil); err != nil {
t.Error("don't want an error but not")
}
AddEntry(nil)
AddEntries(nil, nil)
if len(mirEntries) != 4 {
t.Errorf("want mirEntries's size is 4 but is %d", len(mirEntries))
}
}
14 changes: 7 additions & 7 deletions internal/generator/templates_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/parser/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

var (
// error list
errNilType tagError = "nil type is not valide"
errNotExist tagError = "mir struct tag filed not exist"
errNoPathInfo tagError = "mir struct tag not contains path info"
errNotValideType tagError = "not valide type, just struct and struct ptr is avalibale"
Expand Down
5 changes: 4 additions & 1 deletion internal/parser/reflex.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ func (r *reflex) parse(entries []interface{}) (core.Descriptors, error) {
func (r *reflex) ifaceFrom(entry interface{}) (*core.IfaceDescriptor, error) {
// used to find tagInfo
entryType := reflect.TypeOf(entry)
isPtr := false
if entryType == nil {
return nil, errNilType
}

// get real entry type
isPtr := false
if entryType.Kind() == reflect.Ptr {
isPtr = true
entryType = entryType.Elem()
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.1.0
github.com/alimy/mir/v2 v2.2.0
github.com/go-chi/chi v4.0.3+incompatible
)
17 changes: 4 additions & 13 deletions mirc/cmd/templates/chi_mirc_main.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,23 @@ import (
"github.com/alimy/mir/v2/core"
"github.com/alimy/mir/v2/engine"

routes "{{ .PkgName }}/mirc/routes"
v1 "{{ .PkgName }}/mirc/routes/v1"
v2 "{{ .PkgName }}/mirc/routes/v2"
_ "{{ .PkgName }}/mirc/routes"
_ "{{ .PkgName }}/mirc/routes/v1"
_ "{{ .PkgName }}/mirc/routes/v2"
)

//go:generate go run main.go
func main() {
log.Println("generate code start")
entries := mirEntries()
opts := &core.Options{
RunMode: core.InSerialMode,
GeneratorName: core.GeneratorChi,
GeneratorOpts: core.InitOpts{
core.OptSinkPath: "./gen",
},
}
if err := engine.Generate(entries, opts); err != nil {
if err := engine.Generate(opts); err != nil {
log.Fatal(err)
}
log.Println("generate code finish")
}

func mirEntries() []interface{} {
return []interface{}{
new(routes.Site),
new(v1.Site),
new(v2.Site),
}
}
7 changes: 6 additions & 1 deletion mirc/cmd/templates/chi_mirc_routes_site.tmpl
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package routes

import (
mir "github.com/alimy/mir/v2"
"github.com/alimy/mir/v2"
"github.com/alimy/mir/v2/engine"
)

func init() {
engine.AddEntry(new(Site))
}

// Site site interface info
type Site struct {
Chain mir.Chain `mir:"-"`
Expand Down
7 changes: 6 additions & 1 deletion mirc/cmd/templates/chi_mirc_routes_site_v1.tmpl
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package v1

import (
mir "github.com/alimy/mir/v2"
"github.com/alimy/mir/v2"
"github.com/alimy/mir/v2/engine"
)

func init() {
engine.AddEntry(new(Site))
}

// Site site v1 interface info
type Site struct {
Chain mir.Chain `mir:"-"`
Expand Down
8 changes: 6 additions & 2 deletions mirc/cmd/templates/chi_mirc_routes_site_v2.tmpl
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package v2

import (
mir "github.com/alimy/mir/v2"
"github.com/alimy/mir/v2"
"github.com/alimy/mir/v2/engine"
)

func init() {
engine.AddEntry(new(Site))
}

// Site site v2 interface info
type Site struct {
Group mir.Group `mir:"v2"`
Index mir.Get `mir:"/index/"`
Articles mir.Get `mir:"/articles/{category}/{id:[0-9]+}"`
Category mir.Get `mir:"/category/"`
}

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.1.0
github.com/alimy/mir/v2 v2.2.0
github.com/labstack/echo/v4 v4.1.15
)
Loading

0 comments on commit 4350308

Please sign in to comment.