optimized generator formatted code (#148)

* rebase upstream

* rebase

* trim no need line

* trim no need line

* trim no need line

* update doc

* remove update

* remove no need

* remove no need

* goctl add jwt support

* goctl add jwt support

* goctl add jwt support

* goctl support import

* goctl support import

* support return ()

* revert

* refactor and rename folder to group

* remove no need

* add anonymous annotation

* optimized

* rename

* rename

* update test

* api add middleware support: usage:

@server(
    middleware: M1, M2
)

* api add middleware support: usage:

@server(
    middleware: M1, M2
)

* simple logic

* optimized

* optimized generator formatted code

* optimized generator formatted code

* add more test

Co-authored-by: kingxt <dream4kingxt@163.com>
master
kingxt 4 years ago committed by GitHub
parent 1abe21aa2a
commit 1c1e4bca86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,11 +1,15 @@
package parser package gogen
import ( import (
goformat "go/format"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath"
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/tal-tech/go-zero/tools/goctl/api/parser"
) )
const testApiTemplate = ` const testApiTemplate = `
@ -137,13 +141,50 @@ service A-api {
} }
` `
const apiJwt = `
type Request struct {
Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
}
type Response struct {
Message string ` + "`" + `json:"message"` + "`" + `
}
@server(
jwt: Auth
)
service A-api {
@handler GreetHandler
get /greet/from/:name(Request) returns (Response)
}
`
const apiJwtWithMiddleware = `
type Request struct {
Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
}
type Response struct {
Message string ` + "`" + `json:"message"` + "`" + `
}
@server(
jwt: Auth
middleware: TokenValidate
)
service A-api {
@handler GreetHandler
get /greet/from/:name(Request) returns (Response)
}
`
func TestParser(t *testing.T) { func TestParser(t *testing.T) {
filename := "greet.api" filename := "greet.api"
err := ioutil.WriteFile(filename, []byte(testApiTemplate), os.ModePerm) err := ioutil.WriteFile(filename, []byte(testApiTemplate), os.ModePerm)
assert.Nil(t, err) assert.Nil(t, err)
defer os.Remove(filename) defer os.Remove(filename)
parser, err := NewParser(filename) parser, err := parser.NewParser(filename)
assert.Nil(t, err) assert.Nil(t, err)
api, err := parser.Parse() api, err := parser.Parse()
@ -157,6 +198,8 @@ func TestParser(t *testing.T) {
assert.Equal(t, api.Service.Routes[1].RequestType.Name, "Request") assert.Equal(t, api.Service.Routes[1].RequestType.Name, "Request")
assert.Equal(t, api.Service.Routes[1].ResponseType.Name, "") assert.Equal(t, api.Service.Routes[1].ResponseType.Name, "")
validate(t, filename)
} }
func TestMultiService(t *testing.T) { func TestMultiService(t *testing.T) {
@ -165,7 +208,7 @@ func TestMultiService(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
defer os.Remove(filename) defer os.Remove(filename)
parser, err := NewParser(filename) parser, err := parser.NewParser(filename)
assert.Nil(t, err) assert.Nil(t, err)
api, err := parser.Parse() api, err := parser.Parse()
@ -173,6 +216,8 @@ func TestMultiService(t *testing.T) {
assert.Equal(t, len(api.Service.Routes), 2) assert.Equal(t, len(api.Service.Routes), 2)
assert.Equal(t, len(api.Service.Groups), 2) assert.Equal(t, len(api.Service.Groups), 2)
validate(t, filename)
} }
func TestApiNoInfo(t *testing.T) { func TestApiNoInfo(t *testing.T) {
@ -181,11 +226,13 @@ func TestApiNoInfo(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
defer os.Remove(filename) defer os.Remove(filename)
parser, err := NewParser(filename) parser, err := parser.NewParser(filename)
assert.Nil(t, err) assert.Nil(t, err)
_, err = parser.Parse() _, err = parser.Parse()
assert.Nil(t, err) assert.Nil(t, err)
validate(t, filename)
} }
func TestInvalidApiFile(t *testing.T) { func TestInvalidApiFile(t *testing.T) {
@ -194,7 +241,7 @@ func TestInvalidApiFile(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
defer os.Remove(filename) defer os.Remove(filename)
parser, err := NewParser(filename) parser, err := parser.NewParser(filename)
assert.Nil(t, err) assert.Nil(t, err)
_, err = parser.Parse() _, err = parser.Parse()
@ -207,7 +254,7 @@ func TestAnonymousAnnotation(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
defer os.Remove(filename) defer os.Remove(filename)
parser, err := NewParser(filename) parser, err := parser.NewParser(filename)
assert.Nil(t, err) assert.Nil(t, err)
api, err := parser.Parse() api, err := parser.Parse()
@ -215,6 +262,8 @@ func TestAnonymousAnnotation(t *testing.T) {
assert.Equal(t, len(api.Service.Routes), 1) assert.Equal(t, len(api.Service.Routes), 1)
assert.Equal(t, api.Service.Routes[0].Annotations[0].Value, "GreetHandler") assert.Equal(t, api.Service.Routes[0].Annotations[0].Value, "GreetHandler")
validate(t, filename)
} }
func TestApiHasMiddleware(t *testing.T) { func TestApiHasMiddleware(t *testing.T) {
@ -223,9 +272,61 @@ func TestApiHasMiddleware(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
defer os.Remove(filename) defer os.Remove(filename)
parser, err := NewParser(filename) parser, err := parser.NewParser(filename)
assert.Nil(t, err)
_, err = parser.Parse()
assert.Nil(t, err)
validate(t, filename)
}
func TestApiHasJwt(t *testing.T) {
filename := "jwt.api"
err := ioutil.WriteFile(filename, []byte(apiJwt), os.ModePerm)
assert.Nil(t, err)
defer os.Remove(filename)
parser, err := parser.NewParser(filename)
assert.Nil(t, err) assert.Nil(t, err)
_, err = parser.Parse() _, err = parser.Parse()
assert.Nil(t, err) assert.Nil(t, err)
validate(t, filename)
}
func TestApiHasJwtAndMiddleware(t *testing.T) {
filename := "jwt.api"
err := ioutil.WriteFile(filename, []byte(apiJwtWithMiddleware), os.ModePerm)
assert.Nil(t, err)
defer os.Remove(filename)
parser, err := parser.NewParser(filename)
assert.Nil(t, err)
_, err = parser.Parse()
assert.Nil(t, err)
validate(t, filename)
}
func validate(t *testing.T, api string) {
dir := "_go"
err := DoGenProject(api, dir, true)
defer os.RemoveAll(dir)
assert.Nil(t, err)
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(path, ".go") {
code, err := ioutil.ReadFile(path)
assert.Nil(t, err)
assert.Nil(t, validateCode(string(code)))
}
return nil
})
}
func validateCode(code string) error {
_, err := goformat.Source([]byte(code))
return err
} }

@ -32,8 +32,8 @@ func RegisterHandlers(engine *rest.Server, serverCtx *svc.ServiceContext) {
` `
routesAdditionTemplate = ` routesAdditionTemplate = `
engine.AddRoutes( engine.AddRoutes(
{{.routes}} {{.routes}} {{.jwt}}{{.signature}}
{{.jwt}}{{.signature}}) )
` `
) )
@ -71,6 +71,7 @@ func genRoutes(dir string, api *spec.ApiSpec, force bool) error {
gt := template.Must(template.New("groupTemplate").Parse(routesAdditionTemplate)) gt := template.Must(template.New("groupTemplate").Parse(routesAdditionTemplate))
for _, g := range groups { for _, g := range groups {
var gbuilder strings.Builder var gbuilder strings.Builder
gbuilder.WriteString("[]rest.Route{")
for _, r := range g.routes { for _, r := range g.routes {
fmt.Fprintf(&gbuilder, ` fmt.Fprintf(&gbuilder, `
{ {
@ -80,26 +81,29 @@ func genRoutes(dir string, api *spec.ApiSpec, force bool) error {
},`, },`,
r.method, r.path, r.handler) r.method, r.path, r.handler)
} }
var jwt string var jwt string
if g.jwtEnabled { if g.jwtEnabled {
jwt = fmt.Sprintf(", rest.WithJwt(serverCtx.Config.%s.AccessSecret)", g.authName) jwt = fmt.Sprintf("\n rest.WithJwt(serverCtx.Config.%s.AccessSecret),", g.authName)
} }
var signature string var signature string
if g.signatureEnabled { if g.signatureEnabled {
signature = fmt.Sprintf(", rest.WithSignature(serverCtx.Config.%s.Signature)", g.authName) signature = fmt.Sprintf("\n rest.WithSignature(serverCtx.Config.%s.Signature),", g.authName)
} }
var routes string var routes string
if len(g.middleware) > 0 { if len(g.middleware) > 0 {
gbuilder.WriteString("\n}...,")
var params = g.middleware var params = g.middleware
for i := range params { for i := range params {
params[i] = "serverCtx." + params[i] params[i] = "serverCtx." + params[i]
} }
var middlewareStr = strings.Join(params, ", ") var middlewareStr = strings.Join(params, ", ")
routes = fmt.Sprintf("rest.WithMiddlewares(\n[]rest.Middleware{ %s }, \n[]rest.Route{\n %s \n}...,\n),", routes = fmt.Sprintf("rest.WithMiddlewares(\n[]rest.Middleware{ %s }, \n %s \n),",
middlewareStr, strings.TrimSpace(gbuilder.String())) middlewareStr, strings.TrimSpace(gbuilder.String()))
} else { } else {
routes = fmt.Sprintf("[]rest.Route{\n %s \n},", strings.TrimSpace(gbuilder.String())) gbuilder.WriteString("\n},")
routes = strings.TrimSpace(gbuilder.String())
} }
if err := gt.Execute(&builder, map[string]string{ if err := gt.Execute(&builder, map[string]string{

Loading…
Cancel
Save