Code optimized (#523)
* optimized markdown generator * optimized markdown generator * optimized markdown generator * add more comment * add comment * add comment * add comments for rpc tool * add comments for model tool * add comments for model tool * add comments for model tool * add comments for config tool * add comments for config tool * add comments * add comments * add comments * add comments * add comment * remove rpc main head info * add comment * optimized Co-authored-by: anqiansong <anqiansong@xiaoheiban.cn>
This commit is contained in:
@@ -29,7 +29,7 @@ func DocCommand(c *cli.Context) error {
|
||||
}
|
||||
|
||||
if !util.FileExists(dir) {
|
||||
return errors.New(fmt.Sprintf("dir %s not exsit", dir))
|
||||
return fmt.Errorf("dir %s not exsit", dir)
|
||||
}
|
||||
|
||||
dir, err := filepath.Abs(dir)
|
||||
|
||||
@@ -25,12 +25,13 @@ const (
|
||||
rightBrace = "}"
|
||||
)
|
||||
|
||||
// GoFormatApi format api file
|
||||
func GoFormatApi(c *cli.Context) error {
|
||||
useStdin := c.Bool("stdin")
|
||||
|
||||
var be errorx.BatchError
|
||||
if useStdin {
|
||||
if err := ApiFormatByStdin(); err != nil {
|
||||
if err := apiFormatByStdin(); err != nil {
|
||||
be.Add(err)
|
||||
}
|
||||
} else {
|
||||
@@ -63,7 +64,7 @@ func GoFormatApi(c *cli.Context) error {
|
||||
return be.Err()
|
||||
}
|
||||
|
||||
func ApiFormatByStdin() error {
|
||||
func apiFormatByStdin() error {
|
||||
data, err := ioutil.ReadAll(os.Stdin)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -78,6 +79,7 @@ func ApiFormatByStdin() error {
|
||||
return err
|
||||
}
|
||||
|
||||
// ApiFormatByPath format api from file path
|
||||
func ApiFormatByPath(apiFilePath string) error {
|
||||
data, err := ioutil.ReadFile(apiFilePath)
|
||||
if err != nil {
|
||||
@@ -135,19 +137,19 @@ func apiFormat(data string) (string, error) {
|
||||
|
||||
noCommentLine := util.RemoveComment(line)
|
||||
if noCommentLine == rightParenthesis || noCommentLine == rightBrace {
|
||||
tapCount -= 1
|
||||
tapCount--
|
||||
}
|
||||
if tapCount < 0 {
|
||||
line := strings.TrimSuffix(noCommentLine, rightBrace)
|
||||
line = strings.TrimSpace(line)
|
||||
if strings.HasSuffix(line, leftBrace) {
|
||||
tapCount += 1
|
||||
tapCount++
|
||||
}
|
||||
}
|
||||
util.WriteIndent(&builder, tapCount)
|
||||
builder.WriteString(line + ctlutil.NL)
|
||||
if strings.HasSuffix(noCommentLine, leftParenthesis) || strings.HasSuffix(noCommentLine, leftBrace) {
|
||||
tapCount += 1
|
||||
tapCount++
|
||||
}
|
||||
preLine = line
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ const tmpFile = "%s-%d"
|
||||
|
||||
var tmpDir = path.Join(os.TempDir(), "goctl")
|
||||
|
||||
// GoCommand gen go project files from command line
|
||||
func GoCommand(c *cli.Context) error {
|
||||
apiFile := c.String("api")
|
||||
dir := c.String("dir")
|
||||
@@ -40,6 +41,7 @@ func GoCommand(c *cli.Context) error {
|
||||
return DoGenProject(apiFile, dir, namingStyle)
|
||||
}
|
||||
|
||||
// DoGenProject gen go project files with api file
|
||||
func DoGenProject(apiFile, dir, style string) error {
|
||||
api, err := parser.Parse(apiFile)
|
||||
if err != nil {
|
||||
|
||||
@@ -39,7 +39,7 @@ func {{.HandlerName}}(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
`
|
||||
|
||||
type Handler struct {
|
||||
type handlerInfo struct {
|
||||
ImportPackages string
|
||||
HandlerName string
|
||||
RequestType string
|
||||
@@ -59,7 +59,7 @@ func genHandler(dir string, cfg *config.Config, group spec.Group, route spec.Rou
|
||||
return err
|
||||
}
|
||||
|
||||
return doGenToFile(dir, handler, cfg, group, route, Handler{
|
||||
return doGenToFile(dir, handler, cfg, group, route, handlerInfo{
|
||||
ImportPackages: genHandlerImports(group, route, parentPkg),
|
||||
HandlerName: handler,
|
||||
RequestType: util.Title(route.RequestTypeName()),
|
||||
@@ -71,7 +71,7 @@ func genHandler(dir string, cfg *config.Config, group spec.Group, route spec.Rou
|
||||
}
|
||||
|
||||
func doGenToFile(dir, handler string, cfg *config.Config, group spec.Group,
|
||||
route spec.Route, handleObj Handler) error {
|
||||
route spec.Route, handleObj handlerInfo) error {
|
||||
filename, err := format.FileNamingFormat(cfg.NamingFormat, handler)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -25,6 +25,7 @@ import (
|
||||
`
|
||||
)
|
||||
|
||||
// BuildTypes gen types to string
|
||||
func BuildTypes(types []spec.Type) (string, error) {
|
||||
var builder strings.Builder
|
||||
first := true
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
// JavaCommand the generate java code command entrance
|
||||
func JavaCommand(c *cli.Context) error {
|
||||
apiFile := c.String("api")
|
||||
dir := c.String("dir")
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
// KtCommand the generate kotlin code command entrance
|
||||
func KtCommand(c *cli.Context) error {
|
||||
apiFile := c.String("api")
|
||||
if apiFile == "" {
|
||||
|
||||
@@ -27,7 +27,8 @@ service {{.name}}-api {
|
||||
}
|
||||
`
|
||||
|
||||
func NewService(c *cli.Context) error {
|
||||
// CreateServiceCommand fast create service
|
||||
func CreateServiceCommand(c *cli.Context) error {
|
||||
args := c.Args()
|
||||
dirName := args.First()
|
||||
if len(dirName) == 0 {
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/gen/api"
|
||||
)
|
||||
|
||||
// Api describes syntax for api
|
||||
type Api struct {
|
||||
LinePrefix string
|
||||
Syntax *SyntaxExpr
|
||||
@@ -21,6 +22,7 @@ type Api struct {
|
||||
routeM map[string]PlaceHolder
|
||||
}
|
||||
|
||||
// VisitApi implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitApi(ctx *api.ApiContext) interface{} {
|
||||
var final Api
|
||||
final.importM = map[string]PlaceHolder{}
|
||||
@@ -152,6 +154,7 @@ func (v *ApiVisitor) acceptSyntax(root *Api, final *Api) {
|
||||
}
|
||||
}
|
||||
|
||||
// VisitSpec implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitSpec(ctx *api.SpecContext) interface{} {
|
||||
var root Api
|
||||
if ctx.SyntaxLit() != nil {
|
||||
@@ -178,11 +181,13 @@ func (v *ApiVisitor) VisitSpec(ctx *api.SpecContext) interface{} {
|
||||
return &root
|
||||
}
|
||||
|
||||
// Format provides a formatter for api command, now nothing to do
|
||||
func (a *Api) Format() error {
|
||||
// todo
|
||||
return nil
|
||||
}
|
||||
|
||||
// Equal compares whether the element literals in two Api are equal
|
||||
func (a *Api) Equal(v interface{}) bool {
|
||||
if v == nil {
|
||||
return false
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
)
|
||||
|
||||
type (
|
||||
// Parser provides api parsing capabilities
|
||||
Parser struct {
|
||||
linePrefix string
|
||||
debug bool
|
||||
@@ -19,9 +20,11 @@ type (
|
||||
antlr.DefaultErrorListener
|
||||
}
|
||||
|
||||
// ParserOption defines an function with argument Parser
|
||||
ParserOption func(p *Parser)
|
||||
)
|
||||
|
||||
// NewParser creates an instance for Parser
|
||||
func NewParser(options ...ParserOption) *Parser {
|
||||
p := &Parser{
|
||||
log: console.NewColorConsole(),
|
||||
@@ -425,6 +428,7 @@ func (p *Parser) readContent(filename string) (string, error) {
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
// SyntaxError accepts errors and panic it
|
||||
func (p *Parser) SyntaxError(_ antlr.Recognizer, _ interface{}, line, column int, msg string, _ antlr.RecognitionException) {
|
||||
str := fmt.Sprintf(`%s line %d:%d %s`, p.linePrefix, line, column, msg)
|
||||
if p.debug {
|
||||
@@ -433,12 +437,14 @@ func (p *Parser) SyntaxError(_ antlr.Recognizer, _ interface{}, line, column int
|
||||
panic(str)
|
||||
}
|
||||
|
||||
// WithParserDebug returns a debug ParserOption
|
||||
func WithParserDebug() ParserOption {
|
||||
return func(p *Parser) {
|
||||
p.debug = true
|
||||
}
|
||||
}
|
||||
|
||||
// WithParserPrefix returns a prefix ParserOption
|
||||
func WithParserPrefix(prefix string) ParserOption {
|
||||
return func(p *Parser) {
|
||||
p.linePrefix = prefix
|
||||
|
||||
@@ -12,11 +12,15 @@ import (
|
||||
)
|
||||
|
||||
type (
|
||||
// TokenStream defines a token
|
||||
TokenStream interface {
|
||||
GetStart() antlr.Token
|
||||
GetStop() antlr.Token
|
||||
GetParser() antlr.Parser
|
||||
}
|
||||
|
||||
// ApiVisitor wraps api.BaseApiParserVisitor to call methods which has prefix Visit to
|
||||
// visit node from the api syntax
|
||||
ApiVisitor struct {
|
||||
api.BaseApiParserVisitor
|
||||
debug bool
|
||||
@@ -25,8 +29,10 @@ type (
|
||||
infoFlag bool
|
||||
}
|
||||
|
||||
// VisitorOption defines a function with argument ApiVisitor
|
||||
VisitorOption func(v *ApiVisitor)
|
||||
|
||||
// Spec describes api spec
|
||||
Spec interface {
|
||||
Doc() []Expr
|
||||
Comment() Expr
|
||||
@@ -34,6 +40,7 @@ type (
|
||||
Equal(v interface{}) bool
|
||||
}
|
||||
|
||||
// Expr describes ast expression
|
||||
Expr interface {
|
||||
Prefix() string
|
||||
Line() int
|
||||
@@ -47,6 +54,7 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
// NewApiVisitor creates an instance for ApiVisitor
|
||||
func NewApiVisitor(options ...VisitorOption) *ApiVisitor {
|
||||
v := &ApiVisitor{
|
||||
log: console.NewColorConsole(),
|
||||
@@ -66,12 +74,14 @@ func (v *ApiVisitor) panic(expr Expr, msg string) {
|
||||
panic(errString)
|
||||
}
|
||||
|
||||
// WithVisitorPrefix returns a VisitorOption wrap with specified prefix
|
||||
func WithVisitorPrefix(prefix string) VisitorOption {
|
||||
return func(v *ApiVisitor) {
|
||||
v.prefix = prefix
|
||||
}
|
||||
}
|
||||
|
||||
// WithVisitorDebug returns a debug VisitorOption
|
||||
func WithVisitorDebug() VisitorOption {
|
||||
return func(v *ApiVisitor) {
|
||||
v.debug = true
|
||||
@@ -84,6 +94,7 @@ type defaultExpr struct {
|
||||
start, stop int
|
||||
}
|
||||
|
||||
// NewTextExpr creates a default instance for Expr
|
||||
func NewTextExpr(v string) *defaultExpr {
|
||||
return &defaultExpr{
|
||||
v: v,
|
||||
@@ -201,6 +212,7 @@ func (e *defaultExpr) IsNotNil() bool {
|
||||
return e != nil
|
||||
}
|
||||
|
||||
// EqualDoc compares whether the element literals in two Spec are equal
|
||||
func EqualDoc(spec1, spec2 Spec) bool {
|
||||
if spec1 == nil {
|
||||
return spec2 == nil
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/gen/api"
|
||||
)
|
||||
|
||||
// ImportExpr defines import syntax for api
|
||||
type ImportExpr struct {
|
||||
Import Expr
|
||||
Value Expr
|
||||
@@ -11,6 +12,7 @@ type ImportExpr struct {
|
||||
CommentExpr Expr
|
||||
}
|
||||
|
||||
// VisitImportSpec implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitImportSpec(ctx *api.ImportSpecContext) interface{} {
|
||||
var list []*ImportExpr
|
||||
if ctx.ImportLit() != nil {
|
||||
@@ -25,6 +27,7 @@ func (v *ApiVisitor) VisitImportSpec(ctx *api.ImportSpecContext) interface{} {
|
||||
return list
|
||||
}
|
||||
|
||||
// VisitImportLit implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitImportLit(ctx *api.ImportLitContext) interface{} {
|
||||
importToken := v.newExprWithToken(ctx.GetImportToken())
|
||||
valueExpr := ctx.ImportValue().Accept(v).(Expr)
|
||||
@@ -38,6 +41,7 @@ func (v *ApiVisitor) VisitImportLit(ctx *api.ImportLitContext) interface{} {
|
||||
}
|
||||
}
|
||||
|
||||
// VisitImportBlock implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitImportBlock(ctx *api.ImportBlockContext) interface{} {
|
||||
importToken := v.newExprWithToken(ctx.GetImportToken())
|
||||
values := ctx.AllImportBlockValue()
|
||||
@@ -52,6 +56,7 @@ func (v *ApiVisitor) VisitImportBlock(ctx *api.ImportBlockContext) interface{} {
|
||||
return list
|
||||
}
|
||||
|
||||
// VisitImportBlockValue implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitImportBlockValue(ctx *api.ImportBlockValueContext) interface{} {
|
||||
value := ctx.ImportValue().Accept(v).(Expr)
|
||||
return &ImportExpr{
|
||||
@@ -61,15 +66,18 @@ func (v *ApiVisitor) VisitImportBlockValue(ctx *api.ImportBlockValueContext) int
|
||||
}
|
||||
}
|
||||
|
||||
// VisitImportValue implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitImportValue(ctx *api.ImportValueContext) interface{} {
|
||||
return v.newExprWithTerminalNode(ctx.STRING())
|
||||
}
|
||||
|
||||
// Format provides a formatter for api command, now nothing to do
|
||||
func (i *ImportExpr) Format() error {
|
||||
// todo
|
||||
return nil
|
||||
}
|
||||
|
||||
// Equal compares whether the element literals in two ImportExpr are equal
|
||||
func (i *ImportExpr) Equal(v interface{}) bool {
|
||||
if v == nil {
|
||||
return false
|
||||
@@ -87,10 +95,12 @@ func (i *ImportExpr) Equal(v interface{}) bool {
|
||||
return i.Import.Equal(imp.Import) && i.Value.Equal(imp.Value)
|
||||
}
|
||||
|
||||
// Doc returns the document of ImportExpr, like // some text
|
||||
func (i *ImportExpr) Doc() []Expr {
|
||||
return i.DocExpr
|
||||
}
|
||||
|
||||
// Comment returns the comment of ImportExpr, like // some text
|
||||
func (i *ImportExpr) Comment() Expr {
|
||||
return i.CommentExpr
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/gen/api"
|
||||
)
|
||||
|
||||
// InfoExpr defines info syntax for api
|
||||
type InfoExpr struct {
|
||||
Info Expr
|
||||
Lp Expr
|
||||
@@ -11,6 +12,7 @@ type InfoExpr struct {
|
||||
Kvs []*KvExpr
|
||||
}
|
||||
|
||||
// VisitInfoSpec implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitInfoSpec(ctx *api.InfoSpecContext) interface{} {
|
||||
var expr InfoExpr
|
||||
expr.Info = v.newExprWithToken(ctx.GetInfoToken())
|
||||
@@ -29,11 +31,13 @@ func (v *ApiVisitor) VisitInfoSpec(ctx *api.InfoSpecContext) interface{} {
|
||||
return &expr
|
||||
}
|
||||
|
||||
// Format provides a formatter for api command, now nothing to do
|
||||
func (i *InfoExpr) Format() error {
|
||||
// todo
|
||||
return nil
|
||||
}
|
||||
|
||||
// Equal compares whether the element literals in two InfoExpr are equal
|
||||
func (i *InfoExpr) Equal(v interface{}) bool {
|
||||
if v == nil {
|
||||
return false
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/gen/api"
|
||||
)
|
||||
|
||||
// KvExpr describes key-value for api
|
||||
type KvExpr struct {
|
||||
Key Expr
|
||||
Value Expr
|
||||
@@ -13,6 +14,7 @@ type KvExpr struct {
|
||||
CommentExpr Expr
|
||||
}
|
||||
|
||||
// VisitKvLit implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitKvLit(ctx *api.KvLitContext) interface{} {
|
||||
var kvExpr KvExpr
|
||||
kvExpr.Key = v.newExprWithToken(ctx.GetKey())
|
||||
@@ -48,11 +50,13 @@ func (v *ApiVisitor) VisitKvLit(ctx *api.KvLitContext) interface{} {
|
||||
return &kvExpr
|
||||
}
|
||||
|
||||
// Format provides a formatter for api command, now nothing to do
|
||||
func (k *KvExpr) Format() error {
|
||||
// todo
|
||||
return nil
|
||||
}
|
||||
|
||||
// Equal compares whether the element literals in two KvExpr are equal
|
||||
func (k *KvExpr) Equal(v interface{}) bool {
|
||||
if v == nil {
|
||||
return false
|
||||
@@ -70,10 +74,12 @@ func (k *KvExpr) Equal(v interface{}) bool {
|
||||
return k.Key.Equal(kv.Key) && k.Value.Equal(kv.Value)
|
||||
}
|
||||
|
||||
// Doc returns the document of KvExpr, like // some text
|
||||
func (k *KvExpr) Doc() []Expr {
|
||||
return k.DocExpr
|
||||
}
|
||||
|
||||
// Comment returns the comment of KvExpr, like // some text
|
||||
func (k *KvExpr) Comment() Expr {
|
||||
return k.CommentExpr
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package ast
|
||||
|
||||
// Holder defines a default instance for PlaceHolder
|
||||
var Holder PlaceHolder
|
||||
|
||||
// PlaceHolder defines an empty struct
|
||||
type PlaceHolder struct{}
|
||||
|
||||
@@ -7,13 +7,16 @@ import (
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/gen/api"
|
||||
)
|
||||
|
||||
// Service describes service for api syntax
|
||||
type Service struct {
|
||||
AtServer *AtServer
|
||||
ServiceApi *ServiceApi
|
||||
}
|
||||
|
||||
// KV defines a slice for KvExpr
|
||||
type KV []*KvExpr
|
||||
|
||||
// AtServer describes server metadata for api syntax
|
||||
type AtServer struct {
|
||||
AtServerToken Expr
|
||||
Lp Expr
|
||||
@@ -21,6 +24,7 @@ type AtServer struct {
|
||||
Kv KV
|
||||
}
|
||||
|
||||
// ServiceApi describes service ast for api syntax
|
||||
type ServiceApi struct {
|
||||
ServiceToken Expr
|
||||
Name Expr
|
||||
@@ -29,6 +33,7 @@ type ServiceApi struct {
|
||||
ServiceRoute []*ServiceRoute
|
||||
}
|
||||
|
||||
// ServiceRoute describes service route ast for api syntax
|
||||
type ServiceRoute struct {
|
||||
AtDoc *AtDoc
|
||||
AtServer *AtServer
|
||||
@@ -36,6 +41,7 @@ type ServiceRoute struct {
|
||||
Route *Route
|
||||
}
|
||||
|
||||
// AtDoc describes service comments ast for api syntax
|
||||
type AtDoc struct {
|
||||
AtDocToken Expr
|
||||
Lp Expr
|
||||
@@ -44,6 +50,7 @@ type AtDoc struct {
|
||||
Kv []*KvExpr
|
||||
}
|
||||
|
||||
// AtHandler describes service hander ast for api syntax
|
||||
type AtHandler struct {
|
||||
AtHandlerToken Expr
|
||||
Name Expr
|
||||
@@ -51,6 +58,7 @@ type AtHandler struct {
|
||||
CommentExpr Expr
|
||||
}
|
||||
|
||||
// Route describes route ast for api syntax
|
||||
type Route struct {
|
||||
Method Expr
|
||||
Path Expr
|
||||
@@ -61,12 +69,14 @@ type Route struct {
|
||||
CommentExpr Expr
|
||||
}
|
||||
|
||||
// Body describes request,response body ast for api syntax
|
||||
type Body struct {
|
||||
Lp Expr
|
||||
Rp Expr
|
||||
Name DataType
|
||||
}
|
||||
|
||||
// VisitServiceSpec implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitServiceSpec(ctx *api.ServiceSpecContext) interface{} {
|
||||
var serviceSpec Service
|
||||
if ctx.AtServer() != nil {
|
||||
@@ -77,6 +87,7 @@ func (v *ApiVisitor) VisitServiceSpec(ctx *api.ServiceSpecContext) interface{} {
|
||||
return &serviceSpec
|
||||
}
|
||||
|
||||
// VisitAtServer implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitAtServer(ctx *api.AtServerContext) interface{} {
|
||||
var atServer AtServer
|
||||
atServer.AtServerToken = v.newExprWithTerminalNode(ctx.ATSERVER())
|
||||
@@ -90,6 +101,7 @@ func (v *ApiVisitor) VisitAtServer(ctx *api.AtServerContext) interface{} {
|
||||
return &atServer
|
||||
}
|
||||
|
||||
// VisitServiceApi implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitServiceApi(ctx *api.ServiceApiContext) interface{} {
|
||||
var serviceApi ServiceApi
|
||||
serviceApi.ServiceToken = v.newExprWithToken(ctx.GetServiceToken())
|
||||
@@ -105,6 +117,7 @@ func (v *ApiVisitor) VisitServiceApi(ctx *api.ServiceApiContext) interface{} {
|
||||
return &serviceApi
|
||||
}
|
||||
|
||||
// VisitServiceRoute implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitServiceRoute(ctx *api.ServiceRouteContext) interface{} {
|
||||
var serviceRoute ServiceRoute
|
||||
if ctx.AtDoc() != nil {
|
||||
@@ -121,6 +134,7 @@ func (v *ApiVisitor) VisitServiceRoute(ctx *api.ServiceRouteContext) interface{}
|
||||
return &serviceRoute
|
||||
}
|
||||
|
||||
// VisitAtDoc implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitAtDoc(ctx *api.AtDocContext) interface{} {
|
||||
var atDoc AtDoc
|
||||
atDoc.AtDocToken = v.newExprWithTerminalNode(ctx.ATDOC())
|
||||
@@ -150,6 +164,7 @@ func (v *ApiVisitor) VisitAtDoc(ctx *api.AtDocContext) interface{} {
|
||||
return &atDoc
|
||||
}
|
||||
|
||||
// VisitAtHandler implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitAtHandler(ctx *api.AtHandlerContext) interface{} {
|
||||
var atHandler AtHandler
|
||||
astHandlerExpr := v.newExprWithTerminalNode(ctx.ATHANDLER())
|
||||
@@ -160,6 +175,7 @@ func (v *ApiVisitor) VisitAtHandler(ctx *api.AtHandlerContext) interface{} {
|
||||
return &atHandler
|
||||
}
|
||||
|
||||
// VisitRoute implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitRoute(ctx *api.RouteContext) interface{} {
|
||||
var route Route
|
||||
path := ctx.Path()
|
||||
@@ -193,6 +209,7 @@ func (v *ApiVisitor) VisitRoute(ctx *api.RouteContext) interface{} {
|
||||
return &route
|
||||
}
|
||||
|
||||
// VisitBody implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitBody(ctx *api.BodyContext) interface{} {
|
||||
if ctx.ID() == nil {
|
||||
return nil
|
||||
@@ -211,7 +228,7 @@ func (v *ApiVisitor) VisitBody(ctx *api.BodyContext) interface{} {
|
||||
}
|
||||
}
|
||||
|
||||
// note: forward compatible
|
||||
// VisitReplybody implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitReplybody(ctx *api.ReplybodyContext) interface{} {
|
||||
if ctx.DataType() == nil {
|
||||
return nil
|
||||
@@ -253,10 +270,13 @@ func (v *ApiVisitor) VisitReplybody(ctx *api.ReplybodyContext) interface{} {
|
||||
}
|
||||
}
|
||||
|
||||
// Format provides a formatter for api command, now nothing to do
|
||||
func (b *Body) Format() error {
|
||||
// todo
|
||||
return nil
|
||||
}
|
||||
|
||||
// Equal compares whether the element literals in two Body are equal
|
||||
func (b *Body) Equal(v interface{}) bool {
|
||||
if v == nil {
|
||||
return false
|
||||
@@ -278,19 +298,23 @@ func (b *Body) Equal(v interface{}) bool {
|
||||
return b.Name.Equal(body.Name)
|
||||
}
|
||||
|
||||
// Format provides a formatter for api command, now nothing to do
|
||||
func (r *Route) Format() error {
|
||||
// todo
|
||||
return nil
|
||||
}
|
||||
|
||||
// Doc returns the document of Route, like // some text
|
||||
func (r *Route) Doc() []Expr {
|
||||
return r.DocExpr
|
||||
}
|
||||
|
||||
// Comment returns the comment of Route, like // some text
|
||||
func (r *Route) Comment() Expr {
|
||||
return r.CommentExpr
|
||||
}
|
||||
|
||||
// Equal compares whether the element literals in two Route are equal
|
||||
func (r *Route) Equal(v interface{}) bool {
|
||||
if v == nil {
|
||||
return false
|
||||
@@ -330,19 +354,23 @@ func (r *Route) Equal(v interface{}) bool {
|
||||
return EqualDoc(r, route)
|
||||
}
|
||||
|
||||
// Doc returns the document of AtHandler, like // some text
|
||||
func (a *AtHandler) Doc() []Expr {
|
||||
return a.DocExpr
|
||||
}
|
||||
|
||||
// Comment returns the comment of AtHandler, like // some text
|
||||
func (a *AtHandler) Comment() Expr {
|
||||
return a.CommentExpr
|
||||
}
|
||||
|
||||
// Format provides a formatter for api command, now nothing to do
|
||||
func (a *AtHandler) Format() error {
|
||||
// todo
|
||||
return nil
|
||||
}
|
||||
|
||||
// Equal compares whether the element literals in two AtHandler are equal
|
||||
func (a *AtHandler) Equal(v interface{}) bool {
|
||||
if v == nil {
|
||||
return false
|
||||
@@ -364,11 +392,13 @@ func (a *AtHandler) Equal(v interface{}) bool {
|
||||
return EqualDoc(a, atHandler)
|
||||
}
|
||||
|
||||
// Format provides a formatter for api command, now nothing to do
|
||||
func (a *AtDoc) Format() error {
|
||||
// todo
|
||||
return nil
|
||||
}
|
||||
|
||||
// Equal compares whether the element literals in two AtDoc are equal
|
||||
func (a *AtDoc) Equal(v interface{}) bool {
|
||||
if v == nil {
|
||||
return false
|
||||
@@ -419,11 +449,13 @@ func (a *AtDoc) Equal(v interface{}) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// Format provides a formatter for api command, now nothing to do
|
||||
func (a *AtServer) Format() error {
|
||||
// todo
|
||||
return nil
|
||||
}
|
||||
|
||||
// Equal compares whether the element literals in two AtServer are equal
|
||||
func (a *AtServer) Equal(v interface{}) bool {
|
||||
if v == nil {
|
||||
return false
|
||||
@@ -471,6 +503,7 @@ func (a *AtServer) Equal(v interface{}) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// Equal compares whether the element literals in two ServiceRoute are equal
|
||||
func (s *ServiceRoute) Equal(v interface{}) bool {
|
||||
if v == nil {
|
||||
return false
|
||||
@@ -500,11 +533,13 @@ func (s *ServiceRoute) Equal(v interface{}) bool {
|
||||
return s.Route.Equal(sr.Route)
|
||||
}
|
||||
|
||||
// Format provides a formatter for api command, now nothing to do
|
||||
func (s *ServiceRoute) Format() error {
|
||||
// todo
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetHandler returns handler name of api route
|
||||
func (s *ServiceRoute) GetHandler() Expr {
|
||||
if s.AtHandler != nil {
|
||||
return s.AtHandler.Name
|
||||
@@ -513,11 +548,13 @@ func (s *ServiceRoute) GetHandler() Expr {
|
||||
return s.AtServer.Kv.Get("handler")
|
||||
}
|
||||
|
||||
// Format provides a formatter for api command, now nothing to do
|
||||
func (a *ServiceApi) Format() error {
|
||||
// todo
|
||||
return nil
|
||||
}
|
||||
|
||||
// Equal compares whether the element literals in two ServiceApi are equal
|
||||
func (a *ServiceApi) Equal(v interface{}) bool {
|
||||
if v == nil {
|
||||
return false
|
||||
@@ -569,11 +606,13 @@ func (a *ServiceApi) Equal(v interface{}) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// Format provides a formatter for api command, now nothing to do
|
||||
func (s *Service) Format() error {
|
||||
// todo
|
||||
return nil
|
||||
}
|
||||
|
||||
// Equal compares whether the element literals in two Service are equal
|
||||
func (s *Service) Equal(v interface{}) bool {
|
||||
if v == nil {
|
||||
return false
|
||||
@@ -593,6 +632,7 @@ func (s *Service) Equal(v interface{}) bool {
|
||||
return s.ServiceApi.Equal(service.ServiceApi)
|
||||
}
|
||||
|
||||
// Get returns the tergate KV by specified key
|
||||
func (kv KV) Get(key string) Expr {
|
||||
for _, each := range kv {
|
||||
if each.Key.Text() == key {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/gen/api"
|
||||
)
|
||||
|
||||
// SyntaxExpr describes syntax for api
|
||||
type SyntaxExpr struct {
|
||||
Syntax Expr
|
||||
Assign Expr
|
||||
@@ -12,6 +13,7 @@ type SyntaxExpr struct {
|
||||
CommentExpr Expr
|
||||
}
|
||||
|
||||
// VisitSyntaxLit implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitSyntaxLit(ctx *api.SyntaxLitContext) interface{} {
|
||||
syntax := v.newExprWithToken(ctx.GetSyntaxToken())
|
||||
assign := v.newExprWithToken(ctx.GetAssign())
|
||||
@@ -25,11 +27,13 @@ func (v *ApiVisitor) VisitSyntaxLit(ctx *api.SyntaxLitContext) interface{} {
|
||||
}
|
||||
}
|
||||
|
||||
// Format provides a formatter for api command, now nothing to do
|
||||
func (s *SyntaxExpr) Format() error {
|
||||
// todo
|
||||
return nil
|
||||
}
|
||||
|
||||
// Equal compares whether the element literals in two SyntaxExpr are equal
|
||||
func (s *SyntaxExpr) Equal(v interface{}) bool {
|
||||
if v == nil {
|
||||
return false
|
||||
@@ -49,10 +53,12 @@ func (s *SyntaxExpr) Equal(v interface{}) bool {
|
||||
s.Version.Equal(syntax.Version)
|
||||
}
|
||||
|
||||
// Doc returns the document of SyntaxExpr, like // some text
|
||||
func (s *SyntaxExpr) Doc() []Expr {
|
||||
return s.DocExpr
|
||||
}
|
||||
|
||||
// Comment returns the comment of SyntaxExpr, like // some text
|
||||
func (s *SyntaxExpr) Comment() Expr {
|
||||
return s.CommentExpr
|
||||
}
|
||||
|
||||
@@ -9,13 +9,15 @@ import (
|
||||
)
|
||||
|
||||
type (
|
||||
// TypeAlias、 TypeStruct
|
||||
// TypeExpr describes an expression for TypeAlias and TypeStruct
|
||||
TypeExpr interface {
|
||||
Doc() []Expr
|
||||
Format() error
|
||||
Equal(v interface{}) bool
|
||||
NameExpr() Expr
|
||||
}
|
||||
|
||||
// TypeAlias describes alias ast for api syatax
|
||||
TypeAlias struct {
|
||||
Name Expr
|
||||
Assign Expr
|
||||
@@ -24,6 +26,7 @@ type (
|
||||
CommentExpr Expr
|
||||
}
|
||||
|
||||
// TypeStruct describes structure ast for api syatax
|
||||
TypeStruct struct {
|
||||
Name Expr
|
||||
Struct Expr
|
||||
@@ -33,6 +36,7 @@ type (
|
||||
Fields []*TypeField
|
||||
}
|
||||
|
||||
// TypeField describes field ast for api syntax
|
||||
TypeField struct {
|
||||
IsAnonymous bool
|
||||
// Name is nil if IsAnonymous
|
||||
@@ -43,6 +47,7 @@ type (
|
||||
CommentExpr Expr
|
||||
}
|
||||
|
||||
// DataType describes datatype for api syntax, the default implementation expressions are
|
||||
// Literal, Interface, Map, Array, Time, Pointer
|
||||
DataType interface {
|
||||
Expr() Expr
|
||||
@@ -51,15 +56,18 @@ type (
|
||||
IsNotNil() bool
|
||||
}
|
||||
|
||||
// int, bool, Foo,...
|
||||
// Literal describes the basic types of golang, non-reference types,
|
||||
// such as int, bool, Foo,...
|
||||
Literal struct {
|
||||
Literal Expr
|
||||
}
|
||||
|
||||
// Interface describes the interface type of golang,Its fixed value is interface{}
|
||||
Interface struct {
|
||||
Literal Expr
|
||||
}
|
||||
|
||||
// Map describes the map ast for api syntax
|
||||
Map struct {
|
||||
MapExpr Expr
|
||||
Map Expr
|
||||
@@ -69,6 +77,7 @@ type (
|
||||
Value DataType
|
||||
}
|
||||
|
||||
// Array describes the slice ast for api syntax
|
||||
Array struct {
|
||||
ArrayExpr Expr
|
||||
LBrack Expr
|
||||
@@ -76,10 +85,12 @@ type (
|
||||
Literal DataType
|
||||
}
|
||||
|
||||
// Time describes the time ast for api syntax
|
||||
Time struct {
|
||||
Literal Expr
|
||||
}
|
||||
|
||||
// Pointer describes the pointer ast for api syntax
|
||||
Pointer struct {
|
||||
PointerExpr Expr
|
||||
Star Expr
|
||||
@@ -87,6 +98,7 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
// VisitTypeSpec implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitTypeSpec(ctx *api.TypeSpecContext) interface{} {
|
||||
if ctx.TypeLit() != nil {
|
||||
return []TypeExpr{ctx.TypeLit().Accept(v).(TypeExpr)}
|
||||
@@ -94,6 +106,7 @@ func (v *ApiVisitor) VisitTypeSpec(ctx *api.TypeSpecContext) interface{} {
|
||||
return ctx.TypeBlock().Accept(v)
|
||||
}
|
||||
|
||||
// VisitTypeLit implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitTypeLit(ctx *api.TypeLitContext) interface{} {
|
||||
typeLit := ctx.TypeLitBody().Accept(v)
|
||||
alias, ok := typeLit.(*TypeAlias)
|
||||
@@ -109,6 +122,7 @@ func (v *ApiVisitor) VisitTypeLit(ctx *api.TypeLitContext) interface{} {
|
||||
return typeLit
|
||||
}
|
||||
|
||||
// VisitTypeBlock implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitTypeBlock(ctx *api.TypeBlockContext) interface{} {
|
||||
list := ctx.AllTypeBlockBody()
|
||||
var types []TypeExpr
|
||||
@@ -119,6 +133,7 @@ func (v *ApiVisitor) VisitTypeBlock(ctx *api.TypeBlockContext) interface{} {
|
||||
return types
|
||||
}
|
||||
|
||||
// VisitTypeLitBody implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitTypeLitBody(ctx *api.TypeLitBodyContext) interface{} {
|
||||
if ctx.TypeAlias() != nil {
|
||||
return ctx.TypeAlias().Accept(v)
|
||||
@@ -126,6 +141,7 @@ func (v *ApiVisitor) VisitTypeLitBody(ctx *api.TypeLitBodyContext) interface{} {
|
||||
return ctx.TypeStruct().Accept(v)
|
||||
}
|
||||
|
||||
// VisitTypeBlockBody implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitTypeBlockBody(ctx *api.TypeBlockBodyContext) interface{} {
|
||||
if ctx.TypeBlockAlias() != nil {
|
||||
return ctx.TypeBlockAlias().Accept(v).(*TypeAlias)
|
||||
@@ -133,6 +149,7 @@ func (v *ApiVisitor) VisitTypeBlockBody(ctx *api.TypeBlockBodyContext) interface
|
||||
return ctx.TypeBlockStruct().Accept(v).(*TypeStruct)
|
||||
}
|
||||
|
||||
// VisitTypeStruct implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitTypeStruct(ctx *api.TypeStructContext) interface{} {
|
||||
var st TypeStruct
|
||||
st.Name = v.newExprWithToken(ctx.GetStructName())
|
||||
@@ -168,6 +185,7 @@ func (v *ApiVisitor) VisitTypeStruct(ctx *api.TypeStructContext) interface{} {
|
||||
return &st
|
||||
}
|
||||
|
||||
// VisitTypeBlockStruct implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitTypeBlockStruct(ctx *api.TypeBlockStructContext) interface{} {
|
||||
var st TypeStruct
|
||||
st.Name = v.newExprWithToken(ctx.GetStructName())
|
||||
@@ -200,6 +218,7 @@ func (v *ApiVisitor) VisitTypeBlockStruct(ctx *api.TypeBlockStructContext) inter
|
||||
return &st
|
||||
}
|
||||
|
||||
// VisitTypeBlockAlias implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitTypeBlockAlias(ctx *api.TypeBlockAliasContext) interface{} {
|
||||
var alias TypeAlias
|
||||
alias.Name = v.newExprWithToken(ctx.GetAlias())
|
||||
@@ -212,6 +231,7 @@ func (v *ApiVisitor) VisitTypeBlockAlias(ctx *api.TypeBlockAliasContext) interfa
|
||||
return &alias
|
||||
}
|
||||
|
||||
// VisitTypeAlias implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitTypeAlias(ctx *api.TypeAliasContext) interface{} {
|
||||
var alias TypeAlias
|
||||
alias.Name = v.newExprWithToken(ctx.GetAlias())
|
||||
@@ -224,6 +244,7 @@ func (v *ApiVisitor) VisitTypeAlias(ctx *api.TypeAliasContext) interface{} {
|
||||
return &alias
|
||||
}
|
||||
|
||||
// VisitField implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitField(ctx *api.FieldContext) interface{} {
|
||||
iAnonymousFiled := ctx.AnonymousFiled()
|
||||
iNormalFieldContext := ctx.NormalField()
|
||||
@@ -236,6 +257,7 @@ func (v *ApiVisitor) VisitField(ctx *api.FieldContext) interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
// VisitNormalField implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitNormalField(ctx *api.NormalFieldContext) interface{} {
|
||||
var field TypeField
|
||||
field.Name = v.newExprWithToken(ctx.GetFieldName())
|
||||
@@ -259,6 +281,7 @@ func (v *ApiVisitor) VisitNormalField(ctx *api.NormalFieldContext) interface{} {
|
||||
return &field
|
||||
}
|
||||
|
||||
// VisitAnonymousFiled implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitAnonymousFiled(ctx *api.AnonymousFiledContext) interface{} {
|
||||
start := ctx.GetStart()
|
||||
stop := ctx.GetStop()
|
||||
@@ -282,6 +305,7 @@ func (v *ApiVisitor) VisitAnonymousFiled(ctx *api.AnonymousFiledContext) interfa
|
||||
return &field
|
||||
}
|
||||
|
||||
// VisitDataType implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitDataType(ctx *api.DataTypeContext) interface{} {
|
||||
if ctx.ID() != nil {
|
||||
idExpr := v.newExprWithTerminalNode(ctx.ID())
|
||||
@@ -310,6 +334,7 @@ func (v *ApiVisitor) VisitDataType(ctx *api.DataTypeContext) interface{} {
|
||||
return ctx.TypeStruct().Accept(v)
|
||||
}
|
||||
|
||||
// VisitPointerType implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitPointerType(ctx *api.PointerTypeContext) interface{} {
|
||||
nameExpr := v.newExprWithTerminalNode(ctx.ID())
|
||||
v.exportCheck(nameExpr)
|
||||
@@ -320,6 +345,7 @@ func (v *ApiVisitor) VisitPointerType(ctx *api.PointerTypeContext) interface{} {
|
||||
}
|
||||
}
|
||||
|
||||
// VisitMapType implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitMapType(ctx *api.MapTypeContext) interface{} {
|
||||
return &Map{
|
||||
MapExpr: v.newExprWithText(ctx.GetText(), ctx.GetMapToken().GetLine(), ctx.GetMapToken().GetColumn(),
|
||||
@@ -332,6 +358,7 @@ func (v *ApiVisitor) VisitMapType(ctx *api.MapTypeContext) interface{} {
|
||||
}
|
||||
}
|
||||
|
||||
// VisitArrayType implements from api.BaseApiParserVisitor
|
||||
func (v *ApiVisitor) VisitArrayType(ctx *api.ArrayTypeContext) interface{} {
|
||||
return &Array{
|
||||
ArrayExpr: v.newExprWithText(ctx.GetText(), ctx.GetLbrack().GetLine(), ctx.GetLbrack().GetColumn(), ctx.GetLbrack().GetStart(), ctx.DataType().GetStop().GetStop()),
|
||||
@@ -341,22 +368,27 @@ func (v *ApiVisitor) VisitArrayType(ctx *api.ArrayTypeContext) interface{} {
|
||||
}
|
||||
}
|
||||
|
||||
// NameExpr returns the expression string of TypeAlias
|
||||
func (a *TypeAlias) NameExpr() Expr {
|
||||
return a.Name
|
||||
}
|
||||
|
||||
// Doc returns the document of TypeAlias, like // some text
|
||||
func (a *TypeAlias) Doc() []Expr {
|
||||
return a.DocExpr
|
||||
}
|
||||
|
||||
// Comment returns the comment of TypeAlias, like // some text
|
||||
func (a *TypeAlias) Comment() Expr {
|
||||
return a.CommentExpr
|
||||
}
|
||||
|
||||
// Format provides a formatter for api command, now nothing to do
|
||||
func (a *TypeAlias) Format() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Equal compares whether the element literals in two TypeAlias are equal
|
||||
func (a *TypeAlias) Equal(v interface{}) bool {
|
||||
if v == nil {
|
||||
return false
|
||||
@@ -378,15 +410,18 @@ func (a *TypeAlias) Equal(v interface{}) bool {
|
||||
return EqualDoc(a, alias)
|
||||
}
|
||||
|
||||
// Expr returns the expression string of Literal
|
||||
func (l *Literal) Expr() Expr {
|
||||
return l.Literal
|
||||
}
|
||||
|
||||
// Format provides a formatter for api command, now nothing to do
|
||||
func (l *Literal) Format() error {
|
||||
// todo
|
||||
return nil
|
||||
}
|
||||
|
||||
// Equal compares whether the element literals in two Literal are equal
|
||||
func (l *Literal) Equal(dt DataType) bool {
|
||||
if dt == nil {
|
||||
return false
|
||||
@@ -400,19 +435,23 @@ func (l *Literal) Equal(dt DataType) bool {
|
||||
return l.Literal.Equal(v.Literal)
|
||||
}
|
||||
|
||||
// IsNotNil returns whether the instance is nil or not
|
||||
func (l *Literal) IsNotNil() bool {
|
||||
return l != nil
|
||||
}
|
||||
|
||||
// Expr returns the expression string of Interface
|
||||
func (i *Interface) Expr() Expr {
|
||||
return i.Literal
|
||||
}
|
||||
|
||||
// Format provides a formatter for api command, now nothing to do
|
||||
func (i *Interface) Format() error {
|
||||
// todo
|
||||
return nil
|
||||
}
|
||||
|
||||
// Equal compares whether the element literals in two Interface are equal
|
||||
func (i *Interface) Equal(dt DataType) bool {
|
||||
if dt == nil {
|
||||
return false
|
||||
@@ -426,19 +465,23 @@ func (i *Interface) Equal(dt DataType) bool {
|
||||
return i.Literal.Equal(v.Literal)
|
||||
}
|
||||
|
||||
// IsNotNil returns whether the instance is nil or not
|
||||
func (i *Interface) IsNotNil() bool {
|
||||
return i != nil
|
||||
}
|
||||
|
||||
// Expr returns the expression string of Map
|
||||
func (m *Map) Expr() Expr {
|
||||
return m.MapExpr
|
||||
}
|
||||
|
||||
// Format provides a formatter for api command, now nothing to do
|
||||
func (m *Map) Format() error {
|
||||
// todo
|
||||
return nil
|
||||
}
|
||||
|
||||
// Equal compares whether the element literals in two Map are equal
|
||||
func (m *Map) Equal(dt DataType) bool {
|
||||
if dt == nil {
|
||||
return false
|
||||
@@ -464,19 +507,23 @@ func (m *Map) Equal(dt DataType) bool {
|
||||
return m.Map.Equal(v.Map)
|
||||
}
|
||||
|
||||
// IsNotNil returns whether the instance is nil or not
|
||||
func (m *Map) IsNotNil() bool {
|
||||
return m != nil
|
||||
}
|
||||
|
||||
// Expr returns the expression string of Array
|
||||
func (a *Array) Expr() Expr {
|
||||
return a.ArrayExpr
|
||||
}
|
||||
|
||||
// Format provides a formatter for api command, now nothing to do
|
||||
func (a *Array) Format() error {
|
||||
// todo
|
||||
return nil
|
||||
}
|
||||
|
||||
// Equal compares whether the element literals in two Array are equal
|
||||
func (a *Array) Equal(dt DataType) bool {
|
||||
if dt == nil {
|
||||
return false
|
||||
@@ -494,19 +541,23 @@ func (a *Array) Equal(dt DataType) bool {
|
||||
return a.Literal.Equal(v.Literal)
|
||||
}
|
||||
|
||||
// IsNotNil returns whether the instance is nil or not
|
||||
func (a *Array) IsNotNil() bool {
|
||||
return a != nil
|
||||
}
|
||||
|
||||
// Expr returns the expression string of Time
|
||||
func (t *Time) Expr() Expr {
|
||||
return t.Literal
|
||||
}
|
||||
|
||||
// Format provides a formatter for api command, now nothing to do
|
||||
func (t *Time) Format() error {
|
||||
// todo
|
||||
return nil
|
||||
}
|
||||
|
||||
// Equal compares whether the element literals in two Time are equal
|
||||
func (t *Time) Equal(dt DataType) bool {
|
||||
if dt == nil {
|
||||
return false
|
||||
@@ -520,18 +571,22 @@ func (t *Time) Equal(dt DataType) bool {
|
||||
return t.Literal.Equal(v.Literal)
|
||||
}
|
||||
|
||||
// IsNotNil returns whether the instance is nil or not
|
||||
func (t *Time) IsNotNil() bool {
|
||||
return t != nil
|
||||
}
|
||||
|
||||
// Expr returns the expression string of Pointer
|
||||
func (p *Pointer) Expr() Expr {
|
||||
return p.PointerExpr
|
||||
}
|
||||
|
||||
// Format provides a formatter for api command, now nothing to do
|
||||
func (p *Pointer) Format() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Equal compares whether the element literals in two Pointer are equal
|
||||
func (p *Pointer) Equal(dt DataType) bool {
|
||||
if dt == nil {
|
||||
return false
|
||||
@@ -553,14 +608,17 @@ func (p *Pointer) Equal(dt DataType) bool {
|
||||
return p.Name.Equal(v.Name)
|
||||
}
|
||||
|
||||
// IsNotNil returns whether the instance is nil or not
|
||||
func (p *Pointer) IsNotNil() bool {
|
||||
return p != nil
|
||||
}
|
||||
|
||||
// NameExpr returns the expression string of TypeStruct
|
||||
func (s *TypeStruct) NameExpr() Expr {
|
||||
return s.Name
|
||||
}
|
||||
|
||||
// Equal compares whether the element literals in two TypeStruct are equal
|
||||
func (s *TypeStruct) Equal(dt interface{}) bool {
|
||||
if dt == nil {
|
||||
return false
|
||||
@@ -621,15 +679,18 @@ func (s *TypeStruct) Equal(dt interface{}) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// Doc returns the document of TypeStruct, like // some text
|
||||
func (s *TypeStruct) Doc() []Expr {
|
||||
return s.DocExpr
|
||||
}
|
||||
|
||||
// Format provides a formatter for api command, now nothing to do
|
||||
func (s *TypeStruct) Format() error {
|
||||
// todo
|
||||
return nil
|
||||
}
|
||||
|
||||
// Equal compares whether the element literals in two TypeField are equal
|
||||
func (t *TypeField) Equal(v interface{}) bool {
|
||||
if v == nil {
|
||||
return false
|
||||
@@ -663,14 +724,17 @@ func (t *TypeField) Equal(v interface{}) bool {
|
||||
return EqualDoc(t, f)
|
||||
}
|
||||
|
||||
// Doc returns the document of TypeField, like // some text
|
||||
func (t *TypeField) Doc() []Expr {
|
||||
return t.DocExpr
|
||||
}
|
||||
|
||||
// Comment returns the comment of TypeField, like // some text
|
||||
func (t *TypeField) Comment() Expr {
|
||||
return t.CommentExpr
|
||||
}
|
||||
|
||||
// Format provides a formatter for api command, now nothing to do
|
||||
func (t *TypeField) Format() error {
|
||||
// todo
|
||||
return nil
|
||||
|
||||
@@ -4931,7 +4931,7 @@ func (p *ApiParserParser) Route() (localctx IRouteContext) {
|
||||
}()
|
||||
|
||||
p.EnterOuterAlt(localctx, 1)
|
||||
checkHttpMethod(p)
|
||||
checkHTTPMethod(p)
|
||||
{
|
||||
p.SetState(291)
|
||||
|
||||
|
||||
@@ -74,7 +74,7 @@ func checkKeyValue(p *ApiParserParser) {
|
||||
setCurrentTokenText(p, v)
|
||||
}
|
||||
|
||||
func checkHttpMethod(p *ApiParserParser) {
|
||||
func checkHTTPMethod(p *ApiParserParser) {
|
||||
method := getCurrentTokenText(p)
|
||||
uppler := strings.ToUpper(method)
|
||||
switch uppler {
|
||||
@@ -107,11 +107,13 @@ func checkKey(p *ApiParserParser) {
|
||||
}
|
||||
}
|
||||
|
||||
// IsBasicType returns true if the input argument is basic golang type
|
||||
func IsBasicType(text string) bool {
|
||||
_, ok := kind[text]
|
||||
return ok
|
||||
}
|
||||
|
||||
// IsGolangKeyWord returns true if input argument is golang keyword, but it will be ignored which in excepts
|
||||
func IsGolangKeyWord(text string, excepts ...string) bool {
|
||||
for _, each := range excepts {
|
||||
if text == each {
|
||||
@@ -171,6 +173,7 @@ func isNormal(p *ApiParserParser) bool {
|
||||
return len(list) > 1
|
||||
}
|
||||
|
||||
// MatchTag returns a Boolean value, which returns true if it does matched, otherwise returns fase
|
||||
func MatchTag(v string) bool {
|
||||
return matchRegex(v, tagRegex)
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
normalApi = `
|
||||
normalAPI = `
|
||||
syntax="v1"
|
||||
|
||||
info (
|
||||
@@ -32,7 +32,7 @@ var (
|
||||
post /foo (Foo) returns ([]int)
|
||||
}
|
||||
`
|
||||
missDeclarationApi = `
|
||||
missDeclarationAPI = `
|
||||
@server(
|
||||
foo: bar
|
||||
)
|
||||
@@ -43,7 +43,7 @@ var (
|
||||
}
|
||||
`
|
||||
|
||||
missDeclarationInArrayApi = `
|
||||
missDeclarationInArrayAPI = `
|
||||
@server(
|
||||
foo: bar
|
||||
)
|
||||
@@ -54,7 +54,7 @@ var (
|
||||
}
|
||||
`
|
||||
|
||||
missDeclarationInArrayApi2 = `
|
||||
missDeclarationInArrayAPI2 = `
|
||||
@server(
|
||||
foo: bar
|
||||
)
|
||||
@@ -65,7 +65,7 @@ var (
|
||||
}
|
||||
`
|
||||
|
||||
nestedApiImport = `
|
||||
nestedAPIImport = `
|
||||
import "foo.api"
|
||||
`
|
||||
|
||||
@@ -99,27 +99,27 @@ var (
|
||||
)
|
||||
|
||||
func TestApiParser(t *testing.T) {
|
||||
t.Run("missDeclarationApi", func(t *testing.T) {
|
||||
_, err := parser.ParseContent(missDeclarationApi)
|
||||
t.Run("missDeclarationAPI", func(t *testing.T) {
|
||||
_, err := parser.ParseContent(missDeclarationAPI)
|
||||
assert.Error(t, err)
|
||||
fmt.Printf("%+v\n", err)
|
||||
})
|
||||
|
||||
t.Run("missDeclarationApi", func(t *testing.T) {
|
||||
_, err := parser.ParseContent(missDeclarationInArrayApi)
|
||||
t.Run("missDeclarationAPI", func(t *testing.T) {
|
||||
_, err := parser.ParseContent(missDeclarationInArrayAPI)
|
||||
assert.Error(t, err)
|
||||
fmt.Printf("%+v\n", err)
|
||||
})
|
||||
|
||||
t.Run("missDeclarationApi", func(t *testing.T) {
|
||||
_, err := parser.ParseContent(missDeclarationInArrayApi2)
|
||||
t.Run("missDeclarationAPI", func(t *testing.T) {
|
||||
_, err := parser.ParseContent(missDeclarationInArrayAPI2)
|
||||
assert.Error(t, err)
|
||||
fmt.Printf("%+v\n", err)
|
||||
})
|
||||
|
||||
t.Run("nestedImport", func(t *testing.T) {
|
||||
file := filepath.Join(t.TempDir(), "foo.api")
|
||||
err := ioutil.WriteFile(file, []byte(nestedApiImport), os.ModePerm)
|
||||
err := ioutil.WriteFile(file, []byte(nestedAPIImport), os.ModePerm)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -275,7 +275,7 @@ func TestApiParser(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("normal", func(t *testing.T) {
|
||||
v, err := parser.ParseContent(normalApi)
|
||||
v, err := parser.ParseContent(normalAPI)
|
||||
assert.Nil(t, err)
|
||||
body := &ast.Body{
|
||||
Lp: ast.NewTextExpr("("),
|
||||
|
||||
@@ -15,6 +15,7 @@ type parser struct {
|
||||
spec *spec.ApiSpec
|
||||
}
|
||||
|
||||
// Parse parses the api file
|
||||
func Parse(filename string) (*spec.ApiSpec, error) {
|
||||
astParser := ast.NewParser(ast.WithParserPrefix(filepath.Base(filename)))
|
||||
ast, err := astParser.Parse(filename)
|
||||
@@ -32,6 +33,7 @@ func Parse(filename string) (*spec.ApiSpec, error) {
|
||||
return spec, nil
|
||||
}
|
||||
|
||||
// ParseContent parses the api content
|
||||
func ParseContent(content string) (*spec.ApiSpec, error) {
|
||||
astParser := ast.NewParser()
|
||||
ast, err := astParser.ParseContent(content)
|
||||
|
||||
@@ -16,6 +16,7 @@ const (
|
||||
|
||||
var definedKeys = []string{bodyTagKey, formTagKey, "path"}
|
||||
|
||||
// Routes returns all routes in api service
|
||||
func (s Service) Routes() []Route {
|
||||
var result []Route
|
||||
for _, group := range s.Groups {
|
||||
@@ -24,6 +25,7 @@ func (s Service) Routes() []Route {
|
||||
return result
|
||||
}
|
||||
|
||||
// Tags retuens all tags in Member
|
||||
func (m Member) Tags() []*Tag {
|
||||
tags, err := Parse(m.Tag)
|
||||
if err != nil {
|
||||
@@ -33,6 +35,7 @@ func (m Member) Tags() []*Tag {
|
||||
return tags.Tags()
|
||||
}
|
||||
|
||||
// IsOptional returns true if tag is optional
|
||||
func (m Member) IsOptional() bool {
|
||||
if !m.IsBodyMember() {
|
||||
return false
|
||||
@@ -49,6 +52,7 @@ func (m Member) IsOptional() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// IsOmitEmpty returns true if tag contains omitempty
|
||||
func (m Member) IsOmitEmpty() bool {
|
||||
if !m.IsBodyMember() {
|
||||
return false
|
||||
@@ -65,22 +69,7 @@ func (m Member) IsOmitEmpty() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (m Member) IsOmitempty() bool {
|
||||
if !m.IsBodyMember() {
|
||||
return false
|
||||
}
|
||||
|
||||
tag := m.Tags()
|
||||
for _, item := range tag {
|
||||
if item.Key == bodyTagKey {
|
||||
if stringx.Contains(item.Options, "omitempty") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// GetPropertyName returns json tag value
|
||||
func (m Member) GetPropertyName() (string, error) {
|
||||
tags := m.Tags()
|
||||
for _, tag := range tags {
|
||||
@@ -95,10 +84,12 @@ func (m Member) GetPropertyName() (string, error) {
|
||||
return "", errors.New("json property name not exist, member: " + m.Name)
|
||||
}
|
||||
|
||||
// GetComment returns comment value of Member
|
||||
func (m Member) GetComment() string {
|
||||
return strings.TrimSpace(m.Comment)
|
||||
}
|
||||
|
||||
// IsBodyMember returns true if contains json tag
|
||||
func (m Member) IsBodyMember() bool {
|
||||
if m.IsInline {
|
||||
return true
|
||||
@@ -113,6 +104,7 @@ func (m Member) IsBodyMember() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// IsFormMember returns true if contains form tag
|
||||
func (m Member) IsFormMember() bool {
|
||||
if m.IsInline {
|
||||
return false
|
||||
@@ -127,6 +119,7 @@ func (m Member) IsFormMember() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// GetBodyMembers returns all json fields
|
||||
func (t DefineStruct) GetBodyMembers() []Member {
|
||||
var result []Member
|
||||
for _, member := range t.Members {
|
||||
@@ -137,6 +130,7 @@ func (t DefineStruct) GetBodyMembers() []Member {
|
||||
return result
|
||||
}
|
||||
|
||||
// GetFormMembers returns all form fields
|
||||
func (t DefineStruct) GetFormMembers() []Member {
|
||||
var result []Member
|
||||
for _, member := range t.Members {
|
||||
@@ -147,6 +141,7 @@ func (t DefineStruct) GetFormMembers() []Member {
|
||||
return result
|
||||
}
|
||||
|
||||
// GetNonBodyMembers retruns all have no tag fields
|
||||
func (t DefineStruct) GetNonBodyMembers() []Member {
|
||||
var result []Member
|
||||
for _, member := range t.Members {
|
||||
@@ -157,6 +152,7 @@ func (t DefineStruct) GetNonBodyMembers() []Member {
|
||||
return result
|
||||
}
|
||||
|
||||
// JoinedDoc joins comments and summary value in AtDoc
|
||||
func (r Route) JoinedDoc() string {
|
||||
doc := r.AtDoc.Text
|
||||
if r.AtDoc.Properties != nil {
|
||||
@@ -166,6 +162,7 @@ func (r Route) JoinedDoc() string {
|
||||
return strings.TrimSpace(doc)
|
||||
}
|
||||
|
||||
// GetAnnotation returns the value by specified key
|
||||
func (r Route) GetAnnotation(key string) string {
|
||||
if r.Annotation.Properties == nil {
|
||||
return ""
|
||||
@@ -174,6 +171,7 @@ func (r Route) GetAnnotation(key string) string {
|
||||
return r.Annotation.Properties[key]
|
||||
}
|
||||
|
||||
// GetAnnotation returns the value by specified key
|
||||
func (g Group) GetAnnotation(key string) string {
|
||||
if g.Annotation.Properties == nil {
|
||||
return ""
|
||||
@@ -182,6 +180,7 @@ func (g Group) GetAnnotation(key string) string {
|
||||
return g.Annotation.Properties[key]
|
||||
}
|
||||
|
||||
// ResponseTypeName returns response type name of route
|
||||
func (r Route) ResponseTypeName() string {
|
||||
if r.ResponseType == nil {
|
||||
return ""
|
||||
@@ -190,6 +189,7 @@ func (r Route) ResponseTypeName() string {
|
||||
return r.ResponseType.Name()
|
||||
}
|
||||
|
||||
// RequestTypeName returns request type name of route
|
||||
func (r Route) RequestTypeName() string {
|
||||
if r.RequestType == nil {
|
||||
return ""
|
||||
|
||||
@@ -1,25 +1,31 @@
|
||||
package spec
|
||||
|
||||
// Name returns a basic string, such as int32,int64
|
||||
func (t PrimitiveType) Name() string {
|
||||
return t.RawName
|
||||
}
|
||||
|
||||
// Name returns a structure string, such as User
|
||||
func (t DefineStruct) Name() string {
|
||||
return t.RawName
|
||||
}
|
||||
|
||||
// Name returns a map string, such as map[string]int
|
||||
func (t MapType) Name() string {
|
||||
return t.RawName
|
||||
}
|
||||
|
||||
// Name returns a slice string, such as []int
|
||||
func (t ArrayType) Name() string {
|
||||
return t.RawName
|
||||
}
|
||||
|
||||
// Name returns a pointer string, such as *User
|
||||
func (t PointerType) Name() string {
|
||||
return t.RawName
|
||||
}
|
||||
|
||||
// Name returns a interface string, Its fixed value is interface{}
|
||||
func (t InterfaceType) Name() string {
|
||||
return t.RawName
|
||||
}
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
package spec
|
||||
|
||||
type (
|
||||
// Doc describes document
|
||||
Doc []string
|
||||
|
||||
// Annotation defines key-value
|
||||
Annotation struct {
|
||||
Properties map[string]string
|
||||
}
|
||||
|
||||
// ApiSyntax describes the syntax grammar
|
||||
ApiSyntax struct {
|
||||
Version string
|
||||
}
|
||||
|
||||
// ApiSpec describes a api file
|
||||
ApiSpec struct {
|
||||
Info Info
|
||||
Syntax ApiSyntax
|
||||
@@ -19,15 +23,18 @@ type (
|
||||
Service Service
|
||||
}
|
||||
|
||||
// Import describes api import
|
||||
Import struct {
|
||||
Value string
|
||||
}
|
||||
|
||||
// Group defines a set of routing information
|
||||
Group struct {
|
||||
Annotation Annotation
|
||||
Routes []Route
|
||||
}
|
||||
|
||||
// Info describes info grammar block
|
||||
Info struct {
|
||||
// Deprecated: use Properties instead
|
||||
Title string
|
||||
@@ -42,6 +49,7 @@ type (
|
||||
Properties map[string]string
|
||||
}
|
||||
|
||||
// Member describes the field of a structure
|
||||
Member struct {
|
||||
Name string
|
||||
// 数据类型字面值,如:string、map[int]string、[]int64、[]*User
|
||||
@@ -53,6 +61,7 @@ type (
|
||||
IsInline bool
|
||||
}
|
||||
|
||||
// Route describes api route
|
||||
Route struct {
|
||||
Annotation Annotation
|
||||
Method string
|
||||
@@ -64,26 +73,30 @@ type (
|
||||
AtDoc AtDoc
|
||||
}
|
||||
|
||||
// Service describes api service
|
||||
Service struct {
|
||||
Name string
|
||||
Groups []Group
|
||||
}
|
||||
|
||||
// Type defines api type
|
||||
Type interface {
|
||||
Name() string
|
||||
}
|
||||
|
||||
// DefineStruct describes api structure
|
||||
DefineStruct struct {
|
||||
RawName string
|
||||
Members []Member
|
||||
Docs Doc
|
||||
}
|
||||
|
||||
// 系统预设基本数据类型 bool int32 int64 float32
|
||||
// PrimitiveType describes the basic golang type, such as bool,int32,int64, ...
|
||||
PrimitiveType struct {
|
||||
RawName string
|
||||
}
|
||||
|
||||
// MapType describes a map for api
|
||||
MapType struct {
|
||||
RawName string
|
||||
// only support the PrimitiveType
|
||||
@@ -97,20 +110,24 @@ type (
|
||||
Value Type
|
||||
}
|
||||
|
||||
// ArrayType describes a slice for api
|
||||
ArrayType struct {
|
||||
RawName string
|
||||
Value Type
|
||||
}
|
||||
|
||||
// InterfaceType describes a interface for api
|
||||
InterfaceType struct {
|
||||
RawName string
|
||||
}
|
||||
|
||||
// PointerType describes a pointer for api
|
||||
PointerType struct {
|
||||
RawName string
|
||||
Type Type
|
||||
}
|
||||
|
||||
// AtDoc describes a metadata for api grammar: @doc(...)
|
||||
AtDoc struct {
|
||||
Properties map[string]string
|
||||
Text string
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
var errTagNotExist = errors.New("tag does not exist")
|
||||
|
||||
type (
|
||||
// Tag defines a tag for structure filed
|
||||
Tag struct {
|
||||
// Key is the tag key, such as json, xml, etc..
|
||||
// i.e: `json:"foo,omitempty". Here key is: "json"
|
||||
@@ -24,11 +25,13 @@ type (
|
||||
Options []string
|
||||
}
|
||||
|
||||
// Tags defines a slice for Tag
|
||||
Tags struct {
|
||||
tags []*Tag
|
||||
}
|
||||
)
|
||||
|
||||
// Parse converts tag string into Tag
|
||||
func Parse(tag string) (*Tags, error) {
|
||||
tag = strings.TrimPrefix(tag, "`")
|
||||
tag = strings.TrimSuffix(tag, "`")
|
||||
@@ -44,6 +47,7 @@ func Parse(tag string) (*Tags, error) {
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
// Get gets tag value by specified key
|
||||
func (t *Tags) Get(key string) (*Tag, error) {
|
||||
for _, tag := range t.tags {
|
||||
if tag.Key == key {
|
||||
@@ -54,6 +58,7 @@ func (t *Tags) Get(key string) (*Tag, error) {
|
||||
return nil, errTagNotExist
|
||||
}
|
||||
|
||||
// Keys returns all keys in Tags
|
||||
func (t *Tags) Keys() []string {
|
||||
var keys []string
|
||||
for _, tag := range t.tags {
|
||||
@@ -62,6 +67,7 @@ func (t *Tags) Keys() []string {
|
||||
return keys
|
||||
}
|
||||
|
||||
// Tags returns all tags in Tags
|
||||
func (t *Tags) Tags() []*Tag {
|
||||
return t.tags
|
||||
}
|
||||
|
||||
@@ -11,12 +11,13 @@ import (
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
// TsCommand provides the entry to generting typescript codes
|
||||
func TsCommand(c *cli.Context) error {
|
||||
apiFile := c.String("api")
|
||||
dir := c.String("dir")
|
||||
webApi := c.String("webapi")
|
||||
webAPI := c.String("webapi")
|
||||
caller := c.String("caller")
|
||||
unwrapApi := c.Bool("unwrap")
|
||||
unwrapAPI := c.Bool("unwrap")
|
||||
if len(apiFile) == 0 {
|
||||
return errors.New("missing -api")
|
||||
}
|
||||
@@ -32,7 +33,7 @@ func TsCommand(c *cli.Context) error {
|
||||
}
|
||||
|
||||
logx.Must(util.MkdirIfNotExist(dir))
|
||||
logx.Must(genHandler(dir, webApi, caller, api, unwrapApi))
|
||||
logx.Must(genHandler(dir, webAPI, caller, api, unwrapAPI))
|
||||
logx.Must(genComponents(dir, api))
|
||||
|
||||
fmt.Println(aurora.Green("Done."))
|
||||
|
||||
@@ -18,7 +18,7 @@ const (
|
||||
`
|
||||
)
|
||||
|
||||
func genHandler(dir, webApi, caller string, api *spec.ApiSpec, unwrapApi bool) error {
|
||||
func genHandler(dir, webAPI, caller string, api *spec.ApiSpec, unwrapAPI bool) error {
|
||||
filename := strings.Replace(api.Service.Name, "-api", "", 1) + ".ts"
|
||||
if err := util.RemoveIfExist(path.Join(dir, filename)); err != nil {
|
||||
return err
|
||||
@@ -37,11 +37,11 @@ func genHandler(dir, webApi, caller string, api *spec.ApiSpec, unwrapApi bool) e
|
||||
caller = "webapi"
|
||||
}
|
||||
importCaller := caller
|
||||
if unwrapApi {
|
||||
if unwrapAPI {
|
||||
importCaller = "{ " + importCaller + " }"
|
||||
}
|
||||
if len(webApi) > 0 {
|
||||
imports += `import ` + importCaller + ` from ` + "\"" + webApi + "\""
|
||||
if len(webAPI) > 0 {
|
||||
imports += `import ` + importCaller + ` from ` + "\"" + webAPI + "\""
|
||||
}
|
||||
|
||||
if len(api.Types) != 0 {
|
||||
@@ -53,7 +53,7 @@ func genHandler(dir, webApi, caller string, api *spec.ApiSpec, unwrapApi bool) e
|
||||
imports += fmt.Sprintf(`%sexport * from "%s"`, util.NL, "./"+outputFile)
|
||||
}
|
||||
|
||||
apis, err := genApi(api, caller)
|
||||
apis, err := genAPI(api, caller)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -65,7 +65,7 @@ func genHandler(dir, webApi, caller string, api *spec.ApiSpec, unwrapApi bool) e
|
||||
})
|
||||
}
|
||||
|
||||
func genApi(api *spec.ApiSpec, caller string) (string, error) {
|
||||
func genAPI(api *spec.ApiSpec, caller string) (string, error) {
|
||||
var builder strings.Builder
|
||||
for _, group := range api.Service.Groups {
|
||||
for _, route := range group.Routes {
|
||||
@@ -157,7 +157,7 @@ func callParamsForRoute(route spec.Route, group spec.Group) string {
|
||||
}
|
||||
|
||||
func pathForRoute(route spec.Route, group spec.Group) string {
|
||||
prefix := group.GetAnnotation("pathPrefix")
|
||||
prefix := group.GetAnnotation(pathPrefix)
|
||||
if len(prefix) == 0 {
|
||||
return "\"" + route.Path + "\""
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ func writeProperty(writer io.Writer, member spec.Member, indent int) error {
|
||||
}
|
||||
|
||||
optionalTag := ""
|
||||
if member.IsOptional() || member.IsOmitempty() {
|
||||
if member.IsOptional() || member.IsOmitEmpty() {
|
||||
optionalTag = "?"
|
||||
}
|
||||
name, err := member.GetPropertyName()
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// IsUpperCase returns true if the rune in A-Z
|
||||
func IsUpperCase(r rune) bool {
|
||||
if r >= 'A' && r <= 'Z' {
|
||||
return true
|
||||
@@ -12,6 +13,7 @@ func IsUpperCase(r rune) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// IsLowerCase returns true if the rune in a-z
|
||||
func IsLowerCase(r rune) bool {
|
||||
if r >= 'a' && r <= 'z' {
|
||||
return true
|
||||
@@ -19,6 +21,7 @@ func IsLowerCase(r rune) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// ToSnakeCase returns a copy string by converting camel case into snake case
|
||||
func ToSnakeCase(s string) string {
|
||||
var out []rune
|
||||
for index, r := range s {
|
||||
@@ -44,6 +47,7 @@ func ToSnakeCase(s string) string {
|
||||
return string(out)
|
||||
}
|
||||
|
||||
// ToCamelCase returns a copy string by converting snake case into camel case
|
||||
func ToCamelCase(s string) string {
|
||||
s = ToLower(s)
|
||||
out := []rune{}
|
||||
@@ -66,6 +70,7 @@ func ToCamelCase(s string) string {
|
||||
return string(out)
|
||||
}
|
||||
|
||||
// ToLowerCase converts rune into lower case
|
||||
func ToLowerCase(r rune) rune {
|
||||
dx := 'A' - 'a'
|
||||
if IsUpperCase(r) {
|
||||
@@ -73,6 +78,8 @@ func ToLowerCase(r rune) rune {
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// ToUpperCase converts rune into upper case
|
||||
func ToUpperCase(r rune) rune {
|
||||
dx := 'A' - 'a'
|
||||
if IsLowerCase(r) {
|
||||
@@ -81,6 +88,7 @@ func ToUpperCase(r rune) rune {
|
||||
return r
|
||||
}
|
||||
|
||||
// ToLower returns a copy string by converting it into lower
|
||||
func ToLower(s string) string {
|
||||
var out []rune
|
||||
for _, r := range s {
|
||||
@@ -89,6 +97,7 @@ func ToLower(s string) string {
|
||||
return string(out)
|
||||
}
|
||||
|
||||
// ToUpper returns a copy string by converting it into upper
|
||||
func ToUpper(s string) string {
|
||||
var out []rune
|
||||
for _, r := range s {
|
||||
@@ -97,13 +106,7 @@ func ToUpper(s string) string {
|
||||
return string(out)
|
||||
}
|
||||
|
||||
func LowerFirst(s string) string {
|
||||
if len(s) == 0 {
|
||||
return s
|
||||
}
|
||||
return ToLower(s[:1]) + s[1:]
|
||||
}
|
||||
|
||||
// UpperFirst converts s[0] into upper case
|
||||
func UpperFirst(s string) string {
|
||||
if len(s) == 0 {
|
||||
return s
|
||||
@@ -111,6 +114,7 @@ func UpperFirst(s string) string {
|
||||
return ToUpper(s[:1]) + s[1:]
|
||||
}
|
||||
|
||||
// UnExport converts the first letter into lower case
|
||||
func UnExport(text string) bool {
|
||||
var flag bool
|
||||
str := strings.Map(func(r rune) rune {
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
)
|
||||
|
||||
// MaybeCreateFile creates file if not exists
|
||||
func MaybeCreateFile(dir, subdir, file string) (fp *os.File, created bool, err error) {
|
||||
logx.Must(util.MkdirIfNotExist(path.Join(dir, subdir)))
|
||||
fpath := path.Join(dir, subdir, file)
|
||||
@@ -26,10 +27,12 @@ func MaybeCreateFile(dir, subdir, file string) (fp *os.File, created bool, err e
|
||||
return
|
||||
}
|
||||
|
||||
// WrapErr wraps an error with message
|
||||
func WrapErr(err error, message string) error {
|
||||
return errors.New(message + ", " + err.Error())
|
||||
}
|
||||
|
||||
// Copy calls io.Copy if the source file and destination file exists
|
||||
func Copy(src, dst string) (int64, error) {
|
||||
sourceFileStat, err := os.Stat(src)
|
||||
if err != nil {
|
||||
@@ -55,6 +58,7 @@ func Copy(src, dst string) (int64, error) {
|
||||
return nBytes, err
|
||||
}
|
||||
|
||||
// ComponentName returns component name for typescript
|
||||
func ComponentName(api *spec.ApiSpec) string {
|
||||
name := api.Service.Name
|
||||
if strings.HasSuffix(name, "-api") {
|
||||
@@ -63,12 +67,14 @@ func ComponentName(api *spec.ApiSpec) string {
|
||||
return name + "Components"
|
||||
}
|
||||
|
||||
// WriteIndent writes tab spaces
|
||||
func WriteIndent(writer io.Writer, indent int) {
|
||||
for i := 0; i < indent; i++ {
|
||||
fmt.Fprint(writer, "\t")
|
||||
}
|
||||
}
|
||||
|
||||
// RemoveComment filters comment content
|
||||
func RemoveComment(line string) string {
|
||||
var commentIdx = strings.Index(line, "//")
|
||||
if commentIdx >= 0 {
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
// GoValidateApi verifies whether the api has a syntax error
|
||||
func GoValidateApi(c *cli.Context) error {
|
||||
apiFile := c.String("api")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user