From baba0ad1e51d8a0a026db825b1ecf3662233217e Mon Sep 17 00:00:00 2001 From: pdenert Date: Tue, 5 Nov 2024 18:51:37 +0100 Subject: [PATCH] Use sealed class and json serializable --- packages/patrol_log/lib/patrol_log.dart | 6 -- packages/patrol_log/lib/src/entry.dart | 18 +++++- packages/patrol_log/lib/src/entry.g.dart | 66 +++++++++++++++++++++ packages/patrol_log/lib/src/log_entry.dart | 16 ++--- packages/patrol_log/lib/src/step_entry.dart | 26 +++----- packages/patrol_log/lib/src/test_entry.dart | 23 +++---- packages/patrol_log/pubspec.yaml | 3 + 7 files changed, 107 insertions(+), 51 deletions(-) create mode 100644 packages/patrol_log/lib/src/entry.g.dart diff --git a/packages/patrol_log/lib/patrol_log.dart b/packages/patrol_log/lib/patrol_log.dart index 603013cac..6b028e5ea 100644 --- a/packages/patrol_log/lib/patrol_log.dart +++ b/packages/patrol_log/lib/patrol_log.dart @@ -1,10 +1,4 @@ -/// A library for logging patrol entries. -library patrol_log; - export 'src/entry.dart'; -export 'src/log_entry.dart'; export 'src/patrol_log_reader.dart'; export 'src/patrol_log_writer.dart'; export 'src/patrol_single_test_entry.dart'; -export 'src/step_entry.dart'; -export 'src/test_entry.dart'; diff --git a/packages/patrol_log/lib/src/entry.dart b/packages/patrol_log/lib/src/entry.dart index 5047cbe72..47ead19e5 100644 --- a/packages/patrol_log/lib/src/entry.dart +++ b/packages/patrol_log/lib/src/entry.dart @@ -1,4 +1,14 @@ -abstract class Entry { +import 'package:equatable/equatable.dart'; +import 'package:json_annotation/json_annotation.dart'; +import 'package:patrol_log/src/emojis.dart'; + +part 'log_entry.dart'; +part 'step_entry.dart'; +part 'test_entry.dart'; + +part 'entry.g.dart'; + +sealed class Entry with EquatableMixin { Entry({required this.timestamp, required this.type}); // ignore: avoid_unused_constructor_parameters @@ -15,11 +25,17 @@ abstract class Entry { @override String toString() => throw UnimplementedError('toString is not implemented'); + + @override + List get props => [timestamp, type]; } enum EntryType { + @JsonValue('step') step, + @JsonValue('test') test, + @JsonValue('log') log; } diff --git a/packages/patrol_log/lib/src/entry.g.dart b/packages/patrol_log/lib/src/entry.g.dart new file mode 100644 index 000000000..f412a9984 --- /dev/null +++ b/packages/patrol_log/lib/src/entry.g.dart @@ -0,0 +1,66 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'entry.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +LogEntry _$LogEntryFromJson(Map json) => LogEntry( + message: json['message'] as String, + timestamp: json['timestamp'] == null + ? null + : DateTime.parse(json['timestamp'] as String), + ); + +Map _$LogEntryToJson(LogEntry instance) => { + 'timestamp': instance.timestamp.toIso8601String(), + 'message': instance.message, + }; + +StepEntry _$StepEntryFromJson(Map json) => StepEntry( + action: json['action'] as String, + status: $enumDecode(_$StepEntryStatusEnumMap, json['status']), + exception: json['exception'] as String?, + data: json['data'] as Map?, + timestamp: json['timestamp'] == null + ? null + : DateTime.parse(json['timestamp'] as String), + ); + +Map _$StepEntryToJson(StepEntry instance) => { + 'timestamp': instance.timestamp.toIso8601String(), + 'action': instance.action, + 'status': _$StepEntryStatusEnumMap[instance.status]!, + 'exception': instance.exception, + 'data': instance.data, + }; + +const _$StepEntryStatusEnumMap = { + StepEntryStatus.start: 'start', + StepEntryStatus.success: 'success', + StepEntryStatus.failure: 'failure', +}; + +TestEntry _$TestEntryFromJson(Map json) => TestEntry( + name: json['name'] as String, + status: $enumDecode(_$TestEntryStatusEnumMap, json['status']), + timestamp: json['timestamp'] == null + ? null + : DateTime.parse(json['timestamp'] as String), + error: json['error'] as String?, + ); + +Map _$TestEntryToJson(TestEntry instance) => { + 'timestamp': instance.timestamp.toIso8601String(), + 'name': instance.name, + 'status': _$TestEntryStatusEnumMap[instance.status]!, + 'error': instance.error, + }; + +const _$TestEntryStatusEnumMap = { + TestEntryStatus.start: 'start', + TestEntryStatus.success: 'success', + TestEntryStatus.failure: 'failure', + TestEntryStatus.skip: 'skip', +}; diff --git a/packages/patrol_log/lib/src/log_entry.dart b/packages/patrol_log/lib/src/log_entry.dart index 4975263c4..a2e8bcd63 100644 --- a/packages/patrol_log/lib/src/log_entry.dart +++ b/packages/patrol_log/lib/src/log_entry.dart @@ -1,6 +1,6 @@ -import 'package:patrol_log/src/emojis.dart'; -import 'package:patrol_log/src/entry.dart'; +part of 'entry.dart'; +@JsonSerializable(explicitToJson: true) class LogEntry extends Entry { LogEntry({ required this.message, @@ -11,19 +11,13 @@ class LogEntry extends Entry { ); @override - factory LogEntry.fromJson(Map json) => LogEntry( - timestamp: DateTime.parse(json['timestamp'] as String), - message: json['message'] as String, - ); + factory LogEntry.fromJson(Map json) => + _$LogEntryFromJson(json); final String message; @override - Map toJson() => { - 'message': message, - 'timestamp': timestamp.toIso8601String(), - 'type': type.index, - }; + Map toJson() => _$LogEntryToJson(this); @override String pretty() { diff --git a/packages/patrol_log/lib/src/step_entry.dart b/packages/patrol_log/lib/src/step_entry.dart index f04030695..36c16a9da 100644 --- a/packages/patrol_log/lib/src/step_entry.dart +++ b/packages/patrol_log/lib/src/step_entry.dart @@ -1,6 +1,6 @@ -import 'package:patrol_log/src/emojis.dart'; -import 'package:patrol_log/src/entry.dart'; +part of 'entry.dart'; +@JsonSerializable(explicitToJson: true) class StepEntry extends Entry { StepEntry({ required this.action, @@ -13,14 +13,8 @@ class StepEntry extends Entry { type: EntryType.step, ); - @override - factory StepEntry.fromJson(Map json) => StepEntry( - action: json['action'] as String, - timestamp: DateTime.parse(json['timestamp'] as String), - status: StepEntryStatus.values[json['status'] as int], - exception: json['exception'] as String?, - data: json['data'] as Map?, - ); + factory StepEntry.fromJson(Map json) => + _$StepEntryFromJson(json); final String action; final StepEntryStatus status; @@ -28,14 +22,7 @@ class StepEntry extends Entry { final Map? data; @override - Map toJson() => { - 'action': action, - 'status': status.index, - 'type': type.index, - 'timestamp': timestamp.toIso8601String(), - 'exception': exception, - 'data': data, - }; + Map toJson() => _$StepEntryToJson(this); @override String pretty({int? number}) { @@ -60,6 +47,9 @@ class StepEntry extends Entry { @override String toString() => 'StepEntry(${toJson()})'; + + @override + List get props => [action, status, exception, data, timestamp, type]; } enum StepEntryStatus { diff --git a/packages/patrol_log/lib/src/test_entry.dart b/packages/patrol_log/lib/src/test_entry.dart index 923f749f6..a5c7de68f 100644 --- a/packages/patrol_log/lib/src/test_entry.dart +++ b/packages/patrol_log/lib/src/test_entry.dart @@ -1,6 +1,6 @@ -import 'package:patrol_log/src/emojis.dart'; -import 'package:patrol_log/src/entry.dart'; +part of 'entry.dart'; +@JsonSerializable(explicitToJson: true) class TestEntry extends Entry { TestEntry({ required this.name, @@ -13,12 +13,8 @@ class TestEntry extends Entry { ); @override - factory TestEntry.fromJson(Map json) => TestEntry( - timestamp: DateTime.parse(json['timestamp'] as String), - name: json['name'] as String, - status: TestEntryStatus.values[json['status'] as int], - error: json['error'] as String?, - ); + factory TestEntry.fromJson(Map json) => + _$TestEntryFromJson(json); final String name; final TestEntryStatus status; @@ -27,13 +23,7 @@ class TestEntry extends Entry { Duration executionTime(DateTime start) => timestamp.difference(start); @override - Map toJson() => { - 'name': name, - 'status': status.index, - 'type': type.index, - 'timestamp': timestamp.toIso8601String(), - 'error': error, - }; + Map toJson() => _$TestEntryToJson(this); @override String pretty() { @@ -52,6 +42,9 @@ class TestEntry extends Entry { @override String toString() => 'TestEntry(${toJson()})'; + @override + List get props => [name, status, error, timestamp, type]; + /// Returns `true` if the test is finished successfully or with a failure. bool get isFinished => status == TestEntryStatus.success || status == TestEntryStatus.failure; diff --git a/packages/patrol_log/pubspec.yaml b/packages/patrol_log/pubspec.yaml index d788de3b6..f8ab787cc 100644 --- a/packages/patrol_log/pubspec.yaml +++ b/packages/patrol_log/pubspec.yaml @@ -13,12 +13,15 @@ environment: dependencies: dispose_scope: ^2.1.0 + equatable: ^2.0.5 flutter: sdk: flutter + json_annotation: ^4.8.1 dev_dependencies: build_runner: ^2.4.6 fake_async: ^1.3.1 + json_serializable: ^6.8.0 leancode_lint: ^14.2.0 mocktail: ^1.0.1 test: ^1.24.9