• Stars
    star
    6
  • Rank 2,539,965 (Top 51 %)
  • Language
    Scala
  • Created about 8 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

boilerplate-free typed settings generation in Scala

settler

settler uses macros to generate implementations for your settings traits in Scala. It allows you to have typed configurations for your applications.

It is inspired by owner.

Usage

Given a configuration file like this:

{
  str: "hello world",
  dbs: [
    { key: "k1", value: 123 },
    { key: "k2", value: 321 }
  ]
}

and a piece of code like the following:

import com.unstablebuild.settler._

trait AppSettings {
  def str: String
  def dbs: Set[DBSettings]
  def opt: Option[String]
  def optOrElse: String = opt.getOrElse("default")
}
trait DBSettings {
  def key: String
  def value: Int
}

val settings = Settler.settings[AppSettings](ConfigFactory.parseFile(myFile))

settler will generate the implementation for your trait and allow you to do this:

  println(settings.str)                  // hello world

  settings.dbs.foreach(db =>             // k1: 123
    println(s"${db.key}: ${db.value}"))  // k2: 321

  println(settings.opt)                  // None

  println(settings.optOrElse)            // default

It can be used out-of-the-box with Typesafe's Config or Java Properties. Additionally, you can implement your own ConfigProvider and use whatever source you prefer (custom file formats, databases, Redis, HTTP calls, etc).

Environment Variables Provider

trait Aws {
  @Key(name = "AWS_SECRET_ACCESS_KEY")
  def key: String
  def awsSecretAccessKey: String
}

val s = Settler.settings[Aws](ConfigProvider.fromEnv())

println(s.key == s.awsSecretAccessKey)

Alternative Name

Case necessary, you can modify the key used to retrieve a setting by using the @Key annotation as so:

trait AlternativeName {
  @Key(name = "rightName")
  def wrongName: Int
}

Lazy vs Eagerly Loading

Depending how your flag is declared, it might be lazy or eagerly loaded. As an example consider the following example:

trait Settings {
  val eagerlyLoaded: String
  def lazyLoaded: String
}

Custom Types

settler supports most of the common Scala types, like Int, String, ConfigProvider, Duration, MemorySize, plus these same types wrapped on Seq, Set, and/or Option.

Whenever your trait define types that are unknown to the library, it will try to find an implicit implementation of ConfigParser. ConfigParsers allow the functionallity of the library to be expanded and define custom types.

For example:

trait CustomSettings {
  def customType: Class[_]
}

implicit val classParser = new ConfigParser[Class[_]] {
  override def apply(value: AnyRef): Class[_] = 
    Class.forName(value.toString)
}

val settings = Settler.settings[CustomSettings](
	ConfigFactory.parseString("customType = java.io.File"))

settings.customType == classOf[java.io.File]

A few custom parsers are available by importing com.unstablebuild.settler.parser._.

Config vs Settings

Along the documentation and the code you might see the usage of the words Config and Settings. Although they can broadly be considered synonyms, on the scope of this library Config refers to external libraries, like Typesafe's Config, who provide the mechanism of fetching configuration files and so on. On the other hand, Settings are those same configurations loaded and exposed in a way that makes sense to the application domain.

Install

To use it with SBT, add the following to your build.sbt file:

resolvers += Resolver.sonatypeRepo("public")

libraryDependencies += "com.unstablebuild" %% "settler" % "1.0.0"

Release

./sbt +test +macros/test
./sbt +publishSigned +macros/publishSigned
./sbt sonatypeReleaseAll

Code Format

This project uses Scalafmt for code formatting. This is done with the help of neo-sbt-scalafmt SBT plugin:

./sbt sbt:scalafmt scalafmt test:scalafmt macros/scalafmt macros/test:scalafmt

More Repositories

1

idid

A common interface for different Id types
Scala
14
star
2

coapex

CoAP protocol in Elixir
Elixir
13
star
3

autobreaker

Automatically wrap Scala classes that return Futures with a Circuit Breaker
Scala
9
star
4

SuperMarket

Agile Brazil 2011 presentation content
Java
5
star
5

akka-cluster-examples

A few examples using akka-cluster
Scala
4
star
6

ircbot

simple bot that connects to a IRC room and save the transcripts to a txt file
Scala
4
star
7

draw-diagram

Generate Syntax Diagrams from simplified BNF rules
JavaScript
4
star
8

Mendeley-Importer-for-Safari

A simple button to let you import references and documents to you Mendeley Library with a single click.
JavaScript
4
star
9

s3mp

Simple Serial Slave-Master Protocol (S3MP)
4
star
10

reindxr

automatically (re)index files inside a folder using Apache Lucene
Scala
3
star
11

ditado

Distributed Issue Tracker
Ruby
3
star
12

nginx-route-testing

Test routes declared on your nginx configuration
Python
3
star
13

query-parser

parse advanced search queries using antlr4
JavaScript
2
star
14

filesyncher

Scala
2
star
15

scala-groovy_presentation

JavaScript
2
star
16

TicTacToe

game
Java
2
star
17

scala-jsonr

A very simple library to create JSON strings in Scala
Scala
2
star
18

golang-sks

Simple Key Store
Go
2
star
19

jcmph

Small experiment to crete a Java/Scala API to CMPH (C Minimal Perfect Hashing) Library.
C
2
star
20

code-an

Git/Jira code analyser
Scala
2
star
21

ScalaMines

The Mines game made in Scala
Scala
2
star
22

distributedHappyNumberFinder

Scala
1
star
23

dotfiles-control

Shell
1
star
24

irclog-dist

crazy, but works: it packs irclog, reindxr and ircbot all together
Shell
1
star
25

mapmytweets

Show in a map the tweets of a given account.
Ruby
1
star
26

scala-freenode-search

search the freenode scala channel history
Scala
1
star
27

code-an-gui

JavaScript
1
star
28

elixir-mines

minesweeper in elixir
Elixir
1
star
29

latest-build

JavaScript
1
star
30

pot

Page Object Tester
Ruby
1
star
31

dotfiles

Shell
1
star
32

ReflectionSpike

small experiences with java reflection
Java
1
star
33

theses-search

theses-search
JavaScript
1
star
34

irclog

play application to display content indexed by reindxr
Scala
1
star
35

client-ui

experiments with angularjs, grunt, bower, etc and how can they be applied on our project
JavaScript
1
star
36

moca

a distributed crawler capable of rendering pages with javascript
Scala
1
star
37

view-logs

a silly webserver to return log files in crystal
Crystal
1
star
38

brew-cask-explorer

explore the homepage of packages in homebrew-cask
JavaScript
1
star
39

scala-function-pipeline

A parallel pipeline of Scala functions
Scala
1
star
40

capitalism_with_zmq

ZMQ examples
Ruby
1
star
41

grooveshark-lyrics-safari

safari plugin for http://nettofarah.github.com/grooveshark-lyrics/
JavaScript
1
star
42

dotvim

.vim files
Vim Script
1
star
43

ScalatraInGAE

base project to run scalatra in Google App Engine [need to modify appengine-web.xml]
1
star
44

grooveshark-lyrics

spike to get the lyrics for the current song on grooveshark
JavaScript
1
star
45

rust-bananapi-dht11

A DHT11 driver using Rust for Banana Pi
Rust
1
star
46

textmate_theme

My TextMate theme
1
star
47

1st_Scala

experiences with Scala, ScalaTest and ScalaIDE
Scala
1
star
48

analytics-with-spark

Exercises for the book "Advanced Analytics with Spark"
Shell
1
star
49

mifare-card-reader

Access control using a gumstix board and a Mifare card reader. The access log is accessible through a web interface
C
1
star
50

torri.co

personal website
JavaScript
1
star
51

WebScalaMines

The ScalaMines, but now at your browser
JavaScript
1
star