model.DayOfWeekBSONCodec.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of baku Show documentation
Show all versions of baku Show documentation
helps you focus your REST API back-end on the business logic
package com.github.fluidsonic.baku
import org.bson.BsonReader
import org.bson.BsonWriter
import java.time.DayOfWeek
internal object DayOfWeekBSONCodec : AbstractBSONCodec() {
override fun BsonReader.decode(context: BSONCodingContext) =
readString().let { id ->
when (id) {
"monday" -> DayOfWeek.MONDAY
"tuesday" -> DayOfWeek.TUESDAY
"wednesday" -> DayOfWeek.WEDNESDAY
"thursday" -> DayOfWeek.THURSDAY
"friday" -> DayOfWeek.FRIDAY
"saturday" -> DayOfWeek.SATURDAY
"sunday" -> DayOfWeek.SUNDAY
else -> error("invalid day of week: $id")
}
}
override fun BsonWriter.encode(value: DayOfWeek, context: BSONCodingContext) {
writeString(when (value) {
DayOfWeek.MONDAY -> "monday"
DayOfWeek.TUESDAY -> "tuesday"
DayOfWeek.WEDNESDAY -> "wednesday"
DayOfWeek.THURSDAY -> "thursday"
DayOfWeek.FRIDAY -> "friday"
DayOfWeek.SATURDAY -> "saturday"
DayOfWeek.SUNDAY -> "sunday"
})
}
}