printValue function
Выводит значение в указанном формате.
Поддерживаются следующие форматы:
- OutputFormat.raw - вывести значение как есть
- OutputFormat.json - вывести значение в JSON
- OutputFormat.lines - каждый элемент списка на отдельной строке
- OutputFormat.joined - элементы списка через пробел
- OutputFormat.auto - автоматически выбрать формат
При использовании OutputFormat.auto формат определяется автоматически.
Для List применяется OutputFormat.lines, для Map используется
json формат. Остальные значения выводятся в формате OutputFormat.raw.
Через параметр listPrefix может быть указана строка, вставляемая в начало
элементов списка.
Implementation
void printValue(
Object? value,
OutputFormat outputFormat, {
String? listPrefix,
}) {
if (value == null) {
return;
}
if (outputFormat == OutputFormat.auto) {
if (value is List) {
outputFormat = OutputFormat.lines;
} else if (value is Map) {
outputFormat = OutputFormat.json;
} else {
outputFormat = OutputFormat.raw;
}
}
switch (outputFormat) {
case OutputFormat.raw:
print(value);
return;
case OutputFormat.json:
print(jsonEncode(value));
return;
case OutputFormat.joined:
if (value is! List) {
throw FormatException(
"The 'joined' format requires a YAML list.",
);
}
print(value.map((item) => '${listPrefix ?? ''}$item').join(' '));
return;
case OutputFormat.lines:
if (value is! List) {
throw FormatException("The 'lines' format requires a YAML list.");
}
for (final item in value) {
print('${listPrefix ?? ''}$item');
}
return;
case OutputFormat.auto:
throw StateError('auto format was not resolved.');
}
}