summaryrefslogtreecommitdiff
path: root/formats/hocon/src/main/kotlin/kotlinx/serialization/hocon/Hocon.kt
blob: 163e5627a15ea8f6544352db4c7c27cdad75ce89 (plain)
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
 * 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.builtins.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*
import kotlinx.serialization.encoding.CompositeDecoder.Companion.DECODE_DONE
import kotlinx.serialization.hocon.internal.SuppressAnimalSniffer
import kotlinx.serialization.hocon.internal.*
import kotlinx.serialization.hocon.serializers.*
import kotlinx.serialization.internal.*
import kotlinx.serialization.modules.*
import kotlin.time.*

/**
 * 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].
 *
 * [Duration] objects are encoded/decoded using "HOCON duration format" -
 * [Duration format][https://github.com/lightbend/config/blob/main/HOCON.md#duration-format]
 * [Duration] objects encoded using time unit short names: d, h, m, s, ms, us, ns.
 * Encoding use the largest time unit.
 * Example:
 *      120.seconds -> 2 m
 *      121.seconds -> 121 s
 *      120.minutes -> 2 h
 *      122.minutes -> 122 m
 *      24.hours -> 1 d
 * All restrictions on the maximum and minimum duration are specified in [Duration].
 *
 * It is also possible to encode and decode [java.time.Duration] and [com.typesafe.config.ConfigMemorySize]
 * with provided serializers: [JavaDurationSerializer] and [ConfigMemorySizeSerializer].
 * Because these types are not @[Serializable] by default,
 * one has to apply these serializers manually — either via @Serializable(with=...) / @file:UseSerializers
 * or using [Contextual] and [SerializersModule] mechanisms.
 *
 * @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 encodeDefaults: Boolean,
    internal val useConfigNamingConvention: Boolean,
    internal val useArrayPolymorphism: Boolean,
    internal val classDiscriminator: String,
    override val serializersModule: SerializersModule,
) : SerialFormat {

    /**
     * Decodes the given [config] into a value of type [T] using the given serializer.
     */
    @ExperimentalSerializationApi
    public fun <T> decodeFromConfig(deserializer: DeserializationStrategy<T>, config: Config): T =
        ConfigReader(config).decodeSerializableValue(deserializer)

    /**
     * Encodes the given [value] into a [Config] using the given [serializer].
     * @throws SerializationException If list or primitive type passed as a [value].
     */
    @ExperimentalSerializationApi
    public fun <T> encodeToConfig(serializer: SerializationStrategy<T>, value: T): Config {
        lateinit var configValue: ConfigValue
        val encoder = HoconConfigEncoder(this) { configValue = it }
        encoder.encodeSerializableValue(serializer, value)

        if (configValue !is ConfigObject) {
            throw SerializationException(
                "Value of type '${configValue.valueType()}' can't be used at the root of HOCON Config. " +
                        "It should be either object or map."
            )
        }
        return (configValue as ConfigObject).toConfig()
    }

    /**
     * The default instance of Hocon parser.
     */
    @ExperimentalSerializationApi
    public companion object Default : Hocon(false, false, false, "type", EmptySerializersModule())

    private abstract inner class ConfigConverter<T> : TaggedDecoder<T>(), HoconDecoder {
        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()
                throw ConfigValueTypeCastException<E>(configOrigin)
            }
        }

        private fun getTaggedNumber(tag: T) = validateAndCast<Number>(tag)

        @Suppress("UNCHECKED_CAST")
        protected fun <E> decodeDuration(tag: T): E =
            getValueFromTaggedConfig(tag, ::decodeDurationImpl) as E

        @SuppressAnimalSniffer
        private fun decodeDurationImpl(conf: Config, path: String): Duration =
            conf.decodeJavaDuration(path).toKotlinDuration()

        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)
        }

        override fun <E> decodeConfigValue(extractValueAtPath: (Config, String) -> E): E =
            getValueFromTaggedConfig(currentTag, extractValueAtPath)

    }

    private inner class ConfigReader(val conf: Config, private val isPolymorphic: Boolean = false) : 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 {
            val conventionName = getConventionElementName(index, useConfigNamingConvention)
            return if (!isPolymorphic) composeName(currentTagOrNull.orEmpty(), conventionName) else conventionName
        }

        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 {
            return when {
                deserializer.descriptor.isDuration -> decodeDuration(currentTag)
                deserializer !is AbstractPolymorphicSerializer<*> || useArrayPolymorphism -> deserializer.deserialize(this)
                else -> {
                    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)
                        ?: throw SerializerNotFoundException(type)

                    @Suppress("UNCHECKED_CAST")
                    (actualSerializer as DeserializationStrategy<T>).deserialize(reader)
                }
            }
        }

        override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder {
            val kind = descriptor.hoconKind(useArrayPolymorphism)

            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 PolymorphConfigReader(private val conf: Config) : ConfigConverter<String>() {
        private var ind = -1

        override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder =
            when {
                descriptor.kind.objLike -> ConfigReader(conf, isPolymorphic = true)
                else -> this
            }

        override fun SerialDescriptor.getTag(index: Int): String = getElementName(index)

        override fun decodeElementIndex(descriptor: SerialDescriptor): Int {
            ind++
            return if (ind >= descriptor.elementsCount) DECODE_DONE else ind
        }

        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 <T> decodeSerializableValue(deserializer: DeserializationStrategy<T>): T = when {
            deserializer.descriptor.isDuration -> decodeDuration(ind)
            else -> super.decodeSerializableValue(deserializer)
        }

        override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder =
            when {
                descriptor.kind is PolymorphicKind -> PolymorphConfigReader((list[currentTag] as ConfigObject).toConfig())
                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 <T> decodeSerializableValue(deserializer: DeserializationStrategy<T>): T = when {
            deserializer.descriptor.isDuration -> decodeDuration(ind)
            else -> super.decodeSerializableValue(deserializer)
        }

        override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder =
            when {
                descriptor.kind is PolymorphicKind -> PolymorphConfigReader((values[currentTag / 2] as ConfigObject).toConfig())
                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
    }
}

/**
 * Decodes the given [config] into a value of type [T] using a deserializer retrieved
 * from the reified type parameter.
 */
@ExperimentalSerializationApi
public inline fun <reified T> Hocon.decodeFromConfig(config: Config): T =
    decodeFromConfig(serializersModule.serializer(), config)

/**
 * Encodes the given [value] of type [T] into a [Config] using a serializer retrieved
 * from the reified type parameter.
 */
@ExperimentalSerializationApi
public inline fun <reified T> Hocon.encodeToConfig(value: T): Config =
    encodeToConfig(serializersModule.serializer(), value)

/**
 * 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 {
    return HoconImpl(HoconBuilder(from).apply(builderAction))
}

/**
 * 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

    /**
     * Specifies whether default values of Kotlin properties should be encoded.
     * `false` by default.
     */
    public var encodeDefaults: Boolean = hocon.encodeDefaults

    /**
     * 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(hoconBuilder: HoconBuilder) : Hocon(
    encodeDefaults = hoconBuilder.encodeDefaults,
    useConfigNamingConvention = hoconBuilder.useConfigNamingConvention,
    useArrayPolymorphism = hoconBuilder.useArrayPolymorphism,
    classDiscriminator = hoconBuilder.classDiscriminator,
    serializersModule = hoconBuilder.serializersModule
)