-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement power operator #651
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
67a4be2
feat: implement power arithmetic operation
progress0407 d7744f1
docs: update expressions docs
progress0407 2384029
style: merged from develop and reorder expressions
progress0407 9822e0b
test: remove book property and choose employ fixture
progress0407 bfb8d48
style: rewrite comments to conform to conventions
progress0407 8b80467
fix: fix poorly implemented serializer code
progress0407 69109b2
fix: fix poorly implemented jpql code and test
progress0407 5369802
Merge branch 'develop' into feat/arith-power
progress0407 1bfb2cf
docs: reflect reviews of updating example
progress0407 5d231b1
refactor: reflect reviews of updating example
progress0407 c4a8a6a
Merge branch 'develop' into feat/arith-power
progress0407 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/expression/PowerDslTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.linecorp.kotlinjdsl.dsl.jpql.expression | ||
|
||
import com.linecorp.kotlinjdsl.dsl.jpql.entity.employee.Employee | ||
import com.linecorp.kotlinjdsl.dsl.jpql.queryPart | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expression | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expressions | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.path.Paths | ||
import org.assertj.core.api.WithAssertions | ||
import org.junit.jupiter.api.Test | ||
|
||
class PowerDslTest : WithAssertions { | ||
private val intExpression1 = Paths.path(Employee::age) | ||
private val intExpression2 = Expressions.value(2) | ||
|
||
private val int1 = 2 | ||
|
||
@Test | ||
fun `power() with a property`() { | ||
// when | ||
val expression1 = queryPart { | ||
power(Employee::age, int1) | ||
}.toExpression() | ||
|
||
val expression2 = queryPart { | ||
power(Employee::age, intExpression2) | ||
}.toExpression() | ||
|
||
val actual1: Expression<Number> = expression1 // for type check | ||
val actual2: Expression<Number> = expression2 // for type check | ||
|
||
// then | ||
val expected1 = Expressions.power( | ||
base = Paths.path(Employee::age), | ||
exponent = Expressions.value(int1), | ||
) | ||
|
||
val expected2 = Expressions.power( | ||
base = Paths.path(Employee::age), | ||
exponent = intExpression2, | ||
) | ||
|
||
assertThat(actual1).isEqualTo(expected1) | ||
assertThat(actual2).isEqualTo(expected2) | ||
} | ||
|
||
@Test | ||
fun `power() with a expression`() { | ||
// when | ||
val expression1 = queryPart { | ||
power(intExpression1, int1) | ||
}.toExpression() | ||
|
||
val expression2 = queryPart { | ||
power(intExpression1, intExpression2) | ||
}.toExpression() | ||
|
||
val actual1: Expression<Number> = expression1 // for type check | ||
val actual2: Expression<Number> = expression2 // for type check | ||
|
||
// then | ||
val expected1 = Expressions.power( | ||
base = intExpression1, | ||
exponent = Expressions.value(int1), | ||
) | ||
|
||
val expected2 = Expressions.power( | ||
base = intExpression1, | ||
exponent = intExpression2, | ||
) | ||
|
||
assertThat(actual1).isEqualTo(expected1) | ||
assertThat(actual2).isEqualTo(expected2) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...jpql/src/main/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/expression/impl/JpqlPower.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl | ||
|
||
import com.linecorp.kotlinjdsl.Internal | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expression | ||
|
||
/** | ||
* Expression that calculates the powering of a numeric [base] to a specified [exponent]. | ||
*/ | ||
@Internal | ||
data class JpqlPower<T : Number> internal constructor( | ||
val base: Expression<T>, | ||
val exponent: Expression<T>, | ||
) : Expression<Double> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...rc/main/kotlin/com/linecorp/kotlinjdsl/render/jpql/serializer/impl/JpqlPowerSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.linecorp.kotlinjdsl.render.jpql.serializer.impl | ||
|
||
import com.linecorp.kotlinjdsl.Internal | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlPower | ||
import com.linecorp.kotlinjdsl.render.RenderContext | ||
import com.linecorp.kotlinjdsl.render.jpql.serializer.JpqlRenderSerializer | ||
import com.linecorp.kotlinjdsl.render.jpql.serializer.JpqlSerializer | ||
import com.linecorp.kotlinjdsl.render.jpql.writer.JpqlWriter | ||
import kotlin.reflect.KClass | ||
|
||
@Internal | ||
class JpqlPowerSerializer : JpqlSerializer<JpqlPower<*>> { | ||
override fun handledType(): KClass<JpqlPower<*>> { | ||
return JpqlPower::class | ||
} | ||
|
||
override fun serialize(part: JpqlPower<*>, writer: JpqlWriter, context: RenderContext) { | ||
val delegate = context.getValue(JpqlRenderSerializer) | ||
|
||
writer.write("POWER") | ||
|
||
writer.writeParentheses { | ||
delegate.serialize(part.base, writer, context) | ||
|
||
writer.write(",") | ||
writer.write(" ") | ||
|
||
delegate.serialize(part.exponent, writer, context) | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...est/kotlin/com/linecorp/kotlinjdsl/render/jpql/serializer/impl/JpqlPowerSerializerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.linecorp.kotlinjdsl.render.jpql.serializer.impl | ||
|
||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expressions | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlPower | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.path.Paths | ||
import com.linecorp.kotlinjdsl.render.TestRenderContext | ||
import com.linecorp.kotlinjdsl.render.jpql.entity.employee.Employee | ||
import com.linecorp.kotlinjdsl.render.jpql.serializer.JpqlRenderSerializer | ||
import com.linecorp.kotlinjdsl.render.jpql.serializer.JpqlSerializerTest | ||
import com.linecorp.kotlinjdsl.render.jpql.writer.JpqlWriter | ||
import io.mockk.impl.annotations.MockK | ||
import io.mockk.verifySequence | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
|
||
@JpqlSerializerTest | ||
class JpqlPowerSerializerTest { | ||
private val sut = JpqlPowerSerializer() | ||
|
||
@MockK | ||
private lateinit var writer: JpqlWriter | ||
|
||
@MockK | ||
private lateinit var serializer: JpqlRenderSerializer | ||
|
||
private val expression1 = Paths.path(Employee::age) | ||
private val expression2 = Expressions.value(2) | ||
|
||
@Test | ||
fun handledType() { | ||
// when | ||
val actual = sut.handledType() | ||
|
||
// then | ||
assertThat(actual).isEqualTo(JpqlPower::class) | ||
} | ||
|
||
@Test | ||
fun serialize() { | ||
// given | ||
val part = Expressions.power( | ||
base = expression1, | ||
exponent = expression2, | ||
) | ||
val context = TestRenderContext(serializer) | ||
|
||
// when | ||
sut.serialize(part as JpqlPower<*>, writer, context) | ||
|
||
// then | ||
verifySequence { | ||
writer.write("POWER") | ||
writer.writeParentheses(any()) | ||
serializer.serialize(expression1, writer, context) | ||
writer.write(",") | ||
writer.write(" ") | ||
serializer.serialize(expression2, writer, context) | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about writing something like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you. The sentence below looks easier to read.