Skip to content

Commit

Permalink
Replace ficus with pureconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
danslapman committed Jan 2, 2025
1 parent cb269db commit d1da899
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 39 deletions.
2 changes: 1 addition & 1 deletion backend/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ val mockingbird = (project in file("mockingbird"))
Dependencies.logback
).flatten,
libraryDependencies ++= Seq(
"com.iheart" %% "ficus" % "1.5.0",
"com.github.pureconfig" %% "pureconfig" % "0.17.8",
"io.circe" %% "circe-config" % "0.8.0",
"com.nrinaudo" %% "kantan.xpath" % "0.5.2",
"com.lihaoyi" %% "scalatags" % "0.9.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package ru.tinkoff.tcb.mockingbird.config

import scala.concurrent.duration.FiniteDuration

import com.github.dwickern.macros.NameOf.*
import com.typesafe.config.Config
import com.typesafe.config.ConfigException.Generic
import com.typesafe.config.ConfigFactory
import enumeratum.*
import net.ceedubs.ficus.Ficus.*
import net.ceedubs.ficus.readers.ArbitraryTypeReader.*
import net.ceedubs.ficus.readers.EnumerationReader.*
import net.ceedubs.ficus.readers.ValueReader
import pureconfig.*
import pureconfig.error.CannotConvert
import pureconfig.generic.ProductHint
import pureconfig.generic.auto.*

final case class JsSandboxConfig(allowedClasses: Set[String] = Set())

Expand All @@ -18,21 +17,23 @@ final case class ServerConfig(
port: Int,
allowedOrigins: Seq[String],
healthCheckRoute: Option[String],
sandbox: JsSandboxConfig,
sandbox: JsSandboxConfig = JsSandboxConfig(),
vertx: Config
)

final case class SecurityConfig(secret: String)

final case class ProxyServerAuth(user: String, password: String)

object ProxyServerType extends Enumeration {
val Http = Value("http")
val Socks = Value("socks")
sealed trait ProxyServerType extends EnumEntry
object ProxyServerType extends Enum[ProxyServerType] with PureconfigEnum[ProxyServerType] {
case object Http extends ProxyServerType
case object Socks extends ProxyServerType
val values = findValues
}

final case class ProxyServerConfig(
`type`: ProxyServerType.Value,
`type`: ProxyServerType,
host: String,
port: Int,
nonProxy: Seq[String] = Seq(),
Expand All @@ -46,12 +47,14 @@ object HttpVersion extends Enum[HttpVersion] {
case object HTTP_1_1 extends HttpVersion
case object HTTP_2 extends HttpVersion

implicit val valueReader: ValueReader[HttpVersion] = ValueReader[String].map(s =>
namesToValuesMap.get(s) match {
case Some(v) => v
case None => throw new Generic(s"Cannot get instance of enum HttpVersion from the value of $s")
implicit val configReader: ConfigReader[HttpVersion] =
ConfigReader.fromString { s =>
namesToValuesMap.get(s) match {
case Some(v) => Right(v)
case None =>
Left(CannotConvert(s, nameOfType[HttpVersion], s"Cannot get instance of enum HttpVersion from the value of $s"))
}
}
)
}

final case class ProxyConfig(
Expand Down Expand Up @@ -96,17 +99,20 @@ final case class MockingbirdConfiguration(
)

object MockingbirdConfiguration {
implicit private def hint[T]: ProductHint[T] =
ProductHint[T](ConfigFieldMapping(CamelCase, CamelCase))

def load(): MockingbirdConfiguration =
load(ConfigFactory.load().getConfig("ru.tinkoff.tcb"))
load(ConfigSource.default.at("ru.tinkoff.tcb"))

def load(config: Config): MockingbirdConfiguration =
def load(config: ConfigSource): MockingbirdConfiguration =
MockingbirdConfiguration(
config.as[ServerConfig]("server"),
config.as[SecurityConfig]("security"),
config.as[MongoConfig]("db.mongo"),
config.as[ProxyConfig]("proxy"),
config.as[EventConfig]("event"),
config.as[TracingConfig]("tracing"),
config.at("server").loadOrThrow[ServerConfig],
config.at("security").loadOrThrow[SecurityConfig],
config.at("db.mongo").loadOrThrow[MongoConfig],
config.at("proxy").loadOrThrow[ProxyConfig],
config.at("event").loadOrThrow[EventConfig],
config.at("tracing").loadOrThrow[TracingConfig],
)

private lazy val conf = load()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ru.tinkoff.tcb.mockingbird.config

import enumeratum.*
import pureconfig.ConfigReader

trait PureconfigEnum[T <: EnumEntry] { self: Enum[T] =>
implicit val vreader: ConfigReader[T] =
ConfigReader[String].map(v => self.withNameInsensitive(v))
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import enumeratum.EnumEntry.Uppercase
import sttp.tapir.codec.enumeratum.TapirCodecEnumeratum

import ru.tinkoff.tcb.bson.enumeratum.BsonEnum
import ru.tinkoff.tcb.mockingbird.config.FicusEnum
import ru.tinkoff.tcb.mockingbird.config.PureconfigEnum

sealed trait HttpMethod extends EnumEntry with Uppercase
object HttpMethod
extends Enum[HttpMethod]
with BsonEnum[HttpMethod]
with TapirCodecEnumeratum
with CirceEnum[HttpMethod]
with FicusEnum[HttpMethod] {
with PureconfigEnum[HttpMethod] {
case object Get extends HttpMethod
case object Post extends HttpMethod
case object Head extends HttpMethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ru.tinkoff.tcb.utils.id

import glass.Equivalent
import io.circe.*
import net.ceedubs.ficus.readers.ValueReader
import pureconfig.ConfigReader
import shapeless.tag
import shapeless.tag.@@
import sttp.tapir.Schema
Expand Down Expand Up @@ -40,5 +40,5 @@ trait IDCompanion[I] {

implicit def idLoggable[T](implicit il: Loggable[I]): Loggable[I @@ T] = il.narrow

implicit def idValueReader[T](implicit vr: ValueReader[I]): ValueReader[I @@ T] = vr.map(apply)
implicit def idConfigReader[T](implicit cr: ConfigReader[I]): ConfigReader[I @@ T] = cr.map(apply)
}

0 comments on commit d1da899

Please sign in to comment.