Last active
January 14, 2017 02:41
-
-
Save friedbrice/dd645ef01ecfd7182d00833cd633c11d to your computer and use it in GitHub Desktop.
Server Config Object (hypothetical implementation and usage)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package local | |
import scalaz.Monoid._ | |
object ServerConfig { | |
case class Port(get: Int = 8080) | |
case class Zookeeper(get: String = "default_zookeeper") | |
case class Environemnt(get: String = "default_env") | |
case class ServerConfig( | |
port: Port = Port(), | |
zookeeper: Zookeeper = Zookeeper(), | |
environment: Environment = Environment(), | |
) | |
implicit object MonoidPort extends Monoid[Port] { | |
def zero: Port = Port() | |
// left-biased overriding | |
// if either argument is the default, then the other argument "wins" | |
// if neither argument is the default, then the left argument "wins" | |
def append(x: Port, y: Port): Port = (x, y) match { | |
case (zero, _) => y | |
case (_, zero) => x | |
case _ => x | |
} | |
} | |
implicit object MonoidZookeeper extends Monoid[Zookeeper] { | |
def zero: Zookepper = Zookeeper() | |
// left-biased overriding | |
def append(x: Zookeeper, y: Zookeeper) = (x, y) match { | |
case (zero, _) => y | |
case (_, zero) => x | |
case _ => x | |
} | |
} | |
implicit object MonoidEnvironment extends Monoid[Environment] { | |
def zero: Environment = Environment("default_env") | |
// left-biased overriding | |
def append(x: Zookeeper, y: Zookeeper) = (x, y) match { | |
case (zero, _) => y | |
case (_, zero) => x | |
case _ => x | |
} | |
} | |
implicit object MonoidServerConfig extends Monoid[ServerConfig] { | |
def zero: ServerConfig = ServerConfig() | |
// left-biased overriding | |
def append(x: ServerConfig, y: ServerConfig) = (x, y) match { | |
(ServerConfig(x1, x2, x3), ServerConfig(y1, y2, y3)) => | |
ServerConfig( | |
append(x1, y1), | |
append(x2, y2), | |
append(x3, y3) | |
) | |
} | |
} | |
} | |
object Usage { | |
import local.ServerConfig._ | |
val commandlineServerConfig = ServerConfig( | |
port = Port(22), | |
environment = Environment("dev") | |
) | |
val oroborosServerConfig = ServerConfig( | |
zookeeper = Zookeeper("something_or_other") | |
) | |
val ansibleServerConfig = ServerConfig( | |
zookeeper = Zookeeper("this_not_that"), | |
port = Port(88) | |
) | |
// cascading configs, giving commandlineServerConfig highest priority. | |
// |+| is a method of `Monoid`, it's just infix `append`. | |
// any args that are unset in any of the configs will be set to default | |
// values | |
val realServerConfig = | |
commandlineServerConfig |+| | |
oroborosServerConfig |+| | |
ansibleServerConfig | |
val server = new Server(realServerConfig) | |
server.run() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment