forked from terrastruct/d2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget.go
247 lines (214 loc) · 4.88 KB
/
get.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
package d2oracle
import (
"fmt"
"oss.terrastruct.com/d2/d2ast"
"oss.terrastruct.com/d2/d2format"
"oss.terrastruct.com/d2/d2graph"
"oss.terrastruct.com/d2/d2parser"
)
func GetBoardGraph(g *d2graph.Graph, boardPath []string) *d2graph.Graph {
if len(boardPath) == 0 {
return g
}
for i, b := range g.Layers {
if b.Name == boardPath[0] {
g2 := GetBoardGraph(g.Layers[i], boardPath[1:])
if g2 != nil {
g2.FS = g.FS
}
return g2
}
}
for i, b := range g.Scenarios {
if b.Name == boardPath[0] {
g2 := GetBoardGraph(g.Scenarios[i], boardPath[1:])
if g2 != nil {
g2.FS = g.FS
}
return g2
}
}
for i, b := range g.Steps {
if b.Name == boardPath[0] {
g2 := GetBoardGraph(g.Steps[i], boardPath[1:])
if g2 != nil {
g2.FS = g.FS
}
return g2
}
}
return nil
}
func ReplaceBoardNode(ast, ast2 *d2ast.Map, boardPath []string) bool {
if len(boardPath) == 0 {
return false
}
findMap := func(root *d2ast.Map, name string) *d2ast.Map {
for _, n := range root.Nodes {
if n.MapKey != nil && n.MapKey.Key != nil && n.MapKey.Key.Path[0].Unbox().ScalarString() == name {
return n.MapKey.Value.Map
}
}
return nil
}
layersMap := findMap(ast, "layers")
scenariosMap := findMap(ast, "scenarios")
stepsMap := findMap(ast, "steps")
if layersMap != nil {
m := findMap(layersMap, boardPath[0])
if m != nil {
if len(boardPath) > 1 {
return ReplaceBoardNode(m, ast2, boardPath[1:])
} else {
m.Nodes = ast2.Nodes
return true
}
}
}
if scenariosMap != nil {
m := findMap(scenariosMap, boardPath[0])
if m != nil {
if len(boardPath) > 1 {
return ReplaceBoardNode(m, ast2, boardPath[1:])
} else {
m.Nodes = ast2.Nodes
return true
}
}
}
if stepsMap != nil {
m := findMap(stepsMap, boardPath[0])
if m != nil {
if len(boardPath) > 1 {
return ReplaceBoardNode(m, ast2, boardPath[1:])
} else {
m.Nodes = ast2.Nodes
return true
}
}
}
return false
}
func GetChildrenIDs(g *d2graph.Graph, boardPath []string, absID string) (ids []string, _ error) {
g = GetBoardGraph(g, boardPath)
if g == nil {
return nil, fmt.Errorf("board at path %v not found", boardPath)
}
mk, err := d2parser.ParseMapKey(absID)
if err != nil {
return nil, err
}
obj, ok := g.Root.HasChild(d2graph.Key(mk.Key))
if !ok {
return nil, fmt.Errorf("%v not found", absID)
}
for _, ch := range obj.ChildrenArray {
ids = append(ids, ch.AbsID())
}
return ids, nil
}
func GetParentID(g *d2graph.Graph, boardPath []string, absID string) (string, error) {
g = GetBoardGraph(g, boardPath)
if g == nil {
return "", fmt.Errorf("board at path %v not found", boardPath)
}
mk, err := d2parser.ParseMapKey(absID)
if err != nil {
return "", err
}
obj, ok := g.Root.HasChild(d2graph.Key(mk.Key))
if !ok {
return "", fmt.Errorf("%v not found", absID)
}
return obj.Parent.AbsID(), nil
}
func IsImportedObj(ast *d2ast.Map, obj *d2graph.Object) bool {
for _, ref := range obj.References {
if ref.Key.HasGlob() {
return true
}
if ref.Key.Range.Path != ast.Range.Path {
return true
}
}
return false
}
// Globs count as imported for now
// TODO Probably rename later
func IsImportedEdge(ast *d2ast.Map, edge *d2graph.Edge) bool {
for _, ref := range edge.References {
if ref.Edge.Src.HasGlob() || ref.Edge.Dst.HasGlob() {
return true
}
if ref.Edge.Range.Path != ast.Range.Path {
return true
}
}
return false
}
func GetObj(g *d2graph.Graph, boardPath []string, absID string) *d2graph.Object {
g = GetBoardGraph(g, boardPath)
if g == nil {
return nil
}
mk, _ := d2parser.ParseMapKey(absID)
obj, _ := g.Root.HasChild(d2graph.Key(mk.Key))
return obj
}
func GetEdge(g *d2graph.Graph, boardPath []string, absID string) *d2graph.Edge {
g = GetBoardGraph(g, boardPath)
if g == nil {
return nil
}
for _, e := range g.Edges {
if e.AbsID() == absID {
return e
}
}
return nil
}
func GetObjOrder(g *d2graph.Graph, boardPath []string) ([]string, error) {
g = GetBoardGraph(g, boardPath)
if g == nil {
return nil, fmt.Errorf("board at path %v not found", boardPath)
}
var order []string
queue := []*d2graph.Object{g.Root}
for len(queue) > 0 {
curr := queue[0]
queue = queue[1:]
if curr != g.Root {
order = append(order, curr.AbsID())
}
for _, ch := range curr.ChildrenArray {
queue = append(queue, ch)
}
}
return order, nil
}
func IsLabelKeyID(key, label string) bool {
mk, err := d2parser.ParseMapKey(key)
if err != nil {
return false
}
if len(mk.Edges) > 0 {
return false
}
if mk.Key == nil {
return false
}
return mk.Key.Path[len(mk.Key.Path)-1].Unbox().ScalarString() == label
}
func GetID(key string) string {
mk, err := d2parser.ParseMapKey(key)
if err != nil {
return ""
}
if len(mk.Edges) > 0 {
return ""
}
if mk.Key == nil {
return ""
}
return d2format.Format(d2ast.RawString(mk.Key.Path[len(mk.Key.Path)-1].Unbox().ScalarString(), true))
}