-
Notifications
You must be signed in to change notification settings - Fork 23
Hocon config companions #562
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
00c88ac
Hocon config companions
sebaciv 35f9d91
Merge remote-tracking branch 'refs/remotes/origin/master' into config…
sebaciv 24c80dc
ConfigCompanion - move codecs to val in object, renames
sebaciv b60fe6b
ConfigCompanion - add java.time config codecs
sebaciv 31369b4
Improve SizeInBytes doc
sebaciv 65c5f75
Skip rendering origin comments in ConfigCodec
sebaciv 0ab4592
Add roundtrip test for custom Hocon codecs
sebaciv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
hocon/src/main/scala/com/avsystem/commons/hocon/ConfigCompanion.scala
This file contains hidden or 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,87 @@ | ||
package com.avsystem.commons | ||
package hocon | ||
|
||
import com.avsystem.commons.meta.MacroInstances | ||
import com.avsystem.commons.serialization.GenCodec.ReadFailure | ||
import com.avsystem.commons.serialization.{GenCodec, GenKeyCodec, GenObjectCodec} | ||
import com.typesafe.config.{Config, ConfigFactory, ConfigObject, ConfigRenderOptions} | ||
|
||
import java.time.{Period, Duration as JDuration} | ||
import scala.concurrent.duration.* | ||
import scala.jdk.javaapi.DurationConverters | ||
|
||
trait HoconGenCodecs { | ||
implicit def configCodec: GenCodec[Config] = HoconGenCodecs.ConfigCodec | ||
implicit def finiteDurationCodec: GenCodec[FiniteDuration] = HoconGenCodecs.FiniteDurationCodec | ||
implicit def jDurationCodec: GenCodec[JDuration] = HoconGenCodecs.JavaDurationCodec | ||
implicit def periodCodec: GenCodec[Period] = HoconGenCodecs.PeriodCodec | ||
implicit def sizeInBytesCodec: GenCodec[SizeInBytes] = HoconGenCodecs.SizeInBytesCodec | ||
implicit def classKeyCodec: GenKeyCodec[Class[?]] = HoconGenCodecs.ClassKeyCodec | ||
implicit def classCodec: GenCodec[Class[?]] = HoconGenCodecs.ClassCodec | ||
} | ||
object HoconGenCodecs { | ||
implicit final val ConfigCodec: GenCodec[Config] = GenCodec.nullable( | ||
input => | ||
input.readCustom(ConfigValueMarker).fold(ConfigFactory.parseString(input.readSimple().readString())) { | ||
case obj: ConfigObject => obj.toConfig | ||
case v => throw new ReadFailure(s"expected a config OBJECT, got ${v.valueType}") | ||
}, | ||
(output, value) => | ||
if (!output.writeCustom(ConfigValueMarker, value.root)) { | ||
val renderOptions = ConfigRenderOptions.defaults().setOriginComments(false) | ||
output.writeSimple().writeString(value.root.render(renderOptions)) | ||
}, | ||
) | ||
|
||
implicit final val FiniteDurationCodec: GenCodec[FiniteDuration] = GenCodec.nullable( | ||
input => input.readCustom(DurationMarker).map(DurationConverters.toScala).getOrElse(input.readSimple().readLong().millis), | ||
(output, value) => if (!output.writeCustom(DurationMarker, DurationConverters.toJava(value))) output.writeSimple().writeLong(value.toMillis), | ||
) | ||
|
||
implicit final val JavaDurationCodec: GenCodec[JDuration] = GenCodec.nullable( | ||
input => input.readCustom(DurationMarker).getOrElse(JDuration.ofMillis(input.readSimple().readLong())), | ||
(output, value) => if (!output.writeCustom(DurationMarker, value)) output.writeSimple().writeLong(value.toMillis), | ||
) | ||
|
||
implicit final val PeriodCodec: GenCodec[Period] = GenCodec.nullable( | ||
input => input.readCustom(PeriodMarker).getOrElse(Period.parse(input.readSimple().readString())), | ||
(output, value) => if (!output.writeCustom(PeriodMarker, value)) output.writeSimple().writeString(value.toString), | ||
) | ||
|
||
implicit final val SizeInBytesCodec: GenCodec[SizeInBytes] = GenCodec.nonNull( | ||
input => SizeInBytes(input.readCustom(SizeInBytesMarker).getOrElse(input.readSimple().readLong())), | ||
(output, value) => if (!output.writeCustom(SizeInBytesMarker, value.bytes)) output.writeSimple().writeLong(value.bytes), | ||
) | ||
|
||
implicit final val ClassKeyCodec: GenKeyCodec[Class[?]] = | ||
GenKeyCodec.create(Class.forName, _.getName) | ||
|
||
implicit final val ClassCodec: GenCodec[Class[?]] = | ||
GenCodec.nullableString(Class.forName, _.getName) | ||
} | ||
|
||
object DefaultHoconGenCodecs extends HoconGenCodecs | ||
|
||
trait ConfigObjectCodec[T] { | ||
def objectCodec: GenObjectCodec[T] | ||
} | ||
|
||
abstract class AbstractConfigCompanion[Implicits <: HoconGenCodecs, T]( | ||
implicits: Implicits | ||
)(implicit instances: MacroInstances[Implicits, ConfigObjectCodec[T]] | ||
) { | ||
implicit lazy val codec: GenCodec[T] = instances(implicits, this).objectCodec | ||
|
||
final def read(config: Config): T = HoconInput.read[T](config) | ||
} | ||
|
||
/** | ||
* Base class for companion objects of configuration case classes and sealed traits | ||
* (typically deserialized from HOCON files). | ||
* | ||
* [[DefaultConfigCompanion]] is equivalent to [[com.avsystem.commons.serialization.HasGenCodec HasGenCodec]] | ||
* except that it automatically imports codecs from [[HoconGenCodecs]] - codecs for third party types often used | ||
* in configuration. | ||
*/ | ||
abstract class DefaultConfigCompanion[T](implicit macroCodec: MacroInstances[HoconGenCodecs, ConfigObjectCodec[T]]) | ||
extends AbstractConfigCompanion[HoconGenCodecs, T](DefaultHoconGenCodecs) |
This file contains hidden or 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
15 changes: 15 additions & 0 deletions
15
hocon/src/main/scala/com/avsystem/commons/hocon/SizeInBytes.scala
This file contains hidden or 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,15 @@ | ||
package com.avsystem.commons | ||
package hocon | ||
|
||
/** | ||
* To read the size in bytes represented in [[https://github.com/lightbend/config/blob/master/HOCON.md#size-in-bytes-format HOCON format]], | ||
* use this type together with [[com.avsystem.commons.hocon.SizeInBytesMarker]] when deserializing data from HOCON. | ||
* | ||
* @see [[com.avsystem.commons.hocon.HoconGenCodecs.SizeInBytesCodec]] | ||
*/ | ||
final case class SizeInBytes(bytes: Long) | ||
object SizeInBytes { | ||
final val Zero = SizeInBytes(0) | ||
final val `1KiB` = SizeInBytes(1024L) | ||
final val `1MiB` = SizeInBytes(1024 * 1024L) | ||
} |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.