Skip to content

Commit

Permalink
d2ir: Add filtering on class arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
nhooyr committed Jul 30, 2023
1 parent 571fc62 commit e61ca0b
Show file tree
Hide file tree
Showing 5 changed files with 1,279 additions and 141 deletions.
37 changes: 22 additions & 15 deletions d2ir/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,10 @@ func (c *compiler) compileKey(refctx *RefContext) {
}

func (c *compiler) compileField(dst *Map, kp *d2ast.KeyPath, refctx *RefContext) {
if refctx.Key.Ampersand {
return
}

fa, err := dst.EnsureField(kp, refctx, true)
if err != nil {
c.err.Errors = append(c.err.Errors, err.(d2ast.Error))
Expand Down Expand Up @@ -451,23 +455,26 @@ func (c *compiler) _ampersandFilter(f *Field, refctx *RefContext) bool {
if f2 == nil {
return false
}
if refctx.Key.Primary.Unbox() != nil {
if f2.Primary_ == nil {
return false
}
if refctx.Key.Primary.Unbox().ScalarString() != f2.Primary_.Value.ScalarString() {
return false
}
if refctx.Key.Value.ScalarBox().Unbox() == nil {
c.errorf(refctx.Key, "ampersand filters cannot be composites")
return false
}
if refctx.Key.Value.ScalarBox().Unbox() != nil {
if f2.Primary_ == nil {
return false
}
if refctx.Key.Value.ScalarBox().Unbox().ScalarString() != f2.Primary_.Value.ScalarString() {
return false

if a, ok := f2.Composite.(*Array); ok {
for _, v := range a.Values {
if s, ok := v.(*Scalar); ok {
if refctx.Key.Value.ScalarBox().Unbox().ScalarString() == s.Value.ScalarString() {
return true
}
}
}
} else if refctx.Key.Value.Unbox() != nil {
c.errorf(refctx.Key, "ampersand filters cannot be composites")
}

if f2.Primary_ == nil {
return false
}

if refctx.Key.Value.ScalarBox().Unbox().ScalarString() != f2.Primary_.Value.ScalarString() {
return false
}

Expand Down
26 changes: 26 additions & 0 deletions d2ir/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,37 @@ jeremy: {
&shape: rectangle
}`)
assert.Success(t, err)
assertQuery(t, m, 5, 0, nil, "")
assertQuery(t, m, 1, 0, nil, "jacob")
assertQuery(t, m, 2, 0, nil, "jeremy")
assertQuery(t, m, 0, 0, "I'm a rectangle", "jeremy.label")
},
},
{
name: "array",
run: func(t testing.TB) {
m, err := compile(t, `the-little-cannon: {
class: [server; deployed]
}
dino: {
class: [internal; deployed]
}
catapult: {
class: [jacob; server]
}
*: {
&class: server
style.multiple: true
}
`)
assert.Success(t, err)
assertQuery(t, m, 10, 0, nil, "")
assertQuery(t, m, 3, 0, nil, "the-little-cannon")
assertQuery(t, m, 1, 0, nil, "dino")
assertQuery(t, m, 3, 0, nil, "catapult")
},
},
}

runa(t, tca)
Expand Down
Loading

0 comments on commit e61ca0b

Please sign in to comment.