Skip to content

Instantly share code, notes, and snippets.

View oharaandrew314's full-sized avatar

Andrew O'Hara oharaandrew314

View GitHub Profile
@oharaandrew314
oharaandrew314 / DynamoCat.kt
Last active March 30, 2022 02:20
dynamodb-v2-kotlin-cat-new
data class DynamoCat(
@DynamoKtPartitionKey
val ownerId: Int,
@DynamoKtSortKey
@DynamoKtSecondaryPartitionKey(indexNames = ["names"])
val name: String,
val gender: String,
val features: List<String>
@oharaandrew314
oharaandrew314 / DynamoCat.kt
Last active March 30, 2022 02:20
dynamodb-v2-kotlin-cat
@DynamoDbBean
data class DynamoCat(
@get:DynamoDbPartitionKey var ownerId: Int? = null,
@get:DynamoDbSortKey
@get:DynamoDbSecondaryPartitionKey(indexNames = ["names"])
var name: String? = null,
var gender: String? = null,
// create a data class model, making sure to give it a partition key
data class Cat(
@DynamoKtPartitionKey val name: String,
val lives: Int = 9,
)
// use the new table schema provided by this module to init the table mapper
val tableSchema = DataClassTableSchema(Cat::class)
val cats = DynamoDbEnhancedClient.create().table("cats", tableScema)
@oharaandrew314
oharaandrew314 / Cat.kt
Last active March 16, 2022 01:06
hexagonal-clients-updated-model
data class Cat(
val id: String,
val name: String,
val ownerId: String,
val ownerName: String,
val colours: Set<Colour>, // replaced grey and brown with this
val breed: Breed?,
val appointments: List<Instant>,
val favouriteFood: String? = null
)
@oharaandrew314
oharaandrew314 / Cat.kt
Created March 15, 2022 23:48
hexagonal-clients-port
data class Cat(
val id: String,
val name: String,
val ownerId: String,
val ownerName: String,
val brown: Boolean,
val grey: Boolean,
val breed: Breed?,
val appointments: List<Instant>,
val favouriteFood: String? = null // new field; defaulting to null for backwards compatibility
@oharaandrew314
oharaandrew314 / app.kt
Last active March 17, 2022 15:47
heaxagonal-clients-backcompat
fun main() {
val v1 = CatsDao.v1(ClientV1("http://catdocs.com"))
val v2 = CatsDao.v2(ClientV2("https://api.catdocs.com"))
val backCompat = CatsDao.backCompat(v1 = v1, v2 = v2)
val apiFeatureFlag = FeatureFlag.static(
treatments = mapOf("123" to "v1", "b692b290-40c4-4c0f-89ac-ed0a537e1736" to "v2"),
default = "v2"
)
@oharaandrew314
oharaandrew314 / app.kt
Last active March 17, 2022 15:47
hexagonal-clients-catsUi-updated
class CatUi(private val cats: CatsDao) {
fun renderCatHtml(id: String): String {
val cat = cats[id] ?: return "<h1>Cat $id not found</h1>"
val isBrown = if (cat.brown) "Yes" else "No"
val isGrey = if (cat.grey) "Yes" else "No"
val latestAppointment = cat.appointments.maxOrNull()
val optionalField = if (cat.favouriteFood != null) {
@oharaandrew314
oharaandrew314 / catsDaoV1.kt
Last active March 15, 2022 23:56
hexagonal-clients-catsDaoV1-return-raw-cat
fun CatsDao.Companion.v1(client: ClientV1) = CatsDao { id ->
client[id.toInt()]
}
@oharaandrew314
oharaandrew314 / ClientV2.kt
Last active March 16, 2022 01:37
hexagonal-clients-catsDaoV2
class ClientV2(host: String) {
object Lenses {
val catId = Path.uuid().of("id")
val cat = Body.auto<CatDtoV2>().toLens()
val appointments = Body.auto<List<AppointmentDtoV2>>().toLens()
}
private val backend = ClientFilters.SetHostFrom(Uri.of(host))
@oharaandrew314
oharaandrew314 / ClientV2.kt
Last active March 15, 2022 22:41
hexagonal-clients-clientV2
class ClientV2(host: String) {
object Lenses {
val catId = Path.uuid().of("id")
val cat = Body.auto<CatDtoV2>().toLens()
val appointments = Body.auto<List<AppointmentDtoV2>>().toLens()
}
private val backend = ClientFilters.SetHostFrom(Uri.of(host))