A pure Starlark YAML parser/serializer for Bazel.
- Parse YAML into native Starlark values (
dict,list, scalars) - Multi-document support (
---/...) - Block and flow styles
- Block scalars (
|/>) including chomping indicators - Anchors, aliases, and mapping merge key (
<<) - Round-trip friendly serializer
- Error collection with optional strict mode
bazel_dep(name = "yaml.bzl", version = "<version>")load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "yaml.bzl",
sha256 = "<sha256>",
strip_prefix = "yaml.bzl-<version>",
url = "https://github.com/scottpledger/yaml.bzl/releases/download/v<version>/yaml.bzl-v<version>.tar.gz",
)load("@yaml.bzl//:yaml.bzl", "yaml")
doc = yaml.parse("""
service:
name: demo
ports: [8080, 8443]
""")
data = yaml.get_value(doc)
print(data["service"]["name"]) # demo
out = yaml.dump(data, sort_keys = True)
print(out)Load:
load("@yaml.bzl//:yaml.bzl", "yaml")Available members on yaml:
parse(yaml_string, strict = False) -> documentparse_all(yaml_string, strict = False) -> [document]dump(value, indent = 0, indent_str = " ", explicit_start = False, explicit_end = False, sort_keys = False, flow_style = False, yaml_version = None, tag_directives = None) -> stringdump_all(values, indent = 0, indent_str = " ", explicit_start = True, explicit_end = False, sort_keys = False, flow_style = False, yaml_version = None, tag_directives = None) -> stringvalidate_dump_directives(yaml_version, tag_directives) -> [string](empty ifdump/dump_allwould accept those arguments)tag(tag, value) -> tagged_nodeanchor(name, value) -> anchored_nodealias(name) -> alias_nodeget_value(document) -> anyhas_errors(document) -> boolget_errors(document) -> [error]is_document(node) -> boolis_mapping(value) -> boolis_sequence(value) -> boolis_scalar(value) -> bool
Error constants:
yaml.ERROR_SYNTAXyaml.ERROR_INDENTATIONyaml.ERROR_UNKNOWN_ALIAS