-
Notifications
You must be signed in to change notification settings - Fork 4
/
markdown.go
363 lines (327 loc) · 9.45 KB
/
markdown.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package twowaysql
import (
"fmt"
"io"
"io/fs"
"sort"
"strings"
"github.com/shibukawa/mdd-go"
"gopkg.in/yaml.v2"
)
// Document contains SQL and metadata
type Document struct {
SQL string `json:"sql"`
Title string `json:"title"`
Params []Param `json:"params"`
CRUDMatrix []CRUDMatrix `json:"crud_matrix,omitempty"`
TestCases []TestCase `json:"testcases,omitempty"`
CommonTestFixture Fixture `json:"common_test_fixtures,omitempty"`
}
type Fixture struct {
Lang string
Code string
Tables []Table
}
// document absorb diffs between raw Markdown representation and public Document type
type document struct {
SQL string
Title string
Params []Param
CRUDMatrix []CRUDMatrix
TestCases []testCase
RawCommonTestFixture string
RawCommonTestFixtureLang string
parsedCommonTestFixture []Table
}
// Param is parameter type of 2-Way-SQL
type Param struct {
Name string `json:"name"`
Type ParamType `json:"type"`
Value string `json:"value"`
Description string `json:"description,omitempty"`
}
// CRUDMatrix represents CRUD Matrix
type CRUDMatrix struct {
Table string `json:"table"`
C bool `json:"c"`
R bool `json:"r"`
U bool `json:"u"`
D bool `json:"d"`
Description string `json:"description,omitempty"`
}
type Table struct {
MatchRule MatchRule `json:"type"`
Name string `json:"name"`
Cells [][]string `json:"cells"`
}
type TestCase struct {
Name string
Params map[string]string
TestQuery string
Expect [][]string
Fixtures []Table
}
type testCase struct {
Name string
RawTest string
parsedFixtures []Table
parsedTestQuery string
parsedExpect [][]string
parsedParams map[string]string
}
func parseFixture(src string) (map[string][][]string, bool) {
tempSliceYaml := struct {
Tables map[string][][]string `yaml:"fixtures"`
}{}
tempMapYaml := struct {
Tables map[string][]map[string]string `yaml:"fixtures"`
}{}
if err := yaml.Unmarshal([]byte(src), &tempSliceYaml); err == nil {
return tempSliceYaml.Tables, true
} else if err := yaml.Unmarshal([]byte(src), &tempMapYaml); err == nil {
result := make(map[string][][]string)
for k, v := range tempMapYaml.Tables {
result[k] = convertTableMapToSlice(v)
}
return result, true
}
return nil, false
}
func parseExpect(src string) ([][]string, string, map[string]string, bool) {
tempSliceYaml := struct {
Param map[string]string `yaml:"params"`
TestQuery string `yaml:"testQuery"`
Expect [][]string `yaml:"expect"`
}{}
tempMapYaml := struct {
Param map[string]string `yaml:"params"`
TestQuery string `yaml:"testQuery"`
Expect []map[string]string `yaml:"expect"`
}{}
if err := yaml.Unmarshal([]byte(src), &tempSliceYaml); err == nil {
return tempSliceYaml.Expect, tempSliceYaml.TestQuery, tempSliceYaml.Param, true
} else if err := yaml.Unmarshal([]byte(src), &tempMapYaml); err == nil {
return convertTableMapToSlice(tempMapYaml.Expect), tempMapYaml.TestQuery, tempMapYaml.Param, true
}
return nil, "", nil, false
}
var (
acceptableKeysInGlobalFixture = map[string]bool{
"fixtures": true,
}
acceptableKeysInLocalTestCases = map[string]bool{
"fixtures": true,
"params": true,
"testQuery": true,
"expect": true,
}
)
func checkKeys(src string, acceptableKeys map[string]bool, label string) error {
var temp map[string]any
err := yaml.Unmarshal([]byte(src), &temp)
if err != nil {
return err
}
var mismatch []string
for key := range temp {
if _, ok := acceptableKeys[key]; !ok {
mismatch = append(mismatch, key)
}
}
if len(mismatch) > 0 {
var keys []string
for key := range acceptableKeys {
keys = append(keys, key)
}
sort.Strings(mismatch)
sort.Strings(keys)
return fmt.Errorf("YAML keys %s is invalid in %s (%s are acceptable)", strings.Join(mismatch, ", "), label, strings.Join(keys, ", "))
}
return nil
}
func (d *document) PostProcess() error {
if d.RawCommonTestFixtureLang == "yaml" {
err := checkKeys(d.RawCommonTestFixture, acceptableKeysInGlobalFixture, d.Title)
if err != nil {
return err
}
if parsed, ok := parseFixture(d.RawCommonTestFixture); ok {
for k, cells := range parsed {
d.parsedCommonTestFixture = append(d.parsedCommonTestFixture, Table{
Name: k,
Cells: cells,
})
}
}
}
for i, tc := range d.TestCases {
err := checkKeys(tc.RawTest, acceptableKeysInLocalTestCases, tc.Name+" of "+d.Title)
if err != nil {
return err
}
if parsed, ok := parseFixture(tc.RawTest); ok {
for k, cells := range parsed {
tc.parsedFixtures = append(tc.parsedFixtures, Table{
Name: k,
Cells: cells,
})
}
}
if parsed, testQuery, params, ok := parseExpect(tc.RawTest); ok {
tc.parsedExpect = parsed
tc.parsedTestQuery = testQuery
tc.parsedParams = params
} else {
return fmt.Errorf("can't parse yaml of test '%s'", tc.Name)
}
d.TestCases[i] = tc
}
return nil
}
func (d document) ToDocument() *Document {
result := &Document{
SQL: d.SQL,
Title: d.Title,
Params: d.Params,
CRUDMatrix: d.CRUDMatrix,
}
switch d.RawCommonTestFixtureLang {
case "yaml":
result.CommonTestFixture = Fixture{
Lang: "yaml",
Tables: d.parsedCommonTestFixture,
}
case "sql":
result.CommonTestFixture = Fixture{
Lang: "sql",
Code: d.RawCommonTestFixture,
}
}
for _, tc := range d.TestCases {
result.TestCases = append(result.TestCases, TestCase{
Name: tc.Name,
Params: tc.parsedParams,
TestQuery: tc.parsedTestQuery,
Expect: tc.parsedExpect,
Fixtures: tc.parsedFixtures,
})
}
return result
}
var docJig = mdd.NewDocJig[document]()
func init() {
docJig.Alias("Title").Lang("ja", "タイトル")
docJig.Alias("Parameters", "Parameter").Lang("ja", "パラメータ", "引数")
docJig.Alias("CRUD Matrix", "CRUD Table").Lang("ja", "CRUDマトリックス", "CRUD図", "CRUD表")
docJig.Alias("Name").Lang("ja", "パラメータ名", "名前")
docJig.Alias("Type").Lang("ja", "型", "タイプ")
docJig.Alias("Description", "Desc", "Detail").Lang("ja", "説明", "詳細")
docJig.Alias("Table").Lang("ja", "テーブル")
docJig.Alias("Test", "Tests", "Sample", "Samples", "Example", "Examples").Lang("ja", "テスト", "サンプル", "実行例")
docJig.Alias("Case", "Test Case", "TestCase").Lang("ja", "ケース", "テストケース")
root := docJig.Root().Label("Title")
root.CodeFence("SQL", "sql").SampleCode("select * from table where id = 1;")
params := root.Child(".", "Parameters").Table("Params")
params.Field("Name", "Name").Required()
params.Field("Type").Required().As(func(typeName string, d *document) (any, error) {
t, ok := paramTypeMap[strings.ToLower(typeName)]
if ok {
return t, nil
}
return nil, fmt.Errorf("type '%s' is invalid", typeName)
})
params.Field("Description")
crudMatrix := root.Child(".", "CRUD Matrix").Table("CRUDMatrix")
crudMatrix.Field("Table").Required()
crudMatrix.Field("C").Required()
crudMatrix.Field("R").Required()
crudMatrix.Field("U").Required()
crudMatrix.Field("D").Required()
crudMatrix.Field("Description")
test := root.Child(".", "Test")
test.CodeFence("RawCommonTestFixture", "sql", "yaml").Language("RawCommonTestFixtureLang")
testcase := test.Children("TestCases", "Case")
testcase.Label("Name")
testcase.CodeFence("RawTest", "yaml")
}
// ParseMarkdownFile parses markdown file
func ParseMarkdownFile(filepath string) (*Document, error) {
d, err := docJig.ParseFile(filepath)
if err != nil {
return nil, err
}
return d.ToDocument(), err
}
// ParseMarkdown parses markdown content
func ParseMarkdown(r io.Reader) (*Document, error) {
d, err := docJig.Parse(r)
if err != nil {
return nil, err
}
return d.ToDocument(), err
}
// ParseMarkdown parses markdown content
func ParseMarkdownString(src string) (*Document, error) {
d, err := docJig.ParseString(src)
if err != nil {
return nil, err
}
return d.ToDocument(), err
}
// ParseMarkdownGlob parses markdown files to match patterns
func ParseMarkdownGlob(pattern ...string) (map[string]*Document, error) {
ds, err := docJig.ParseGlob(pattern...)
if err != nil {
return nil, err
}
result := make(map[string]*Document)
for k, d := range ds {
result[k] = d.ToDocument()
}
return result, err
}
// ParseMarkdown parses markdown content
func ParseMarkdownFS(fsys fs.FS, pattern ...string) (map[string]*Document, error) {
ds, err := docJig.ParseFS(fsys, pattern...)
if err != nil {
return nil, err
}
result := make(map[string]*Document)
for k, d := range ds {
result[k] = d.ToDocument()
}
return result, err
}
// ParseMarkdown parses markdown content
func GenerateMarkdown(w io.Writer, lang string) error {
return docJig.GenerateTemplate(w, mdd.GenerateOption{
Language: lang,
})
}
func convertTableMapToSlice(table []map[string]string) [][]string {
var headers []string
existingCheck := map[string]bool{}
for _, row := range table {
for k := range row {
if !existingCheck[k] {
headers = append(headers, k)
existingCheck[k] = true
}
}
}
sort.Strings(headers)
slices := make([][]string, len(table)+1)
slices[0] = headers
for r, row := range table {
rowSlice := make([]string, len(headers))
for c, h := range headers {
if v, ok := row[h]; ok {
rowSlice[c] = v
} else {
rowSlice[c] = ""
}
}
slices[r+1] = rowSlice
}
return slices
}