d2ir: Add filtering on class arrays

This commit is contained in:
Anmol Sethi 2023-07-29 18:02:09 -07:00
parent 0f45d2b082
commit 7ae2d78834
No known key found for this signature in database
GPG key ID: 8CEF1878FF10ADEB
5 changed files with 1279 additions and 141 deletions

View file

@ -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))
@ -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 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
}
}
}
}
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
}
} 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
}

View file

@ -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)

1229
testdata/d2ir/TestCompile/filters/array.exp.json generated vendored Normal file

File diff suppressed because it is too large Load diff

View file

@ -247,7 +247,7 @@
"name": "shape",
"primary": {
"value": {
"range": "TestCompile/filters/base.d2,7:9:70-7:18:79",
"range": "TestCompile/filters/base.d2,4:8:44-4:17:53",
"value": [
{
"string": "rectangle",
@ -318,68 +318,6 @@
}
}
},
{
"string": {
"range": "TestCompile/filters/base.d2,7:2:63-7:7:68",
"value": [
{
"string": "shape",
"raw_string": "shape"
}
]
},
"key_path": {
"range": "TestCompile/filters/base.d2,7:2:63-7:7:68",
"path": [
{
"unquoted_string": {
"range": "TestCompile/filters/base.d2,7:2:63-7:7:68",
"value": [
{
"string": "shape",
"raw_string": "shape"
}
]
}
}
]
},
"context": {
"edge": null,
"key": {
"range": "TestCompile/filters/base.d2,7:1:62-7:18:79",
"ampersand": true,
"key": {
"range": "TestCompile/filters/base.d2,7:2:63-7:7:68",
"path": [
{
"unquoted_string": {
"range": "TestCompile/filters/base.d2,7:2:63-7:7:68",
"value": [
{
"string": "shape",
"raw_string": "shape"
}
]
}
}
]
},
"primary": {},
"value": {
"unquoted_string": {
"range": "TestCompile/filters/base.d2,7:9:70-7:18:79",
"value": [
{
"string": "rectangle",
"raw_string": "rectangle"
}
]
}
}
}
}
},
{
"string": {
"range": "TestCompile/filters/base.d2,7:2:63-7:7:68",

View file

@ -247,7 +247,7 @@
"name": "shape",
"primary": {
"value": {
"range": "TestCompile/filters/order.d2,8:9:94-8:18:103",
"range": "TestCompile/filters/order.d2,4:8:44-4:17:53",
"value": [
{
"string": "rectangle",
@ -318,68 +318,6 @@
}
}
},
{
"string": {
"range": "TestCompile/filters/order.d2,8:2:87-8:7:92",
"value": [
{
"string": "shape",
"raw_string": "shape"
}
]
},
"key_path": {
"range": "TestCompile/filters/order.d2,8:2:87-8:7:92",
"path": [
{
"unquoted_string": {
"range": "TestCompile/filters/order.d2,8:2:87-8:7:92",
"value": [
{
"string": "shape",
"raw_string": "shape"
}
]
}
}
]
},
"context": {
"edge": null,
"key": {
"range": "TestCompile/filters/order.d2,8:1:86-8:18:103",
"ampersand": true,
"key": {
"range": "TestCompile/filters/order.d2,8:2:87-8:7:92",
"path": [
{
"unquoted_string": {
"range": "TestCompile/filters/order.d2,8:2:87-8:7:92",
"value": [
{
"string": "shape",
"raw_string": "shape"
}
]
}
}
]
},
"primary": {},
"value": {
"unquoted_string": {
"range": "TestCompile/filters/order.d2,8:9:94-8:18:103",
"value": [
{
"string": "rectangle",
"raw_string": "rectangle"
}
]
}
}
}
}
},
{
"string": {
"range": "TestCompile/filters/order.d2,8:2:87-8:7:92",