Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce named mappings to template substitution #726

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

Vigilans
Copy link

@Vigilans Vigilans commented Dec 23, 2024

This PR adds support for named mappings's syntax ${name[key]} in template package.

Named Mappings

By Mapping, we mean a user-supplied function to lookup value from key: type Mapping func(string) (string, bool). In compose it is provied with environment lookup.

A named mappings is a collection of Mapping, indexed by name key. It allows temporarily switching to other mappings other than default during interpolation.

Template Syntax

Named mapping are used with syntax ${name[key]}, where an alternative Mapping is retrieved by key name, and look it up using key key.

It supports all functions in default mappings: ${name[key]:-default}, ${name[key]:+presence}, ${name[key]?error}.

It also supports mapping key to be a templated value, e.g. ${env[DIR_${compose[replica-index]}]}.

Vigilans added 9 commits June 2, 2024 14:08
…ithDefaultWhenAbsence` and `withRequired` functions

* By directly using `Substitute` function, `Config` cannot pass further, thus not preserved

Signed-off-by: Vigilans <[email protected]>
* Named mapping is enabled only when `Config.namedMappings` is provided, otherwise compatibility is kept

Signed-off-by: Vigilans <[email protected]>
* Support formats like `${mapping[key]:-defaultValue}` `${mapping[key]:+presenceValue}`
* Support detecting presence of key in a named mapping through `${mapping[key]?errorMessage}`

Signed-off-by: Vigilans <[email protected]>
* So named mappings can use panic to report error

Signed-off-by: Vigilans <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant