Skip to content

Commit

Permalink
Removae WrappedLocalDateTime and WrappedSubType to use a deserializer
Browse files Browse the repository at this point in the history
instead.
  • Loading branch information
Bram-- committed Feb 22, 2024
1 parent bc12719 commit bd33ab4
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 84 deletions.
4 changes: 3 additions & 1 deletion src/main/kotlin/org/audux/bgg/response/Plays.kt
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ data class PlayItem(
)

/** A SubType of a thing e.g. board game. */
data class SubType(val subtype: WrappedSubType)
data class SubType(
@JsonDeserialize(using = WrappedSubTypeDeserializer::class)
val subtype: org.audux.bgg.common.SubType)

/**
* Represents a person in the play i.e. their username, id, what color they played, how they did
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/org/audux/bgg/response/Things.kt
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,8 @@ data class Comment(
/** A single listing for the thing i.e. a 'for sale'-listing. */
data class MarketplaceListing(
/** When the listing was created. */
val listDate: WrappedLocalDateTime,
@JsonDeserialize(using = WrappedLocalDateTimeDeserializer::class)
val listDate: LocalDateTime,

/** The requested price for the listing. */
val price: Price,
Expand Down
23 changes: 20 additions & 3 deletions src/main/kotlin/org/audux/bgg/response/TypeDeserializers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import com.fasterxml.jackson.core.JsonToken
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonDeserializer
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.Locale
import org.audux.bgg.common.FamilyType
import org.audux.bgg.common.ForumListType
import org.audux.bgg.common.PlayThingType
Expand Down Expand Up @@ -102,14 +104,29 @@ internal class WrappedLocalDateDeserializer : JsonDeserializer<LocalDate?>() {
* Deserializes `<elementName value="2012-10-12" />` objects into a `LocalDate.of(2012, 10, 12)
* property.
*/
internal class WrappedLocalDateTimeDeserializer : JsonDeserializer<LocalDate?>() {
internal class WrappedLocalDateTimeDeserializer : JsonDeserializer<LocalDateTime?>() {
override fun deserialize(parser: JsonParser?, context: DeserializationContext?) =
readWrappedValue(parser) {
val formatter = DateTimeFormatter.ofPattern("E, dd MMM yyyy HH:mm:ss Z")
LocalDate.parse(it.valueAsString, formatter)
it.valueAsString
?.takeIf { str -> str.isNotBlank() }
?.let { str ->
val formatter =
DateTimeFormatter.ofPattern("E, dd MMM yyyy HH:mm:ss Z")
.localizedBy(Locale.US)
LocalDateTime.parse(str, formatter)
}
}
}

/**
* Deserializes `<elementName value="2012-10-12" />` objects into a `LocalDate.of(2012, 10, 12)
* property.
*/
internal class WrappedSubTypeDeserializer : JsonDeserializer<SubType?>() {
override fun deserialize(parser: JsonParser?, context: DeserializationContext?) =
readWrappedValue(parser) { SubType.fromParam(parser?.valueAsString) }
}

/** Deserializes and trims strings. */
private fun <T> readWrappedValue(parser: JsonParser?, read: (JsonParser) -> T): T? =
parser?.let {
Expand Down
32 changes: 0 additions & 32 deletions src/main/kotlin/org/audux/bgg/response/Wrappedvalues.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class PlaysResponseTest {
objectType = PlayThingType.THING,
objectId = 77130,
subTypes =
listOf(SubType(WrappedSubType(org.audux.bgg.common.SubType.BOARD_GAME)))
listOf(SubType(org.audux.bgg.common.SubType.BOARD_GAME))
)
)
assertThat(civGame.players)
Expand Down
8 changes: 4 additions & 4 deletions src/test/kotlin/org/audux/bgg/response/ThingsResponseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ package org.audux.bgg.response

import com.fasterxml.jackson.databind.ObjectMapper
import com.google.common.truth.Truth.assertThat
import java.net.URI
import java.time.LocalDate
import org.audux.bgg.common.ThingType
import org.audux.bgg.util.TestUtils
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import java.net.URI
import java.time.LocalDate
import java.time.LocalDateTime

/** Test class for [Things] data classes. */
class ThingsResponseTest {
Expand Down Expand Up @@ -336,8 +337,7 @@ class ThingsResponseTest {
assertThat(things.things).hasSize(1)
val listings = things.things[0].listings
assertThat(listings).hasSize(11)
// WrappedLocalDateTimeSubject.assertThat(listings[0].listDate)
// .isEqualTo(LocalDateTime.of(2023, 10, 6, 19, 41, 25))
assertThat(listings[0].listDate).isEqualTo(LocalDateTime.of(2023, 10, 6, 19, 41, 25))
assertThat(listings[0].price.value).isEqualTo(80.00)
assertThat(listings[0].price.currency).isEqualTo("USD")
assertThat(listings[0].condition).isEqualTo("new")
Expand Down
42 changes: 0 additions & 42 deletions src/test/kotlin/org/audux/bgg/response/WrappedValueSubject.kt

This file was deleted.

0 comments on commit bd33ab4

Please sign in to comment.