parseAt method

YamlNode parseAt(
  1. Iterable<Object?> path, {
  2. YamlNode orElse()?,
})

Parses the document to return YamlNode currently present at path.

If no YamlNodes exist at path, the result of invoking the orElse function is returned.

If orElse is omitted, it defaults to throwing a ArgumentError.

To get a default value when path does not point to a value in the YamlNode-tree, simply pass orElse: () => ....

Example: (using orElse)

final myYamlEditor('{"key": "value"}');
final node = myYamlEditor.valueAt(
  ['invalid', 'path'],
  orElse: () => wrapAsYamlNode(null),
);
print(node.value); // null

Example: (common usage)

  final doc = YamlEditor('''
a: 1
b:
  d: 4
  e: [5, 6, 7]
c: 3
''');
print(doc.parseAt(['b', 'e', 2])); // 7

The value returned by parseAt is invalidated when the documented is mutated, as illustrated below:

Example: (old parseAt value is invalidated)

final doc = YamlEditor("YAML: YAML Ain't Markup Language");
final node = doc.parseAt(['YAML']);

print(node.value); // Expected output: "YAML Ain't Markup Language"

doc.update(['YAML'], 'YAML');

final newNode = doc.parseAt(['YAML']);

// Note that the value does not change
print(newNode.value); // "YAML"
print(node.value); // "YAML Ain't Markup Language"

Implementation

YamlNode parseAt(Iterable<Object?> path, {YamlNode Function()? orElse}) {
  return _traverse(path, orElse: orElse);
}