dartObjectToString function
taken from https://github.com/angel-dart-archive/serialize/blob/be6a3669cca34cd83d189a1169edf6f381101cd8/angel_serialize_generator/lib/angel_serialize_generator.dart#L77 check https://github.com/google/json_serializable.dart/blob/d2fe5141a333e2109fd1511e1520bc13374a63e9/json_serializable/lib/src/json_key_utils.dart#L44
Implementation
String dartObjectToString(DartObject v) {
final type = v.type;
if (v.isNull) return 'null';
if (v.toBoolValue() != null) return v.toBoolValue().toString();
if (v.toIntValue() != null) return v.toIntValue().toString();
if (v.toDoubleValue() != null) return v.toDoubleValue().toString();
if (v.toSymbolValue() != null) return '#' + v.toSymbolValue()!;
if (v.toTypeValue() != null)
return v.toTypeValue()!.getDisplayString(withNullability: true);
if (v.toListValue() != null) {
return 'const [${v.toListValue()!.map(dartObjectToString).join(', ')}]';
}
if (v.toMapValue() != null) {
return 'const {${v.toMapValue()!.entries.map((entry) {
final k = dartObjectToString(entry.key!);
final v = dartObjectToString(entry.value!);
return '$k: $v';
}).join(', ')}}';
}
if (v.toStringValue() != null) {
return literalString(v.toStringValue()!).accept(DartEmitter()).toString();
}
if (type is InterfaceType && type.element.isEnum) {
// Find the index of the enum, then find the member.
for (final field in type.element.fields) {
if (field.isEnumConstant && field.isStatic) {
final value = type.element.getField(field.name)!.computeConstantValue();
if (value == v) {
return '${type.name}.${field.name}';
}
}
}
}
throw ArgumentError(v.toString());
}