Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

d2oracle: set null #1877

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 37 additions & 11 deletions d2oracle/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,11 @@ func _set(g *d2graph.Graph, baseAST *d2ast.Map, key string, tag, value *string)
}

if value != nil {
mk.Value = d2ast.MakeValueBox(d2ast.RawString(*value, false))
if *value == "null" {
mk.Value = d2ast.MakeValueBox(&d2ast.Null{})
} else {
mk.Value = d2ast.MakeValueBox(d2ast.RawString(*value, false))
}
} else {
mk.Value = d2ast.ValueBox{}
}
Expand Down Expand Up @@ -890,11 +894,11 @@ func Delete(g *d2graph.Graph, boardPath []string, key string) (_ *d2graph.Graph,
baseAST = boardG.BaseAST
}

g2, err := deleteReserved(g, baseAST, mk)
g2, err := deleteReserved(boardG, boardPath, g.AST, baseAST, mk)
if err != nil {
return nil, err
}
if g != g2 {
if boardG != g2 {
return g2, nil
}

Expand Down Expand Up @@ -1228,7 +1232,7 @@ func renameConflictsToParent(g *d2graph.Graph, key *d2ast.KeyPath) (*d2graph.Gra
return g, nil
}

func deleteReserved(g *d2graph.Graph, baseAST *d2ast.Map, mk *d2ast.Key) (*d2graph.Graph, error) {
func deleteReserved(g *d2graph.Graph, boardPath []string, originalAST, baseAST *d2ast.Map, mk *d2ast.Key) (*d2graph.Graph, error) {
targetKey := mk.Key
if len(mk.Edges) == 1 {
if mk.EdgeKey == nil {
Expand Down Expand Up @@ -1285,7 +1289,7 @@ func deleteReserved(g *d2graph.Graph, baseAST *d2ast.Map, mk *d2ast.Key) (*d2gra
}
}
if isNestedKey {
deleted, err := deleteObjField(g, baseAST, obj, id)
deleted, err := deleteObjField(g, boardPath, originalAST, baseAST, obj, id)
if err != nil {
return nil, err
}
Expand All @@ -1304,14 +1308,13 @@ func deleteReserved(g *d2graph.Graph, baseAST *d2ast.Map, mk *d2ast.Key) (*d2gra
id == "left" ||
id == "top" ||
id == "link" {
deleted, err := deleteObjField(g, baseAST, obj, id)
deleted, err := deleteObjField(g, boardPath, originalAST, baseAST, obj, id)
if err != nil {
return nil, err
}
if !deleted && imported {
mk.Value = d2ast.MakeValueBox(&d2ast.Null{})
appendMapKey(baseAST, mk)
} else {
}
}
break
Expand All @@ -1323,6 +1326,13 @@ func deleteReserved(g *d2graph.Graph, baseAST *d2ast.Map, mk *d2ast.Key) (*d2gra
imported = IsImportedObj(baseAST, obj)
}

if len(boardPath) > 0 {
replaced := ReplaceBoardNode(originalAST, baseAST, boardPath)
if !replaced {
return nil, fmt.Errorf("board %v AST not found", boardPath)
}
return recompile(g)
}
return recompile(g)
}

Expand Down Expand Up @@ -1385,7 +1395,7 @@ func deleteEdgeField(g *d2graph.Graph, ast *d2ast.Map, e *d2graph.Edge, field st
return deleted, nil
}

func deleteObjField(g *d2graph.Graph, ast *d2ast.Map, obj *d2graph.Object, field string) (deleted bool, _ error) {
func deleteObjField(g *d2graph.Graph, boardPath []string, originalAST, ast *d2ast.Map, obj *d2graph.Object, field string) (deleted bool, _ error) {
objK, err := d2parser.ParseKey(obj.AbsID())
if err != nil {
return false, err
Expand Down Expand Up @@ -1416,9 +1426,25 @@ func deleteObjField(g *d2graph.Graph, ast *d2ast.Map, obj *d2graph.Object, field
if deleted2 {
deleted = true
}
g2, err := recompile(g)
if err != nil {
return false, err
var g2 *d2graph.Graph
if len(boardPath) > 0 {
replaced := ReplaceBoardNode(originalAST, ast, boardPath)
if !replaced {
return false, fmt.Errorf("board %v AST not found", boardPath)
}
g2, err = recompile(g)
if err != nil {
return false, err
}
g2 = GetBoardGraph(g2, boardPath)
if g2 == nil {
return false, fmt.Errorf("board %v not found", boardPath)
}
} else {
g2, err = recompile(g)
if err != nil {
return false, err
}
}
if _, ok := g2.Root.HasChild(objGK); !ok {
// Nope, so can't delete it, just remove the field then
Expand Down
48 changes: 48 additions & 0 deletions d2oracle/edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"oss.terrastruct.com/d2/d2graph"
"oss.terrastruct.com/d2/d2oracle"
"oss.terrastruct.com/d2/d2target"
"oss.terrastruct.com/d2/d2themes"
"oss.terrastruct.com/d2/d2themes/d2themescatalog"
)

// TODO: make assertions less specific
Expand Down Expand Up @@ -5695,6 +5697,7 @@ func TestDelete(t *testing.T) {
testCases := []struct {
name string
boardPath []string
theme *d2themes.Theme

text string
fsTexts map[string]string
Expand Down Expand Up @@ -7618,6 +7621,44 @@ a
exp: `*.b
a
a.b: null
`,
},
{
name: "delete-layer-style",

text: `layers: {
x: {
a.style.fill: red
}
}
`,
theme: &d2themescatalog.Origami,
boardPath: []string{"x"},
key: `a.style.fill`,
exp: `layers: {
x: {
a
}
}
`,
},
{
name: "delete-theme-set",

text: `layers: {
x: {
a
}
}
`,
theme: &d2themescatalog.Origami,
boardPath: []string{"x"},
key: `a.style.fill-pattern`,
exp: `layers: {
x: {
a: {style.fill-pattern: null}
}
}
`,
},
}
Expand All @@ -7630,6 +7671,7 @@ a.b: null
et := editTest{
text: tc.text,
fsTexts: tc.fsTexts,
theme: tc.theme,
testFunc: func(g *d2graph.Graph) (*d2graph.Graph, error) {
return d2oracle.Delete(g, tc.boardPath, tc.key)
},
Expand All @@ -7645,6 +7687,7 @@ a.b: null

type editTest struct {
text string
theme *d2themes.Theme
fsTexts map[string]string
testFunc func(*d2graph.Graph) (*d2graph.Graph, error)

Expand Down Expand Up @@ -7672,6 +7715,11 @@ func (tc editTest) run(t *testing.T) {
})
assert.Success(t, err)

if tc.theme != nil {
err := g.ApplyTheme(tc.theme.ID)
assert.Success(t, err)
}

g, err = tc.testFunc(g)
if tc.expErr != "" {
if err == nil {
Expand Down
150 changes: 150 additions & 0 deletions testdata/d2oracle/TestSet/set-null.exp.json

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