Skip to content

Instantly share code, notes, and snippets.

@AndreSteenveld
Last active September 3, 2020 09:54
Show Gist options
  • Save AndreSteenveld/045cbb70e166a54a5dee435e898c51e3 to your computer and use it in GitHub Desktop.
Save AndreSteenveld/045cbb70e166a54a5dee435e898c51e3 to your computer and use it in GitHub Desktop.
More mixing with YTT
version: "1.0"
---
#@ load( "@ytt:overlay", "overlay" )
#@ load( "@ytt:template", "template" )
---
#@ def _mixin_array( path, value ):
#@ if path[ 0 ].endswith( "prefix" ):
#@overlay/match by=overlay.index( 0 )
#@overlay/insert before=True
- #@ value if len( path ) == 1 else _mixin( path[1:], value )
#@ elif path[ 0 ].endswith( "append" ) or path[ 0 ] == "-":
#@overlay/append
- #@ value if len( path ) == 1 else _mixin( path[1:], value )
#@ end
#@ end
---
#@ def _mixin_map( path, value ):
#@ if len( path ) == 0:
_: #@ template.replace( value )
#@ elif len( path ) == 1:
#@yaml/text-templated-strings
(@= path[ 0 ] @): #@ value
#@ else:
#@yaml/text-templated-strings
(@= path[ 0 ] @): #@ _mixin( path[1:], value )
#@ end
#@ end
---
#@ def _mixin( path, value ):
#@ if len( path ) and path[ 0 ].startswith( "-" ):
#@ return _mixin_array( path, value )
#@ else:
#@ return _mixin_map( path, value )
#@ end
#@ end
---
#@ def mixin( path, *values ):
#@ for value in values:
---
#@overlay/match by=overlay.all
#@overlay/match-child-defaults missing_ok=True
--- #@ _mixin( path, value )
#@ end
#@ end
---
---
#@ def fragment( ):
services:
fifth:
property: "with value"
#@ end
---
#@ def more_services( ):
sixth: { }
seventh: { }
#@ end
#@overlay/match by=overlay.all
#@overlay/match-child-defaults missing_ok=True
---
services:
first: {}
second:
replace_me: "bla bla"
third: { }
--- #@ template.replace( mixin( [ "services", "first", "thing" ], "abc" ) )
--- #@ template.replace( mixin( [ "services", "second", "replace_me" ], 123 ) )
--- #@ template.replace( mixin( [ "services", "third", "list", "-" ], 1, 2, 3 ) )
--- #@ template.replace( mixin( [ "services", "third", "list", "-append" ], 4, 5, 6 ) )
--- #@ template.replace( mixin( [ "services", "third", "list", "-prefix" ], 7, 8, 9 ) )
--- #@ template.replace( mixin( [ "services", "forth", "list", "-" ], "a", "b", "c" ) )
--- #@ template.replace( mixin( [ ], fragment( ) ) )
--- #@ template.replace( mixin( [ "services", "fifth", "other" ], "property" ) )
--- #@ template.replace( mixin( [ "services" ], more_services( ) ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment