-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow two-step parsing and decoding/binding
With this approach, if there are multiple prefixes and config classes to be bound, we parse the configuration only a single time, and decode multiple times.
- Loading branch information
1 parent
f0f6b50
commit d196ce4
Showing
4 changed files
with
160 additions
and
89 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
hoplite-core/src/main/kotlin/com/sksamuel/hoplite/ConfigBinder.kt
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,36 @@ | ||
package com.sksamuel.hoplite | ||
|
||
import com.sksamuel.hoplite.env.Environment | ||
import com.sksamuel.hoplite.internal.ConfigParser | ||
import kotlin.reflect.KClass | ||
|
||
/** | ||
* Maintains a parsed configuration which can be used to bind to multiple configuration classes with a prefix. | ||
*/ | ||
class ConfigBinder( | ||
@PublishedApi internal val configParser: ConfigParser, | ||
@PublishedApi internal val environment: Environment?, | ||
private val onFailure: List<(Throwable) -> Unit> = emptyList(), | ||
) { | ||
/** | ||
* Binds the configuration to a configuration class, looking at with the given prefix. | ||
*/ | ||
inline fun <reified A : Any> bind(prefix: String): ConfigResult<A> = bind(A::class, prefix) | ||
|
||
/** | ||
* Binds the configuration to a configuration class, looking at with the given prefix. | ||
*/ | ||
inline fun <reified A : Any> bindOrThrow(prefix: String): A = bindOrThrow(A::class, prefix) | ||
|
||
/** | ||
* Binds the configuration to a configuration class with the given prefix. | ||
*/ | ||
fun <A : Any> bind(type: KClass<A>, prefix: String): ConfigResult<A> = | ||
configParser.decode(type, environment, prefix) | ||
|
||
/** | ||
* Binds the configuration to a configuration class with the given prefix, or throws if the result is not valid. | ||
*/ | ||
fun <A : Any> bindOrThrow(type: KClass<A>, prefix: String): A = | ||
configParser.decode(type, environment, prefix).returnOrThrow(onFailure) | ||
} |
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