feat: Added passthrough semantics, to support dynamic sections in config#24
Open
svjson wants to merge 1 commit into
Open
feat: Added passthrough semantics, to support dynamic sections in config#24svjson wants to merge 1 commit into
svjson wants to merge 1 commit into
Conversation
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
BACKGROUND
The config shape described by the "defaults" passed to the exported config function doubles as both configuration defaults and the configuration schema.
If no defaults are provided for a path in the config structure, additional properties present in either config file or env will be discarded when configuration is resolved. What seems to be the reason for this in the current implementation is that the default value is used to deduce the value type and perform coercion - ie, a property with a numeric default value will be transformed to Number when encountered as en env variable(which is string only).
This means that all config keys must be known up front. This works for most applications, but complicates things for configurations that require dynamic sections.
WHAT
Added a "passthrough" concept, where a specific path in the configuration structure is marked as "keep everything under this key".
The trade-off is that automatic coercion is not possible for unknown fields.
Defaults can still be specified under a passthrough "section".
This change is backwards compatible, does not affect any current usage and is only activated by explicit usage of passthrough().
HOW
passthrough() / passthrough({ ... }) returns a branded object that is identified during resolution/inspection and activates passthrough semantics when encountered.
EXAMPLE (from README.md)
Passthrough branches
Use
passthrough()when a branch should keep accepting whatever shows up belowthat path instead of using
defaultsas a coercion shape.Below a
passthrough()branch: