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

Reduce the number of suppressions for internal access #308

Merged
merged 4 commits into from
Feb 28, 2024
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
10 changes: 5 additions & 5 deletions core/common/src/serializers/DateTimePeriodSerializers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import kotlinx.serialization.encoding.*
public object DateTimePeriodComponentSerializer: KSerializer<DateTimePeriod> {

override val descriptor: SerialDescriptor =
buildClassSerialDescriptor("DateTimePeriod") {
buildClassSerialDescriptor("kotlinx.datetime.DateTimePeriod") {
element<Int>("years", isOptional = true)
element<Int>("months", isOptional = true)
element<Int>("days", isOptional = true)
Expand Down Expand Up @@ -81,7 +81,7 @@ public object DateTimePeriodComponentSerializer: KSerializer<DateTimePeriod> {
public object DateTimePeriodIso8601Serializer: KSerializer<DateTimePeriod> {

override val descriptor: SerialDescriptor =
PrimitiveSerialDescriptor("DateTimePeriod", PrimitiveKind.STRING)
PrimitiveSerialDescriptor("kotlinx.datetime.DateTimePeriod", PrimitiveKind.STRING)

override fun deserialize(decoder: Decoder): DateTimePeriod =
DateTimePeriod.parse(decoder.decodeString())
Expand Down Expand Up @@ -110,7 +110,7 @@ public object DatePeriodComponentSerializer: KSerializer<DatePeriod> {
private fun unexpectedNonzero(fieldName: String, value: Int) = unexpectedNonzero(fieldName, value.toLong())

override val descriptor: SerialDescriptor =
buildClassSerialDescriptor("DatePeriod") {
buildClassSerialDescriptor("kotlinx.datetime.DatePeriod") {
element<Int>("years", isOptional = true)
element<Int>("months", isOptional = true)
element<Int>("days", isOptional = true)
Expand Down Expand Up @@ -166,7 +166,7 @@ public object DatePeriodComponentSerializer: KSerializer<DatePeriod> {
public object DatePeriodIso8601Serializer: KSerializer<DatePeriod> {

override val descriptor: SerialDescriptor =
PrimitiveSerialDescriptor("DatePeriod", PrimitiveKind.STRING)
PrimitiveSerialDescriptor("kotlinx.datetime.DatePeriod", PrimitiveKind.STRING)

override fun deserialize(decoder: Decoder): DatePeriod =
when (val period = DateTimePeriod.parse(decoder.decodeString())) {
Expand All @@ -178,4 +178,4 @@ public object DatePeriodIso8601Serializer: KSerializer<DatePeriod> {
encoder.encodeString(value.toString())
}

}
}
35 changes: 18 additions & 17 deletions core/common/src/serializers/DateTimeUnitSerializers.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 JetBrains s.r.o.
* Copyright 2019-2023 JetBrains s.r.o.
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
*/

Expand All @@ -23,7 +23,7 @@ public object TimeBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.TimeBase

// https://youtrack.jetbrains.com/issue/KT-63939
override val descriptor: SerialDescriptor by lazy(LazyThreadSafetyMode.PUBLICATION) {
buildClassSerialDescriptor("TimeBased") {
buildClassSerialDescriptor("kotlinx.datetime.TimeBased") {
element<Long>("nanoseconds")
}
}
Expand All @@ -35,7 +35,6 @@ public object TimeBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.TimeBase
}

@OptIn(ExperimentalSerializationApi::class)
@Suppress("INVISIBLE_MEMBER") // to be able to throw `MissingFieldException`
override fun deserialize(decoder: Decoder): DateTimeUnit.TimeBased {
var seen = false
var nanoseconds = 0L
Expand All @@ -51,12 +50,12 @@ public object TimeBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.TimeBase
seen = true
}
CompositeDecoder.DECODE_DONE -> break@loop // https://youtrack.jetbrains.com/issue/KT-42262
else -> throw UnknownFieldException(elementIndex)
else -> throwUnknownIndexException(elementIndex)
}
}
}
}
if (!seen) throw MissingFieldException("nanoseconds")
if (!seen) throw MissingFieldException(missingField = "nanoseconds", serialName = descriptor.serialName)
ilya-g marked this conversation as resolved.
Show resolved Hide resolved
return DateTimeUnit.TimeBased(nanoseconds)
}
}
Expand All @@ -70,7 +69,7 @@ public object DayBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.DayBased>

// https://youtrack.jetbrains.com/issue/KT-63939
override val descriptor: SerialDescriptor by lazy(LazyThreadSafetyMode.PUBLICATION) {
buildClassSerialDescriptor("DayBased") {
buildClassSerialDescriptor("kotlinx.datetime.DayBased") {
element<Int>("days")
}
}
Expand All @@ -82,7 +81,6 @@ public object DayBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.DayBased>
}

@OptIn(ExperimentalSerializationApi::class)
@Suppress("INVISIBLE_MEMBER") // to be able to throw `MissingFieldException`
override fun deserialize(decoder: Decoder): DateTimeUnit.DayBased {
var seen = false
var days = 0
Expand All @@ -98,12 +96,12 @@ public object DayBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.DayBased>
seen = true
}
CompositeDecoder.DECODE_DONE -> break@loop // https://youtrack.jetbrains.com/issue/KT-42262
else -> throw UnknownFieldException(elementIndex)
else -> throwUnknownIndexException(elementIndex)
}
}
}
}
if (!seen) throw MissingFieldException("days")
if (!seen) throw MissingFieldException(missingField = "days", serialName = descriptor.serialName)
return DateTimeUnit.DayBased(days)
}
}
Expand All @@ -117,7 +115,7 @@ public object MonthBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.MonthBa

// https://youtrack.jetbrains.com/issue/KT-63939
override val descriptor: SerialDescriptor by lazy(LazyThreadSafetyMode.PUBLICATION) {
buildClassSerialDescriptor("MonthBased") {
buildClassSerialDescriptor("kotlinx.datetime.MonthBased") {
element<Int>("months")
}
}
Expand All @@ -129,7 +127,6 @@ public object MonthBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.MonthBa
}

@OptIn(ExperimentalSerializationApi::class)
@Suppress("INVISIBLE_MEMBER") // to be able to throw `MissingFieldException`
override fun deserialize(decoder: Decoder): DateTimeUnit.MonthBased {
var seen = false
var months = 0
Expand All @@ -145,12 +142,12 @@ public object MonthBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.MonthBa
seen = true
}
CompositeDecoder.DECODE_DONE -> break@loop // https://youtrack.jetbrains.com/issue/KT-42262
else -> throw UnknownFieldException(elementIndex)
else -> throwUnknownIndexException(elementIndex)
}
}
}
}
if (!seen) throw MissingFieldException("months")
if (!seen) throw MissingFieldException(missingField = "months", serialName = descriptor.serialName)
return DateTimeUnit.MonthBased(months)
}
}
Expand All @@ -160,7 +157,7 @@ public object MonthBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.MonthBa
*
* JSON example: `{"type":"DayBased","days":15}`
*/
@Suppress("EXPERIMENTAL_API_USAGE_ERROR", "INVISIBLE_MEMBER")
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") // https://github.com/Kotlin/kotlinx.serialization/issues/2460
@OptIn(InternalSerializationApi::class)
public object DateBasedDateTimeUnitSerializer: AbstractPolymorphicSerializer<DateTimeUnit.DateBased>() {

Expand Down Expand Up @@ -197,9 +194,9 @@ public object DateBasedDateTimeUnitSerializer: AbstractPolymorphicSerializer<Dat
*
* JSON example: `{"type":"MonthBased","days":15}`
*/
@Suppress("EXPERIMENTAL_API_USAGE_ERROR", "INVISIBLE_MEMBER")
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") // https://github.com/Kotlin/kotlinx.serialization/issues/2460
@OptIn(InternalSerializationApi::class)
public object DateTimeUnitSerializer: AbstractPolymorphicSerializer<DateTimeUnit>() {
public object DateTimeUnitSerializer : AbstractPolymorphicSerializer<DateTimeUnit>() {

// https://youtrack.jetbrains.com/issue/KT-63939
private val impl by lazy(LazyThreadSafetyMode.PUBLICATION) {
Expand All @@ -224,4 +221,8 @@ public object DateTimeUnitSerializer: AbstractPolymorphicSerializer<DateTimeUnit
override val descriptor: SerialDescriptor
get() = impl.descriptor

}
}

internal fun throwUnknownIndexException(index: Int): Nothing {
throw SerializationException("An unknown field for index $index")
}
20 changes: 7 additions & 13 deletions core/common/src/serializers/DayOfWeekSerializers.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2019-2021 JetBrains s.r.o.
* Copyright 2019-2023 JetBrains s.r.o.
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
*/

package kotlinx.datetime.serializers

import kotlinx.datetime.DayOfWeek
import kotlinx.datetime.*
import kotlinx.serialization.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*
Expand All @@ -16,14 +16,8 @@ import kotlinx.serialization.internal.*
*
* JSON example: `"MONDAY"`
*/
@Suppress("INVISIBLE_MEMBER")
public object DayOfWeekSerializer: KSerializer<DayOfWeek> {
private val impl = EnumSerializer("Month", DayOfWeek.values())

override val descriptor: SerialDescriptor
get() = impl.descriptor

override fun deserialize(decoder: Decoder): DayOfWeek = impl.deserialize(decoder)

override fun serialize(encoder: Encoder, value: DayOfWeek): Unit = impl.serialize(encoder, value)
}
@Suppress("EnumValuesSoftDeprecate") // createEnumSerializer requires an array
public object DayOfWeekSerializer : KSerializer<DayOfWeek> by createEnumSerializer<DayOfWeek>(
"kotlinx.datetime.DayOfWeek",
DayOfWeek.values()
)
18 changes: 10 additions & 8 deletions core/common/src/serializers/InstantSerializers.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 JetBrains s.r.o.
* Copyright 2019-2023 JetBrains s.r.o.
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
*/

Expand All @@ -18,10 +18,10 @@ import kotlinx.serialization.encoding.*
* @see Instant.toString
* @see Instant.parse
*/
public object InstantIso8601Serializer: KSerializer<Instant> {
public object InstantIso8601Serializer : KSerializer<Instant> {

override val descriptor: SerialDescriptor =
PrimitiveSerialDescriptor("Instant", PrimitiveKind.STRING)
PrimitiveSerialDescriptor("kotlinx.datetime.Instant", PrimitiveKind.STRING)

override fun deserialize(decoder: Decoder): Instant =
Instant.parse(decoder.decodeString())
Expand All @@ -37,29 +37,31 @@ public object InstantIso8601Serializer: KSerializer<Instant> {
*
* JSON example: `{"epochSeconds":1607505416,"nanosecondsOfSecond":124000}`
*/
public object InstantComponentSerializer: KSerializer<Instant> {
public object InstantComponentSerializer : KSerializer<Instant> {

override val descriptor: SerialDescriptor =
buildClassSerialDescriptor("Instant") {
buildClassSerialDescriptor("kotlinx.datetime.Instant") {
element<Long>("epochSeconds")
element<Long>("nanosecondsOfSecond", isOptional = true)
}

@OptIn(ExperimentalSerializationApi::class)
@Suppress("INVISIBLE_MEMBER") // to be able to throw `MissingFieldException`
override fun deserialize(decoder: Decoder): Instant =
decoder.decodeStructure(descriptor) {
var epochSeconds: Long? = null
var nanosecondsOfSecond = 0
loop@while (true) {
loop@ while (true) {
when (val index = decodeElementIndex(descriptor)) {
0 -> epochSeconds = decodeLongElement(descriptor, 0)
1 -> nanosecondsOfSecond = decodeIntElement(descriptor, 1)
CompositeDecoder.DECODE_DONE -> break@loop // https://youtrack.jetbrains.com/issue/KT-42262
else -> throw SerializationException("Unexpected index: $index")
}
}
if (epochSeconds == null) throw MissingFieldException("epochSeconds")
if (epochSeconds == null) throw MissingFieldException(
missingField = "epochSeconds",
serialName = descriptor.serialName
)
Instant.fromEpochSeconds(epochSeconds, nanosecondsOfSecond)
}

Expand Down
15 changes: 7 additions & 8 deletions core/common/src/serializers/LocalDateSerializers.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 JetBrains s.r.o.
* Copyright 2019-2023 JetBrains s.r.o.
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
*/

Expand All @@ -21,7 +21,7 @@ import kotlinx.serialization.encoding.*
public object LocalDateIso8601Serializer: KSerializer<LocalDate> {

override val descriptor: SerialDescriptor =
PrimitiveSerialDescriptor("LocalDate", PrimitiveKind.STRING)
PrimitiveSerialDescriptor("kotlinx.datetime.LocalDate", PrimitiveKind.STRING)

override fun deserialize(decoder: Decoder): LocalDate =
LocalDate.parse(decoder.decodeString())
Expand All @@ -40,14 +40,13 @@ public object LocalDateIso8601Serializer: KSerializer<LocalDate> {
public object LocalDateComponentSerializer: KSerializer<LocalDate> {

override val descriptor: SerialDescriptor =
buildClassSerialDescriptor("LocalDate") {
buildClassSerialDescriptor("kotlinx.datetime.LocalDate") {
element<Int>("year")
element<Short>("month")
element<Short>("day")
}

@OptIn(ExperimentalSerializationApi::class)
@Suppress("INVISIBLE_MEMBER") // to be able to throw `MissingFieldException`
override fun deserialize(decoder: Decoder): LocalDate =
decoder.decodeStructure(descriptor) {
var year: Int? = null
Expand All @@ -59,12 +58,12 @@ public object LocalDateComponentSerializer: KSerializer<LocalDate> {
1 -> month = decodeShortElement(descriptor, 1)
2 -> day = decodeShortElement(descriptor, 2)
CompositeDecoder.DECODE_DONE -> break@loop // https://youtrack.jetbrains.com/issue/KT-42262
else -> throw SerializationException("Unexpected index: $index")
else -> throwUnknownIndexException(index)
}
}
if (year == null) throw MissingFieldException("year")
if (month == null) throw MissingFieldException("month")
if (day == null) throw MissingFieldException("day")
if (year == null) throw MissingFieldException(missingField = "year", serialName = descriptor.serialName)
if (month == null) throw MissingFieldException(missingField = "month", serialName = descriptor.serialName)
if (day == null) throw MissingFieldException(missingField = "day", serialName = descriptor.serialName)
LocalDate(year, month.toInt(), day.toInt())
}

Expand Down
17 changes: 8 additions & 9 deletions core/common/src/serializers/LocalDateTimeSerializers.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 JetBrains s.r.o.
* Copyright 2019-2023 JetBrains s.r.o.
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
*/

Expand All @@ -21,7 +21,7 @@ import kotlinx.serialization.encoding.*
public object LocalDateTimeIso8601Serializer: KSerializer<LocalDateTime> {

override val descriptor: SerialDescriptor =
PrimitiveSerialDescriptor("LocalDateTime", PrimitiveKind.STRING)
PrimitiveSerialDescriptor("kotlinx.datetime.LocalDateTime", PrimitiveKind.STRING)

override fun deserialize(decoder: Decoder): LocalDateTime =
LocalDateTime.parse(decoder.decodeString())
Expand All @@ -40,7 +40,7 @@ public object LocalDateTimeIso8601Serializer: KSerializer<LocalDateTime> {
public object LocalDateTimeComponentSerializer: KSerializer<LocalDateTime> {

override val descriptor: SerialDescriptor =
buildClassSerialDescriptor("LocalDateTime") {
buildClassSerialDescriptor("kotlinx.datetime.LocalDateTime") {
element<Int>("year")
element<Short>("month")
element<Short>("day")
Expand All @@ -51,7 +51,6 @@ public object LocalDateTimeComponentSerializer: KSerializer<LocalDateTime> {
}

@OptIn(ExperimentalSerializationApi::class)
@Suppress("INVISIBLE_MEMBER") // to be able to throw `MissingFieldException`
override fun deserialize(decoder: Decoder): LocalDateTime =
decoder.decodeStructure(descriptor) {
var year: Int? = null
Expand All @@ -74,11 +73,11 @@ public object LocalDateTimeComponentSerializer: KSerializer<LocalDateTime> {
else -> throw SerializationException("Unexpected index: $index")
}
}
if (year == null) throw MissingFieldException("year")
if (month == null) throw MissingFieldException("month")
if (day == null) throw MissingFieldException("day")
if (hour == null) throw MissingFieldException("hour")
if (minute == null) throw MissingFieldException("minute")
if (year == null) throw MissingFieldException(missingField = "year", serialName = descriptor.serialName)
if (month == null) throw MissingFieldException(missingField = "month", serialName = descriptor.serialName)
if (day == null) throw MissingFieldException(missingField = "day", serialName = descriptor.serialName)
if (hour == null) throw MissingFieldException(missingField = "hour", serialName = descriptor.serialName)
if (minute == null) throw MissingFieldException(missingField = "minute", serialName = descriptor.serialName)
LocalDateTime(year, month.toInt(), day.toInt(), hour.toInt(), minute.toInt(), second.toInt(), nanosecond)
}

Expand Down
Loading