Skip to content

Commit

Permalink
add opaque type for env data
Browse files Browse the repository at this point in the history
  • Loading branch information
lbialy committed Oct 2, 2024
1 parent ffbae83 commit 3ef2b44
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
19 changes: 9 additions & 10 deletions besom-cfg/lib/src/main/scala/Configured.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import scala.util.control.NoStackTrace

final val Version = "0.4.0-SNAPSHOT"

implicit val envDefault: Default[Map[String, String]] = new Default[Map[String, String]]:
def default: Map[String, String] = sys.env

// trait Constraint[A]:
// def validate(a: A): Boolean
// def &(other: Constraint[A]): Constraint[A] =
Expand Down Expand Up @@ -53,13 +50,17 @@ trait Configured[A]:
object Configured:

trait FromEnv[A] extends Configured[A]:
type INPUT = Map[String, String]
def schema: Schema
def newInstance(input: INPUT): A
type INPUT = FromEnv.EnvData

object FromEnv:
val MediumIdentifier = "env"

opaque type EnvData >: Map[String, String] = Map[String, String]
extension (env: EnvData) def unwrap: Map[String, String] = env
object EnvData:
given Default[EnvData] = new Default[EnvData]:
def default: EnvData = sys.env

inline def derived[A <: Product]: Configured.FromEnv[A] = ${ derivedImpl[A] }

def derivedImpl[A <: Product: Type](using ctx: Quotes): Expr[Configured.FromEnv[A]] =
Expand Down Expand Up @@ -92,12 +93,10 @@ object Configured:
new Configured.FromEnv[A] {

def schema = $schemaExpr
def newInstance(input: Map[String, String]): A =
$fromEnvExpr.decode(input, from.env.EnvPath.Root) match
def newInstance(input: INPUT): A =
$fromEnvExpr.decode(input.unwrap, from.env.EnvPath.Root) match
case Validated.Valid(a) => a
case Validated.Invalid(errors) => throw ConfigurationError(errors.toVector)

def default(using Default[INPUT]): INPUT = summon[Default[INPUT]].default
}
}
end derivedImpl
Expand Down
10 changes: 6 additions & 4 deletions besom-cfg/lib/src/test/scala/ConfiguredTest.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package besom.cfg

import besom.cfg.Configured.FromEnv

case class Test1(los: List[String]) derives Configured.FromEnv

case class Test2(name: String, int: Int, struct: First, list: List[Double]) derives Configured.FromEnv
Expand Down Expand Up @@ -101,8 +103,8 @@ class ConfiguredTest extends munit.FunSuite:
val env = Map("los.0" -> "test", "los.1" -> "test2").withBesomCfgPrefix

test("resolve configuration - use default") {
given Default[Map[String, String]] = new Default[Map[String, String]]:
def default: Map[String, String] = env
given Default[FromEnv.EnvData] = new Default[FromEnv.EnvData]:
def default: FromEnv.EnvData = env

try
resolveConfiguration[Test1] match
Expand All @@ -124,8 +126,8 @@ class ConfiguredTest extends munit.FunSuite:
}

test("resolve configuration - use default and either variant") {
given Default[Map[String, String]] = new Default[Map[String, String]]:
def default: Map[String, String] = env
given Default[FromEnv.EnvData] = new Default[FromEnv.EnvData]:
def default: FromEnv.EnvData = env

resolveConfigurationEither[Test1] match
case Right(Test1(los)) =>
Expand Down

0 comments on commit 3ef2b44

Please sign in to comment.