From 19a77fda76d33b0f7d437804fd52d5ccd98b0d7c Mon Sep 17 00:00:00 2001 From: pdenert Date: Wed, 6 Nov 2024 13:46:39 +0100 Subject: [PATCH] Fix - add entry type to generated json --- packages/patrol_log/lib/src/entry.dart | 17 ++++++++++++++--- packages/patrol_log/lib/src/entry.g.dart | 15 +++++++++++++++ packages/patrol_log/lib/src/log_entry.dart | 13 +++++++------ .../patrol_log/lib/src/patrol_log_reader.dart | 5 ++++- packages/patrol_log/lib/src/step_entry.dart | 8 +++----- packages/patrol_log/lib/src/test_entry.dart | 8 +++----- 6 files changed, 46 insertions(+), 20 deletions(-) diff --git a/packages/patrol_log/lib/src/entry.dart b/packages/patrol_log/lib/src/entry.dart index 47ead19e5..256a14005 100644 --- a/packages/patrol_log/lib/src/entry.dart +++ b/packages/patrol_log/lib/src/entry.dart @@ -1,5 +1,6 @@ import 'package:equatable/equatable.dart'; import 'package:json_annotation/json_annotation.dart'; +import 'package:patrol_log/patrol_log.dart'; import 'package:patrol_log/src/emojis.dart'; part 'log_entry.dart'; @@ -31,12 +32,22 @@ sealed class Entry with EquatableMixin { } enum EntryType { - @JsonValue('step') step, - @JsonValue('test') test, - @JsonValue('log') log; + + static EntryType byName(String name) { + switch (name) { + case 'step': + return EntryType.step; + case 'test': + return EntryType.test; + case 'log': + return EntryType.log; + default: + throw ArgumentError('Unknown EntryType: $name'); + } + } } /// The number of spaces used for indentation in the pretty print. diff --git a/packages/patrol_log/lib/src/entry.g.dart b/packages/patrol_log/lib/src/entry.g.dart index f412a9984..537f6e9f1 100644 --- a/packages/patrol_log/lib/src/entry.g.dart +++ b/packages/patrol_log/lib/src/entry.g.dart @@ -11,13 +11,22 @@ LogEntry _$LogEntryFromJson(Map json) => LogEntry( timestamp: json['timestamp'] == null ? null : DateTime.parse(json['timestamp'] as String), + type: $enumDecodeNullable(_$EntryTypeEnumMap, json['type']) ?? + EntryType.log, ); Map _$LogEntryToJson(LogEntry instance) => { 'timestamp': instance.timestamp.toIso8601String(), + 'type': _$EntryTypeEnumMap[instance.type]!, 'message': instance.message, }; +const _$EntryTypeEnumMap = { + EntryType.step: 'step', + EntryType.test: 'test', + EntryType.log: 'log', +}; + StepEntry _$StepEntryFromJson(Map json) => StepEntry( action: json['action'] as String, status: $enumDecode(_$StepEntryStatusEnumMap, json['status']), @@ -26,10 +35,13 @@ StepEntry _$StepEntryFromJson(Map json) => StepEntry( timestamp: json['timestamp'] == null ? null : DateTime.parse(json['timestamp'] as String), + type: $enumDecodeNullable(_$EntryTypeEnumMap, json['type']) ?? + EntryType.step, ); Map _$StepEntryToJson(StepEntry instance) => { 'timestamp': instance.timestamp.toIso8601String(), + 'type': _$EntryTypeEnumMap[instance.type]!, 'action': instance.action, 'status': _$StepEntryStatusEnumMap[instance.status]!, 'exception': instance.exception, @@ -49,10 +61,13 @@ TestEntry _$TestEntryFromJson(Map json) => TestEntry( ? null : DateTime.parse(json['timestamp'] as String), error: json['error'] as String?, + type: $enumDecodeNullable(_$EntryTypeEnumMap, json['type']) ?? + EntryType.test, ); Map _$TestEntryToJson(TestEntry instance) => { 'timestamp': instance.timestamp.toIso8601String(), + 'type': _$EntryTypeEnumMap[instance.type]!, 'name': instance.name, 'status': _$TestEntryStatusEnumMap[instance.status]!, 'error': instance.error, diff --git a/packages/patrol_log/lib/src/log_entry.dart b/packages/patrol_log/lib/src/log_entry.dart index a2e8bcd63..5f4f68c0b 100644 --- a/packages/patrol_log/lib/src/log_entry.dart +++ b/packages/patrol_log/lib/src/log_entry.dart @@ -1,14 +1,12 @@ part of 'entry.dart'; -@JsonSerializable(explicitToJson: true) +@JsonSerializable() class LogEntry extends Entry { LogEntry({ required this.message, DateTime? timestamp, - }) : super( - timestamp: timestamp ?? DateTime.now(), - type: EntryType.log, - ); + super.type = EntryType.log, + }) : super(timestamp: timestamp ?? DateTime.now()); @override factory LogEntry.fromJson(Map json) => @@ -21,9 +19,12 @@ class LogEntry extends Entry { @override String pretty() { - return '$indentation${Emojis.log} $message'; + return '$indentation${Emojis.log} $message'; } @override String toString() => 'LogEntry(${toJson()})'; + + @override + List get props => [message, timestamp, type]; } diff --git a/packages/patrol_log/lib/src/patrol_log_reader.dart b/packages/patrol_log/lib/src/patrol_log_reader.dart index ee1adc8f6..54ddeb17b 100644 --- a/packages/patrol_log/lib/src/patrol_log_reader.dart +++ b/packages/patrol_log/lib/src/patrol_log_reader.dart @@ -112,7 +112,8 @@ class PatrolLogReader { /// Parses patrol log entry from JSON. static Entry parseEntry(String entryJson) { final json = jsonDecode(entryJson) as Map; - final type = EntryType.values[json['type'] as int]; + + final type = EntryType.byName(json['type'] as String); switch (type) { case EntryType.step: return StepEntry.fromJson(json); @@ -120,6 +121,8 @@ class PatrolLogReader { return TestEntry.fromJson(json); case EntryType.log: return LogEntry.fromJson(json); + default: + throw UnimplementedError('Unknown entry type'); } } diff --git a/packages/patrol_log/lib/src/step_entry.dart b/packages/patrol_log/lib/src/step_entry.dart index 36c16a9da..65f3d9d77 100644 --- a/packages/patrol_log/lib/src/step_entry.dart +++ b/packages/patrol_log/lib/src/step_entry.dart @@ -1,6 +1,6 @@ part of 'entry.dart'; -@JsonSerializable(explicitToJson: true) +@JsonSerializable() class StepEntry extends Entry { StepEntry({ required this.action, @@ -8,10 +8,8 @@ class StepEntry extends Entry { this.exception, this.data, DateTime? timestamp, - }) : super( - timestamp: timestamp ?? DateTime.now(), - type: EntryType.step, - ); + super.type = EntryType.step, + }) : super(timestamp: timestamp ?? DateTime.now()); factory StepEntry.fromJson(Map json) => _$StepEntryFromJson(json); diff --git a/packages/patrol_log/lib/src/test_entry.dart b/packages/patrol_log/lib/src/test_entry.dart index 1b222f408..6dd6eda5e 100644 --- a/packages/patrol_log/lib/src/test_entry.dart +++ b/packages/patrol_log/lib/src/test_entry.dart @@ -1,16 +1,14 @@ part of 'entry.dart'; -@JsonSerializable(explicitToJson: true) +@JsonSerializable() class TestEntry extends Entry { TestEntry({ required this.name, required this.status, DateTime? timestamp, this.error, - }) : super( - timestamp: timestamp ?? DateTime.now(), - type: EntryType.test, - ); + super.type = EntryType.test, + }) : super(timestamp: timestamp ?? DateTime.now()); @override factory TestEntry.fromJson(Map json) =>