-
Notifications
You must be signed in to change notification settings - Fork 612
/
Hocon.kt
301 lines (251 loc) · 12.9 KB
/
Hocon.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* 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 kotlinx.serialization.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*
import kotlinx.serialization.encoding.CompositeDecoder.Companion.DECODE_DONE
import kotlinx.serialization.internal.*
import kotlinx.serialization.modules.*
/**
* Allows [deserialization][decodeFromConfig]
* of [Config] object from popular Lightbend/config library into Kotlin objects.
*
* [Config] object represents "Human-Optimized Config Object Notation" —
* [HOCON][https://github.com/lightbend/config#using-hocon-the-json-superset].
*
* @param [useConfigNamingConvention] switches naming resolution to config naming convention (hyphen separated).
* @param serializersModule A [SerializersModule] which should contain registered serializers
* for [Contextual] and [Polymorphic] serialization, if you have any.
*/
@ExperimentalSerializationApi
public sealed class Hocon(
internal val useConfigNamingConvention: Boolean,
internal val useArrayPolymorphism: Boolean,
internal val classDiscriminator: String,
override val serializersModule: SerializersModule
) : SerialFormat {
@ExperimentalSerializationApi
public fun <T> decodeFromConfig(deserializer: DeserializationStrategy<T>, config: Config): T =
ConfigReader(config).decodeSerializableValue(deserializer)
/**
* The default instance of Hocon parser.
*/
@ExperimentalSerializationApi
public companion object Default : Hocon(false, false, "type", EmptySerializersModule) {
private val NAMING_CONVENTION_REGEX by lazy { "[A-Z]".toRegex() }
}
private abstract inner class ConfigConverter<T> : TaggedDecoder<T>() {
override val serializersModule: SerializersModule
get() = this@Hocon.serializersModule
abstract fun <E> getValueFromTaggedConfig(tag: T, valueResolver: (Config, String) -> E): E
private inline fun <reified E : Any> validateAndCast(tag: T): E {
return try {
when (E::class) {
Number::class -> getValueFromTaggedConfig(tag) { config, path -> config.getNumber(path) } as E
Boolean::class -> getValueFromTaggedConfig(tag) { config, path -> config.getBoolean(path) } as E
String::class -> getValueFromTaggedConfig(tag) { config, path -> config.getString(path) } as E
else -> getValueFromTaggedConfig(tag) { config, path -> config.getAnyRef(path) } as E
}
} catch (e: ConfigException) {
val configOrigin = e.origin()
val requiredType = E::class.simpleName
throw SerializationException("${configOrigin.description()} required to be of type $requiredType")
}
}
private fun getTaggedNumber(tag: T) = validateAndCast<Number>(tag)
override fun decodeTaggedString(tag: T) = validateAndCast<String>(tag)
override fun decodeTaggedBoolean(tag: T) = validateAndCast<Boolean>(tag)
override fun decodeTaggedByte(tag: T): Byte = getTaggedNumber(tag).toByte()
override fun decodeTaggedShort(tag: T): Short = getTaggedNumber(tag).toShort()
override fun decodeTaggedInt(tag: T): Int = getTaggedNumber(tag).toInt()
override fun decodeTaggedLong(tag: T): Long = getTaggedNumber(tag).toLong()
override fun decodeTaggedFloat(tag: T): Float = getTaggedNumber(tag).toFloat()
override fun decodeTaggedDouble(tag: T): Double = getTaggedNumber(tag).toDouble()
override fun decodeTaggedChar(tag: T): Char {
val s = validateAndCast<String>(tag)
if (s.length != 1) throw SerializationException("String \"$s\" is not convertible to Char")
return s[0]
}
override fun decodeTaggedValue(tag: T): Any = getValueFromTaggedConfig(tag) { c, s -> c.getAnyRef(s) }
override fun decodeTaggedNotNullMark(tag: T) = getValueFromTaggedConfig(tag) { c, s -> !c.getIsNull(s) }
override fun decodeTaggedEnum(tag: T, enumDescriptor: SerialDescriptor): Int {
val s = validateAndCast<String>(tag)
return enumDescriptor.getElementIndexOrThrow(s)
}
}
private inner class ConfigReader(val conf: Config) : ConfigConverter<String>() {
private var ind = -1
override fun decodeElementIndex(descriptor: SerialDescriptor): Int {
while (++ind < descriptor.elementsCount) {
val name = descriptor.getTag(ind)
if (conf.hasPathOrNull(name)) {
return ind
}
}
return DECODE_DONE
}
private fun composeName(parentName: String, childName: String) =
if (parentName.isEmpty()) childName else "$parentName.$childName"
override fun SerialDescriptor.getTag(index: Int): String =
composeName(currentTagOrNull ?: "", getConventionElementName(index))
private fun SerialDescriptor.getConventionElementName(index: Int): String {
val originalName = getElementName(index)
return if (!useConfigNamingConvention) originalName
else originalName.replace(NAMING_CONVENTION_REGEX) { "-${it.value.lowercase()}" }
}
override fun decodeNotNullMark(): Boolean {
// Tag might be null for top-level deserialization
val currentTag = currentTagOrNull ?: return !conf.isEmpty
return decodeTaggedNotNullMark(currentTag)
}
override fun <T> decodeSerializableValue(deserializer: DeserializationStrategy<T>): T {
if (deserializer !is AbstractPolymorphicSerializer<*> || useArrayPolymorphism) {
return deserializer.deserialize(this)
}
val config = if (currentTagOrNull != null) conf.getConfig(currentTag) else conf
val reader = ConfigReader(config)
val type = reader.decodeTaggedString(classDiscriminator)
val actualSerializer = deserializer.findPolymorphicSerializerOrNull(reader, type)
?: throwSerializerNotFound(type)
@Suppress("UNCHECKED_CAST")
return (actualSerializer as DeserializationStrategy<T>).deserialize(reader)
}
private fun throwSerializerNotFound(type: String?): Nothing {
val suffix = if (type == null) "missing class discriminator ('null')" else "class discriminator '$type'"
throw SerializationException("Polymorphic serializer was not found for $suffix")
}
override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder {
val kind = when (descriptor.kind) {
is PolymorphicKind -> {
if (useArrayPolymorphism) StructureKind.LIST else StructureKind.MAP
}
else -> descriptor.kind
}
return when {
kind.listLike -> ListConfigReader(conf.getList(currentTag))
kind.objLike -> if (ind > -1) ConfigReader(conf.getConfig(currentTag)) else this
kind == StructureKind.MAP ->
// if current tag is null - map in the root of config
MapConfigReader(if (currentTagOrNull != null) conf.getObject(currentTag) else conf.root())
else -> this
}
}
override fun <E> getValueFromTaggedConfig(tag: String, valueResolver: (Config, String) -> E): E {
return valueResolver(conf, tag)
}
}
private inner class ListConfigReader(private val list: ConfigList) : ConfigConverter<Int>() {
private var ind = -1
override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder =
when {
descriptor.kind.listLike -> ListConfigReader(list[currentTag] as ConfigList)
descriptor.kind.objLike -> ConfigReader((list[currentTag] as ConfigObject).toConfig())
descriptor.kind == StructureKind.MAP -> MapConfigReader(list[currentTag] as ConfigObject)
else -> this
}
override fun SerialDescriptor.getTag(index: Int) = index
override fun decodeElementIndex(descriptor: SerialDescriptor): Int {
ind++
return if (ind > list.size - 1) DECODE_DONE else ind
}
override fun <E> getValueFromTaggedConfig(tag: Int, valueResolver: (Config, String) -> E): E {
val tagString = tag.toString()
val configValue = valueResolver(list[tag].atKey(tagString), tagString)
return configValue
}
}
private inner class MapConfigReader(map: ConfigObject) : ConfigConverter<Int>() {
private var ind = -1
private val keys: List<String>
private val values: List<ConfigValue>
init {
val entries = map.entries.toList() // to fix traversal order
keys = entries.map(MutableMap.MutableEntry<String, ConfigValue>::key)
values = entries.map(MutableMap.MutableEntry<String, ConfigValue>::value)
}
private val indexSize = values.size * 2
override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder =
when {
descriptor.kind.listLike -> ListConfigReader(values[currentTag / 2] as ConfigList)
descriptor.kind.objLike -> ConfigReader((values[currentTag / 2] as ConfigObject).toConfig())
descriptor.kind == StructureKind.MAP -> MapConfigReader(values[currentTag / 2] as ConfigObject)
else -> this
}
override fun SerialDescriptor.getTag(index: Int) = index
override fun decodeElementIndex(descriptor: SerialDescriptor): Int {
ind++
return if (ind >= indexSize) DECODE_DONE else ind
}
override fun <E> getValueFromTaggedConfig(tag: Int, valueResolver: (Config, String) -> E): E {
val idx = tag / 2
val tagString = tag.toString()
val configValue = if (tag % 2 == 0) { // entry as string
ConfigValueFactory.fromAnyRef(keys[idx]).atKey(tagString)
} else {
val configValue = values[idx]
configValue.atKey(tagString)
}
return valueResolver(configValue, tagString)
}
}
private fun SerialDescriptor.getElementIndexOrThrow(name: String): Int {
val index = getElementIndex(name)
if (index == CompositeDecoder.UNKNOWN_NAME)
throw SerializationException("$serialName does not contain element with name '$name'")
return index
}
private val SerialKind.listLike get() = this == StructureKind.LIST || this is PolymorphicKind
private val SerialKind.objLike get() = this == StructureKind.CLASS || this == StructureKind.OBJECT
}
/**
* Decodes the given [config] into a value of type [T] using a deserialize retrieved
* from reified type parameter.
*/
@ExperimentalSerializationApi
public inline fun <reified T> Hocon.decodeFromConfig(config: Config): T =
decodeFromConfig(serializersModule.serializer(), config)
/**
* Creates an instance of [Hocon] configured from the optionally given [Hocon instance][from]
* and adjusted with [builderAction].
*/
@ExperimentalSerializationApi
public fun Hocon(from: Hocon = Hocon, builderAction: HoconBuilder.() -> Unit): Hocon {
val builder = HoconBuilder(from)
builder.builderAction()
return HoconImpl(builder.useConfigNamingConvention, builder.useArrayPolymorphism, builder.classDiscriminator, builder.serializersModule)
}
/**
* Builder of the [Hocon] instance provided by `Hocon` factory function.
*/
@ExperimentalSerializationApi
public class HoconBuilder internal constructor(hocon: Hocon) {
/**
* Module with contextual and polymorphic serializers to be used in the resulting [Hocon] instance.
*/
public var serializersModule: SerializersModule = hocon.serializersModule
/**
* Switches naming resolution to config naming convention: hyphen separated.
*/
public var useConfigNamingConvention: Boolean = hocon.useConfigNamingConvention
/**
* Switches polymorphic serialization to the default array format.
* This is an option for legacy polymorphism format and should not be generally used.
* `false` by default.
*/
public var useArrayPolymorphism: Boolean = hocon.useArrayPolymorphism
/**
* Name of the class descriptor property for polymorphic serialization.
* "type" by default.
*/
public var classDiscriminator: String = hocon.classDiscriminator
}
@OptIn(ExperimentalSerializationApi::class)
private class HoconImpl(
useConfigNamingConvention: Boolean,
useArrayPolymorphism: Boolean,
classDiscriminator: String,
serializersModule: SerializersModule
) : Hocon(useConfigNamingConvention, useArrayPolymorphism, classDiscriminator, serializersModule)