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

Null keyword #1446

Merged
merged 12 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
17 changes: 14 additions & 3 deletions d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func (c *compiler) compileBoard(g *d2graph.Graph, ir *d2ir.Map) *d2graph.Graph {
ir = ir.Copy(nil).(*d2ir.Map)
// c.preprocessSeqDiagrams(ir)
c.compileMap(g.Root, ir)
c.nullify(g)
if len(c.err.Errors) == 0 {
c.validateKeys(g.Root, ir)
}
Expand Down Expand Up @@ -340,9 +341,6 @@ func (c *compiler) compileField(obj *d2graph.Object, f *d2ir.Field) {
func (c *compiler) compileLabel(attrs *d2graph.Attributes, f d2ir.Node) {
scalar := f.Primary().Value
switch scalar := scalar.(type) {
case *d2ast.Null:
// TODO: Delete instead.
attrs.Label.Value = scalar.ScalarString()
case *d2ast.BlockString:
if strings.TrimSpace(scalar.ScalarString()) == "" {
c.errorf(f.LastPrimaryKey(), "block string cannot be empty")
Expand Down Expand Up @@ -836,6 +834,19 @@ func (c *compiler) compileArrowheads(edge *d2graph.Edge, f *d2ir.Field) {
}
}

func (c *compiler) nullify(g *d2graph.Graph) {
alixander marked this conversation as resolved.
Show resolved Hide resolved
for _, obj := range g.Objects {
if len(obj.References) > 0 && obj.References[len(obj.References)-1].Nulled() {
g.DeleteObject(obj)
}
}
for _, e := range g.Edges {
if len(e.References) > 0 && e.References[len(e.References)-1].Nulled() {
g.DeleteEdge(e)
}
}
}

// TODO add more, e.g. C, bash
var ShortToFullLanguageAliases = map[string]string{
"md": "markdown",
Expand Down
150 changes: 150 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2743,6 +2743,7 @@ func TestCompile2(t *testing.T) {

t.Run("boards", testBoards)
t.Run("seqdiagrams", testSeqDiagrams)
t.Run("nulls", testNulls)
}

func testBoards(t *testing.T) {
Expand Down Expand Up @@ -2923,6 +2924,155 @@ Office chatter: {
})
}

func testNulls(t *testing.T) {
t.Parallel()

t.Run("basic", func(t *testing.T) {
t.Parallel()

tca := []struct {
name string
skip bool
run func(t *testing.T)
}{
{
name: "shape",
run: func(t *testing.T) {
g := assertCompile(t, `
a
a: null
`, "")
assert.Equal(t, 0, len(g.Objects))
},
},
{
name: "edge",
run: func(t *testing.T) {
g := assertCompile(t, `
a -> b
(a -> b)[0]: null
`, "")
assert.Equal(t, 2, len(g.Objects))
assert.Equal(t, 0, len(g.Edges))
},
},
}

for _, tc := range tca {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
if tc.skip {
t.SkipNow()
}
tc.run(t)
})
}
})

t.Run("reappear", func(t *testing.T) {
t.Parallel()

tca := []struct {
name string
skip bool
run func(t *testing.T)
}{
{
name: "shape",
run: func(t *testing.T) {
g := assertCompile(t, `
a
a: null
a
`, "")
assert.Equal(t, 1, len(g.Objects))
},
},
{
name: "edge",
run: func(t *testing.T) {
g := assertCompile(t, `
a -> b
(a -> b)[0]: null
a -> b
`, "")
assert.Equal(t, 2, len(g.Objects))
assert.Equal(t, 1, len(g.Edges))
},
},
}

for _, tc := range tca {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
if tc.skip {
t.SkipNow()
}
tc.run(t)
})
}
})

t.Run("implicit", func(t *testing.T) {
t.Parallel()

tca := []struct {
name string
skip bool
run func(t *testing.T)
}{
{
name: "delete-connection",
run: func(t *testing.T) {
g := assertCompile(t, `
x -> y
y: null
`, "")
assert.Equal(t, 1, len(g.Objects))
assert.Equal(t, 0, len(g.Edges))
},
},
{
name: "no-delete-connection",
run: func(t *testing.T) {
g := assertCompile(t, `
y: null
x -> y
`, "")
assert.Equal(t, 2, len(g.Objects))
assert.Equal(t, 1, len(g.Edges))
},
},
{
name: "delete-children",
run: func(t *testing.T) {
g := assertCompile(t, `
x.y.z
a.b.c

x: null
a.b: null
`, "")
assert.Equal(t, 1, len(g.Objects))
},
},
}

for _, tc := range tca {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
if tc.skip {
t.SkipNow()
}
tc.run(t)
})
}
})
}

func assertCompile(t *testing.T, text string, expErr string) *d2graph.Graph {
d2Path := fmt.Sprintf("d2/testdata/d2compiler/%v.d2", t.Name())
g, err := d2compiler.Compile(d2Path, strings.NewReader(text), nil)
Expand Down
39 changes: 39 additions & 0 deletions d2graph/d2graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,37 @@ func (g *Graph) RootBoard() *Graph {
return g
}

// DeleteObject deletes an object along with all its descendants.
// It also deletes all edges connected to it or a descendant.
func (g *Graph) DeleteObject(obj *Object) {
for i, obj2 := range g.Objects {
if obj == obj2 {
g.Objects = append(g.Objects[:i], g.Objects[i+1:]...)
i--
}
}
obj.Parent.removeChild(obj)

for i, e := range g.Edges {
if e.Src == obj || e.Dst == obj {
g.Edges = append(g.Edges[:i], g.Edges[i+1:]...)
}
}

for _, ch := range obj.ChildrenArray {
g.DeleteObject(ch)
}
}

func (g *Graph) DeleteEdge(e *Edge) {
for i, e2 := range g.Edges {
if e == e2 {
g.Edges = append(g.Edges[:i], g.Edges[i+1:]...)
i--
}
}
}

type LayoutGraph func(context.Context, *Graph) error

// TODO consider having different Scalar types
Expand Down Expand Up @@ -193,6 +224,10 @@ type Reference struct {
ScopeAST *d2ast.Map `json:"-"`
}

func (r *Reference) Nulled() bool {
return r.MapKey != nil && len(r.MapKey.Edges) == 0 && r.MapKey.Value.Null != nil
}

func (r Reference) MapKeyEdgeDest() bool {
return r.Key == r.MapKey.Edges[r.MapKeyEdgeIndex].Dst
}
Expand Down Expand Up @@ -1120,6 +1155,10 @@ type EdgeReference struct {
ScopeAST *d2ast.Map `json:"-"`
}

func (er *EdgeReference) Nulled() bool {
return er.MapKey != nil && er.MapKey.Value.Null != nil
}

func (e *Edge) GetAstEdge() *d2ast.Edge {
return e.References[0].Edge
}
Expand Down
Loading