support type def without struct token (#210)

* add comment support

* add comment support

* 1. group support multi level folder
2. remove force flag

* bug fix

* refactor parser and remove deprecated code

* refactor parser and remove deprecated code

* refactor parser and remove deprecated code

* refactor parser and remove deprecated code

* refactor parser and remove deprecated code

* refactor parser and remove deprecated code

* refactor parser and remove deprecated code

* support type def without struct token

* support type def without struct token

* support type def without struct token

* support type def without struct token

* support type def without struct token

* support type def without struct token

* support type def without struct token

* optimized

* optimized

* optimized

Co-authored-by: kim <xutao@xiaoheiban.cn>
This commit is contained in:
kingxt
2020-11-17 15:25:13 +08:00
committed by GitHub
parent 3819f67cf4
commit a92f65580c
8 changed files with 102 additions and 115 deletions

View File

@@ -4,25 +4,17 @@ import (
"bufio"
"errors"
"fmt"
"go/format"
"go/scanner"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strconv"
"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"
)
var (
reg = regexp.MustCompile("type (?P<name>.*)[\\s]+{")
)
func GoFormatApi(c *cli.Context) error {
useStdin := c.Bool("stdin")
@@ -65,10 +57,7 @@ func ApiFormatByStdin() error {
return err
}
result, err := apiFormat(string(data))
if err != nil {
return err
}
result := apiFormat(string(data))
_, err = fmt.Print(result)
if err != nil {
@@ -83,98 +72,28 @@ func ApiFormatByPath(apiFilePath string) error {
return err
}
result, err := apiFormat(string(data))
if err != nil {
return err
}
result := apiFormat(string(data))
if err := ioutil.WriteFile(apiFilePath, []byte(result), os.ModePerm); err != nil {
return err
}
return nil
}
func apiFormat(data string) (string, error) {
r := reg.ReplaceAllStringFunc(data, func(m string) string {
parts := reg.FindStringSubmatch(m)
if len(parts) < 2 {
return m
}
if !strings.Contains(m, "struct") {
return "type " + parts[1] + " struct {"
}
return m
})
apiStruct, err := parser.ParseApi(r)
if err != nil {
return "", err
}
info := strings.TrimSpace(apiStruct.Info)
if len(apiStruct.Service) == 0 {
return data, nil
}
fs, err := format.Source([]byte(strings.TrimSpace(apiStruct.Type)))
if err != nil {
str := err.Error()
lineNumber := strings.Index(str, ":")
if lineNumber > 0 {
ln, err := strconv.ParseInt(str[:lineNumber], 10, 64)
if err != nil {
return "", err
}
pn := 0
if len(info) > 0 {
pn = countRune(info, '\n') + 1
}
number := int(ln) + pn + 1
return "", errors.New(fmt.Sprintf("line: %d, %s", number, str[lineNumber+1:]))
}
return "", err
}
var result string
if len(strings.TrimSpace(info)) > 0 {
result += strings.TrimSpace(info) + "\n\n"
}
if len(strings.TrimSpace(apiStruct.Imports)) > 0 {
result += strings.TrimSpace(apiStruct.Imports) + "\n\n"
}
if len(strings.TrimSpace(string(fs))) > 0 {
result += strings.TrimSpace(string(fs)) + "\n\n"
}
if len(strings.TrimSpace(apiStruct.Service)) > 0 {
result += formatService(apiStruct.Service) + "\n\n"
}
return strings.TrimSpace(result), nil
}
func formatService(str string) string {
func apiFormat(data string) string {
var builder strings.Builder
scanner := bufio.NewScanner(strings.NewReader(str))
scanner := bufio.NewScanner(strings.NewReader(data))
var tapCount = 0
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == ")" || line == "}" {
noCommentLine := util.RemoveComment(line)
if noCommentLine == ")" || noCommentLine == "}" {
tapCount -= 1
}
util.WriteIndent(&builder, tapCount)
builder.WriteString(line + "\n")
if strings.HasSuffix(line, "(") || strings.HasSuffix(line, "{") {
if strings.HasSuffix(noCommentLine, "(") || strings.HasSuffix(noCommentLine, "{") {
tapCount += 1
}
}
return strings.TrimSpace(builder.String())
}
func countRune(s string, r rune) int {
count := 0
for _, c := range s {
if c == r {
count++
}
}
return count
}