forked from sksamuel/hoplite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support node normalization, add a path normalizer
The purpose of the path normalizer is to normalize all inbound paths by making them lower case, and removing any "-" characters. Normalizing paths means sources with different idiomatic approaches to defining key values will all map correctly to the defined config classes. The only downside to this is multiple config attributes in the same class that differ only by case can no longer be disambiguated. This should be a rare case and the advantages are more than worth losing this "feature". We also add a LowercaseParameterMapper by default which can handle the normalized paths.
- Loading branch information
1 parent
d2c7a6d
commit d911168
Showing
11 changed files
with
176 additions
and
32 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
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
11 changes: 11 additions & 0 deletions
11
hoplite-core/src/main/kotlin/com/sksamuel/hoplite/transformer/NodeTransformer.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,11 @@ | ||
package com.sksamuel.hoplite.transformer | ||
|
||
import com.sksamuel.hoplite.* | ||
|
||
/** | ||
* A [NodeTransformer] is a function that transforms a node into another node. Any type of node transformation can | ||
* be applied at configuration loading time. | ||
*/ | ||
interface NodeTransformer { | ||
fun transform(node: Node): Node | ||
} |
35 changes: 35 additions & 0 deletions
35
hoplite-core/src/main/kotlin/com/sksamuel/hoplite/transformer/PathNormalizer.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,35 @@ | ||
package com.sksamuel.hoplite.transformer | ||
|
||
import com.sksamuel.hoplite.* | ||
|
||
/** | ||
* To support loading configuration from a tree based on multiple sources with different idiomatic conventions, such | ||
* as HOCON which prefers kebab case, and environment variables which are upper-case, the path normalizer normalizes | ||
* all paths so that the cascade happens correctly. For example, a `foo.conf` containing the HOCON standard naming | ||
* of `abc.foo-bar` and an env var `ABC_FOOBAR` would both get mapped to data class `Foo { val fooBar: String }` | ||
* assuming there is a Lowercase parameter mapper present. | ||
* | ||
* Note that with path normalization, parameters with the same name but different case will be considered the same, | ||
* and assigned the same value. This should generally be a situation one should avoid, but if it does happen, please | ||
* consider the use of the @[ConfigAlias] annotation to disambiguate the properties. | ||
* | ||
* Path normalization does the following for all node keys and each element of each node's path: | ||
* * Removes dashes | ||
* * Converts to lower-case | ||
*/ | ||
object PathNormalizer : NodeTransformer { | ||
fun normalizePathElement(element: String): String = element.replace("-", "").lowercase() | ||
|
||
override fun transform(node: Node): Node = node | ||
.transform { | ||
val normalizedPathNode = it.withPath( | ||
it.path.copy(keys = it.path.keys.map { key -> | ||
normalizePathElement(key) | ||
}) | ||
) | ||
when (normalizedPathNode){ | ||
is MapNode -> normalizedPathNode.copy(map = normalizedPathNode.map.mapKeys { (key, _) -> normalizePathElement(key) }) | ||
else -> normalizedPathNode | ||
} | ||
} | ||
} |
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
Oops, something went wrong.