Skip to content

Commit

Permalink
d2oralce handle deleting imported
Browse files Browse the repository at this point in the history
  • Loading branch information
alixander committed Feb 3, 2024
1 parent 1e5d8fa commit b5bfb09
Show file tree
Hide file tree
Showing 4 changed files with 588 additions and 17 deletions.
45 changes: 28 additions & 17 deletions d2oracle/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -920,34 +920,45 @@ func Delete(g *d2graph.Graph, boardPath []string, key string) (_ *d2graph.Graph,

prevG, _ := recompile(boardG)

boardG, err = renameConflictsToParent(boardG, mk.Key)
if err != nil {
return nil, err
}

obj, ok := boardG.Root.HasChild(d2graph.Key(mk.Key))
if !ok {
return g, nil
}

if len(boardPath) > 0 {
writeableRefs := getWriteableRefs(obj, baseAST)
if len(writeableRefs) != len(obj.References) {
mk.Value = d2ast.MakeValueBox(&d2ast.Null{})
}
}
imported := IsImported(baseAST, obj)

if _, ok := mk.Value.Unbox().(*d2ast.Null); !ok {
boardG, err = deleteObject(boardG, baseAST, mk.Key, obj)
if imported {
println(d2format.Format(boardG.AST))
mk.Value = d2ast.MakeValueBox(&d2ast.Null{})
appendMapKey(baseAST, mk)
} else {
boardG, err = renameConflictsToParent(boardG, mk.Key)
if err != nil {
return nil, err
}
obj, ok = boardG.Root.HasChild(d2graph.Key(mk.Key))
if !ok {
return g, nil
}
if len(boardPath) > 0 {
writeableRefs := getWriteableRefs(obj, baseAST)
if len(writeableRefs) != len(obj.References) {
mk.Value = d2ast.MakeValueBox(&d2ast.Null{})
}
}

if err := updateNear(prevG, boardG, &key, nil, false); err != nil {
return nil, err
if _, ok := mk.Value.Unbox().(*d2ast.Null); !ok {
boardG, err = deleteObject(boardG, baseAST, mk.Key, obj)
if err != nil {
return nil, err
}

if err := updateNear(prevG, boardG, &key, nil, false); err != nil {
return nil, err
}
} else {
appendMapKey(baseAST, mk)
}
} else {
appendMapKey(baseAST, mk)
}

if len(boardPath) > 0 {
Expand Down
47 changes: 47 additions & 0 deletions d2oracle/edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7158,6 +7158,53 @@ scenarios: {
(a -> b)[0]: null
}
}
`,
},
{
name: "import/1",

text: `...@meow
y
`,
fsTexts: map[string]string{
"meow.d2": `x: {
a
}
`,
},
key: `x`,
exp: `...@meow
y
x: null
`,
},
{
name: "import/2",

text: `...@meow
scenarios: {
y: {
c
}
}
`,
fsTexts: map[string]string{
"meow.d2": `x: {
a
}
`,
},
boardPath: []string{"y"},
key: `x`,
exp: `...@meow
scenarios: {
y: {
c
x: null
}
}
`,
},
}
Expand Down
153 changes: 153 additions & 0 deletions testdata/d2oracle/TestDelete/import/1.exp.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b5bfb09

Please sign in to comment.