GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - andr83/scalaconfig: Scala way to work with Typesafe Config

BranchesTags
Open more actions menu

Latest commit

 

History

53 Commits
53 Commits

Folders and files

NameName
Last commit message
Last commit date
project
project
 
 
src
src
 
 
.gitignore
.gitignore
 
 
.travis.yml
.travis.yml
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
build.sbt
build.sbt
 
 

Repository files navigation

scalaconfig

Build Status codecov Maven Central

ScalaConfig is a lightweight wrapper over Typesafe Lightbend Config library provides scala friendly access. It is implemented with type classes pattern and use shapeless for reading case classes.

Current documentation is a actual for 0.7 version.

ScalaConfig adds additional metods:

  • as[A](path) - return Either[Seq[Throwable], A] by path in config object
  • as[A] - convert config object to Either[Seq[Throwable], A]
  • asUnsafe[A](path) - return value of type A by path in config object. On fail a first exception will thrown.
  • asUnsafe[A] - convert config object to value of type A. On fail a first exception will thrown.

Supported types

  • Primitive (Int, Long, Float, Double, Boolean)
  • String, Symbol
  • Typesafe Config and ConfigValue
  • FiniteDuration
  • Properties
  • Collections (List[A], Set[A], Map[String, A], Map[String, AnyRef], Array[A], etc. All types with a CanBuildFrom instance are supported)
  • Option[A]
  • Case classes

Examples

import com.github.andr83.scalaconfig._

val config: Config = ConfigFactory.load()

val host   = config.asUnsafe[String]("host")
val port   = config.asUnsafe[Int]("port")
val path   = config.asUnsafe[Option[String]]("path")
val users  = config.asUnsafe[List[String]]("access.users")

case class DbConfig(host: String, port: Int, user: Option[String] = None, passwd: Option[String] = None)

val dbConfig: Reader.Result[DbConfig]               = config.as[DbConfig]("db")
val dbConfig2: Reader.Result[DbConfig]              = config.as[DbConfig] // Direct `config` mapping to case class
val dbConfig3: Reader.Result[Map[String, String]]   = config.as[Map[String, String]]
val dbConfig3: Reader.Result[Map[String, AnyRef]]]  = config.as[Map[String, AnyRef]]

// Custom reader 
class User(name: String, password: String)

implicit def userReader: Reader[User] = Reader.pure((config: Config, path: String) => {
    val userConfig = config.getConfig(path)
    new User(
      user = userConfig.asUnsafe[String]("name"),
      password = userConfig.asUnsafe[String]("password")
    )
  }
})

// OR
implicit def userReader: Reader[User] = Reader.pureV((config: Config, path: String) => {
    val userConfig = config.getConfig(path)
    
    val userE = userConfig.as[String]("name")
    val passwordE = userConfig.as[String]("password")
    
    // with Cats or Scalaz it can be of course more elegant!
    (userE, passwordE)  match {
      case (Right(user), Right(password)) => Right(new User(user, password))
      case ( Left(errors1), Left(errors2)) => Left(errors1 ++ errors2)
      case (Left(errors), _) => Left(errors)
      case (_, Left(errors)) => Left(errors)
    }
  }
})

Implementation details

https://andr83.io/en/1384/

Usage

Latest release.

// for >= Scala 2.11.x, 2.12.x, 2.13.x
libraryDependencies += "com.github.andr83" %% "scalaconfig" % "0.7"

If you want scala 2.10 support please use 0.4 version.

Develop branch.

resolvers += Resolver.sonatypeRepo("snapshots")

libraryDependencies += "com.github.andr83" %% "scalaconfig" % "0.8-SNAPSHOT"

License

MIT License

Copyright (c) 2016 Andrei Tupitcyn

About

Scala way to work with Typesafe Config

Topics

configscalatypesafe-config

Resources

Readme
MIT license
Activity

Stars

28 stars

Watchers

4 watching

Forks

4 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.