simplify board paths

This commit is contained in:
Alexander Wang 2023-06-21 14:26:13 -07:00
parent 68bc0f8caa
commit 69d8d74364
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
2 changed files with 74 additions and 86 deletions

View file

@ -470,7 +470,7 @@ layers: {
}
`,
key: `b`,
boardPath: []string{"root", "layers", "x"},
boardPath: []string{"x"},
expKey: `b`,
exp: `a
@ -495,7 +495,7 @@ layers: {
}
`,
key: `a -> b`,
boardPath: []string{"root", "layers", "x"},
boardPath: []string{"x"},
expKey: `(a -> b)[0]`,
exp: `a
@ -520,7 +520,7 @@ layers: {
}
`,
key: `a -> b`,
boardPath: []string{"root", "layers", "x"},
boardPath: []string{"x"},
expKey: `(a -> b)[1]`,
exp: `a -> b
@ -546,7 +546,7 @@ scenarios: {
}
`,
key: `c`,
boardPath: []string{"root", "scenarios", "x"},
boardPath: []string{"x"},
expKey: `c`,
exp: `a
@ -573,7 +573,7 @@ scenarios: {
}
`,
key: `a -> b`,
boardPath: []string{"root", "scenarios", "x"},
boardPath: []string{"x"},
expKey: `(a -> b)[0]`,
exp: `a
@ -599,7 +599,7 @@ scenarios: {
}
`,
key: `a -> b`,
boardPath: []string{"root", "scenarios", "x"},
boardPath: []string{"x"},
expKey: `(a -> b)[1]`,
exp: `a -> b
@ -625,7 +625,7 @@ steps: {
}
`,
key: `c`,
boardPath: []string{"root", "steps", "x"},
boardPath: []string{"x"},
expKey: `c`,
exp: `a
@ -652,7 +652,7 @@ steps: {
}
`,
key: `d -> b`,
boardPath: []string{"root", "steps", "x"},
boardPath: []string{"x"},
expKey: `(d -> b)[0]`,
exp: `a
@ -679,7 +679,7 @@ steps: {
}
`,
key: `d`,
boardPath: []string{"root", "steps", "x"},
boardPath: []string{"x"},
expKey: `d 2`,
exp: `a
@ -1721,7 +1721,7 @@ layers: {
`,
key: `a.style.opacity`,
value: go2.Pointer(`0.2`),
boardPath: []string{"root", "layers", "x"},
boardPath: []string{"x"},
exp: `a
@ -1745,7 +1745,7 @@ layers: {
`,
key: `a.style.opacity`,
value: go2.Pointer(`0.2`),
boardPath: []string{"root", "layers", "x"},
boardPath: []string{"x"},
exp: `a
@ -1770,7 +1770,7 @@ scenarios: {
`,
key: `a.style.opacity`,
value: go2.Pointer(`0.2`),
boardPath: []string{"root", "scenarios", "x"},
boardPath: []string{"x"},
exp: `a: outer
@ -1798,7 +1798,7 @@ scenarios: {
`,
key: `a.b.style.opacity`,
value: go2.Pointer(`0.2`),
boardPath: []string{"root", "scenarios", "x"},
boardPath: []string{"x"},
exp: `a: {
b: outer
@ -1826,7 +1826,7 @@ scenarios: {
`,
key: `a.style.opacity`,
value: go2.Pointer(`0.2`),
boardPath: []string{"root", "scenarios", "x"},
boardPath: []string{"x"},
exp: `a
@ -1855,7 +1855,7 @@ scenarios: {
`,
key: `a`,
value: go2.Pointer(`b`),
boardPath: []string{"root", "scenarios", "x"},
boardPath: []string{"x"},
exp: `a: {
style.opacity: 0.2
@ -1885,7 +1885,7 @@ scenarios: {
`,
key: `a`,
value: go2.Pointer(`b`),
boardPath: []string{"root", "scenarios", "x"},
boardPath: []string{"x"},
exp: `a: {
style.opacity: 0.2
@ -4880,7 +4880,7 @@ layers: {
`,
key: `c`,
newKey: `b.c`,
boardPath: []string{"root", "layers", "x"},
boardPath: []string{"x"},
exp: `a
@ -4907,7 +4907,7 @@ scenarios: {
`,
key: `a`,
newKey: `b.a`,
boardPath: []string{"root", "scenarios", "x"},
boardPath: []string{"x"},
expErr: `failed to move: "a" to "b.a": operation would modify AST outside of given scope`,
},
@ -6612,7 +6612,7 @@ layers: {
}
`,
key: `c`,
boardPath: []string{"root", "layers", "x"},
boardPath: []string{"x"},
exp: `a
@ -6636,7 +6636,7 @@ scenarios: {
}
`,
key: `c`,
boardPath: []string{"root", "scenarios", "x"},
boardPath: []string{"x"},
exp: `a
@ -6660,7 +6660,7 @@ scenarios: {
}
`,
key: `a`,
boardPath: []string{"root", "scenarios", "x"},
boardPath: []string{"x"},
expErr: `failed to delete "a": operation would modify AST outside of given scope`,
},

View file

@ -13,41 +13,21 @@ func GetBoardGraph(g *d2graph.Graph, boardPath []string) *d2graph.Graph {
if len(boardPath) == 0 {
return g
}
switch boardPath[0] {
case "root":
if g.Parent == nil {
return GetBoardGraph(g, boardPath[1:])
}
return nil
case "layers":
if len(boardPath) < 2 {
return nil
}
for i, b := range g.Layers {
if b.Name == boardPath[1] {
return GetBoardGraph(g.Layers[i], boardPath[2:])
}
}
case "scenarios":
if len(boardPath) < 2 {
return nil
}
for i, b := range g.Scenarios {
if b.Name == boardPath[1] {
return GetBoardGraph(g.Scenarios[i], boardPath[2:])
}
}
case "steps":
if len(boardPath) < 2 {
return nil
}
for i, b := range g.Steps {
if b.Name == boardPath[1] {
return GetBoardGraph(g.Steps[i], boardPath[2:])
}
for i, b := range g.Layers {
if b.Name == boardPath[0] {
return GetBoardGraph(g.Layers[i], boardPath[1:])
}
}
for i, b := range g.Scenarios {
if b.Name == boardPath[0] {
return GetBoardGraph(g.Scenarios[i], boardPath[1:])
}
}
for i, b := range g.Steps {
if b.Name == boardPath[0] {
return GetBoardGraph(g.Steps[i], boardPath[1:])
}
}
return nil
}
@ -55,44 +35,52 @@ func ReplaceBoardNode(ast, ast2 *d2ast.Map, boardPath []string) bool {
if len(boardPath) == 0 {
return false
}
switch boardPath[0] {
case "root":
return ReplaceBoardNode(ast, ast2, boardPath[1:])
case "layers":
if len(boardPath) < 2 {
return false
}
for _, n := range ast.Nodes {
if n.MapKey != nil && n.MapKey.Key != nil && n.MapKey.Key.Path[0].Unbox().ScalarString() == "layers" {
return ReplaceBoardNode(n.MapKey.Value.Map, ast2, boardPath[1:])
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
}
}
case "scenarios":
if len(boardPath) < 2 {
return false
}
for _, n := range ast.Nodes {
if n.MapKey != nil && n.MapKey.Key != nil && n.MapKey.Key.Path[0].Unbox().ScalarString() == "scenarios" {
return ReplaceBoardNode(n.MapKey.Value.Map, ast2, boardPath[1:])
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
}
}
case "steps":
if len(boardPath) < 2 {
return false
}
for _, n := range ast.Nodes {
if n.MapKey != nil && n.MapKey.Key != nil && n.MapKey.Key.Path[0].Unbox().ScalarString() == "steps" {
return ReplaceBoardNode(n.MapKey.Value.Map, ast2, boardPath[1:])
}
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
}
}
default:
for _, n := range ast.Nodes {
if n.MapKey != nil && n.MapKey.Key != nil && n.MapKey.Key.Path[0].Unbox().ScalarString() == boardPath[0] {
if len(boardPath) == 1 {
n.MapKey.Value.Map.Nodes = ast2.Nodes
return true
}
return ReplaceBoardNode(n.MapKey.Value.Map, ast2, boardPath[1:])
}
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
}
}
}