type should not define nested (#212)
* nest type should not supported * nest type should not supported * nest type should not supported * nest type should not supported * new test * new test
This commit is contained in:
@@ -11,10 +11,18 @@ import (
|
||||
"strings"
|
||||
|
||||
"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"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
const (
|
||||
leftParenthesis = "("
|
||||
rightParenthesis = ")"
|
||||
leftBrace = "{"
|
||||
rightBrace = "}"
|
||||
)
|
||||
|
||||
func GoFormatApi(c *cli.Context) error {
|
||||
useStdin := c.Bool("stdin")
|
||||
|
||||
@@ -57,7 +65,10 @@ func ApiFormatByStdin() error {
|
||||
return err
|
||||
}
|
||||
|
||||
result := apiFormat(string(data))
|
||||
result, err := apiFormat(string(data))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = fmt.Print(result)
|
||||
if err != nil {
|
||||
@@ -72,28 +83,44 @@ func ApiFormatByPath(apiFilePath string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
result := apiFormat(string(data))
|
||||
result, err := apiFormat(string(data))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := ioutil.WriteFile(apiFilePath, []byte(result), os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func apiFormat(data string) string {
|
||||
func apiFormat(data string) (string, error) {
|
||||
_, err := parser.ParseApi(data)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var builder strings.Builder
|
||||
scanner := bufio.NewScanner(strings.NewReader(data))
|
||||
s := bufio.NewScanner(strings.NewReader(data))
|
||||
var tapCount = 0
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
for s.Scan() {
|
||||
line := strings.TrimSpace(s.Text())
|
||||
noCommentLine := util.RemoveComment(line)
|
||||
if noCommentLine == ")" || noCommentLine == "}" {
|
||||
if noCommentLine == rightParenthesis || noCommentLine == rightBrace {
|
||||
tapCount -= 1
|
||||
}
|
||||
if tapCount < 0 {
|
||||
line = strings.TrimSuffix(line, rightBrace)
|
||||
line = strings.TrimSpace(line)
|
||||
if strings.HasSuffix(line, leftBrace) {
|
||||
tapCount += 1
|
||||
}
|
||||
}
|
||||
util.WriteIndent(&builder, tapCount)
|
||||
builder.WriteString(line + "\n")
|
||||
if strings.HasSuffix(noCommentLine, "(") || strings.HasSuffix(noCommentLine, "{") {
|
||||
if strings.HasSuffix(noCommentLine, leftParenthesis) || strings.HasSuffix(noCommentLine, leftBrace) {
|
||||
tapCount += 1
|
||||
}
|
||||
}
|
||||
return strings.TrimSpace(builder.String())
|
||||
return strings.TrimSpace(builder.String()), nil
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@ service A-api {
|
||||
)
|
||||
|
||||
func TestInlineTypeNotExist(t *testing.T) {
|
||||
r := apiFormat(notFormattedStr)
|
||||
r, err := apiFormat(notFormattedStr)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, r, formattedStr)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user