-
Notifications
You must be signed in to change notification settings - Fork 620
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce HoconEncoder and HoconDecoder interfaces (#2094)
Analogues for JsonEncoder/Decoder should ease writing hocon-specific serializers for various classes. Add java.time.Duration and ConfigMemorySize serializers for HOCON. --------- Co-authored-by: Leonid Startsev <sandwwraith@users.noreply.github.com>
- Loading branch information
1 parent
90113a9
commit acb0988
Showing
13 changed files
with
827 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
formats/hocon/src/main/kotlin/kotlinx/serialization/hocon/HoconDecoder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package kotlinx.serialization.hocon | ||
|
||
import com.typesafe.config.Config | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
|
||
/** | ||
* Decoder used by Hocon during deserialization. | ||
* This interface allows to call methods from the Lightbend/config library on the [Config] object to intercept default deserialization process. | ||
* | ||
* Usage example (nested config serialization): | ||
* ``` | ||
* @Serializable | ||
* data class Example( | ||
* @Serializable(NestedConfigSerializer::class) | ||
* val d: Config | ||
* ) | ||
* object NestedConfigSerializer : KSerializer<Config> { | ||
* override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("package.Config", PrimitiveKind.STRING) | ||
* | ||
* override fun deserialize(decoder: Decoder): Config = | ||
* if (decoder is HoconDecoder) decoder.decodeConfigValue { conf, path -> conf.getConfig(path) } | ||
* else throw SerializationException("This class can be decoded only by Hocon format") | ||
* | ||
* override fun serialize(encoder: Encoder, value: Config) { | ||
* if (encoder is AbstractHoconEncoder) encoder.encodeConfigValue(value.root()) | ||
* else throw SerializationException("This class can be encoded only by Hocon format") | ||
* } | ||
* } | ||
* | ||
* val nestedConfig = ConfigFactory.parseString("nested { value = \"test\" }") | ||
* val globalConfig = Hocon.encodeToConfig(Example(nestedConfig)) // d: { nested: { value = "test" } } | ||
* val newNestedConfig = Hocon.decodeFromConfig(Example.serializer(), globalConfig) | ||
* ``` | ||
*/ | ||
@ExperimentalSerializationApi | ||
sealed interface HoconDecoder { | ||
|
||
/** | ||
* Decodes the value at the current path from the input. | ||
* Allows to call methods on a [Config] instance. | ||
* | ||
* @param E type of value | ||
* @param extractValueAtPath lambda for extracting value, where conf - original config object, path - current path expression being decoded. | ||
* @return result of lambda execution | ||
*/ | ||
fun <E> decodeConfigValue(extractValueAtPath: (conf: Config, path: String) -> E): E | ||
} |
204 changes: 39 additions & 165 deletions
204
formats/hocon/src/main/kotlin/kotlinx/serialization/hocon/HoconEncoder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,169 +1,43 @@ | ||
/* | ||
* Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.serialization.hocon | ||
|
||
import com.typesafe.config.* | ||
import kotlin.time.* | ||
import kotlinx.serialization.* | ||
import kotlinx.serialization.builtins.serializer | ||
import kotlinx.serialization.descriptors.* | ||
import kotlinx.serialization.encoding.* | ||
import kotlinx.serialization.internal.* | ||
import kotlinx.serialization.modules.* | ||
|
||
@ExperimentalSerializationApi | ||
internal abstract class AbstractHoconEncoder( | ||
private val hocon: Hocon, | ||
private val valueConsumer: (ConfigValue) -> Unit, | ||
) : NamedValueEncoder() { | ||
|
||
override val serializersModule: SerializersModule | ||
get() = hocon.serializersModule | ||
|
||
private var writeDiscriminator: Boolean = false | ||
|
||
override fun elementName(descriptor: SerialDescriptor, index: Int): String { | ||
return descriptor.getConventionElementName(index, hocon.useConfigNamingConvention) | ||
} | ||
|
||
override fun composeName(parentName: String, childName: String): String = childName | ||
|
||
protected abstract fun encodeTaggedConfigValue(tag: String, value: ConfigValue) | ||
protected abstract fun getCurrent(): ConfigValue | ||
|
||
override fun encodeTaggedValue(tag: String, value: Any) = encodeTaggedConfigValue(tag, configValueOf(value)) | ||
override fun encodeTaggedNull(tag: String) = encodeTaggedConfigValue(tag, configValueOf(null)) | ||
override fun encodeTaggedChar(tag: String, value: Char) = encodeTaggedString(tag, value.toString()) | ||
|
||
override fun encodeTaggedEnum(tag: String, enumDescriptor: SerialDescriptor, ordinal: Int) { | ||
encodeTaggedString(tag, enumDescriptor.getElementName(ordinal)) | ||
} | ||
|
||
override fun shouldEncodeElementDefault(descriptor: SerialDescriptor, index: Int): Boolean = hocon.encodeDefaults | ||
|
||
override fun <T> encodeSerializableValue(serializer: SerializationStrategy<T>, value: T) { | ||
when { | ||
serializer.descriptor == Duration.serializer().descriptor -> encodeDuration(value as Duration) | ||
serializer !is AbstractPolymorphicSerializer<*> || hocon.useArrayPolymorphism -> serializer.serialize(this, value) | ||
else -> { | ||
@Suppress("UNCHECKED_CAST") | ||
val casted = serializer as AbstractPolymorphicSerializer<Any> | ||
val actualSerializer = casted.findPolymorphicSerializer(this, value as Any) | ||
writeDiscriminator = true | ||
|
||
actualSerializer.serialize(this, value) | ||
} | ||
} | ||
} | ||
|
||
override fun beginStructure(descriptor: SerialDescriptor): CompositeEncoder { | ||
val consumer = | ||
if (currentTagOrNull == null) valueConsumer | ||
else { value -> encodeTaggedConfigValue(currentTag, value) } | ||
val kind = descriptor.hoconKind(hocon.useArrayPolymorphism) | ||
|
||
return when { | ||
kind.listLike -> HoconConfigListEncoder(hocon, consumer) | ||
kind.objLike -> HoconConfigEncoder(hocon, consumer) | ||
kind == StructureKind.MAP -> HoconConfigMapEncoder(hocon, consumer) | ||
else -> this | ||
}.also { encoder -> | ||
if (writeDiscriminator) { | ||
encoder.encodeTaggedString(hocon.classDiscriminator, descriptor.serialName) | ||
writeDiscriminator = false | ||
} | ||
} | ||
} | ||
|
||
override fun endEncode(descriptor: SerialDescriptor) { | ||
valueConsumer(getCurrent()) | ||
} | ||
|
||
private fun configValueOf(value: Any?) = ConfigValueFactory.fromAnyRef(value) | ||
|
||
private fun encodeDuration(value: Duration) { | ||
val result = value.toComponents { seconds, nanoseconds -> | ||
when { | ||
nanoseconds == 0 -> { | ||
if (seconds % 60 == 0L) { // minutes | ||
if (seconds % 3600 == 0L) { // hours | ||
if (seconds % 86400 == 0L) { // days | ||
"${seconds / 86400} d" | ||
} else { | ||
"${seconds / 3600} h" | ||
} | ||
} else { | ||
"${seconds / 60} m" | ||
} | ||
} else { | ||
"$seconds s" | ||
} | ||
} | ||
nanoseconds % 1_000_000 == 0 -> "${seconds * 1_000 + nanoseconds / 1_000_000} ms" | ||
nanoseconds % 1_000 == 0 -> "${seconds * 1_000_000 + nanoseconds / 1_000} us" | ||
else -> "${value.inWholeNanoseconds} ns" | ||
} | ||
} | ||
encodeString(result) | ||
} | ||
} | ||
|
||
@ExperimentalSerializationApi | ||
internal class HoconConfigEncoder(hocon: Hocon, configConsumer: (ConfigValue) -> Unit) : | ||
AbstractHoconEncoder(hocon, configConsumer) { | ||
|
||
private val configMap = mutableMapOf<String, ConfigValue>() | ||
|
||
override fun encodeTaggedConfigValue(tag: String, value: ConfigValue) { | ||
configMap[tag] = value | ||
} | ||
|
||
override fun getCurrent(): ConfigValue = ConfigValueFactory.fromMap(configMap) | ||
} | ||
|
||
@ExperimentalSerializationApi | ||
internal class HoconConfigListEncoder(hocon: Hocon, configConsumer: (ConfigValue) -> Unit) : | ||
AbstractHoconEncoder(hocon, configConsumer) { | ||
|
||
private val values = mutableListOf<ConfigValue>() | ||
|
||
override fun elementName(descriptor: SerialDescriptor, index: Int): String = index.toString() | ||
|
||
override fun encodeTaggedConfigValue(tag: String, value: ConfigValue) { | ||
values.add(tag.toInt(), value) | ||
} | ||
|
||
override fun getCurrent(): ConfigValue = ConfigValueFactory.fromIterable(values) | ||
} | ||
|
||
import com.typesafe.config.ConfigValue | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
|
||
/** | ||
* Encoder used by Hocon during serialization. | ||
* This interface allows intercepting serialization process and insertion of arbitrary [ConfigValue] into the output. | ||
* | ||
* Usage example (nested config serialization): | ||
* ``` | ||
* @Serializable | ||
* data class Example( | ||
* @Serializable(NestedConfigSerializer::class) | ||
* val d: Config | ||
* ) | ||
* object NestedConfigSerializer : KSerializer<Config> { | ||
* override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("package.Config", PrimitiveKind.STRING) | ||
* | ||
* override fun deserialize(decoder: Decoder): Config = | ||
* if (decoder is HoconDecoder) decoder.decodeConfigValue { conf, path -> conf.getConfig(path) } | ||
* else throw SerializationException("This class can be decoded only by Hocon format") | ||
* | ||
* override fun serialize(encoder: Encoder, value: Config) { | ||
* if (encoder is HoconEncoder) encoder.encodeConfigValue(value.root()) | ||
* else throw SerializationException("This class can be encoded only by Hocon format") | ||
* } | ||
* } | ||
* val nestedConfig = ConfigFactory.parseString("nested { value = \"test\" }") | ||
* val globalConfig = Hocon.encodeToConfig(Example(nestedConfig)) // d: { nested: { value = "test" } } | ||
* val newNestedConfig = Hocon.decodeFromConfig(Example.serializer(), globalConfig) | ||
* ``` | ||
*/ | ||
@ExperimentalSerializationApi | ||
internal class HoconConfigMapEncoder(hocon: Hocon, configConsumer: (ConfigValue) -> Unit) : | ||
AbstractHoconEncoder(hocon, configConsumer) { | ||
|
||
private val configMap = mutableMapOf<String, ConfigValue>() | ||
|
||
private lateinit var key: String | ||
private var isKey: Boolean = true | ||
|
||
override fun encodeTaggedConfigValue(tag: String, value: ConfigValue) { | ||
if (isKey) { | ||
key = when (value.valueType()) { | ||
ConfigValueType.OBJECT, ConfigValueType.LIST -> throw InvalidKeyKindException(value) | ||
else -> value.unwrappedNullable().toString() | ||
} | ||
isKey = false | ||
} else { | ||
configMap[key] = value | ||
isKey = true | ||
} | ||
} | ||
|
||
override fun getCurrent(): ConfigValue = ConfigValueFactory.fromMap(configMap) | ||
|
||
// Without cast to `Any?` Kotlin will assume unwrapped value as non-nullable by default | ||
// and will call `Any.toString()` instead of extension-function `Any?.toString()`. | ||
// We can't cast value in place using `(value.unwrapped() as Any?).toString()` because of warning "No cast needed". | ||
private fun ConfigValue.unwrappedNullable(): Any? = unwrapped() | ||
sealed interface HoconEncoder { | ||
|
||
/** | ||
* Appends the given [ConfigValue] element to the current output. | ||
* | ||
* @param value to insert | ||
*/ | ||
fun encodeConfigValue(value: ConfigValue) | ||
} |
Oops, something went wrong.