tools icon indicating copy to clipboard operation
tools copied to clipboard

YamlMap.asMap & YamlList.asList

Open jtmcdole opened this issue 7 months ago • 2 comments

Would be nice to have something like the following. We use Yaml for configs-for-humans and json_serializable to handle parsing.

extension YamlMapToMap on YamlMap {
  Map<String, Object?> get asMap => <String, Object?>{
    for (final MapEntry(:key, :value) in entries)
      if (value is YamlMap)
        '$key': value.asMap
      else if (value is YamlList)
        '$key': value.asList
      else
        '$key': value,
  };
}

extension YamlListToList on YamlList {
  List<Object?> get asList => <Object?>[
    for (final value in nodes)
      if (value is YamlMap)
        value.asMap
      else if (value is YamlList)
        value.asList
      else
        value,
  ];
}

jtmcdole avatar May 19 '25 21:05 jtmcdole

That looks like a toJson function.

Seems wasteful to parts parse into Yaml, and then convert it (eagerly) into a simpler format.

lrhn avatar May 23 '25 18:05 lrhn

Our config is in yaml. Yaml is a superset of json. If you told me we had a yaml direct to json (natural dart objects), then I'd switch.

Passing a 'yamlmap' to a json serializable will runtime fail with complex enough yaml.

jtmcdole avatar May 24 '25 00:05 jtmcdole