Files

53 lines
1.3 KiB
Dart
Raw Permalink Normal View History

class FollowUpMessage {
const FollowUpMessage({
required this.id,
required this.seq,
required this.role,
required this.content,
required this.timestamp,
});
final String id;
final int seq;
final String role;
final String content;
final DateTime timestamp;
factory FollowUpMessage.fromJson(Map<String, dynamic> json) {
return FollowUpMessage(
id: _requiredString(json, 'id'),
seq: _requiredInt(json, 'seq'),
role: _requiredString(json, 'role'),
content: _requiredStringAllowEmpty(json, 'content'),
timestamp: DateTime.parse(_requiredString(json, 'timestamp')).toLocal(),
);
}
}
String _requiredString(Map<String, dynamic> json, String key) {
final value = json[key];
if (value is! String || value.isEmpty) {
throw FormatException('Missing required string: $key');
}
return value;
}
String _requiredStringAllowEmpty(Map<String, dynamic> json, String key) {
final value = json[key];
if (value is! String) {
throw FormatException('Missing required string: $key');
}
return value;
}
int _requiredInt(Map<String, dynamic> json, String key) {
final value = json[key];
if (value is int) {
return value;
}
if (value is num) {
return value.toInt();
}
throw FormatException('Missing required int: $key');
}