Skip to content

Instantly share code, notes, and snippets.

@pivotaljohn
Created November 16, 2022 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pivotaljohn/36a9c1163f5109d9df921785524f4f1c to your computer and use it in GitHub Desktop.
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.
---
id: 1
type: book
meta:
format: .mobi
modified: false
---
id: 2
type: book
meta:
format: .epub
modified: true
#@ 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