ensureDefaults function
Returns a copy of message with missing fields filled in with proto3
default values according to descriptor.
Fields already present in the map are left unchanged. Fields absent from the map are inserted with their type-appropriate default: 0, 0.0, false, "", empty list, or null.
Implementation
Map<String, Object?> ensureDefaults(
Map<String, Object?> message,
List<Map<String, Object?>> descriptor,
) {
final result = Map<String, Object?>.from(message);
for (final field in descriptor) {
final name = field['name'] as String;
if (result.containsKey(name)) continue;
final type = field['type'] as String;
final label = field['label'] as String?;
final isMap = field['mapEntry'] == true;
if (isMap) {
result[name] = <String, Object?>{};
} else if (label == 'LABEL_REPEATED') {
result[name] = <Object?>[];
} else {
result[name] = _defaultForType(type);
}
}
return result;
}