|
| 1 | +package com.avsystem.commons |
| 2 | +package hocon |
| 3 | + |
| 4 | +import com.avsystem.commons.meta.MacroInstances |
| 5 | +import com.avsystem.commons.misc.ValueOf |
| 6 | +import com.avsystem.commons.serialization.{GenCodec, GenKeyCodec, GenObjectCodec, HasGenObjectCodecWithDeps} |
| 7 | +import com.avsystem.commons.serialization.GenCodec.ReadFailure |
| 8 | +import com.typesafe.config.{Config, ConfigFactory, ConfigObject} |
| 9 | + |
| 10 | +import scala.concurrent.duration.* |
| 11 | + |
| 12 | +trait CommonsHoconCodecs { |
| 13 | + implicit final val configCodec: GenCodec[Config] = GenCodec.nullable( |
| 14 | + input => |
| 15 | + input.readCustom(ConfigValueMarker).map { |
| 16 | + case obj: ConfigObject => obj.toConfig |
| 17 | + case v => throw new ReadFailure(s"expected a config OBJECT, got ${v.valueType}") |
| 18 | + }.getOrElse { |
| 19 | + ConfigFactory.parseString(input.readSimple().readString()) |
| 20 | + }, |
| 21 | + (output, value) => |
| 22 | + if (!output.writeCustom(ConfigValueMarker, value.root)) { |
| 23 | + output.writeSimple().writeString(value.root.render) |
| 24 | + }, |
| 25 | + ) |
| 26 | + |
| 27 | + implicit final val finiteDurationCodec: GenCodec[FiniteDuration] = GenCodec.nullable( |
| 28 | + input => input.readCustom(DurationMarker).fold(input.readSimple().readLong())(_.toMillis).millis, |
| 29 | + (output, value) => output.writeSimple().writeLong(value.toMillis), |
| 30 | + ) |
| 31 | + |
| 32 | + implicit final val sizeInBytesCodec: GenCodec[SizeInBytes] = GenCodec.nonNull( |
| 33 | + input => SizeInBytes(input.readCustom(SizeInBytesMarker).getOrElse(input.readSimple().readLong())), |
| 34 | + (output, value) => output.writeSimple().writeLong(value.bytes), |
| 35 | + ) |
| 36 | + |
| 37 | + implicit final val classKeyCodec: GenKeyCodec[Class[?]] = |
| 38 | + GenKeyCodec.create(Class.forName, _.getName) |
| 39 | + |
| 40 | + implicit final val classCodec: GenCodec[Class[?]] = |
| 41 | + GenCodec.nullableString(Class.forName, _.getName) |
| 42 | +} |
| 43 | +object CommonsHoconCodecs extends CommonsHoconCodecs |
| 44 | + |
| 45 | +/** |
| 46 | + * Base class for companion objects of configuration case classes and sealed traits |
| 47 | + * (typically deserialized from HOCON files). |
| 48 | + * |
| 49 | + * [[ConfigCompanion]] is equivalent to [[com.avsystem.commons.serialization.HasGenCodec HasGenCodec]] |
| 50 | + * except that it automatically imports codecs from [[CommonsHoconCodecs]] - codecs for third party types often used |
| 51 | + * in configuration. |
| 52 | + */ |
| 53 | +abstract class ConfigCompanion[T](implicit |
| 54 | + macroCodec: MacroInstances[CommonsHoconCodecs.type, () => GenObjectCodec[T]], |
| 55 | +) extends HasGenObjectCodecWithDeps[CommonsHoconCodecs.type, T] { |
| 56 | + final def read(config: Config): T = HoconInput.read[T](config) |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * A version of [[ConfigCompanion]] which injects additional implicits into macro materialization. |
| 61 | + * Implicits are imported from an object specified with type parameter `D`. |
| 62 | + * It must be a singleton object type, i.e. `SomeObject.type`. |
| 63 | + */ |
| 64 | +abstract class ConfigCompanionWithDeps[T, D <: CommonsHoconCodecs](implicit |
| 65 | + deps: ValueOf[D], |
| 66 | + macroCodec: MacroInstances[D, () => GenObjectCodec[T]], |
| 67 | +) extends HasGenObjectCodecWithDeps[D, T] { |
| 68 | + final def read(config: Config): T = HoconInput.read[T](config) |
| 69 | +} |
0 commit comments