Created
November 16, 2022 21:32
-
-
Save pivotaljohn/36a9c1163f5109d9df921785524f4f1c to your computer and use it in GitHub Desktop.
Rename a key within a map without modifying the rest of its contents.
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
--- | |
id: 1 | |
type: book | |
meta: | |
format: .mobi | |
modified: false | |
--- | |
id: 2 | |
type: book | |
meta: | |
format: .epub | |
modified: true |
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
#@ load("@ytt:overlay", "overlay") | |
#@yaml/text-templated-strings | |
--- | |
#@ def rename_key(orig, old_key, new_key): | |
#@overlay/remove | |
(@= old_key @): null | |
#@overlay/match missing_ok=True | |
(@= new_key @): #@ orig[old_key] | |
#@ end | |
#@overlay/match by=overlay.all, expects="1+" | |
--- | |
#@overlay/replace via=lambda left, right: overlay.apply(left, rename_key(left, "format", "ebook")) | |
meta: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment