- Notifications
You must be signed in to change notification settings - Fork21
Yet another Typesafe config Scala wrapper powered by circe
License
circe/circe-config
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Small library for translating betweenHOCON,Java properties, and JSONdocuments and circe's JSON AST.
At a high-level it can be used as acirce powered front-end for theTypesafeconfig library to enable boilerplate free loading of settings into Scala types.More generally it provides parsers and printers for interoperating withTypesafe config's JSON AST.
To use this library configure your sbt project with the following line:
libraryDependencies+="io.circe"%%"circe-config"%"0.8.0"
The following examples useio.circe:circe-generic
as a dependency toautomatically derive decoders. They load the configuration found inapplication.conf.
scala>importcom.typesafe.config.{ConfigFactory,ConfigMemorySize }scala>importio.circe.generic.auto._scala>importio.circe.config.syntax._scala>importscala.concurrent.duration.FiniteDurationscala>caseclassServerSettings(host:String,port:Int,timeout:FiniteDuration,maxUpload:ConfigMemorySize)scala>caseclassHttpSettings(server:ServerSettings,version:Option[Double])scala>caseclassAppSettings(http:HttpSettings)// Load default configuration and decode instancesscala>importio.circe.config.parserscala> parser.decode[AppSettings]()res0:Either[io.circe.Error,AppSettings]=Right(AppSettings(HttpSettings(ServerSettings(localhost,8080,5 seconds,ConfigMemorySize(5242880)),Some(1.1))))scala> parser.decodePath[ServerSettings]("http.server")res1:Either[io.circe.Error,ServerSettings]=Right(ServerSettings(localhost,8080,5 seconds,ConfigMemorySize(5242880)))scala>valconfig=ConfigFactory.load()// Decode instances from an already loaded configurationscala> config.as[ServerSettings]("http.server")res2:Either[io.circe.Error,ServerSettings]=Right(ServerSettings(localhost,8080,5 seconds,ConfigMemorySize(5242880)))scala> config.as[HttpSettings]("http")res3:Either[io.circe.Error,HttpSettings]=Right(HttpSettings(ServerSettings(localhost,8080,5 seconds,ConfigMemorySize(5242880)),Some(1.1)))scala> config.as[AppSettings]res4:Either[io.circe.Error,AppSettings]=Right(AppSettings(HttpSettings(ServerSettings(localhost,8080,5 seconds,ConfigMemorySize(5242880)),Some(1.1))))
If you are usingcats.effect.IO
, or some other typeF[_]
that provides acats.ApplicativeError
, you can use the following:
scala>importcats.effect.unsafe.implicits.globalscala>importcats.effect.IOscala>importio.circe.generic.auto._scala>importio.circe.config.parserscala>caseclassServerSettings(host:String,port:Int)scala>caseclassHttpSettings(server:ServerSettings,version:Option[Double])scala>caseclassAppSettings(http:HttpSettings)scala> parser.decodeF[IO,AppSettings]()res0: cats.effect.IO[AppSettings]=IO(AppSettings(HttpSettings(ServerSettings(localhost,8080),Some(1.1))))scala>valsettings:IO[AppSettings]= parser.decodeF[IO,AppSettings]()scala> settings.unsafeRunSync()res1:AppSettings=AppSettings(HttpSettings(ServerSettings(localhost,8080),Some(1.1)))scala> parser.decodePathF[IO,ServerSettings]("http.server")res2: cats.effect.IO[ServerSettings]=IO(ServerSettings(localhost,8080))scala> parser.decodePathF[IO,ServerSettings]("path.not.found").attempt.unsafeRunSync()res3:Either[Throwable,ServerSettings]=Left(io.circe.ParsingFailure:Path not found in config)
This makes the configuration directly available in yourF[_]
, such ascats.effect.IO
, which handles any errors.
Contributions are very welcome. Please seeinstructions onhow to create issues and submit patches.
To release versionx.y.z
run:
> sbt -Dproject.version=x.y.z release
circe-config is licensed under theApache License, Version 2.0 (the"License"); you may not use this software except in compliance with the License.
Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.
About
Yet another Typesafe config Scala wrapper powered by circe