Merge pull request #1432 from alixander/simplify-path
d2oracle: simplify board paths
This commit is contained in:
commit
25b68c6140
2 changed files with 74 additions and 86 deletions
|
|
@ -470,7 +470,7 @@ layers: {
|
||||||
}
|
}
|
||||||
`,
|
`,
|
||||||
key: `b`,
|
key: `b`,
|
||||||
boardPath: []string{"root", "layers", "x"},
|
boardPath: []string{"x"},
|
||||||
|
|
||||||
expKey: `b`,
|
expKey: `b`,
|
||||||
exp: `a
|
exp: `a
|
||||||
|
|
@ -495,7 +495,7 @@ layers: {
|
||||||
}
|
}
|
||||||
`,
|
`,
|
||||||
key: `a -> b`,
|
key: `a -> b`,
|
||||||
boardPath: []string{"root", "layers", "x"},
|
boardPath: []string{"x"},
|
||||||
|
|
||||||
expKey: `(a -> b)[0]`,
|
expKey: `(a -> b)[0]`,
|
||||||
exp: `a
|
exp: `a
|
||||||
|
|
@ -520,7 +520,7 @@ layers: {
|
||||||
}
|
}
|
||||||
`,
|
`,
|
||||||
key: `a -> b`,
|
key: `a -> b`,
|
||||||
boardPath: []string{"root", "layers", "x"},
|
boardPath: []string{"x"},
|
||||||
|
|
||||||
expKey: `(a -> b)[1]`,
|
expKey: `(a -> b)[1]`,
|
||||||
exp: `a -> b
|
exp: `a -> b
|
||||||
|
|
@ -546,7 +546,7 @@ scenarios: {
|
||||||
}
|
}
|
||||||
`,
|
`,
|
||||||
key: `c`,
|
key: `c`,
|
||||||
boardPath: []string{"root", "scenarios", "x"},
|
boardPath: []string{"x"},
|
||||||
|
|
||||||
expKey: `c`,
|
expKey: `c`,
|
||||||
exp: `a
|
exp: `a
|
||||||
|
|
@ -573,7 +573,7 @@ scenarios: {
|
||||||
}
|
}
|
||||||
`,
|
`,
|
||||||
key: `a -> b`,
|
key: `a -> b`,
|
||||||
boardPath: []string{"root", "scenarios", "x"},
|
boardPath: []string{"x"},
|
||||||
|
|
||||||
expKey: `(a -> b)[0]`,
|
expKey: `(a -> b)[0]`,
|
||||||
exp: `a
|
exp: `a
|
||||||
|
|
@ -599,7 +599,7 @@ scenarios: {
|
||||||
}
|
}
|
||||||
`,
|
`,
|
||||||
key: `a -> b`,
|
key: `a -> b`,
|
||||||
boardPath: []string{"root", "scenarios", "x"},
|
boardPath: []string{"x"},
|
||||||
|
|
||||||
expKey: `(a -> b)[1]`,
|
expKey: `(a -> b)[1]`,
|
||||||
exp: `a -> b
|
exp: `a -> b
|
||||||
|
|
@ -625,7 +625,7 @@ steps: {
|
||||||
}
|
}
|
||||||
`,
|
`,
|
||||||
key: `c`,
|
key: `c`,
|
||||||
boardPath: []string{"root", "steps", "x"},
|
boardPath: []string{"x"},
|
||||||
|
|
||||||
expKey: `c`,
|
expKey: `c`,
|
||||||
exp: `a
|
exp: `a
|
||||||
|
|
@ -652,7 +652,7 @@ steps: {
|
||||||
}
|
}
|
||||||
`,
|
`,
|
||||||
key: `d -> b`,
|
key: `d -> b`,
|
||||||
boardPath: []string{"root", "steps", "x"},
|
boardPath: []string{"x"},
|
||||||
|
|
||||||
expKey: `(d -> b)[0]`,
|
expKey: `(d -> b)[0]`,
|
||||||
exp: `a
|
exp: `a
|
||||||
|
|
@ -679,7 +679,7 @@ steps: {
|
||||||
}
|
}
|
||||||
`,
|
`,
|
||||||
key: `d`,
|
key: `d`,
|
||||||
boardPath: []string{"root", "steps", "x"},
|
boardPath: []string{"x"},
|
||||||
|
|
||||||
expKey: `d 2`,
|
expKey: `d 2`,
|
||||||
exp: `a
|
exp: `a
|
||||||
|
|
@ -1721,7 +1721,7 @@ layers: {
|
||||||
`,
|
`,
|
||||||
key: `a.style.opacity`,
|
key: `a.style.opacity`,
|
||||||
value: go2.Pointer(`0.2`),
|
value: go2.Pointer(`0.2`),
|
||||||
boardPath: []string{"root", "layers", "x"},
|
boardPath: []string{"x"},
|
||||||
|
|
||||||
exp: `a
|
exp: `a
|
||||||
|
|
||||||
|
|
@ -1745,7 +1745,7 @@ layers: {
|
||||||
`,
|
`,
|
||||||
key: `a.style.opacity`,
|
key: `a.style.opacity`,
|
||||||
value: go2.Pointer(`0.2`),
|
value: go2.Pointer(`0.2`),
|
||||||
boardPath: []string{"root", "layers", "x"},
|
boardPath: []string{"x"},
|
||||||
|
|
||||||
exp: `a
|
exp: `a
|
||||||
|
|
||||||
|
|
@ -1770,7 +1770,7 @@ scenarios: {
|
||||||
`,
|
`,
|
||||||
key: `a.style.opacity`,
|
key: `a.style.opacity`,
|
||||||
value: go2.Pointer(`0.2`),
|
value: go2.Pointer(`0.2`),
|
||||||
boardPath: []string{"root", "scenarios", "x"},
|
boardPath: []string{"x"},
|
||||||
|
|
||||||
exp: `a: outer
|
exp: `a: outer
|
||||||
|
|
||||||
|
|
@ -1798,7 +1798,7 @@ scenarios: {
|
||||||
`,
|
`,
|
||||||
key: `a.b.style.opacity`,
|
key: `a.b.style.opacity`,
|
||||||
value: go2.Pointer(`0.2`),
|
value: go2.Pointer(`0.2`),
|
||||||
boardPath: []string{"root", "scenarios", "x"},
|
boardPath: []string{"x"},
|
||||||
|
|
||||||
exp: `a: {
|
exp: `a: {
|
||||||
b: outer
|
b: outer
|
||||||
|
|
@ -1826,7 +1826,7 @@ scenarios: {
|
||||||
`,
|
`,
|
||||||
key: `a.style.opacity`,
|
key: `a.style.opacity`,
|
||||||
value: go2.Pointer(`0.2`),
|
value: go2.Pointer(`0.2`),
|
||||||
boardPath: []string{"root", "scenarios", "x"},
|
boardPath: []string{"x"},
|
||||||
|
|
||||||
exp: `a
|
exp: `a
|
||||||
|
|
||||||
|
|
@ -1855,7 +1855,7 @@ scenarios: {
|
||||||
`,
|
`,
|
||||||
key: `a`,
|
key: `a`,
|
||||||
value: go2.Pointer(`b`),
|
value: go2.Pointer(`b`),
|
||||||
boardPath: []string{"root", "scenarios", "x"},
|
boardPath: []string{"x"},
|
||||||
|
|
||||||
exp: `a: {
|
exp: `a: {
|
||||||
style.opacity: 0.2
|
style.opacity: 0.2
|
||||||
|
|
@ -1885,7 +1885,7 @@ scenarios: {
|
||||||
`,
|
`,
|
||||||
key: `a`,
|
key: `a`,
|
||||||
value: go2.Pointer(`b`),
|
value: go2.Pointer(`b`),
|
||||||
boardPath: []string{"root", "scenarios", "x"},
|
boardPath: []string{"x"},
|
||||||
|
|
||||||
exp: `a: {
|
exp: `a: {
|
||||||
style.opacity: 0.2
|
style.opacity: 0.2
|
||||||
|
|
@ -4880,7 +4880,7 @@ layers: {
|
||||||
`,
|
`,
|
||||||
key: `c`,
|
key: `c`,
|
||||||
newKey: `b.c`,
|
newKey: `b.c`,
|
||||||
boardPath: []string{"root", "layers", "x"},
|
boardPath: []string{"x"},
|
||||||
|
|
||||||
exp: `a
|
exp: `a
|
||||||
|
|
||||||
|
|
@ -4907,7 +4907,7 @@ scenarios: {
|
||||||
`,
|
`,
|
||||||
key: `a`,
|
key: `a`,
|
||||||
newKey: `b.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`,
|
expErr: `failed to move: "a" to "b.a": operation would modify AST outside of given scope`,
|
||||||
},
|
},
|
||||||
|
|
@ -6612,7 +6612,7 @@ layers: {
|
||||||
}
|
}
|
||||||
`,
|
`,
|
||||||
key: `c`,
|
key: `c`,
|
||||||
boardPath: []string{"root", "layers", "x"},
|
boardPath: []string{"x"},
|
||||||
|
|
||||||
exp: `a
|
exp: `a
|
||||||
|
|
||||||
|
|
@ -6636,7 +6636,7 @@ scenarios: {
|
||||||
}
|
}
|
||||||
`,
|
`,
|
||||||
key: `c`,
|
key: `c`,
|
||||||
boardPath: []string{"root", "scenarios", "x"},
|
boardPath: []string{"x"},
|
||||||
|
|
||||||
exp: `a
|
exp: `a
|
||||||
|
|
||||||
|
|
@ -6660,7 +6660,7 @@ scenarios: {
|
||||||
}
|
}
|
||||||
`,
|
`,
|
||||||
key: `a`,
|
key: `a`,
|
||||||
boardPath: []string{"root", "scenarios", "x"},
|
boardPath: []string{"x"},
|
||||||
|
|
||||||
expErr: `failed to delete "a": operation would modify AST outside of given scope`,
|
expErr: `failed to delete "a": operation would modify AST outside of given scope`,
|
||||||
},
|
},
|
||||||
|
|
|
||||||
118
d2oracle/get.go
118
d2oracle/get.go
|
|
@ -13,41 +13,21 @@ func GetBoardGraph(g *d2graph.Graph, boardPath []string) *d2graph.Graph {
|
||||||
if len(boardPath) == 0 {
|
if len(boardPath) == 0 {
|
||||||
return g
|
return g
|
||||||
}
|
}
|
||||||
switch boardPath[0] {
|
for i, b := range g.Layers {
|
||||||
case "root":
|
if b.Name == boardPath[0] {
|
||||||
if g.Parent == nil {
|
return GetBoardGraph(g.Layers[i], boardPath[1:])
|
||||||
return GetBoardGraph(g, boardPath[1:])
|
}
|
||||||
}
|
}
|
||||||
return nil
|
for i, b := range g.Scenarios {
|
||||||
case "layers":
|
if b.Name == boardPath[0] {
|
||||||
if len(boardPath) < 2 {
|
return GetBoardGraph(g.Scenarios[i], boardPath[1:])
|
||||||
return nil
|
}
|
||||||
}
|
}
|
||||||
for i, b := range g.Layers {
|
for i, b := range g.Steps {
|
||||||
if b.Name == boardPath[1] {
|
if b.Name == boardPath[0] {
|
||||||
return GetBoardGraph(g.Layers[i], boardPath[2:])
|
return GetBoardGraph(g.Steps[i], boardPath[1:])
|
||||||
}
|
|
||||||
}
|
|
||||||
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:])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -55,44 +35,52 @@ func ReplaceBoardNode(ast, ast2 *d2ast.Map, boardPath []string) bool {
|
||||||
if len(boardPath) == 0 {
|
if len(boardPath) == 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
switch boardPath[0] {
|
|
||||||
case "root":
|
findMap := func(root *d2ast.Map, name string) *d2ast.Map {
|
||||||
return ReplaceBoardNode(ast, ast2, boardPath[1:])
|
for _, n := range root.Nodes {
|
||||||
case "layers":
|
if n.MapKey != nil && n.MapKey.Key != nil && n.MapKey.Key.Path[0].Unbox().ScalarString() == name {
|
||||||
if len(boardPath) < 2 {
|
return n.MapKey.Value.Map
|
||||||
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:])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case "scenarios":
|
return nil
|
||||||
if len(boardPath) < 2 {
|
}
|
||||||
return false
|
|
||||||
}
|
layersMap := findMap(ast, "layers")
|
||||||
for _, n := range ast.Nodes {
|
scenariosMap := findMap(ast, "scenarios")
|
||||||
if n.MapKey != nil && n.MapKey.Key != nil && n.MapKey.Key.Path[0].Unbox().ScalarString() == "scenarios" {
|
stepsMap := findMap(ast, "steps")
|
||||||
return ReplaceBoardNode(n.MapKey.Value.Map, ast2, boardPath[1:])
|
|
||||||
|
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
|
if scenariosMap != nil {
|
||||||
}
|
m := findMap(scenariosMap, boardPath[0])
|
||||||
for _, n := range ast.Nodes {
|
if m != nil {
|
||||||
if n.MapKey != nil && n.MapKey.Key != nil && n.MapKey.Key.Path[0].Unbox().ScalarString() == "steps" {
|
if len(boardPath) > 1 {
|
||||||
return ReplaceBoardNode(n.MapKey.Value.Map, ast2, 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 stepsMap != nil {
|
||||||
if len(boardPath) == 1 {
|
m := findMap(stepsMap, boardPath[0])
|
||||||
n.MapKey.Value.Map.Nodes = ast2.Nodes
|
if m != nil {
|
||||||
return true
|
if len(boardPath) > 1 {
|
||||||
}
|
return ReplaceBoardNode(m, ast2, boardPath[1:])
|
||||||
return ReplaceBoardNode(n.MapKey.Value.Map, ast2, boardPath[1:])
|
} else {
|
||||||
|
m.Nodes = ast2.Nodes
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue