Skip to content

Commit

Permalink
Continue refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
deusaquilus committed Jan 9, 2024
1 parent 8f4ae28 commit 4ce7f9a
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 28 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,11 @@ data class Customer(val something: String, @Component val name: Name, @Component
on(something).match(
case( Customer[Name[Is(), Is()], Partner(Is())] )
.thenThis {{ first, last, id ->
.thenThis { first, last, id ->
// You can use the `this` keyword to refer to the `Customer` instance (also `this` can be omitted entirely).
// (the components first, last, id are also available here for convenience)
this.something + this.somethingElse
}}
}
// Other cases...
)
```
Expand Down
24 changes: 12 additions & 12 deletions decomat-core/src/main/kotlin/io/decomat/ThenPattern1.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class ThenIs<R>(
}

inline fun thenIf(crossinline f: (R) -> Boolean) = ThenIs(pat) { r: R -> useComponents(r, f) }
inline fun thenIfThis(crossinline f: R.() -> (R) -> Boolean) = ThenIs(pat) { r: R -> useComponents(r, f(r)) }
inline fun thenIfThis(crossinline f: R.(R) -> Boolean) = ThenIs(pat) { r: R -> useComponents(r, { c -> f(r, c) }) }
inline fun <O> then(crossinline f: (R) -> O): Case<O, R> = StageCase(pat, check) { r: R -> useComponents(r, f) }
inline fun <O> thenThis(crossinline f: R.() -> (R) -> O): Case<O, R> = StageCase(pat, check) { r: R -> useComponents(r, f(r)) }
inline fun <O> thenThis(crossinline f: R.(R) -> O): Case<O, R> = StageCase(pat, check) { r: R -> useComponents(r, { c -> f(r, c) }) }
}


Expand All @@ -24,15 +24,15 @@ class Then0<P1: Pattern0<R1>, R1, R>(
override val pat: Pattern1<P1, R1, R>,
override val check: (R) -> Boolean
): Stage<Pattern1<P1, R1, R>, R> {
inline fun <O> useComponents(r: R, f: (Components1<R1>) -> O): O {
inline fun <O> useComponents(r: R, f: (R1) -> O): O {
val (r1) = pat.divideIntoComponentsAny(r as Any)
return f(Components1(r1))
return f(r1)
}

inline fun thenIf(crossinline f: (Components1<R1>) -> Boolean) = Then0(pat) { r: R -> useComponents(r, f) }
inline fun thenIfThis(crossinline f: R.() -> (Components1<R1>) -> Boolean) = Then0(pat) { r: R -> useComponents(r, f(r)) }
inline fun <O> then(crossinline f: (Components1<R1>) -> O): Case<O, R> = StageCase(pat, check) { r: R -> useComponents(r, f) }
inline fun <O> thenThis(crossinline f: R.() -> (Components1<R1>) -> O): Case<O, R> = StageCase(pat, check) { r: R -> useComponents(r, f(r)) }
inline fun thenIf(crossinline f: (R1) -> Boolean) = Then0(pat) { r: R -> useComponents(r, f) }
inline fun thenIfThis(crossinline f: R.(R1) -> Boolean) = Then0(pat) { r: R -> useComponents(r, { c -> f(r, c) }) }
inline fun <O> then(crossinline f: (R1) -> O): Case<O, R> = StageCase(pat, check) { r: R -> useComponents(r, f) }
inline fun <O> thenThis(crossinline f: R.(R1) -> O): Case<O, R> = StageCase(pat, check) { r: R -> useComponents(r, { c -> f(r, c) }) }
}

fun <P1: Pattern1<P11, R11, R1>, P11: Pattern<R11>, R11, R1, R> case(pat: Pattern1<P1, R1, R>) = Then1(pat, {true})
Expand All @@ -48,9 +48,9 @@ class Then1<P1: Pattern1<P11, R11, R1>, P11: Pattern<R11>, R11, R1, R>(
}

inline fun thenIf(crossinline f: (Components1<R11>) -> Boolean) = Then1(pat) { r: R -> useComponents(r, f) }
inline fun thenIfThis(crossinline f: R.() -> (Components1<R11>) -> Boolean) = Then1(pat) { r: R -> useComponents(r, f(r)) }
inline fun thenIfThis(crossinline f: R.(Components1<R11>) -> Boolean) = Then1(pat) { r: R -> useComponents(r, { c -> f(r, c) }) }
inline fun <O> then(crossinline f: (Components1<R11>) -> O) = StageCase(pat, check) { r: R -> useComponents(r, f) }
inline fun <O> thenThis(crossinline f: R.() -> (Components1<R11>) -> O) = StageCase(pat, check) { r: R -> useComponents(r, f(r)) }
inline fun <O> thenThis(crossinline f: R.(Components1<R11>) -> O) = StageCase(pat, check) { r: R -> useComponents(r, { c -> f(r, c) }) }
}

/** Generic match for Pattern2<PatternA, PatternB> where we don't know what A and B are */
Expand All @@ -69,9 +69,9 @@ class Then2<P1: Pattern2<P11, P12, R11, R12, R1>, P11: Pattern<R11>, R11, P12: P
}

inline fun thenIf(crossinline f: (Components2<R11, R12>) -> Boolean) = Then2(pat) { r: R -> useComponents(r, f) }
inline fun thenIfThis(crossinline f: R.() -> (Components2<R11, R12>) -> Boolean) = Then2(pat) { r: R -> useComponents(r, f(r)) }
inline fun thenIfThis(crossinline f: R.(Components2<R11, R12>) -> Boolean) = Then2(pat) { r: R -> useComponents(r, { c -> f(r, c) }) }
inline fun <O> then(crossinline f: (Components2<R11, R12>) -> O) = StageCase(pat, check) { r: R -> useComponents(r, f) }
inline fun <O> thenThis(crossinline f: R.() -> (Components2<R11, R12>) -> O) = StageCase(pat, check) { r: R -> useComponents(r, f(r)) }
inline fun <O> thenThis(crossinline f: R.(Components2<R11, R12>) -> O) = StageCase(pat, check) { r: R -> useComponents(r, { c -> f(r, c) }) }
}

//fun <P: Pattern2<P1, P2, R1, R2, R>, P1: Pattern<R1>, P2: Pattern<R2>, R1, R2, R> case(pat: Pattern2<P1, P2, R1, R2, R>) = GenericThen2X(pat, {true})
Expand Down
14 changes: 10 additions & 4 deletions decomat-core/src/templates/Pattern3.ftl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[#ftl]



[#macro Pattern num]
[@compress single_line=true]
[#if num == 0]Pattern0<R>
Expand Down Expand Up @@ -96,6 +95,8 @@
[/@compress]
[/#macro]



[@output file="decomat-core/build/templates/io/decomat/Then2.kt"]
package io.decomat

Expand All @@ -116,12 +117,17 @@ class Then${i1}${i2}<P1: [@PatternVars 1 i1 /], P2: [@PatternVars 2 i2 /], R>(
[#if i2 != 0]val [@vars 2 i2 /] = pat.pattern2.divideIntoComponentsAny(r2 as Any)[#else]//skip[/#if]
return f([@compVars2 i1, i2 /])
}
inline fun thenIf(crossinline f: ([@Components 1 i1 /], [@Components 2 i2 /]) -> Boolean) =
Then${i1}${i2}(pat) { v -> useComponents(v, { c1, c2 -> f(c1, c2) }) }

inline fun thenIfThis(crossinline f: R.([@Components 1 i1 /], [@Components 2 i2 /]) -> Boolean) =
Then${i1}${i2}(pat) { v -> useComponents(v, { c1, c2 -> f(v, c1, c2) }) }

inline fun <O> then(crossinline f: ([@Components 1 i1 /], [@Components 2 i2 /]) -> O) =
StageCase(pat, check) { value -> useComponents(value, f) }
StageCase(pat, check) { v -> useComponents(v, { c1, c2 -> f(c1, c2) }) }

inline fun <O> thenThis(crossinline f: R.() -> ([@Components 1 i1 /], [@Components 2 i2 /]) -> O) =
StageCase(pat, check) { v -> useComponents(v, f(v)) }
inline fun <O> thenThis(crossinline f: R.([@Components 1 i1 /], [@Components 2 i2 /]) -> O) =
StageCase(pat, check) { v -> useComponents(v, { c1, c2 -> f(v, c1, c2) }) }
}
[/#list]
[/#list]
Expand Down
44 changes: 37 additions & 7 deletions decomat-core/src/test/kotlin/io/decomat/ThenTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.decomat

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull

public class ThenTest: DecomatTest {
// Just check that the types don't fail
Expand All @@ -17,14 +19,14 @@ public class ThenTest: DecomatTest {

@Test
fun `Then0 - distinct(Is) - Filter`() =
assert(
case(Distinct_M(Is<Entity>())).then { (a) -> Res1(a) }.eval(Distinct(foo)) == Res1(foo)
assertEquals<Res1<Entity>>(
case(Distinct_M(Is<Entity>())).then { a -> Res1(a) }.eval(Distinct(foo)), Res1(foo)
)

@Test
fun `Then0 - distinct(Is)`() =
assert(
case(Distinct_M(Is<Entity>())).then { (a) -> Res1(a) }.eval(Distinct(foo)) == Res1(foo)
assertEquals(
case(Distinct_M(Is<Entity>())).then { a -> Res1(a) }.eval(Distinct(foo)), Res1(foo)
)

@Test
Expand All @@ -48,7 +50,7 @@ public class ThenTest: DecomatTest {
@Test
fun `Then00 - flatMap(Is, Is) - thenThis`() =
assert(
case(FlatMap_M(Is(), Is())).thenThis {{ a, b -> Res3(a, b, body) }}.eval(FlatMap(foo, bar)) == Res3(foo, bar, bar)
case(FlatMap_M(Is(), Is())).thenThis { a, b -> Res3(a, b, body) }.eval(FlatMap(foo, bar)) == Res3(foo, bar, bar)
)

@Test
Expand All @@ -57,6 +59,36 @@ public class ThenTest: DecomatTest {
case(FlatMap_M(Is(), Is())).then { a, b -> Res2(a, b) }.eval(FlatMap(foo, bar)) == Res2(foo, bar)
)

@Test
fun `Then00-thenIf - flatMap(Is, Is)`() =
assert(
case(FlatMap_M(Is(), Is())).thenIf { a, b -> a == foo && b == bar }.then { a, b -> Res2(a, b) }.eval(FlatMap(foo, bar)) == Res2(foo, bar)
)

@Test
fun `Then00-!thenIf - flatMap(Is, Is)`() =
assert(
case(FlatMap_M(Is(), Is())).thenIf { a, b -> a != foo || b != bar }.then { a, b -> Res2(a, b) }.evalSafe(FlatMap(foo, bar)) == null
)

@Test
fun `Then00-thenIfThis - flatMap(Is, Is)`() =
assertEquals<Res2<Query, Query>>(
case(FlatMap_M(Is(), Is())).thenIfThis { a, b ->
a == foo && b == bar && this.head == foo && this.body == bar
}.then { a, b -> Res2(a, b) }.eval(FlatMap(foo, bar)),
// ==
Res2(foo, bar)
)

@Test
fun `Then00-!thenIfThis - flatMap(Is, Is)`() =
assertNull(
case(FlatMap_M(Is(), Is())).thenIfThis { a, b ->
a != foo || b != bar || this.head != foo || this.body != bar
}.then { a, b -> Res2(a, b) }.evalSafe(FlatMap(foo, bar))
)

@Test
fun `Then01 - flatMap(Is, Distinct)`() =
assert(
Expand Down Expand Up @@ -104,6 +136,4 @@ public class ThenTest: DecomatTest {
assert(
case(FlatMap_M(Map_M(Is(), Is()), Map_M(Is(), Is()))).then { (a1, a2), (b1, b2) -> Res4(a1, a2, b1, b2) }.eval(FlatMap(Map(foo, bar), Map(baz, waz))) == Res4(foo, bar, baz, waz)
)

// TODO Need test for Then31-33 and Then000-Then333
}
2 changes: 1 addition & 1 deletion decomat-ksp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ plugins {

dependencies {
project(":decomat-core")
implementation("com.google.devtools.ksp:symbol-processing-api:1.9.20-1.0.14")
implementation("com.google.devtools.ksp:symbol-processing-api:2.0.0-Beta2-1.0.16")
implementation(kotlin("stdlib-jdk8"))
testImplementation(kotlin("test"))
//implementation("com.facebook:ktfmt:0.43")
Expand Down
4 changes: 2 additions & 2 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pluginManagement {
plugins {
id("com.google.devtools.ksp") version "1.9.20-1.0.14"
kotlin("jvm") version "1.9.20"
id("com.google.devtools.ksp") version "2.0.0-Beta2-1.0.16"
kotlin("jvm") version "2.0.0-Beta2"
id("dev.anies.gradle.template") version "0.0.2"
}
repositories {
Expand Down

0 comments on commit 4ce7f9a

Please sign in to comment.