-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Java property file as a source (#164)
* Make coproduct test consistent * Add support for java properties * Implement java property source * Implement java property source
- Loading branch information
1 parent
a57817f
commit 1ae6062
Showing
3 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
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
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
54 changes: 54 additions & 0 deletions
54
examples/src/main/scala/zio/config/examples/SimpleExampleUsingJavaProperties.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package zio.config.examples | ||
|
||
import zio.App | ||
import zio.config._ | ||
import ConfigDescriptor._ | ||
import zio.ZIO | ||
import zio.console.Console.Live.console._ | ||
|
||
/** | ||
* An example of an entire application that uses java properties | ||
*/ | ||
final case class ApplicationConfig(bridgeIp: String, userName: String) | ||
|
||
object ApplicationConfig { | ||
val configuration = | ||
((string("bridgeIp")) |@| string("username"))(ApplicationConfig.apply, ApplicationConfig.unapply) | ||
} | ||
|
||
// The main App | ||
object SimpleExampleMain extends App { | ||
override def run(args: List[String]): ZIO[zio.ZEnv, Nothing, Int] = { | ||
val pgm = | ||
for { | ||
fileLocation <- ZIO.effect(System.getProperty("user.home") + "/somefile.properties") | ||
// there are many ways of doing this: example: {{{ read(configuration from ConfigSource.fromJavaProperties(propertyFile))) }}}, you may try that as well. | ||
config <- Config.fromPropertyFile(fileLocation, ApplicationConfig.configuration) | ||
_ <- SimpleExample.finalExecution.provide(config) | ||
} yield () | ||
|
||
pgm.foldM( | ||
throwable => putStr(throwable.getMessage()) *> ZIO.succeed(1), | ||
_ => putStrLn("hurray !! Application ran successfully..") *> ZIO.succeed(0) | ||
) | ||
} | ||
} | ||
|
||
// The core application functions | ||
object SimpleExample { | ||
val printConfigs: ZIO[Config[ApplicationConfig], Nothing, Unit] = | ||
for { | ||
appConfig <- config[ApplicationConfig] | ||
_ <- putStrLn(appConfig.bridgeIp) | ||
_ <- putStrLn(appConfig.userName) | ||
} yield () | ||
|
||
val finalExecution: ZIO[Config[ApplicationConfig], Nothing, Unit] = | ||
for { | ||
_ <- printConfigs | ||
_ <- putStrLn(s"processing data......") | ||
} yield () | ||
} | ||
// A note that, with magnolia module (which is still experimental), you can skip writing the {{ configuration }} in ApplicationConfig object | ||
// import zio.config.magnolia.ConfigDescriptorProvider_, | ||
// val configuration = description[ApplicationConfig], and requires case class names as configuration keys |