Skip to content
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

[kotlin-client][multiplatform] add support for kotlinx.datetime.LocalTime #19590

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -583,12 +583,15 @@ private void processKotlinxDate() {

typeMapping.put("date-time", "Instant");
typeMapping.put("date", "LocalDate");
typeMapping.put("time", "LocalTime");

typeMapping.put("DateTime", "Instant");
typeMapping.put("Date", "LocalDate");
typeMapping.put("Time", "LocalTime");

importMapping.put("Instant", "kotlinx.datetime.Instant");
importMapping.put("LocalDate", "kotlinx.datetime.LocalDate");
importMapping.put("LocalTime", "kotlinx.datetime.LocalTime");
}

private void processJVMRetrofit2Library(String infrastructureFolder) {
Expand Down Expand Up @@ -654,7 +657,7 @@ private void addSupportingSerializerAdapters(final String infrastructureFolder)
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/LocalDateAdapter.kt.mustache", infrastructureFolder, "LocalDateAdapter.kt"));
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/LocalDateTimeAdapter.kt.mustache", infrastructureFolder, "LocalDateTimeAdapter.kt"));
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/OffsetDateTimeAdapter.kt.mustache", infrastructureFolder, "OffsetDateTimeAdapter.kt"));
addKotlinxDateTimeInstantAdapter(infrastructureFolder);
addKotlinxDateTimeAdapters(infrastructureFolder);
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/BigDecimalAdapter.kt.mustache", infrastructureFolder, "BigDecimalAdapter.kt"));
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/BigIntegerAdapter.kt.mustache", infrastructureFolder, "BigIntegerAdapter.kt"));
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/URIAdapter.kt.mustache", infrastructureFolder, "URIAdapter.kt"));
Expand All @@ -665,7 +668,7 @@ private void addSupportingSerializerAdapters(final String infrastructureFolder)
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/LocalDateAdapter.kt.mustache", infrastructureFolder, "LocalDateAdapter.kt"));
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/LocalDateTimeAdapter.kt.mustache", infrastructureFolder, "LocalDateTimeAdapter.kt"));
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/OffsetDateTimeAdapter.kt.mustache", infrastructureFolder, "OffsetDateTimeAdapter.kt"));
addKotlinxDateTimeInstantAdapter(infrastructureFolder);
addKotlinxDateTimeAdapters(infrastructureFolder);
break;

case jackson:
Expand All @@ -689,9 +692,10 @@ private void addSupportingSerializerAdapters(final String infrastructureFolder)
}
}

private void addKotlinxDateTimeInstantAdapter(final String infrastructureFolder) {
private void addKotlinxDateTimeAdapters(final String infrastructureFolder) {
if (DateLibrary.KOTLINX_DATETIME.value.equals(dateLibrary)) {
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/InstantAdapter.kt.mustache", infrastructureFolder, "InstantAdapter.kt"));
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/LocalTimeAdapter.kt.mustache", infrastructureFolder, "LocalTimeAdapter.kt"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,51 @@ import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.SerialDescriptor
{{/kotlinx_serialization}}
{{^threetenbp}}
{{^kotlinx-datetime}}
import java.time.LocalDate
import java.time.format.DateTimeFormatter
{{/kotlinx-datetime}}
{{/threetenbp}}
{{#threetenbp}}
import org.threeten.bp.LocalDate
import org.threeten.bp.format.DateTimeFormatter
{{/threetenbp}}
{{#kotlinx-datetime}}
import kotlinx.datetime.LocalDate
{{/kotlinx-datetime}}

{{#moshi}}
{{#nonPublicApi}}internal {{/nonPublicApi}}class LocalDateAdapter {
@ToJson
fun toJson(value: LocalDate): String {
{{#kotlinx-datetime}}
return value.toString()
{{/kotlinx-datetime}}
{{^kotlinx-datetime}}
return DateTimeFormatter.ISO_LOCAL_DATE.format(value)
{{/kotlinx-datetime}}
}

@FromJson
fun fromJson(value: String): LocalDate {
return LocalDate.parse(value, DateTimeFormatter.ISO_LOCAL_DATE)
return LocalDate.parse(value{{^kotlinx-datetime}}, DateTimeFormatter.ISO_LOCAL_DATE{{/kotlinx-datetime}})
}

}
{{/moshi}}
{{#gson}}
{{#nonPublicApi}}internal {{/nonPublicApi}}class LocalDateAdapter(private val formatter: DateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE) : TypeAdapter<LocalDate>() {
{{#nonPublicApi}}internal {{/nonPublicApi}}class LocalDateAdapter({{^kotlinx-datetime}}private val formatter: DateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE{{/kotlinx-datetime}}) : TypeAdapter<LocalDate>() {
@Throws(IOException::class)
override fun write(out: JsonWriter?, value: LocalDate?) {
if (value == null) {
out?.nullValue()
} else {
{{#kotlinx-datetime}}
out?.value(value.toString())
{{/kotlinx-datetime}}
{{^kotlinx-datetime}}
out?.value(formatter.format(value))
{{/kotlinx-datetime}}
}
}

Expand All @@ -64,13 +79,14 @@ import org.threeten.bp.format.DateTimeFormatter
return null
}
else -> {
return LocalDate.parse(out.nextString(), formatter)
return LocalDate.parse(out.nextString(){{^kotlinx-datetime}}, formatter{{/kotlinx-datetime}})
}
}
}
}
{{/gson}}
{{#kotlinx_serialization}}
{{^kotlinx-datetime}}
@Serializer(forClass = LocalDate::class)
{{#nonPublicApi}}internal {{/nonPublicApi}}object LocalDateAdapter : KSerializer<LocalDate> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("LocalDate", PrimitiveKind.STRING)
Expand All @@ -83,4 +99,5 @@ import org.threeten.bp.format.DateTimeFormatter
return LocalDate.parse(decoder.decodeString(), DateTimeFormatter.ISO_LOCAL_DATE)
}
}
{{/kotlinx-datetime}}
{{/kotlinx_serialization}}
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,51 @@ import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.SerialDescriptor
{{/kotlinx_serialization}}
{{^threetenbp}}
{{^kotlinx-datetime}}
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
{{/kotlinx-datetime}}
{{/threetenbp}}
{{#threetenbp}}
import org.threeten.bp.LocalDateTime
import org.threeten.bp.format.DateTimeFormatter
{{/threetenbp}}
{{#kotlinx-datetime}}
import kotlinx.datetime.LocalDateTime
{{/kotlinx-datetime}}

{{#moshi}}
{{#nonPublicApi}}internal {{/nonPublicApi}}class LocalDateTimeAdapter {
@ToJson
fun toJson(value: LocalDateTime): String {
{{#kotlinx-datetime}}
return value.toString()
{{/kotlinx-datetime}}
{{^kotlinx-datetime}}
return DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(value)
{{/kotlinx-datetime}}
}

@FromJson
fun fromJson(value: String): LocalDateTime {
return LocalDateTime.parse(value, DateTimeFormatter.ISO_LOCAL_DATE_TIME)
return LocalDateTime.parse(value{{^kotlinx-datetime}}, DateTimeFormatter.ISO_LOCAL_DATE_TIME{{/kotlinx-datetime}})
}

}
{{/moshi}}
{{#gson}}
{{#nonPublicApi}}internal {{/nonPublicApi}}class LocalDateTimeAdapter(private val formatter: DateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME) : TypeAdapter<LocalDateTime>() {
{{#nonPublicApi}}internal {{/nonPublicApi}}class LocalDateTimeAdapter({{^kotlinx-datetime}}private val formatter: DateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME{{/kotlinx-datetime}}) : TypeAdapter<LocalDateTime>() {
@Throws(IOException::class)
override fun write(out: JsonWriter?, value: LocalDateTime?) {
if (value == null) {
out?.nullValue()
} else {
{{#kotlinx-datetime}}
out?.value(value.toString())
{{/kotlinx-datetime}}
{{^kotlinx-datetime}}
out?.value(formatter.format(value))
{{/kotlinx-datetime}}
}
}

Expand All @@ -64,13 +79,14 @@ import org.threeten.bp.format.DateTimeFormatter
return null
}
else -> {
return LocalDateTime.parse(out.nextString(), formatter)
return LocalDateTime.parse(out.nextString(){{^kotlinx-datetime}}, formatter{{/kotlinx-datetime}})
}
}
}
}
{{/gson}}
{{#kotlinx_serialization}}
{{^kotlinx-datetime}}
@Serializer(forClass = LocalDateTime::class)
{{#nonPublicApi}}internal {{/nonPublicApi}}object LocalDateTimeAdapter : KSerializer<LocalDateTime> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("LocalDateTime", PrimitiveKind.STRING)
Expand All @@ -83,4 +99,5 @@ import org.threeten.bp.format.DateTimeFormatter
return LocalDateTime.parse(decoder.decodeString(), DateTimeFormatter.ISO_LOCAL_DATE_TIME)
}
}
{{/kotlinx-datetime}}
{{/kotlinx_serialization}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package {{packageName}}.infrastructure

{{#moshi}}
import com.squareup.moshi.FromJson
import com.squareup.moshi.ToJson
{{/moshi}}
{{#gson}}
import com.google.gson.TypeAdapter
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonWriter
import com.google.gson.stream.JsonToken.NULL
import java.io.IOException
{{/gson}}
import kotlinx.datetime.LocalTime

{{#moshi}}
{{#nonPublicApi}}internal {{/nonPublicApi}}class LocalTimeAdapter {
@ToJson
fun toJson(value: LocalTime): String {
return value.toString()
}

@FromJson
fun fromJson(value: String): LocalTime {
return LocalTime.parse(value)
}

}
{{/moshi}}
{{#gson}}
{{#nonPublicApi}}internal {{/nonPublicApi}}class LocalTimeAdapter : TypeAdapter<LocalTime>() {
@Throws(IOException::class)
override fun write(out: JsonWriter?, value: LocalTime?) {
if (value == null) {
out?.nullValue()
} else {
out?.value(value.toString())
}
}

@Throws(IOException::class)
override fun read(out: JsonReader?): LocalTime? {
out ?: return null

when (out.peek()) {
NULL -> {
out.nextNull()
return null
}
else -> {
return LocalTime.parse(out.nextString())
}
}
}
}
{{/gson}}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.threeten.bp.OffsetDateTime
{{/threetenbp}}
{{#kotlinx-datetime}}
import kotlinx.datetime.Instant
import kotlinx.datetime.LocalTime
{{/kotlinx-datetime}}
import java.util.UUID
{{/gson}}
Expand Down Expand Up @@ -66,6 +67,7 @@ import java.util.concurrent.atomic.AtomicLong
.add(OffsetDateTimeAdapter())
{{#kotlinx-datetime}}
.add(InstantAdapter())
.add(LocalTimeAdapter())
{{/kotlinx-datetime}}
.add(LocalDateTimeAdapter())
.add(LocalDateAdapter())
Expand All @@ -92,6 +94,7 @@ import java.util.concurrent.atomic.AtomicLong
.registerTypeAdapter(OffsetDateTime::class.java, OffsetDateTimeAdapter())
{{#kotlinx-datetime}}
.registerTypeAdapter(Instant::class.java, InstantAdapter())
.registerTypeAdapter(LocalTime::class.java, LocalTimeAdapter())
{{/kotlinx-datetime}}
.registerTypeAdapter(LocalDateTime::class.java, LocalDateTimeAdapter())
.registerTypeAdapter(LocalDate::class.java, LocalDateAdapter())
Expand Down Expand Up @@ -122,9 +125,11 @@ import java.util.concurrent.atomic.AtomicLong
val kotlinxSerializationAdapters = SerializersModule {
contextual(BigDecimal::class, BigDecimalAdapter)
contextual(BigInteger::class, BigIntegerAdapter)
{{^kotlinx-datetime}}
contextual(LocalDate::class, LocalDateAdapter)
contextual(LocalDateTime::class, LocalDateTimeAdapter)
contextual(OffsetDateTime::class, OffsetDateTimeAdapter)
{{/kotlinx-datetime}}
contextual(UUID::class, UUIDAdapter)
contextual(AtomicInteger::class, AtomicIntegerAdapter)
contextual(AtomicLong::class, AtomicLongAdapter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ src/main/kotlin/org/openapitools/client/infrastructure/Errors.kt
src/main/kotlin/org/openapitools/client/infrastructure/InstantAdapter.kt
src/main/kotlin/org/openapitools/client/infrastructure/LocalDateAdapter.kt
src/main/kotlin/org/openapitools/client/infrastructure/LocalDateTimeAdapter.kt
src/main/kotlin/org/openapitools/client/infrastructure/LocalTimeAdapter.kt
src/main/kotlin/org/openapitools/client/infrastructure/OffsetDateTimeAdapter.kt
src/main/kotlin/org/openapitools/client/infrastructure/PartConfig.kt
src/main/kotlin/org/openapitools/client/infrastructure/RequestConfig.kt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ package org.openapitools.client.infrastructure

import com.squareup.moshi.FromJson
import com.squareup.moshi.ToJson
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import kotlinx.datetime.LocalDate

class LocalDateAdapter {
@ToJson
fun toJson(value: LocalDate): String {
return DateTimeFormatter.ISO_LOCAL_DATE.format(value)
return value.toString()
}

@FromJson
fun fromJson(value: String): LocalDate {
return LocalDate.parse(value, DateTimeFormatter.ISO_LOCAL_DATE)
return LocalDate.parse(value)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ package org.openapitools.client.infrastructure

import com.squareup.moshi.FromJson
import com.squareup.moshi.ToJson
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import kotlinx.datetime.LocalDateTime

class LocalDateTimeAdapter {
@ToJson
fun toJson(value: LocalDateTime): String {
return DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(value)
return value.toString()
}

@FromJson
fun fromJson(value: String): LocalDateTime {
return LocalDateTime.parse(value, DateTimeFormatter.ISO_LOCAL_DATE_TIME)
return LocalDateTime.parse(value)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.openapitools.client.infrastructure

import com.squareup.moshi.FromJson
import com.squareup.moshi.ToJson
import kotlinx.datetime.LocalTime

class LocalTimeAdapter {
@ToJson
fun toJson(value: LocalTime): String {
return value.toString()
}

@FromJson
fun fromJson(value: String): LocalTime {
return LocalTime.parse(value)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ object Serializer {
val moshiBuilder: Moshi.Builder = Moshi.Builder()
.add(OffsetDateTimeAdapter())
.add(InstantAdapter())
.add(LocalTimeAdapter())
.add(LocalDateTimeAdapter())
.add(LocalDateAdapter())
.add(UUIDAdapter())
Expand Down
Loading