Dumping YAML Nodes topic
In YAML, you can declare nodes with(out) their properties. This package allows you to declare such a node based on how it subclasses the YamlNode
super class. These include:
DartNode
- a node with no properties. You will rarely need to use this class.CompactYamlNode
- a node with(out) properties.
Dumping nodes with properties
Your object must implement
the CompactYamlNode
interface and define the abstract properties exposed by the class. A compact* node here doesn't mean a node that uses the compact notation (search "compact notation" in YAML spec) but a node that is:
- Light - aliases are not unpacked if an anchor for such an alias exists.
- Dense - contains information that (almost) resembles the initial source string parsed.
* See dictionary definition of compact
.
Your external implementation must adhere to the rules the class specifies. That is:
- If the object provides an
alias
thenanchor
andtag
MUST benull
. - If
anchor
ortag
is provided,alias
MUST be null.
dumpCompactNode
always checks the alias first before dumping the object being aliased. It also uses an opinionated dumping style to guarantee compatibility with a variety of YAML parsers written in other languages such as:
- If a node is a collection (inherits from
Iterable
orMap
) and has atag
oranchor
, it is dumped as a flow node. - Always dumps the node as a directive document with the yaml directive indicating the current parser version the dumper is using as a reference.
- All properties are always inline with the anchor leading followed by the tag. However, do not use to inform your coding decisions.
Tip
You don't need to implement
the CompactYamlNode
interface if the object is a YamlSourceNode
.
Consider the class below.
final class Fancy<T> implements CompactYamlNode {
Fancy(
this.wrapped, {
this.alias,
this.anchor,
this.tag,
NodeStyle? style,
}) : nodeStyle = style ?? NodeStyle.flow;
final T wrapped;
@override
final String? alias;
@override
final String? anchor;
@override
final NodeStyle nodeStyle;
@override
final ResolvedTag? tag;
}
dumpCompactNode
allows you to define a custom unpacking function to prevent the object from being dumped as a Scalar
.
dumpCompactNode(
Fancy(
'fancy',
anchor: 'anchor',
tag: NodeTag(TagShorthand.fromTagUri(TagHandle.primary(), 'tag'), null),
),
nodeUnpacker: (f) => f.wrapped,
),
# Output is a full yaml document
%YAML 1.2
---
&anchor !tag fancy
...
Dumping node without properties
You can call dumpYamlNode
. This is the same as calling dumpSequence
or dumpMapping
or dumpScalar
and thus a CompactYamlNode
cannot define an unpacking function.
Classes
- CompactYamlNode Parsing Nodes Dumping YAML Nodes
- A YamlNode with a set of node properties. This node is not necessarily limited to YAML's compact notation unless such a notation is required when the object is being dumped.
Functions
-
dumpCompactNode<
N extends CompactYamlNode> (N node, {required Object? nodeUnpacker(N node)?, ScalarStyle scalarStyle = ScalarStyle.plain}) → String Dumping YAML Nodes Dumping YAML Documents -
Dumps a
node
with its properties if any are present. Any CompactYamlNode subtype that is not a Mapping, Sequence or Scalar should define anodeUnpacker
function that prevents thenode
from being dumped as a Scalar. -
dumpYamlNode<
N extends YamlNode> (N node, {NodeStyle style = NodeStyle.block, ScalarStyle scalarStyle = ScalarStyle.plain}) → String Dumping YAML Nodes - Dumps a YamlNode to a YAML source string with no properties. This is the classic output for existing YAML dumpers.