-
Notifications
You must be signed in to change notification settings - Fork 6
/
xml.go
790 lines (733 loc) · 25.6 KB
/
xml.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
package importer
import (
"bytes"
"encoding/xml"
"fmt"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/antchfx/xmlquery"
"github.com/emirpasic/gods/sets/treeset"
"github.com/pkg/errors"
"github.com/tableauio/tableau/internal/importer/book"
"github.com/tableauio/tableau/internal/types"
"github.com/tableauio/tableau/internal/xlsxgen"
"github.com/tableauio/tableau/log"
"github.com/tableauio/tableau/log/core"
"github.com/tableauio/tableau/options"
"github.com/tableauio/tableau/proto/tableaupb"
)
type XMLImporter struct {
*book.Book
}
var attrRegexp *regexp.Regexp
var tagRegexp *regexp.Regexp
var scalarListRegexp *regexp.Regexp
var metasheetRegexp *regexp.Regexp
const (
xmlProlog = `<?xml version='1.0' encoding='UTF-8'?>`
atTableauDisplacement = `ATABLEAU`
ungreedyPropGroup = `(\|\{[^\{\}]+\})?` // e.g.: |{default:"100"}
metasheetItemBlock = `<Item(\s+\S+\s*=\s*("\S+"|'\S+'))+\s*/>` // e.g.: <Item Sheet="XXXConf" Sep="|"/>
sheetBlock = `<%v(>(.*\n)*</%v>|\s*/>)` // e.g.: <XXXConf>...</XXXConf>
)
func init() {
attrRegexp = regexp.MustCompile(`\s*=\s*("|')` + types.TypeGroup + ungreedyPropGroup + `("|')`) // e.g.: = "int32|{range:"1,~"}"
tagRegexp = regexp.MustCompile(`>` + types.TypeGroup + ungreedyPropGroup + `</`) // e.g.: >int32|{range:"1,~"}</
scalarListRegexp = regexp.MustCompile(`([A-Za-z_]+)([0-9]+)`) // e.g.: Para1, Para2, Para3, ...
// metasheet regexp, e.g.:
// <!--
// <@TABLEAU>
// <Item Sheet="Server" />
// </@TABLEAU>
// <Server>
// <Weight Num="map<uint32, Weight>"/>
// </Server>
// -->
metasheetRegexp = regexp.MustCompile(fmt.Sprintf(`<!--\s+(<%v(>(\s+`+metasheetItemBlock+`\s+)*</%v>|\s*/>)(.*\n)+?)-->`, book.MetasheetName, book.MetasheetName))
}
// TODO: options
func NewXMLImporter(filename string, sheets []string, parser book.SheetParser, mode ImporterMode) (*XMLImporter, error) {
newBook, err := parseXML(filename, sheets, parser, mode)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse xml:%s", filename)
}
if newBook == nil {
log.Debugf("xml:%s parsed to an empty book", filename)
bookName := strings.TrimSuffix(filepath.Base(filename), filepath.Ext(filename))
return &XMLImporter{
Book: book.NewBook(bookName, filename, nil),
}, nil
}
if log.Level() == core.DebugLevel.CapitalString() {
newBook.ExportCSV()
}
return &XMLImporter{
Book: newBook,
}, nil
}
// splitRawXML splits the raw xml into metasheet and content (which is the xml data)
func splitRawXML(rawXML string) (metasheet, content string) {
matches := matchMetasheet(rawXML)
if len(matches) < 2 {
return "", rawXML
}
content = strings.ReplaceAll(rawXML, matches[0], "")
metasheet = xmlProlog + "\n" + escapeAttrs(strings.ReplaceAll(matches[1], book.MetasheetName, atTableauDisplacement))
return metasheet, content
}
// parseXML parse sheets in the XML file named `filename` and return a book with multiple sheets
// in TABLEAU grammar which can be exported to protobuf by excel parser.
func parseXML(filename string, sheetNames []string, parser book.SheetParser, mode ImporterMode) (*book.Book, error) {
log.Debugf("xml: %s", filename)
buf, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
// pre check if exists `@TABLEAU`
metasheet, content := splitRawXML(string(buf))
if metasheet == "" {
log.Debugf("xml:%s no need parse: %s not found", filename, book.MetasheetName)
return nil, nil
}
// The first pass
bookName := strings.TrimSuffix(filepath.Base(filename), filepath.Ext(filename))
newBook := book.NewBook(bookName, filename, parser)
xmlMeta, err := readXMLFile(metasheet, content, newBook, mode)
if err != nil {
return nil, err
}
for _, xmlSheet := range xmlMeta.SheetList {
sheetName := xmlSheet.Meta.Name
// The second pass
if err := preprocess(xmlSheet, xmlSheet.Meta); err != nil {
return nil, errors.Wrapf(err, "failed to preprocess for sheet: %s", sheetName)
}
// The third pass
sheet, err := genSheet(xmlSheet)
if err != nil {
return nil, errors.Wrapf(err, "failed to genSheet for sheet: %s", sheetName)
}
newBook.AddSheet(sheet)
}
// parse meta sheet
if parser != nil {
if err := newBook.ParseMetaAndPurge(); err != nil {
return nil, errors.WithMessage(err, "failed to parse metasheet")
}
}
if len(sheetNames) > 0 {
newBook.Squeeze(sheetNames)
}
return newBook, nil
}
// --------------------------------------------- THE FIRST PASS ------------------------------------ //
// The first pass simply reads xml file with xmlquery, construct a recursively self-described tree
// structure defined in xml.proto and put it into memory.
//
// readXMLFile read the raw xml rooted at `root`, specify which sheets to parse and return a XMLBook.
func readXMLFile(metasheet, content string, newBook *book.Book, mode ImporterMode) (*tableaupb.XMLBook, error) {
xmlMeta := &tableaupb.XMLBook{
SheetMap: make(map[string]int32),
}
// sheetName -> {colName -> val}
metasheetMap := make(map[string]map[string]string)
// parse metasheet
metaRoot, err := xmlquery.Parse(strings.NewReader(metasheet))
if err != nil {
return nil, errors.Wrapf(err, "failed to parse @TABLEAU string: %s", metasheet)
}
for n := metaRoot.FirstChild; n != nil; n = n.NextSibling {
if n.Type != xmlquery.ElementNode {
continue
}
// <@TABLEAU>...</@TABLEAU>
if n.Data == atTableauDisplacement {
var sheet *book.Sheet
if metasheetMap, sheet, err = genMetasheet(n); err != nil {
return nil, errors.Wrapf(err, "failed to generate metasheet")
}
newBook.AddSheet(sheet)
continue
}
sheetName := n.Data
xmlSheet := getXMLSheet(xmlMeta, sheetName)
if err := parseMetaNode(n, xmlSheet); err != nil {
return nil, errors.Wrapf(err, "failed to parseMetaNode for sheet:%s", sheetName)
}
}
if mode == Protogen {
// strip template sheets
for sheet, colMap := range metasheetMap {
if template, ok := colMap["Template"]; !ok || template != "true" {
continue
}
matches := matchSheetBlock(content, sheet)
if len(matches) == 0 {
continue
}
content = strings.ReplaceAll(content, matches[0], "")
}
}
// parse data content
dataRoot, err := xmlquery.Parse(strings.NewReader(content))
if err != nil {
return nil, errors.Wrapf(err, "failed to parse @TABLEAU string: %s", content)
}
for n := dataRoot.FirstChild; n != nil; n = n.NextSibling {
if n.Type != xmlquery.ElementNode {
continue
}
sheetName := n.Data
xmlSheet := getXMLSheet(xmlMeta, sheetName)
if err := parseDataNode(n, xmlSheet); err != nil {
return nil, errors.Wrapf(err, "failed to parseDataNode for sheet:%s", sheetName)
}
}
return xmlMeta, nil
}
// genMetasheet generates metasheet according to `
//
// <@TABLEAU>
// <Item Sheet="XXXConf" />
// </@TABLEAU>`
func genMetasheet(tableauNode *xmlquery.Node) (map[string]map[string]string, *book.Sheet, error) {
// sheetName -> {colName -> val}
metasheetMap := make(map[string]map[string]string)
nameRow := book.MetasheetOptions().Namerow - 1
dataRow := book.MetasheetOptions().Datarow - 1
set := treeset.NewWithStringComparator()
for n := tableauNode.FirstChild; n != nil; n = n.NextSibling {
if n.Type != xmlquery.ElementNode {
continue
}
sheetMap := make(map[string]string)
for _, attr := range n.Attr {
sheetMap[attr.Name.Local] = attr.Value
}
sheetMap["Nested"] = "true"
// use explicit settings to avoid implicit settings exception.
sheetMap["Namerow"] = "1"
sheetMap["Typerow"] = "2"
sheetMap["Noterow"] = "3"
sheetMap["Datarow"] = "4"
sheetMap["Nameline"] = "1"
sheetMap["Typeline"] = "1"
sheetName, ok := sheetMap["Sheet"]
if !ok {
return metasheetMap, nil, fmt.Errorf("@TABLEAU not specified sheetName by keyword `Sheet`")
}
metasheetMap[sheetName] = sheetMap
for k := range sheetMap {
set.Add(k)
}
}
rows := make([][]string, len(metasheetMap)+int(dataRow))
for _, k := range set.Values() {
rows[nameRow] = append(rows[nameRow], k.(string))
}
for _, sheet := range metasheetMap {
for _, k := range set.Values() {
if v, ok := sheet[k.(string)]; ok {
rows[dataRow] = append(rows[dataRow], v)
} else {
rows[dataRow] = append(rows[dataRow], "")
}
}
dataRow++
}
sheet := book.NewSheet(book.MetasheetName, rows)
return metasheetMap, sheet, nil
}
// addMetaNodeAttr adds an attribute to MetaNode AttrMap if not exists and otherwise replaces the attribute value
func addMetaNodeAttr(attrMap *tableaupb.XMLNode_AttrMap, name, val string) {
if idx, ok := attrMap.Map[name]; !ok {
attrMap.Map[name] = int32(len(attrMap.List))
attrMap.List = append(attrMap.List, &tableaupb.XMLNode_AttrMap_Attr{
Name: name,
Value: val,
})
} else {
// replace attribute value by metaSheet
metaAttr := attrMap.List[idx]
metaAttr.Value = val
}
}
// addDataNodeAttr adds an attribute to DataNode AttrMap
func addDataNodeAttr(metaMap, dataMap *tableaupb.XMLNode_AttrMap, name, val string) {
if _, ok := metaMap.Map[name]; !ok {
metaMap.Map[name] = int32(len(metaMap.List))
metaMap.List = append(metaMap.List, &tableaupb.XMLNode_AttrMap_Attr{
Name: name,
Value: inferType(val),
})
}
dataMap.Map[name] = int32(len(dataMap.List))
dataMap.List = append(dataMap.List, &tableaupb.XMLNode_AttrMap_Attr{
Name: name,
Value: val,
})
}
// hasChild check if xml node has any child element node
func hasChild(n *xmlquery.Node) bool {
for n := n.FirstChild; n != nil; n = n.NextSibling {
if n.Type == xmlquery.ElementNode {
return true
}
}
return false
}
// getTextContent get the text node from xml node
func getTextContent(n *xmlquery.Node) string {
if hasChild(n) {
return ""
}
for n := n.FirstChild; n != nil; n = n.NextSibling {
if n.Type == xmlquery.TextNode {
return strings.TrimSpace(n.Data)
}
}
return ""
}
// parseMetaNode parse xml node `curr` and construct the meta tree in `xmlSheet`.
func parseMetaNode(curr *xmlquery.Node, xmlSheet *tableaupb.XMLSheet) error {
_, path := getNodePath(curr)
meta := xmlSheet.MetaNodeMap[path]
for _, attr := range curr.Attr {
attrName := attr.Name.Local
t := attr.Value
if len(meta.AttrMap.List) > 0 && isCrossCell(t) {
return fmt.Errorf("%s=\"%s\" is a complex type, must be the first attribute", attrName, t)
}
addMetaNodeAttr(meta.AttrMap, attrName, t)
}
for n := curr.FirstChild; n != nil; n = n.NextSibling {
if n.Type != xmlquery.ElementNode {
continue
}
// e.g.: <MaxNum>int32</MaxNum>
if innerText := getTextContent(n); innerText != "" {
attrName := n.Data
addMetaNodeAttr(meta.AttrMap, attrName, innerText)
continue
}
childName := n.Data
if _, ok := meta.ChildMap[childName]; !ok {
newNode := newNode(childName, meta)
meta.ChildMap[childName] = &tableaupb.XMLNode_IndexList{
Indexes: []int32{int32(len(meta.ChildList))},
}
meta.ChildList = append(meta.ChildList, newNode)
registerMetaNode(xmlSheet, newNode)
}
if err := parseMetaNode(n, xmlSheet); err != nil {
return errors.Wrapf(err, "failed to parseMetaNode for %s@%s", childName, meta.Name)
}
}
return nil
}
// parseDataNode parse xml node `curr`, complete the meta tree and fill the data into `xmlSheet`.
func parseDataNode(curr *xmlquery.Node, xmlSheet *tableaupb.XMLSheet) error {
_, path := getNodePath(curr)
meta := xmlSheet.MetaNodeMap[path]
data_nodes := xmlSheet.DataNodeMap[path].Nodes
data := data_nodes[len(data_nodes)-1]
for _, attr := range curr.Attr {
attrName := attr.Name.Local
addDataNodeAttr(meta.AttrMap, data.AttrMap, attrName, attr.Value)
}
for n := curr.FirstChild; n != nil; n = n.NextSibling {
if n.Type != xmlquery.ElementNode {
continue
}
// e.g.: <MaxNum>100</MaxNum>
if innerText := getTextContent(n); innerText != "" {
attrName := n.Data
addDataNodeAttr(meta.AttrMap, data.AttrMap, attrName, innerText)
continue
}
childName := n.Data
dataChild := newNode(childName, data)
registerDataNode(xmlSheet, dataChild)
if _, ok := meta.ChildMap[childName]; !ok {
newNode := newNode(childName, meta)
meta.ChildMap[childName] = &tableaupb.XMLNode_IndexList{
Indexes: []int32{int32(len(meta.ChildList))},
}
meta.ChildList = append(meta.ChildList, newNode)
registerMetaNode(xmlSheet, newNode)
}
if err := parseDataNode(n, xmlSheet); err != nil {
return errors.Wrapf(err, "failed to parseDataNode for %s@%s", childName, meta.Name)
}
if list, ok := data.ChildMap[childName]; !ok {
data.ChildMap[childName] = &tableaupb.XMLNode_IndexList{
Indexes: []int32{int32(len(data.ChildList))},
}
} else {
list.Indexes = append(list.Indexes, int32(len(data.ChildList)))
}
data.ChildList = append(data.ChildList, dataChild)
}
return nil
}
// escapeMetaDoc escape characters for all attribute values in the document. e.g.:
//
// <ServerConf key="map<uint32,ServerConf>" Open="bool">
// ...
// </ServerConf>
//
// will be converted to
//
// <ServerConf key="map<uint32,ServerConf>" Open="bool">
// ...
// </ServerConf>
func escapeAttrs(doc string) string {
escapedDoc := attrRegexp.ReplaceAllStringFunc(doc, func(s string) string {
matches := matchAttr(s)
var typeBuf, propBuf bytes.Buffer
xml.EscapeText(&typeBuf, []byte(matches[2]))
xml.EscapeText(&propBuf, []byte(matches[3]))
return fmt.Sprintf("=\"%s%s\"", typeBuf.String(), propBuf.String())
})
escapedDoc = tagRegexp.ReplaceAllStringFunc(escapedDoc, func(s string) string {
matches := matchTag(s)
var typeBuf, propBuf bytes.Buffer
xml.EscapeText(&typeBuf, []byte(matches[1]))
xml.EscapeText(&propBuf, []byte(matches[2]))
return fmt.Sprintf(">%s%s</", typeBuf.String(), propBuf.String())
})
return escapedDoc
}
// getNodePath get the root and the path walking from root to `curr` in the tree.
func getNodePath(curr *xmlquery.Node) (root *xmlquery.Node, path string) {
path = curr.Data
for n := curr.Parent; n != nil; n = n.Parent {
if n.Data == "" {
root = n
} else {
path = fmt.Sprintf("%s/%s", n.Data, path)
}
}
return root, path
}
// inferType infer type from the node value, e.g.:
// - 4324342: `int32`
// - 4324324324324343243432: `int64`
// - 4535ffdr43t3r: `string`
func inferType(value string) string {
if _, err := strconv.Atoi(value); err == nil {
return "int32"
} else if _, err := strconv.ParseInt(value, 10, 64); err == nil {
return "int64"
} else {
return "string"
}
}
func matchAttr(s string) []string {
return attrRegexp.FindStringSubmatch(s)
}
func matchTag(s string) []string {
return tagRegexp.FindStringSubmatch(s)
}
func matchScalarList(s string) []string {
return scalarListRegexp.FindStringSubmatch(s)
}
func matchMetasheet(s string) []string {
return metasheetRegexp.FindStringSubmatch(s)
}
func matchSheetBlock(xml, sheetName string) []string {
sheetRegexp := regexp.MustCompile(fmt.Sprintf(sheetBlock, sheetName, sheetName))
return sheetRegexp.FindStringSubmatch(xml)
}
func newOrderedAttrMap() *tableaupb.XMLNode_AttrMap {
return &tableaupb.XMLNode_AttrMap{
Map: make(map[string]int32),
}
}
func newNode(nodeName string, parent *tableaupb.XMLNode) *tableaupb.XMLNode {
node := &tableaupb.XMLNode{
Name: nodeName,
AttrMap: newOrderedAttrMap(),
ChildMap: make(map[string]*tableaupb.XMLNode_IndexList),
Parent: parent,
}
if parent != nil {
node.Path = fmt.Sprintf("%s/%s", parent.Path, nodeName)
} else {
node.Path = nodeName
}
return node
}
func registerMetaNode(xmlSheet *tableaupb.XMLSheet, node *tableaupb.XMLNode) {
if _, ok := xmlSheet.MetaNodeMap[node.Path]; !ok {
xmlSheet.MetaNodeMap[node.Path] = node
} else {
log.Panicf("duplicated path registered in MetaNodeMap|Path:%s", node.Path)
}
}
func registerDataNode(xmlSheet *tableaupb.XMLSheet, node *tableaupb.XMLNode) {
if list, ok := xmlSheet.DataNodeMap[node.Path]; !ok {
xmlSheet.DataNodeMap[node.Path] = &tableaupb.XMLSheet_NodeList{
Nodes: []*tableaupb.XMLNode{node},
}
} else {
list.Nodes = append(list.Nodes, node)
}
}
func newXMLSheet(sheetName string) *tableaupb.XMLSheet {
return &tableaupb.XMLSheet{
Meta: newNode(sheetName, nil),
Data: newNode(sheetName, nil),
MetaNodeMap: make(map[string]*tableaupb.XMLNode),
DataNodeMap: make(map[string]*tableaupb.XMLSheet_NodeList),
}
}
func getXMLSheet(xmlMeta *tableaupb.XMLBook, sheetName string) *tableaupb.XMLSheet {
if idx, ok := xmlMeta.SheetMap[sheetName]; !ok {
xmlSheet := newXMLSheet(sheetName)
registerMetaNode(xmlSheet, xmlSheet.Meta)
registerDataNode(xmlSheet, xmlSheet.Data)
xmlMeta.SheetMap[sheetName] = int32(len(xmlMeta.SheetList))
xmlMeta.SheetList = append(xmlMeta.SheetList, xmlSheet)
return xmlSheet
} else {
return xmlMeta.SheetList[idx]
}
}
// --------------------------------------------- THE SECOND PASS ------------------------------------ //
// The second pass preprocesses the tree structure. In this phase the parser will do some necessary jobs
// before generating a 2-dimensional sheet, like correctType which make the types of attributes in the
// nodes meet the requirements of protogen.
func preprocess(xmlSheet *tableaupb.XMLSheet, node *tableaupb.XMLNode) error {
// rearrange attributes
if err := rearrangeAttrs(node.AttrMap); err != nil {
return errors.Wrapf(err, "failed to rearrangeAttrs")
}
// fix node types when it is the first attribute
for i, attr := range node.AttrMap.List {
if i == 0 {
attr.Value = fixNodeType(xmlSheet, node, attr.Value)
}
}
// recursively preprocess
for _, child := range node.ChildList {
if err := preprocess(xmlSheet, child); err != nil {
return errors.Wrapf(err, "failed to preprocess node:%s", child.Name)
}
}
return nil
}
// rearrangeAttrs change the order of attributes, e.g.:
// - attributes with cross-type types, such as cross-cell map (list, keyed-list, etc.),
// will be placed at the first.
// - simple list like `Param1, Param2, Param3, ...` will be grouped together and
// the type of `Param1` will be changed to something like `[]int32`.
func rearrangeAttrs(attrMap *tableaupb.XMLNode_AttrMap) error {
typeMap := make(map[string]string)
indexMap := make(map[int]int)
for i, attr := range attrMap.List {
mustFirst := isCrossCell(attr.Value)
if mustFirst {
swapAttr(attrMap, i, 0)
typeMap[attr.Name] = attr.Value
continue
}
matches := matchScalarList(attr.Name)
if len(matches) > 0 && types.IsScalarType(attr.Value) {
num, err := strconv.Atoi(matches[2])
if err != nil {
log.Errorf("strconv.Atoi failed|attr:%s|num:%s|err:%s", attr.Name, matches[2], err)
continue
}
indexMap[num] = i
}
}
// start with 1, e.g.: Param1, Param2, ...
for i, dst := 1, len(attrMap.List)-len(indexMap); ; i, dst = i+1, dst+1 {
index, ok := indexMap[i]
if !ok {
break
}
if i == 1 {
attr := attrMap.List[index]
attr.Value = fmt.Sprintf("[]%s", attr.Value)
}
swapAttr(attrMap, index, dst)
}
if len(typeMap) > 1 {
return fmt.Errorf("more than one non-scalar types: %v", typeMap)
}
return nil
}
// fixNodeType fix the type of `curr` in the `xmlSheet` based on its `oriType`. e.g.:
// - map<uint32,Weight>: {Test}map<uint32,Weight>
// - int32: {StructConf}{Weight}int32
// - []int64: {MapConf}[]int64
//
// NOTE: list and keyedlist auto-deduction not supported temporarily
func fixNodeType(xmlSheet *tableaupb.XMLSheet, curr *tableaupb.XMLNode, oriType string) (t string) {
t = oriType
// add type prefixes
for n, c := curr, curr; n != nil && n.Parent != nil; n, c = n.Parent, n {
if n == curr {
// curr is cross-cell, parent should not add prefix
if isCrossCell(oriType) {
continue
}
} else {
// not the first attr or not the first child, fix ok
if len(n.AttrMap.List) > 0 || !isFirstChild(c) {
break
}
}
if isRepeated(xmlSheet, n) {
if n == curr {
t = fmt.Sprintf("[%s]<%s>", n.Name, t)
} else {
t = fmt.Sprintf("[%s]%s", n.Name, t)
}
} else {
t = fmt.Sprintf("{%s}%s", n.Name, t)
}
}
return t
}
// isRepeated check if `curr` has other sibling nodes with the same name with itself.
func isRepeated(xmlSheet *tableaupb.XMLSheet, curr *tableaupb.XMLNode) bool {
strList := strings.Split(curr.Path, "/")
parentPath := strings.Join(strList[:len(strList)-1], "/")
if nodes, ok := xmlSheet.DataNodeMap[parentPath]; ok {
for _, n := range nodes.Nodes {
if indexes, ok := n.ChildMap[curr.Name]; ok && len(indexes.Indexes) > 1 {
return true
}
}
}
return false
}
// isCrossCell check if type string `t` is a cross-cell type.
func isCrossCell(t string) bool {
if types.IsMap(t) { // map case
desc := types.MatchMap(t)
return !(types.IsScalarType(desc.ValueType) || types.IsEnum(desc.ValueType))
} else if types.IsList(t) { // list case
desc := types.MatchList(t)
return desc.ElemType != ""
} else if types.IsKeyedList(t) { // keyed-list case
desc := types.MatchKeyedList(t)
return desc.ElemType != ""
} else if types.IsStruct(t) { // struct case
desc := types.MatchStruct(t)
return types.IsScalarType(desc.ColumnType) || types.IsEnum(desc.ColumnType)
}
return false
}
// isFirstChild check if `node` is the first child of its parent node.
func isFirstChild(node *tableaupb.XMLNode) bool {
if node.Parent == nil {
return false
}
return node.Parent.ChildList[0] == node
}
func swapAttr(attrMap *tableaupb.XMLNode_AttrMap, i, j int) {
attr := attrMap.List[i]
attrMap.Map[attr.Name] = int32(j)
attrMap.Map[attrMap.List[j].Name] = int32(i)
attrMap.List[i] = attrMap.List[j]
attrMap.List[j] = attr
}
// --------------------------------------------- THE THIRD PASS ------------------------------------ //
// The third pass transforms the recursive tree structure into a 2-dimensional sheet,
// which can be further processed into protoconf.
//
// genSheet generates a `book.Sheet` which can be furtherly processed by `protogen`.
func genSheet(xmlSheet *tableaupb.XMLSheet) (sheet *book.Sheet, err error) {
sheetName := xmlSheet.Meta.Name
header := options.NewDefault().Proto.Input.Header
metaSheet := xlsxgen.NewMetaSheet(sheetName, header, false)
// generate sheet header rows
if err := genHeaderRows(xmlSheet.Meta, metaSheet, ""); err != nil {
return nil, errors.Wrapf(err, "failed to genHeaderRows for sheet: %s", sheetName)
}
// fill sheet data rows
if err := fillDataRows(xmlSheet.Data, metaSheet, "", int(metaSheet.Datarow)-1); err != nil {
return nil, errors.Wrapf(err, "failed to fillDataRows for sheet: %s", sheetName)
}
// unpack rows from the MetaSheet struct
var rows [][]string
for i := 0; i < len(metaSheet.Rows); i++ {
var row []string
for _, cell := range metaSheet.Rows[i].Cells {
row = append(row, cell.Data)
}
rows = append(rows, row)
}
// insert sheets into map for importer
sheet = book.NewSheet(sheetName, rows)
return sheet, nil
}
// genHeaderRows recursively read meta info from `node` and generates the header rows of `metaSheet`, which is a 2-dimensional IR.
func genHeaderRows(node *tableaupb.XMLNode, metaSheet *xlsxgen.MetaSheet, prefix string) error {
curPrefix := newPrefix(prefix, node.Name, metaSheet.Worksheet)
for _, attr := range node.AttrMap.List {
metaSheet.SetColType(curPrefix+attr.Name, attr.Value)
}
for _, child := range node.ChildList {
if err := genHeaderRows(child, metaSheet, curPrefix); err != nil {
return errors.Wrapf(err, "failed to genHeaderRows for %s@%s", child.Name, curPrefix)
}
}
return nil
}
// fillDataRows recursively read data from `node` and fill them to the data rows of `metaSheet`, which is a 2-dimensional IR.
func fillDataRows(node *tableaupb.XMLNode, metaSheet *xlsxgen.MetaSheet, prefix string, cursor int) error {
curPrefix := newPrefix(prefix, node.Name, metaSheet.Worksheet)
// clear to the bottom, since `metaSheet.NewRow()` will copy all data of all columns to create a new row
if len(node.ChildList) == 0 {
for tmpCusor := cursor; tmpCusor < len(metaSheet.Rows); tmpCusor++ {
metaSheet.ForEachCol(tmpCusor, func(name string, cell *xlsxgen.Cell) error {
if strings.HasPrefix(name, curPrefix) {
cell.Data = ""
}
return nil
})
}
}
for _, attr := range node.AttrMap.List {
colName := curPrefix + attr.Name
// fill values to the bottom when backtrace to top line
for tmpCusor := cursor; tmpCusor < len(metaSheet.Rows); tmpCusor++ {
metaSheet.Cell(tmpCusor, len(metaSheet.Rows[metaSheet.Namerow-1].Cells), colName).Data = attr.Value
}
}
// iterate over child nodes
nodeMap := make(map[string]int)
for _, child := range node.ChildList {
tagName := child.Name
if count, existed := nodeMap[tagName]; existed {
// duplicate means a list, should expand vertically
row := metaSheet.NewRow()
if err := fillDataRows(child, metaSheet, curPrefix, row.Index); err != nil {
return errors.Wrapf(err, "fillDataRows %dth node %s@%s failed", count+1, tagName, curPrefix)
}
nodeMap[tagName]++
} else {
if err := fillDataRows(child, metaSheet, curPrefix, cursor); err != nil {
return errors.Wrapf(err, "fillDataRows 1st node %s@%s failed", tagName, curPrefix)
}
nodeMap[tagName] = 1
}
}
return nil
}
func newPrefix(prefix, curNode, sheetName string) string {
// sheet name should not occur in the prefix
if curNode != sheetName {
return prefix + curNode
} else {
return prefix
}
}