-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Path normalization via new
NodeTransformer
interface (#413)
Co-authored-by: Sam <[email protected]>
- Loading branch information
1 parent
6664a4a
commit a269ae5
Showing
32 changed files
with
460 additions
and
168 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
33 changes: 33 additions & 0 deletions
33
hoplite-core/src/main/kotlin/com/sksamuel/hoplite/decoder/AbstractUnnormalizedKeysDecoder.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,33 @@ | ||
package com.sksamuel.hoplite.decoder | ||
|
||
import com.sksamuel.hoplite.ConfigResult | ||
import com.sksamuel.hoplite.DecoderContext | ||
import com.sksamuel.hoplite.MapNode | ||
import com.sksamuel.hoplite.Node | ||
import com.sksamuel.hoplite.transform | ||
import kotlin.reflect.KType | ||
|
||
/** | ||
* A decoder which decodes based on unnormalized keys. | ||
* | ||
* This is useful for decoders that need to know the original key names. | ||
* | ||
* It restores the original key names from the node source key. | ||
*/ | ||
abstract class AbstractUnnormalizedKeysDecoder<T> : NullHandlingDecoder<T> { | ||
override fun safeDecode(node: Node, type: KType, context: DecoderContext): ConfigResult<T> { | ||
val unnormalizedNode = node.transform { | ||
val sourceKey = it.sourceKey | ||
when (it) { | ||
is MapNode -> it.copy(map = it.map.mapKeys { (k, v) -> | ||
(v.sourceKey ?: k).removePrefix("$sourceKey.") | ||
}) | ||
else -> it | ||
} | ||
} | ||
|
||
return safeDecodeUnnormalized(unnormalizedNode, type, context) | ||
} | ||
|
||
abstract fun safeDecodeUnnormalized(node: Node, type: KType, context: DecoderContext): ConfigResult<T> | ||
} |
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
Oops, something went wrong.