format service and add test (#197)
Co-authored-by: kim <xutao@xiaoheiban.cn>
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package format
|
package format
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/format"
|
"go/format"
|
||||||
@@ -144,10 +145,28 @@ func apiFormat(data string) (string, error) {
|
|||||||
result += strings.TrimSpace(string(fs)) + "\n\n"
|
result += strings.TrimSpace(string(fs)) + "\n\n"
|
||||||
}
|
}
|
||||||
if len(strings.TrimSpace(apiStruct.Service)) > 0 {
|
if len(strings.TrimSpace(apiStruct.Service)) > 0 {
|
||||||
result += strings.TrimSpace(apiStruct.Service) + "\n\n"
|
result += formatService(apiStruct.Service) + "\n\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
return result, nil
|
return strings.TrimSpace(result), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatService(str string) string {
|
||||||
|
var builder strings.Builder
|
||||||
|
scanner := bufio.NewScanner(strings.NewReader(str))
|
||||||
|
var tapCount = 0
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := strings.TrimSpace(scanner.Text())
|
||||||
|
if line == ")" || line == "}" {
|
||||||
|
tapCount -= 1
|
||||||
|
}
|
||||||
|
util.WriteIndent(&builder, tapCount)
|
||||||
|
builder.WriteString(line + "\n")
|
||||||
|
if strings.HasSuffix(line, "(") || strings.HasSuffix(line, "{") {
|
||||||
|
tapCount += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return strings.TrimSpace(builder.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func countRune(s string, r rune) int {
|
func countRune(s string, r rune) int {
|
||||||
|
|||||||
47
tools/goctl/api/format/format_test.go
Normal file
47
tools/goctl/api/format/format_test.go
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
package format
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
notFormattedStr = `
|
||||||
|
type Request struct {
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Response struct {
|
||||||
|
Message string
|
||||||
|
}
|
||||||
|
|
||||||
|
service A-api {
|
||||||
|
@server(
|
||||||
|
handler: GreetHandler
|
||||||
|
)
|
||||||
|
get /greet/from/:name(Request) returns (Response)
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
formattedStr = `type Request struct {
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Response struct {
|
||||||
|
Message string
|
||||||
|
}
|
||||||
|
|
||||||
|
service A-api {
|
||||||
|
@server(
|
||||||
|
handler: GreetHandler
|
||||||
|
)
|
||||||
|
get /greet/from/:name(Request) returns (Response)
|
||||||
|
}`
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestInlineTypeNotExist(t *testing.T) {
|
||||||
|
r, err := apiFormat(notFormattedStr)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, r, formattedStr)
|
||||||
|
}
|
||||||
@@ -26,14 +26,8 @@ func getParentPackage(dir string) (string, error) {
|
|||||||
return filepath.ToSlash(filepath.Join(projectCtx.Path, strings.TrimPrefix(projectCtx.WorkDir, projectCtx.Dir))), nil
|
return filepath.ToSlash(filepath.Join(projectCtx.Path, strings.TrimPrefix(projectCtx.WorkDir, projectCtx.Dir))), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeIndent(writer io.Writer, indent int) {
|
|
||||||
for i := 0; i < indent; i++ {
|
|
||||||
fmt.Fprint(writer, "\t")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func writeProperty(writer io.Writer, name, tp, tag, comment string, indent int) error {
|
func writeProperty(writer io.Writer, name, tp, tag, comment string, indent int) error {
|
||||||
writeIndent(writer, indent)
|
util.WriteIndent(writer, indent)
|
||||||
var err error
|
var err error
|
||||||
if len(comment) > 0 {
|
if len(comment) > 0 {
|
||||||
comment = strings.TrimPrefix(comment, "//")
|
comment = strings.TrimPrefix(comment, "//")
|
||||||
|
|||||||
@@ -1,95 +0,0 @@
|
|||||||
package parser
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
|
||||||
)
|
|
||||||
|
|
||||||
type typeState struct {
|
|
||||||
*baseState
|
|
||||||
annos []spec.Annotation
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTypeState(state *baseState, annos []spec.Annotation) state {
|
|
||||||
return &typeState{
|
|
||||||
baseState: state,
|
|
||||||
annos: annos,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *typeState) process(api *spec.ApiSpec) (state, error) {
|
|
||||||
var name string
|
|
||||||
var members []spec.Member
|
|
||||||
parser := &typeEntityParser{
|
|
||||||
acceptName: func(n string) {
|
|
||||||
name = n
|
|
||||||
},
|
|
||||||
acceptMember: func(member spec.Member) {
|
|
||||||
members = append(members, member)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
ent := newEntity(s.baseState, api, parser)
|
|
||||||
if err := ent.process(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
api.Types = append(api.Types, spec.Type{
|
|
||||||
Name: name,
|
|
||||||
Annotations: s.annos,
|
|
||||||
Members: members,
|
|
||||||
})
|
|
||||||
|
|
||||||
return newRootState(s.r, s.lineNumber), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type typeEntityParser struct {
|
|
||||||
acceptName func(name string)
|
|
||||||
acceptMember func(member spec.Member)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *typeEntityParser) parseLine(line string, api *spec.ApiSpec, annos []spec.Annotation) error {
|
|
||||||
index := strings.Index(line, "//")
|
|
||||||
comment := ""
|
|
||||||
if index >= 0 {
|
|
||||||
comment = line[index+2:]
|
|
||||||
line = strings.TrimSpace(line[:index])
|
|
||||||
}
|
|
||||||
fields := strings.Fields(line)
|
|
||||||
if len(fields) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if len(fields) == 1 {
|
|
||||||
p.acceptMember(spec.Member{
|
|
||||||
Annotations: annos,
|
|
||||||
Name: fields[0],
|
|
||||||
Type: fields[0],
|
|
||||||
IsInline: true,
|
|
||||||
})
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
name := fields[0]
|
|
||||||
tp := fields[1]
|
|
||||||
var tag string
|
|
||||||
if len(fields) > 2 {
|
|
||||||
tag = fields[2]
|
|
||||||
} else {
|
|
||||||
tag = fmt.Sprintf("`json:\"%s\"`", util.Untitle(name))
|
|
||||||
}
|
|
||||||
|
|
||||||
p.acceptMember(spec.Member{
|
|
||||||
Annotations: annos,
|
|
||||||
Name: name,
|
|
||||||
Type: tp,
|
|
||||||
Tag: tag,
|
|
||||||
Comment: comment,
|
|
||||||
IsInline: false,
|
|
||||||
})
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *typeEntityParser) setEntityName(name string) {
|
|
||||||
p.acceptName(name)
|
|
||||||
}
|
|
||||||
@@ -75,3 +75,9 @@ func ComponentName(api *spec.ApiSpec) string {
|
|||||||
}
|
}
|
||||||
return name + "Components"
|
return name + "Components"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WriteIndent(writer io.Writer, indent int) {
|
||||||
|
for i := 0; i < indent; i++ {
|
||||||
|
fmt.Fprint(writer, "\t")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user