Skip to content

Instantly share code, notes, and snippets.

@pivotaljohn
Last active August 1, 2022 22:26
Show Gist options
  • Save pivotaljohn/ca2283673ffd8d1b71bb425a7f9770c0 to your computer and use it in GitHub Desktop.
Save pivotaljohn/ca2283673ffd8d1b71bb425a7f9770c0 to your computer and use it in GitHub Desktop.
From a dot-path notation to a YAML structure.
#@ load("@ytt:yaml", "yaml")
#@ load("@ytt:regexp", "regexp")
#@ load("@ytt:overlay", "overlay")
#@ def as_dict(parts, leaf):
#@ if len(parts) == 0:
#@ return leaf
#@ end
#@ return {parts[0]: as_dict(parts[1:], leaf)}
#@ end
#@ def split_dot_path(path):
#@ # "replace any dot that does NOT have a leading slash with ':::'
#@ path = regexp.replace("([^\\\])\.", path, "$1:::")
#@ path = path.replace("\.", ".") # consume escaping of '.'
#@ return path.split(":::")
#@ end
#! original config: want to preserve all this...
---
config:
my_app:
other_stuff: 123
more_stuff: true
#! merge in `image` at the location specified in `path`.
#@ image = "demo.image.url/example/test:1.0.0"
#@ path = "my_app.awesome.custom\.image.field"
#@ custom_config = as_dict(split_dot_path(path), image)
#@overlay/match by=overlay.subset({"config":{}})
---
#@overlay/match-child-defaults missing_ok=True
config: #@ custom_config
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment