/
SimpleLib.scala
64 lines (52 loc) · 2.36 KB
/
SimpleLib.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package simplelib
import com.typesafe.config._
// Whenever you write a library, allow people to supply a Config but
// also default to ConfigFactory.load if they don't supply one.
// Libraries generally have some kind of Context or other object
// where it's convenient to place the configuration.
// we have a constructor allowing the app to provide a custom Config
class SimpleLibContext(config: Config) {
// This verifies that the Config is sane and has our
// reference config. Importantly, we specify the "simple-lib"
// path so we only validate settings that belong to this
// library. Otherwise, we might throw mistaken errors about
// settings we know nothing about.
config.checkValid(ConfigFactory.defaultReference(), "simple-lib")
// This uses the standard default Config, if none is provided,
// which simplifies apps willing to use the defaults
def this() {
this(ConfigFactory.load())
}
// this is the amazing functionality provided by simple-lib
def printSetting(path: String) {
println("The setting '" + path + "' is: " + config.getString(path))
}
}
// Here is an OPTIONAL alternative way to access settings, which
// has the advantage of validating fields on startup and avoiding
// typos. This is redundant with the SimpleLibContext above,
// in fact we'll show a settings-based context below.
class SimpleLibSettings(config: Config) {
// checkValid(), just as in the plain SimpleLibContext
config.checkValid(ConfigFactory.defaultReference(), "simple-lib")
// note that these fields are NOT lazy, because if we're going to
// get any exceptions, we want to get them on startup.
val foo: String = config.getString("simple-lib.foo")
val hello: String = config.getString("simple-lib.hello")
val whatever: String = config.getString("simple-lib.whatever")
}
// This is a different way to do SimpleLibContext, using the
// SimpleLibSettings class to encapsulate and validate your
// settings on startup
class SimpleLibContext2(config: Config) {
val settings = new SimpleLibSettings(config)
def this() {
this(ConfigFactory.load())
}
// this is the amazing functionality provided by simple-lib with a Settings class
def printSettings() {
println("foo=" + settings.foo)
println("hello=" + settings.hello)
println("whatever=" + settings.whatever)
}
}