-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from EMH333/master
Add support for type aliases
- Loading branch information
Showing
14 changed files
with
208 additions
and
28 deletions.
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
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
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
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
26 changes: 26 additions & 0 deletions
26
example/src/main/kotlin/pl/touk/krush/typealiases/VisitorLog.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,26 @@ | ||
package pl.touk.krush.typealiases | ||
|
||
import javax.persistence.* | ||
|
||
typealias VisitorList = List<String> | ||
typealias PlainString = String | ||
|
||
@Entity | ||
data class VisitorLog( | ||
@Id @GeneratedValue | ||
val id: Long? = null, | ||
@Convert(converter = VisitorListConverter::class) | ||
val visitors: VisitorList, | ||
val guard: PlainString | ||
) | ||
|
||
@Converter | ||
class VisitorListConverter : AttributeConverter<VisitorList, String> { | ||
override fun convertToDatabaseColumn(attribute: VisitorList?): String { | ||
return attribute?.joinToString(",") ?: "" | ||
} | ||
|
||
override fun convertToEntityAttribute(dbData: String?): VisitorList { | ||
return dbData?.split(",") ?: emptyList() | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
example/src/test/kotlin/pl/touk/krush/typealiases/VisitorLogTest.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,35 @@ | ||
package pl.touk.krush.typealiases | ||
|
||
import org.assertj.core.api.Assertions | ||
import org.jetbrains.exposed.sql.SchemaUtils | ||
import org.jetbrains.exposed.sql.select | ||
import org.jetbrains.exposed.sql.transactions.transaction | ||
import org.junit.jupiter.api.Test | ||
import pl.touk.krush.base.BaseDatabaseTest | ||
import pl.touk.krush.types.Event | ||
import pl.touk.krush.types.EventTable | ||
import pl.touk.krush.types.insert | ||
import pl.touk.krush.types.toEventList | ||
import java.time.* | ||
import java.util.* | ||
|
||
class VisitorLogTest : BaseDatabaseTest() { | ||
|
||
@Test | ||
fun shouldHandleTypeAliases() { | ||
transaction { | ||
SchemaUtils.create(VisitorLogTable) | ||
|
||
// given | ||
val log = VisitorLogTable.insert(VisitorLog(visitors = listOf("Krush", "Kotlin", "Gradle" ), guard = "Kelly")) | ||
|
||
//when | ||
val logs = (VisitorLogTable) | ||
.select { VisitorLogTable.guard eq "Kelly" } | ||
.toVisitorLogList() | ||
|
||
//then | ||
Assertions.assertThat(logs).containsOnly(log) | ||
} | ||
} | ||
} |
Oops, something went wrong.