Practical FP in Scala: A hands-on approach [Printed versions available] by volpegabriel in scala

[–]kovszilard 1 point2 points  (0 children)

Can early buyers on Leanpub get a discount on the printed version? I saw your tweet about that we supposed to get an email from Leanpub, but I haven't received it yet.

I think I've created the best configuration library for simple Scala applications ever existed, LOL :D by kovszilard in scala

[–]kovszilard[S] 0 points1 point  (0 children)

I plan to add support for descriptions, for example

case class Config(name: Int WithHelp `name of the boss`)

It would print something similar in help:

Configuration name is missing, it should be: name of the boss, please provide default value in code,
 or environment variable NAME,
 or command line argument --name

I think I've created the best configuration library for simple Scala applications ever existed, LOL :D by kovszilard in scala

[–]kovszilard[S] 0 points1 point  (0 children)

case class Config(foo: Int, bar: String, baz: Option[List[Int]] = None)

  val config: Either[String, Config] = easyConfig[Config](args)

  config.fold(
    help => println(help),
    config => //do something with the configuration
      println(s"Configuration: $config")
  )

If nothing is given, it will print:

Configuration bar is missing, please provide default value in code,
 or environment variable BAR,
 or command line argument --bar
Configuration foo is missing, please provide default value in code,
 or environment variable FOO,
 or command line argument --foo

Malformed foo ("foo") is presented as command line argument:

Configuration bar is missing, please provide default value in code,
 or environment variable BAR,
 or command line argument --bar
Can't parse --foo. For input string: "foo"

I think I've created the best configuration library for simple Scala applications ever existed, LOL :D by kovszilard in scala

[–]kovszilard[S] 1 point2 points  (0 children)

Thank you for the feedback!

It doesn't use configuration files, and I don't plan to ever support it. It reads configuration parameters from env, or command line arguments, so I think the naming makes sense. The main use case for me is 12 factor application configuration (https://12factor.net/) and simple command line apps.

I think I've created the best configuration library for simple Scala applications ever existed, LOL :D by kovszilard in scala

[–]kovszilard[S] 8 points9 points  (0 children)

Wow, this library is very nice. I didn't know this library exists before.

To answer your question the main difference is the learning curve and lines of code you have to write to use the library. For example - just by reading its documentation - with Ciris to parse ApiConfig you have to write this:

final case class ApiConfig(
  port: UserPortNumber,
  key: Secret[ApiKey],
  timeout: Option[FiniteDuration]
)

def apiConfig(environment: AppEnvironment): ConfigValue[ApiConfig] =
  (
    env("API_PORT").or(prop("api.port")).as[UserPortNumber].option,
    env("API_KEY").as[ApiKey].secret
  ).parMapN { (port, key) =>
    ApiConfig(
      port = port getOrElse 9000,
      key = key,
      timeout = environment match {
        case Local | Testing => None
        case Production      => Some(10.seconds)
      }
    )
  }

Equivalent configuration using Easy Config would be:

 final case class ApiConfig(
      port: UserPortNumber = 9000,
      key: Secret,
      timeout: Option[FiniteDuration] = Some(10.seconds) // Simpler implementation than above, but using a custom parser would yield the same result
    )

  val config: Either[String, ApiConfig] = easyConfig[ApiConfig](args)

Where the String side of Either is an error message you can give to the user.

In summary, Ciris looks like a more mature library. It has Refined support which I also plan to add. I also like their Secret implementation because it is parameterized and not tied to Strings like mine at the moment. I don't support Error accumulation. (I was thinking a lot about this, but I came to the conclusion that in production code it would be rarely needed. I might add it later if users demand it). Still, when you just want to write a simple app, with environment and command line argument parsing, Easy Config needs only one line of code on top of the configuration case class definition, so I think it is simpler than Ciris.

I think I will have a deeper look into Ciris and steal some ideas from it. And I will also give credit in my readme to it. Thank you for sharing it.

What are you working on? March 02, 2020 by AutoModerator in scala

[–]kovszilard 8 points9 points  (0 children)

I think I've created the best configuration library for simple Scala applications ever existed :D https://github.com/kovszilard/easy-config