Skip to content

Commit

Permalink
Merge pull request #2334 from alixander/fix-leaf-true
Browse files Browse the repository at this point in the history
fix &leaf filter
  • Loading branch information
alixander authored Feb 5, 2025
2 parents 14ef255 + 97cbd76 commit 8ecd875
Show file tree
Hide file tree
Showing 4 changed files with 735 additions and 33 deletions.
36 changes: 35 additions & 1 deletion d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5212,21 +5212,55 @@ y.link: https://google.com
},
},
{
name: "leaf-filter",
name: "leaf-filter-1",
run: func(t *testing.T) {
g, _ := assertCompile(t, `
**: {
&leaf: false
style.fill: red
}
**: {
&leaf: true
style.stroke: yellow
}
a.b.c
`, ``)
assert.Equal(t, "a", g.Objects[0].ID)
assert.Equal(t, "red", g.Objects[0].Attributes.Style.Fill.Value)
assert.Equal(t, (*d2graph.Scalar)(nil), g.Objects[0].Attributes.Style.Stroke)
assert.Equal(t, "b", g.Objects[1].ID)
assert.Equal(t, "red", g.Objects[1].Attributes.Style.Fill.Value)
assert.Equal(t, (*d2graph.Scalar)(nil), g.Objects[1].Attributes.Style.Stroke)
assert.Equal(t, "c", g.Objects[2].ID)
assert.Equal(t, (*d2graph.Scalar)(nil), g.Objects[2].Attributes.Style.Fill)
assert.Equal(t, "yellow", g.Objects[2].Attributes.Style.Stroke.Value)
},
},
{
name: "leaf-filter-2",
run: func(t *testing.T) {
g, _ := assertCompile(t, `
**: {
&leaf: true
style.stroke: yellow
}
a: {
b -> c
}
d: {
e
}
`, ``)
assert.Equal(t, "a", g.Objects[0].ID)
assert.Equal(t, (*d2graph.Scalar)(nil), g.Objects[0].Attributes.Style.Stroke)
assert.Equal(t, "b", g.Objects[1].ID)
assert.Equal(t, "yellow", g.Objects[1].Attributes.Style.Stroke.Value)
assert.Equal(t, "c", g.Objects[2].ID)
assert.Equal(t, "yellow", g.Objects[2].Attributes.Style.Stroke.Value)
assert.Equal(t, "d", g.Objects[3].ID)
assert.Equal(t, (*d2graph.Scalar)(nil), g.Objects[3].Attributes.Style.Stroke)
assert.Equal(t, "e", g.Objects[4].ID)
assert.Equal(t, "yellow", g.Objects[4].Attributes.Style.Stroke.Value)
},
},
{
Expand Down
17 changes: 17 additions & 0 deletions d2ir/d2ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,23 @@ func (m *Map) IsContainer() bool {
if m == nil {
return false
}
// Check references as the fields and edges may not be compiled yet
f := m.Parent().(*Field)
for _, ref := range f.References {
if ref.Primary() && ref.Context_.Key != nil && ref.Context_.Key.Value.Map != nil {
for _, n := range ref.Context_.Key.Value.Map.Nodes {
if len(n.MapKey.Edges) > 0 {
return true
}
if n.MapKey.Key != nil {
_, isReserved := d2ast.ReservedKeywords[n.MapKey.Key.Path[0].Unbox().ScalarString()]
if !(isReserved && f.Name.IsUnquoted()) {
return true
}
}
}
}
}
for _, f := range m.Fields {
_, isReserved := d2ast.ReservedKeywords[f.Name.ScalarString()]
if !(isReserved && f.Name.IsUnquoted()) {
Expand Down
Loading

0 comments on commit 8ecd875

Please sign in to comment.