ConfigMap constructor

ConfigMap(
  1. String text
)

Parses text into a config map.

The text is usually read from a config file, but can be obtained from other sources too. Since the strict_config config format is a subset of YAML, the text must be valid YAML.

This implementation may accept YAML that is not strict_config config format. This will not cause any problems unless that part of the YAML is examined. This allows the YAML to contain values that are used by other programs, but are not examined when it is used as a strict_config config. This is an implementation specific behaviour that is not guaranteed. But if it changes, it will be a breaking change indicated by incrementing the package's major version number. Currently, it is unlikely to change, since it would require a large amount of code to be written.

Throws a ConfigExceptionFormat if the text has a syntax error that makes it invalid as a strict_config config.

Implementation

factory ConfigMap(String text) {
  try {
    final doc = loadYaml(text);
    if (doc is YamlMap) {
      // text contains a YAML map
      return ConfigMap._fromYamlMap('', doc);
    } else if (doc == null) {
      // no YAML data in text: treat as empty map
      return ConfigMap._fromYamlMap('', YamlMap());
    } else {
      throw ConfigExceptionFormat('top-level of config is not a map');
      // e.g. if the text only contains a single integer: it is valid YAML,
      // but not very useful as a config.
    }
  } on YamlException catch (e) {
    throw ConfigExceptionFormat(e.toString());
  }
}