Skip to content

Commit

Permalink
dsl: update filter(Entity.Type, invert = false)
Browse files Browse the repository at this point in the history
  • Loading branch information
junkdog committed Nov 18, 2023
1 parent 850e049 commit 5d24cbe
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Upcoming Release

### New/Tweaks
- DSL: `filter(Enitity.Type, invert)` filters entities by type, now has an `invert` parameter.
- Faster template processing by lazily parsing method parameters.

## sift-0.15.0 2023-08-21
Expand Down
12 changes: 11 additions & 1 deletion cli/src/main/resources/META-INF/native-image/reflect-config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[
{
"name":"[B"
},
{
"name":"[C"
},
Expand Down Expand Up @@ -312,6 +315,9 @@
{
"name":"kotlin.Boolean"
},
{
"name":"kotlin.ByteArray"
},
{
"name":"kotlin.Char"
},
Expand Down Expand Up @@ -739,7 +745,11 @@
"methods":[{"name":"<init>","parameterTypes":["org.objectweb.asm.Type"] }, {"name":"<init>","parameterTypes":["sift.core.dsl.SiftType"] }, {"name":"<init>","parameterTypes":["sift.core.dsl.SiftType","int","kotlin.jvm.internal.DefaultConstructorMarker"] }, {"name":"<init>","parameterTypes":["sift.core.dsl.Type"] }]
},
{
"name":"sift.core.api.Action$InjectClass"
"name":"sift.core.api.Action$InjectClass",
"allDeclaredFields":true,
"queryAllDeclaredMethods":true,
"queryAllDeclaredConstructors":true,
"methods":[{"name":"<init>","parameterTypes":["byte[]"] }, {"name":"<init>","parameterTypes":["byte[]","int","kotlin.jvm.internal.DefaultConstructorMarker"] }]
},
{
"name":"sift.core.api.Action$IntoAnnotations",
Expand Down
7 changes: 5 additions & 2 deletions core/src/main/kotlin/sift/core/api/Action.kt
Original file line number Diff line number Diff line change
Expand Up @@ -993,10 +993,13 @@ sealed class Action<IN, OUT> {
}
}

internal data class EntityFilter<T : Element>(val entity: Entity.Type) : IsoAction<T>() {
internal data class EntityFilter<T : Element>(
val entity: Entity.Type,
val invert: Boolean
) : IsoAction<T>() {
override fun id() = "filter-entity($entity)"
override fun execute(ctx: Context, input: Iter<T>): Iter<T> {
return input.filter { it in ctx.entityService }
return input.filter { (it in ctx.entityService) xor invert }
}
}

Expand Down
9 changes: 6 additions & 3 deletions core/src/main/kotlin/sift/core/dsl/Core.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ abstract class Core<ELEMENT : Element>(
private var stack: MutableList<Action<*, *>> = mutableListOf()

/**
* Filters elements that belongs to [entity].
* Filters elements owned by [entity].
*
* @param entity the entity to filter elements by
* @param invert if true, returns elements which do not belong to [entity]
*/
fun filter(entity: Entity.Type) {
action += Action.EntityFilter(entity)
fun filter(entity: Entity.Type, invert: Boolean = false) {
action += Action.EntityFilter(entity, invert)
}
}
25 changes: 25 additions & 0 deletions core/src/test/kotlin/sift/core/dsl/DslTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,31 @@ class DslTest {
)
}

@Test
fun `filter by entity type`() {
val a = Entity.Type("a")
val b = Entity.Type("b")

val cns = listOf(SomeController::class, NotAController::class).map(::classNode)

template {
classes {
filter("SomeController")
entity(a, label("a: \${name}"), property("name", readName()))
}

classes("match classes except those owned by 'a'") {
filter(a, invert = true)
entity(b, label("b: \${name}"), property("name", readName()))
}
}.expecting(cns, listOf(a, b), """
── a + b
├─ a: SomeController
└─ b: NotAController
"""
)
}

@Test
fun `read endpoint and construct label`() {
val endpoint = Entity.Type("endpoint")
Expand Down

0 comments on commit 5d24cbe

Please sign in to comment.