Skip to content

Commit

Permalink
feat: add rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
2637309949 committed Jul 1, 2020
1 parent 1a4138c commit 5fbb867
Show file tree
Hide file tree
Showing 25 changed files with 531 additions and 63 deletions.
7 changes: 7 additions & 0 deletions cmd/dolphin/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var lines = []pipe.Pipe{
&modules.Main{},
&modules.App{},
&modules.Ctr{},
&modules.Proto{},
&modules.Srv{},
&modules.Model{},
&modules.Bean{},
Expand Down Expand Up @@ -128,5 +129,11 @@ func (gen *Gen) BuildWithCfg(cfg *pipe.TmplCfg) error {
logrus.Error(err)
}
}
if cfg.GOProto && path.Ext(cfg.FilePath) == ".proto" {
cmd := exec.Command("protoc", "-I", path.Dir(cfg.FilePath), cfg.FilePath, "--go_out=plugins=grpc:"+path.Dir(cfg.FilePath))
if err := cmd.Run(); err != nil && err != exec.ErrNotFound {
logrus.Error(err)
}
}
return nil
}
1 change: 1 addition & 0 deletions cmd/dolphin/gen/modules/auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (auto *Auto) Build(dir string, node *schema.Application) ([]*pipe.TmplCfg,
"PackageName": node.PackageName,
"Name": node.Name,
"Controllers": node.Controllers,
"Services": node.Services,
"Tables": node.Tables,
}
autoByte, _ := vfsutil.ReadFile(template.Assets, "auto.tmpl")
Expand Down
56 changes: 56 additions & 0 deletions cmd/dolphin/gen/modules/proto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) 2018-2020 Double All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

package modules

import (
"path"

"github.com/2637309949/dolphin/cmd/dolphin/gen/pipe"
"github.com/2637309949/dolphin/cmd/dolphin/gen/template"
"github.com/2637309949/dolphin/cmd/dolphin/schema"
"github.com/2637309949/dolphin/packages/viper"
"github.com/shurcooL/httpfs/vfsutil"
)

// Proto struct
type Proto struct {
}

// Name defined pipe name
func (oa *Proto) Name() string {
return "proto"
}

// Build func
func (oa *Proto) Build(dir string, node *schema.Application) ([]*pipe.TmplCfg, error) {
var tmplCfgs []*pipe.TmplCfg
ctrByte, _ := vfsutil.ReadFile(template.Assets, "proto.tmpl")
rpcByte, _ := vfsutil.ReadFile(template.Assets, "rpc.tmpl")
for _, s := range node.Services {
data := map[string]interface{}{
"PackageName": node.PackageName,
"Name": node.Name,
"Service": s,
}
tmplCfg := &pipe.TmplCfg{
Text: string(ctrByte),
FilePath: path.Join(dir, viper.GetString("dir.rpc"), "proto", s.Name+".proto"),
Data: data,
Overlap: pipe.OverlapInc,
GOFmt: false,
GOProto: true,
}
tmplCfgs = append(tmplCfgs, tmplCfg)
tmplCfg = &pipe.TmplCfg{
Text: string(rpcByte),
FilePath: path.Join(dir, viper.GetString("dir.rpc"), s.Name+".go"),
Data: data,
Overlap: pipe.OverlapInc,
GOFmt: true,
}
tmplCfgs = append(tmplCfgs, tmplCfg)
}
return tmplCfgs, nil
}
1 change: 1 addition & 0 deletions cmd/dolphin/gen/pipe/pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type (
Data interface{}
Overlap Overlap
GOFmt bool
GOProto bool
}
// Pipe interface
Pipe interface {
Expand Down
34 changes: 25 additions & 9 deletions cmd/dolphin/gen/template/assets.go

Large diffs are not rendered by default.

44 changes: 18 additions & 26 deletions cmd/dolphin/gen/template/assets/auto.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"{{.PackageName}}/model"

"github.com/2637309949/dolphin/packages/viper"
"github.com/2637309949/dolphin/packages/logrus"
"github.com/2637309949/dolphin/platform/util"
)

// Name project
Expand Down Expand Up @@ -46,8 +48,16 @@ func {{.ToUpperCase .Name}}Routes(engine *Engine) {
var {{.ToUpperCase .Name}}Instance = New{{.ToUpperCase .Name}}()
{{end}}

// SyncModels defined
func SyncModels() error {

{{- range .Services}}
// {{.ToUpperCase .Name}} defined
func {{.ToUpperCase .Name}}Service(engine *Engine) {
proto.Register{{.ToUpperCase .Name}}Server(engine.GRPC, &rpc.{{.ToUpperCase .Name}}{})
}
{{- end}}

// SyncModel defined
func SyncModel() error {
mseti := App.Manager.MSet()
{{- range .Tables}}
mseti.Add(new(model.{{.ToUpperCase .Name}}){{- if ne .Bind "" }}, "{{.Bind}}"{{- end}})
Expand All @@ -63,34 +73,16 @@ func SyncCtr() error {
return nil
}

// Executor defined
type Executor []func() error

// Add defined
func (act *Executor) Add(i ...func() error) *Executor {
*act = append(*act, i...)
return act
}

// Execute defined
func (act *Executor) Execute() error {
for _, v := range *act {
if err := v(); err != nil {
return err
}
}
// SyncService defined
func SyncService() error {
{{- range .Services}}
{{.ToUpperCase .Name}}Service(App)
{{- end}}
return nil
}

// NewExecutor defined
func NewExecutor(i ...func() error) *Executor {
e := &Executor{}
e.Add(i...)
return e
}

// ExecutorInstance defined
var ExecutorInstance = NewExecutor(SyncModels, SyncCtr)
var ExecutorInstance = util.NewExecutor(SyncModel, SyncCtr, SyncService)

func init() {
if err := ExecutorInstance.Execute(); err != nil {
Expand Down
20 changes: 20 additions & 0 deletions cmd/dolphin/gen/template/assets/proto.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Code generated by dol build. Only Generate by tools if not existed, your can rewrite platform.App default action
// source:

syntax = "proto3";

package proto;

// {{.Service.ToUpperCase .Service.Name}} defined
service {{.Service.ToUpperCase .Service.Name}} {
{{- range .Service.RPCS}}
rpc {{.ToUpperCase .Name}} ({{.PRef .Request.Type}}) returns ({{.PRef .Reply.Type}}) {}
{{- end}}
}

{{range .Service.RPCS}}
// {{.PRef .Request.Type}} defined
message {{.PRef .Request.Type}} {}
// {{.PRef .Reply.Type}} defined
message {{.PRef .Reply.Type}} {}
{{end}}
18 changes: 18 additions & 0 deletions cmd/dolphin/gen/template/assets/rpc.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package rpc

import (
"{{.PackageName}}/rpc/proto"

"golang.org/x/net/context"
)

// {{.Service.ToUpperCase .Service.Name}} defined
type {{.Service.ToUpperCase .Service.Name}} struct{}

{{range .Service.RPCS}}
// {{.ToUpperCase .Name}} defined
func (s *{{$.Service.ToUpperCase $.Service.Name}}) {{.ToUpperCase .Name}}(ctx context.Context, in *proto.{{.PRef .Request.Type}}) (*proto.{{.PRef .Reply.Type}}, error) {
return &proto.{{.PRef .Reply.Type}}{}, nil
}
{{end}}

3 changes: 2 additions & 1 deletion cmd/dolphin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func InitViper(cmd *cobra.Command, args []string) {
viper.SetDefault("dir.util", "util")
viper.SetDefault("dir.model", "model")
viper.SetDefault("dir.srv", "srv")
viper.SetDefault("dir.rpc", "rpc")
viper.SetDefault("swag.license.name", "Apache 2.0")
viper.SetDefault("swag.license.url", "http://www.apache.org/licenses/LICENSE-2.0.html")
viper.SetDefault("swag.securitydefinitions.oauth2.accessCode", "OAuth2AccessCode")
Expand Down Expand Up @@ -96,7 +97,7 @@ var (
if err := p.Walk(wd); err != nil {
return err
}
pipes := append(args, "main", "app", "ctr", "srv", "model", "bean", "auto", "tool", "sql", "sqlmap", "oauth", "script", "doc")
pipes := append(args, "main", "app", "ctr", "proto", "srv", "model", "bean", "auto", "tool", "sql", "sqlmap", "oauth", "script", "doc")
g := gen.New(p.Application)
g.AddPipe(gen.GetPipesByName(pipes...)...)
err = g.BuildWithDir(wd)
Expand Down
69 changes: 69 additions & 0 deletions cmd/dolphin/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,75 @@ func (parser *AppParser) parseParam(xmlPath string, attr []xml.Attr) *schema.Par
return param
}

func (parser *AppParser) parseService(xmlPath string, attr []xml.Attr) *schema.Service {
service := &schema.Service{}
service.Path = xmlPath
for _, attr := range attr {
attrName := attr.Name.Local
attrValue := attr.Value
if strings.TrimSpace(attrValue) == "" {
continue
}
switch true {
case attrName == "name":
service.Name = attrValue
case attrName == "desc":
service.Desc = attrValue
}
}
return service
}

func (parser *AppParser) parseRPC(xmlPath string, attr []xml.Attr) *schema.RPC {
rpc := &schema.RPC{Request: &schema.Request{}, Reply: &schema.Reply{}}
for _, attr := range attr {
attrName := attr.Name.Local
attrValue := attr.Value
if strings.TrimSpace(attrValue) == "" {
continue
}
switch true {
case attrName == "name":
rpc.Name = attrValue
case attrName == "desc":
rpc.Desc = attrValue
}
}
return rpc
}

func (parser *AppParser) parseRequest(xmlPath string, attr []xml.Attr, rpc *schema.RPC) {
for _, attr := range attr {
attrName := attr.Name.Local
attrValue := attr.Value
if strings.TrimSpace(attrValue) == "" {
continue
}
switch true {
case attrName == "desc":
rpc.Request.Desc = attrValue
case attrName == "type":
rpc.Request.Type = attrValue
}
}
}

func (parser *AppParser) parseReply(xmlPath string, attr []xml.Attr, rpc *schema.RPC) {
for _, attr := range attr {
attrName := attr.Name.Local
attrValue := attr.Value
if strings.TrimSpace(attrValue) == "" {
continue
}
switch true {
case attrName == "desc":
rpc.Reply.Desc = attrValue
case attrName == "type":
rpc.Reply.Type = attrValue
}
}
}

func (parser *AppParser) parseBean(xmlPath string, attr []xml.Attr) *schema.Bean {
bean := &schema.Bean{}
bean.Path = xmlPath
Expand Down
15 changes: 15 additions & 0 deletions cmd/dolphin/parser/xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ func (parser *AppParser) parse(xmlPath string) error {
var prop *schema.Prop
var table *schema.Table
var column *schema.Column

var service *schema.Service
var rpc *schema.RPC
for t, err := decoder.Token(); err == nil; t, err = decoder.Token() {
switch token := t.(type) {
case xml.StartElement:
Expand All @@ -73,6 +76,14 @@ func (parser *AppParser) parse(xmlPath string) error {
parser.parseFailure(xmlPath, token.Attr, api)
case token.Name.Local == "param":
param = parser.parseParam(xmlPath, token.Attr)
case token.Name.Local == "service":
service = parser.parseService(xmlPath, token.Attr)
case token.Name.Local == "rpc":
rpc = parser.parseRPC(xmlPath, token.Attr)
case token.Name.Local == "request":
parser.parseRequest(xmlPath, token.Attr, rpc)
case token.Name.Local == "reply":
parser.parseReply(xmlPath, token.Attr, rpc)
case token.Name.Local == "bean":
bean = parser.parseBean(xmlPath, token.Attr)
case token.Name.Local == "prop":
Expand All @@ -88,6 +99,10 @@ func (parser *AppParser) parse(xmlPath string) error {
parser.Application.Controllers = append(parser.Application.Controllers, controller)
case token.Name.Local == "api":
controller.APIS = append(controller.APIS, api)
case token.Name.Local == "service":
parser.Application.Services = append(parser.Application.Services, service)
case token.Name.Local == "rpc":
service.RPCS = append(service.RPCS, rpc)
case token.Name.Local == "param":
api.Params = append(api.Params, param)
case token.Name.Local == "bean":
Expand Down
10 changes: 9 additions & 1 deletion cmd/dolphin/schema/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,15 @@ func (c *Common) Ref(m string) string {
}
return fmt.Sprintf("model.%v", c.ToUpperCase(m))
}
return m
return c.ToUpperCase(m)
}

// PRef defined model name
func (c *Common) PRef(m string) string {
if strings.HasPrefix(m, "$") {
return fmt.Sprintf("%v", c.ToUpperCase(strings.ReplaceAll(m, "$", "")))
}
return c.ToUpperCase(m)
}

// SRef defined model name
Expand Down
28 changes: 27 additions & 1 deletion cmd/dolphin/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,38 @@ type API struct {
Return *Return
}

// Controller struct
// Controller deifned
type Controller struct {
Common
APIS []*API
Prefix string
}

// Service defined
type Service struct {
Common
RPCS []*RPC
}

// RPC defined
type RPC struct {
Common
Request *Request
Reply *Reply
}

// Request defined
type Request struct {
Common
Type string
}

// Reply defined
type Reply struct {
Common
Type string
}

// Prop struct
type Prop struct {
Common
Expand Down Expand Up @@ -91,6 +116,7 @@ type Application struct {
Common
PackageName string `validate:"required"`
Controllers []*Controller
Services []*Service
Beans []*Bean
Tables []*Table
}
Loading

0 comments on commit 5fbb867

Please sign in to comment.