optimized goctl format (#336)
* fix format * refactor * refactor * optimized * refactor * refactor * refactor * add js path prefix
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"go/format"
|
||||
"go/scanner"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
@@ -13,6 +14,7 @@ import (
|
||||
"github.com/tal-tech/go-zero/core/errorx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/parser"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/api/util"
|
||||
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@@ -103,24 +105,108 @@ func apiFormat(data string) (string, error) {
|
||||
var builder strings.Builder
|
||||
s := bufio.NewScanner(strings.NewReader(data))
|
||||
var tapCount = 0
|
||||
var newLineCount = 0
|
||||
var preLine string
|
||||
for s.Scan() {
|
||||
line := strings.TrimSpace(s.Text())
|
||||
if len(line) == 0 {
|
||||
if newLineCount > 0 {
|
||||
continue
|
||||
}
|
||||
newLineCount++
|
||||
} else {
|
||||
if preLine == rightBrace {
|
||||
builder.WriteString(ctlutil.NL)
|
||||
}
|
||||
newLineCount = 0
|
||||
}
|
||||
|
||||
if tapCount == 0 {
|
||||
format, err := formatGoTypeDef(line, s, &builder)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if format {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
noCommentLine := util.RemoveComment(line)
|
||||
if noCommentLine == rightParenthesis || noCommentLine == rightBrace {
|
||||
tapCount -= 1
|
||||
}
|
||||
if tapCount < 0 {
|
||||
line = strings.TrimSuffix(line, rightBrace)
|
||||
line := strings.TrimSuffix(noCommentLine, rightBrace)
|
||||
line = strings.TrimSpace(line)
|
||||
if strings.HasSuffix(line, leftBrace) {
|
||||
tapCount += 1
|
||||
}
|
||||
}
|
||||
util.WriteIndent(&builder, tapCount)
|
||||
builder.WriteString(line + "\n")
|
||||
builder.WriteString(line + ctlutil.NL)
|
||||
if strings.HasSuffix(noCommentLine, leftParenthesis) || strings.HasSuffix(noCommentLine, leftBrace) {
|
||||
tapCount += 1
|
||||
}
|
||||
preLine = line
|
||||
}
|
||||
return strings.TrimSpace(builder.String()), nil
|
||||
}
|
||||
|
||||
func formatGoTypeDef(line string, scanner *bufio.Scanner, builder *strings.Builder) (bool, error) {
|
||||
noCommentLine := util.RemoveComment(line)
|
||||
tokenCount := 0
|
||||
if strings.HasPrefix(noCommentLine, "type") && (strings.HasSuffix(noCommentLine, leftParenthesis) ||
|
||||
strings.HasSuffix(noCommentLine, leftBrace)) {
|
||||
var typeBuilder strings.Builder
|
||||
typeBuilder.WriteString(mayInsertStructKeyword(line, &tokenCount) + ctlutil.NL)
|
||||
for scanner.Scan() {
|
||||
noCommentLine := util.RemoveComment(scanner.Text())
|
||||
typeBuilder.WriteString(mayInsertStructKeyword(scanner.Text(), &tokenCount) + ctlutil.NL)
|
||||
if noCommentLine == rightBrace || noCommentLine == rightParenthesis {
|
||||
tokenCount--
|
||||
}
|
||||
if tokenCount == 0 {
|
||||
ts, err := format.Source([]byte(typeBuilder.String()))
|
||||
if err != nil {
|
||||
return false, errors.New("error format \n" + typeBuilder.String())
|
||||
}
|
||||
|
||||
result := strings.ReplaceAll(string(ts), " struct ", " ")
|
||||
result = strings.ReplaceAll(result, "type ()", "")
|
||||
builder.WriteString(result)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func mayInsertStructKeyword(line string, token *int) string {
|
||||
insertStruct := func() string {
|
||||
if strings.Contains(line, " struct") {
|
||||
return line
|
||||
}
|
||||
index := strings.Index(line, leftBrace)
|
||||
return line[:index] + " struct " + line[index:]
|
||||
}
|
||||
|
||||
noCommentLine := util.RemoveComment(line)
|
||||
if strings.HasSuffix(noCommentLine, leftBrace) {
|
||||
*token++
|
||||
return insertStruct()
|
||||
}
|
||||
if strings.HasSuffix(noCommentLine, rightBrace) {
|
||||
noCommentLine = strings.TrimSuffix(noCommentLine, rightBrace)
|
||||
noCommentLine = util.RemoveComment(noCommentLine)
|
||||
if strings.HasSuffix(noCommentLine, leftBrace) {
|
||||
return insertStruct()
|
||||
}
|
||||
}
|
||||
if strings.HasSuffix(noCommentLine, leftParenthesis) {
|
||||
*token++
|
||||
}
|
||||
return line
|
||||
}
|
||||
|
||||
@@ -24,11 +24,11 @@ handler: GreetHandler
|
||||
}
|
||||
`
|
||||
|
||||
formattedStr = `type Request struct {
|
||||
formattedStr = `type Request {
|
||||
Name string
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
type Response {
|
||||
Message string
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ service A-api {
|
||||
}`
|
||||
)
|
||||
|
||||
func TestInlineTypeNotExist(t *testing.T) {
|
||||
func TestFormat(t *testing.T) {
|
||||
r, err := apiFormat(notFormattedStr)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, r, formattedStr)
|
||||
|
||||
Reference in New Issue
Block a user