forked from sleepinggenius2/gosmi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
143 lines (122 loc) · 2.49 KB
/
table.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
package gosmi
import (
"github.com/min-oc/gosmi/smi"
"github.com/min-oc/gosmi/types"
)
type Table struct {
SmiNode
Columns map[string]SmiNode
ColumnOrder []string
Implied bool
Index []SmiNode
}
func (t SmiNode) AsTable() Table {
columns, columnOrder := t.GetColumns()
return Table{
SmiNode: t,
Columns: columns,
ColumnOrder: columnOrder,
Implied: t.GetImplied(),
Index: t.GetIndex(),
}
}
func (t SmiNode) getRow() (row *types.SmiNode) {
switch t.Kind {
case types.NodeRow:
row = t.GetRaw()
case types.NodeTable:
row = smi.GetFirstChildNode(t.smiNode)
if row == nil {
return
}
default:
return
}
if row.NodeKind != types.NodeRow {
// TODO: error
return nil
}
return
}
func (t SmiNode) GetRow() (row SmiNode) {
smiRow := t.getRow()
if smiRow == nil {
return
}
return CreateNode(smiRow)
}
func (t SmiNode) GetColumns() (columns map[string]SmiNode, columnOrder []string) {
row := t.getRow()
if row == nil {
return
}
columns = make(map[string]SmiNode)
columnOrder = make([]string, 0, 2)
for smiColumn := smi.GetFirstChildNode(row); smiColumn != nil; smiColumn = smi.GetNextChildNode(smiColumn) {
if smiColumn.NodeKind != types.NodeColumn {
// TODO: error
return
}
column := CreateNode(smiColumn)
columns[column.Name] = column
columnOrder = append(columnOrder, column.Name)
}
return
}
func (t SmiNode) GetImplied() bool {
row := t.getRow()
if row == nil {
return false
}
return row.Implied
}
func (t SmiNode) GetAugment() (row SmiNode) {
smiRow := t.getRow()
if smiRow == nil {
return
}
if smiRow.IndexKind != types.IndexAugment {
return
}
smiRow = smi.GetRelatedNode(smiRow)
if smiRow == nil {
return
}
if smiRow.NodeKind != types.NodeRow {
// TODO: error
return
}
return CreateNode(smiRow)
}
func (t SmiNode) GetIndex() (index []SmiNode) {
row := t.getRow()
if row == nil {
return
}
if row.IndexKind == types.IndexAugment {
row = smi.GetRelatedNode(row)
if row == nil {
return
}
if row.NodeKind != types.NodeRow {
// TODO: error
return
}
} else if row.IndexKind != types.IndexIndex {
// TODO: unsupported
return
}
for smiElement := smi.GetFirstElement(row); smiElement != nil; smiElement = smi.GetNextElement(smiElement) {
smiColumn := smi.GetElementNode(smiElement)
if smiColumn == nil {
// TODO: error
return
}
if smiColumn.NodeKind != types.NodeColumn {
// TODO: error
return
}
index = append(index, CreateNode(smiColumn))
}
return
}