/
format.go
248 lines (218 loc) · 5.86 KB
/
format.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package format
import (
"bufio"
"errors"
"fmt"
"go/format"
"go/scanner"
"io"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"github.com/wangliujing/goctl/api/parser"
"github.com/wangliujing/goctl/api/util"
"github.com/wangliujing/goctl/pkg/env"
apiF "github.com/wangliujing/goctl/pkg/parser/api/format"
"github.com/wangliujing/goctl/util/pathx"
"github.com/zeromicro/go-zero/core/errorx"
)
const (
leftParenthesis = "("
rightParenthesis = ")"
leftBrace = "{"
rightBrace = "}"
)
var (
// VarBoolUseStdin describes whether to use stdin or not.
VarBoolUseStdin bool
// VarBoolSkipCheckDeclare describes whether to skip.
VarBoolSkipCheckDeclare bool
// VarStringDir describes the directory.
VarStringDir string
// VarBoolIgnore describes whether to ignore.
VarBoolIgnore bool
)
// GoFormatApi format api file
func GoFormatApi(_ *cobra.Command, _ []string) error {
var be errorx.BatchError
if VarBoolUseStdin {
if err := apiFormatReader(os.Stdin, VarStringDir, VarBoolSkipCheckDeclare); err != nil {
be.Add(err)
}
} else {
if len(VarStringDir) == 0 {
return errors.New("missing -dir")
}
_, err := os.Lstat(VarStringDir)
if err != nil {
return errors.New(VarStringDir + ": No such file or directory")
}
err = filepath.Walk(VarStringDir, func(path string, fi os.FileInfo, errBack error) (err error) {
if strings.HasSuffix(path, ".api") {
if err := ApiFormatByPath(path, VarBoolSkipCheckDeclare); err != nil {
be.Add(util.WrapErr(err, fi.Name()))
}
}
return nil
})
be.Add(err)
}
if be.NotNil() {
scanner.PrintError(os.Stderr, be.Err())
os.Exit(1)
}
return be.Err()
}
// apiFormatReader
// filename is needed when there are `import` literals.
func apiFormatReader(reader io.Reader, filename string, skipCheckDeclare bool) error {
data, err := io.ReadAll(reader)
if err != nil {
return err
}
result, err := apiFormat(string(data), skipCheckDeclare, filename)
if err != nil {
return err
}
_, err = fmt.Print(result)
return err
}
// ApiFormatByPath format api from file path
func ApiFormatByPath(apiFilePath string, skipCheckDeclare bool) error {
if env.UseExperimental() {
return apiF.File(apiFilePath)
}
data, err := os.ReadFile(apiFilePath)
if err != nil {
return err
}
abs, err := filepath.Abs(apiFilePath)
if err != nil {
return err
}
result, err := apiFormat(string(data), skipCheckDeclare, abs)
if err != nil {
return err
}
_, err = parser.ParseContentWithParserSkipCheckTypeDeclaration(result, abs)
if err != nil {
return err
}
return os.WriteFile(apiFilePath, []byte(result), os.ModePerm)
}
func apiFormat(data string, skipCheckDeclare bool, filename ...string) (string, error) {
var err error
if skipCheckDeclare {
_, err = parser.ParseContentWithParserSkipCheckTypeDeclaration(data, filename...)
} else {
_, err = parser.ParseContent(data, filename...)
}
if err != nil {
return "", err
}
var builder strings.Builder
s := bufio.NewScanner(strings.NewReader(data))
tapCount := 0
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(pathx.NL)
}
newLineCount = 0
}
if tapCount == 0 {
ft, err := formatGoTypeDef(line, s, &builder)
if err != nil {
return "", err
}
if ft {
continue
}
}
noCommentLine := util.RemoveComment(line)
if noCommentLine == rightParenthesis || noCommentLine == rightBrace {
tapCount--
}
if tapCount < 0 {
line := strings.TrimSuffix(noCommentLine, rightBrace)
line = strings.TrimSpace(line)
if strings.HasSuffix(line, leftBrace) {
tapCount++
}
}
if line != "" {
util.WriteIndent(&builder, tapCount)
}
builder.WriteString(line + pathx.NL)
if strings.HasSuffix(noCommentLine, leftParenthesis) || strings.HasSuffix(noCommentLine, leftBrace) {
tapCount++
}
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) + pathx.NL)
for scanner.Scan() {
noCommentLine := util.RemoveComment(scanner.Text())
typeBuilder.WriteString(mayInsertStructKeyword(scanner.Text(), &tokenCount) + pathx.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++
}
if strings.Contains(noCommentLine, "`") {
return util.UpperFirst(strings.TrimSpace(line))
}
return line
}