-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
iss #24: config parse rules & helpers
- Loading branch information
Showing
13 changed files
with
298 additions
and
58 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
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
File renamed without changes.
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
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
23 changes: 23 additions & 0 deletions
23
core/src/main/scala/ru/maizy/ambient7/core/config/helper/ConfigRuleOps.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,23 @@ | ||
package ru.maizy.ambient7.core.config.helper | ||
|
||
/** | ||
* Copyright (c) Nikita Kovaliov, maizy.ru, 2017 | ||
* See LICENSE.txt for details. | ||
*/ | ||
|
||
import ru.maizy.ambient7.core.config.{ Ambient7Options, ParsingError, UniversalConfigReader } | ||
|
||
object ConfigRuleOps { | ||
|
||
import UniversalConfigReader._ | ||
|
||
implicit class IfSuccessOp[T](configRes: configs.Result[T]) { | ||
|
||
def ifSuccess(saveValue: T => Ambient7Options): ParseResult = { | ||
configRes match { | ||
case configs.Result.Failure(error) => Left(ParsingError.withMessages(error.messages)) | ||
case configs.Result.Success(value) => Right(saveValue(value)) | ||
} | ||
} | ||
} | ||
} |
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,4 @@ | ||
sample { | ||
string = "abcd" | ||
array = ["a", "b"] | ||
} |
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
78 changes: 78 additions & 0 deletions
78
core/src/test/scala/ru/maizy/ambient7/core/tests/config/SampleReaders.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,78 @@ | ||
package ru.maizy.ambient7.core.tests.config | ||
|
||
/** | ||
* Copyright (c) Nikita Kovaliov, maizy.ru, 2017 | ||
* See LICENSE.txt for details. | ||
*/ | ||
|
||
import ru.maizy.ambient7.core.config.{ DbOptions, UniversalConfigReader } | ||
|
||
trait SampleReaders { | ||
|
||
class ReaderWithSimpleOpts extends UniversalConfigReader { | ||
override def appName: String = "test app" | ||
|
||
override def fillReader(): Unit = { | ||
cliParser.opt[String]("option") | ||
.abbr("o") | ||
.action { (value, opts) => opts.copy(mainDb = Some(DbOptions(url = Some(value)))) } | ||
|
||
() | ||
} | ||
} | ||
|
||
class ReaderWithConfig(requireConfig: Boolean) extends UniversalConfigReader { | ||
override def appName: String = "test app" | ||
|
||
override def fillReader(): Unit = { | ||
fillConfigOptions(requireConfig) | ||
cliParser.opt[String]("option") | ||
.abbr("o") | ||
.action { (value, opts) => opts.copy(mainDb = Some(DbOptions(url = Some(value)))) } | ||
|
||
() | ||
} | ||
} | ||
|
||
class ReaderWithConfigRule(requireConfig: Boolean) extends UniversalConfigReader { | ||
override def appName: String = "test app" | ||
override def fillReader(): Unit = { | ||
fillConfigOptions(requireConfig) | ||
appendConfigRule { (config, opts) => | ||
Right(opts.copy(mainDb = Some(DbOptions(url=Some(config.getString("sample.string")))))) | ||
} | ||
() | ||
} | ||
} | ||
|
||
class ReaderWithSimpleConfigRule(path: String) extends UniversalConfigReader { | ||
override def appName: String = "test app" | ||
override def fillReader(): Unit = { | ||
fillConfigOptions(requireConfig = true) | ||
appendSimpleConfigRule[String](path) { (value, opts) => | ||
opts.copy(mainDb = Some(DbOptions(url = Some(value)))) | ||
} | ||
() | ||
} | ||
} | ||
|
||
class ReaderWithSimpleOptionalConfigRule(path: String) extends UniversalConfigReader { | ||
override def appName: String = "test app" | ||
override def fillReader(): Unit = { | ||
fillConfigOptions(requireConfig = true) | ||
appendSimpleOptionalConfigRule[String](path) { (value, opts) => | ||
opts.copy(mainDb = Some(DbOptions(url = Some(value)))) | ||
} | ||
() | ||
} | ||
} | ||
|
||
class ReaderConfigRuleAndCliOption extends ReaderWithConfigRule(requireConfig = true) { | ||
override def fillReader(): Unit = { | ||
super.fillReader() | ||
cliParser.opt[String]("some") | ||
.action { (value, opts) => opts.copy(mainDb = Some(DbOptions(url = Some(value)))) } | ||
() | ||
} | ||
} | ||
} |
Oops, something went wrong.