Carvel Logo

YAMLFragments

Overview

YAMLFragment is a type of value that is defined directly in YAML (instead of plain Starlark). For example, function val() returns a value of type yamlfragment.

#@ def vals():
key1: val1
key2:
  subkey: val2
#@ end

YAMLFragment may contain:

  • YAML document set (array of YAML documents)
  • YAML array
  • YAML map
  • null

Given various contents it wraps, YAMLFragment currently exposes limited ways of accessing its contents directly. Following accessors are available in v0.26.0+.

YAML Document Set

#@ def docs():
---
doc1
---
doc2
---
doc3
#@ end
  • access contents of a document at particular index
docs()[1] # returns "doc2"
  • loop over each document, setting val to its contents
for val in docs():
  val # ...
end

YAML Array

#@ def vals():
- val1
- val2
#@ end
  • access contents of an array item at particular index
vals()[1] # returns "val2"
  • loop over each array item, setting val to its contents
for val in vals():
  val # ...
end

YAML Map

#@ def vals():
key1: val1
key2:
  subkey: val2
#@ end
  • access contents of a map item with particular key
vals()["key1"] # returns "val1"
  • check if map contains particular key
"key1" in vals() # returns True
"key6" in vals() # returns False
  • loop over each map item, setting val to its contents
for key in vals():
  val = vals()[key] # ...
end
  • convert to a dictionary
dict(**vals()) # returns {"key1": "val1", "key2": yamlfragment({"subkey": "val2"})}

(Help improve our docs: edit this page on GitHub)