wrapAsYamlNode function

YamlNode wrapAsYamlNode(
  1. Object? value, {
  2. CollectionStyle collectionStyle = CollectionStyle.ANY,
  3. ScalarStyle scalarStyle = ScalarStyle.ANY,
})

Wraps value into a YamlNode.

Maps, Lists and Scalars will be wrapped as YamlMaps, YamlLists, and YamlScalars respectively. If collectionStyle/scalarStyle is defined, and value is a collection or scalar, the wrapped YamlNode will have the respective style, otherwise it defaults to the ANY style.

If value is a Map or List, then wrapAsYamlNode will be called recursively on all children, and collectionStyle/scalarStyle will be applied to any children that are not instances of YamlNode.

If a YamlNode is passed in, no further wrapping will be done, and the collectionStyle/scalarStyle will not be applied.

Implementation

YamlNode wrapAsYamlNode(
  Object? value, {
  CollectionStyle collectionStyle = CollectionStyle.ANY,
  ScalarStyle scalarStyle = ScalarStyle.ANY,
}) {
  if (value is YamlScalar) {
    assertValidScalar(value.value);
    return value;
  } else if (value is YamlList) {
    for (final item in value.nodes) {
      wrapAsYamlNode(item);
    }

    return value;
  } else if (value is YamlMap) {
    /// Both [entry.key] and [entry.values] are guaranteed to be [YamlNode]s,
    /// so running this will just assert that they are valid scalars.
    for (final entry in value.nodes.entries) {
      wrapAsYamlNode(entry.key);
      wrapAsYamlNode(entry.value);
    }

    return value;
  } else if (value is Map) {
    return YamlMapWrap(
      value,
      collectionStyle: collectionStyle,
      scalarStyle: scalarStyle,
    );
  } else if (value is List) {
    return YamlListWrap(
      value,
      collectionStyle: collectionStyle,
      scalarStyle: scalarStyle,
    );
  } else {
    assertValidScalar(value);

    return YamlScalarWrap(value, style: scalarStyle);
  }
}