checkedYamlDecode<T> function
T
checkedYamlDecode<T>(})
Decodes yamlContent
as YAML and calls constructor
with the resulting
Map.
If there are errors thrown while decoding yamlContent
, if it is not a
Map or if CheckedFromJsonException
is thrown when calling constructor
,
a ParsedYamlException will be thrown.
If sourceUrl
is passed, it's used as the URL from which the YAML
originated for error reporting. It can be a String, a Uri, or null
.
If allowNull
is true
, a null
value from yamlContent
will be allowed
and passed to constructor
. constructor
, therefore, will need to handle
null
values.
Implementation
T checkedYamlDecode<T>(
String yamlContent,
T Function(Map?) constructor, {
Uri? sourceUrl,
bool allowNull = false,
}) {
YamlNode yaml;
try {
yaml = loadYamlNode(yamlContent, sourceUrl: sourceUrl);
} on YamlException catch (e) {
throw ParsedYamlException.fromYamlException(e);
}
Map? map;
if (yaml is YamlMap) {
map = yaml;
} else if (allowNull && yaml is YamlScalar && yaml.value == null) {
// TODO: test this case!
map = null;
} else {
throw ParsedYamlException('Not a map', yaml);
}
try {
return constructor(map);
} on CheckedFromJsonException catch (e) {
throw toParsedYamlException(e);
}
}