Skip to content

Commit

Permalink
Fix - add entry type to generated json
Browse files Browse the repository at this point in the history
  • Loading branch information
pdenert committed Nov 6, 2024
1 parent 1bf2600 commit 19a77fd
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 20 deletions.
17 changes: 14 additions & 3 deletions packages/patrol_log/lib/src/entry.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions packages/patrol_log/lib/src/entry.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions packages/patrol_log/lib/src/log_entry.dart
Original file line number Diff line number Diff line change
@@ -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<String, dynamic> json) =>
Expand All @@ -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<Object?> get props => [message, timestamp, type];
}
5 changes: 4 additions & 1 deletion packages/patrol_log/lib/src/patrol_log_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,17 @@ class PatrolLogReader {
/// Parses patrol log entry from JSON.
static Entry parseEntry(String entryJson) {
final json = jsonDecode(entryJson) as Map<String, dynamic>;
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);
case EntryType.test:
return TestEntry.fromJson(json);
case EntryType.log:
return LogEntry.fromJson(json);
default:
throw UnimplementedError('Unknown entry type');
}
}

Expand Down
8 changes: 3 additions & 5 deletions packages/patrol_log/lib/src/step_entry.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
part of 'entry.dart';

@JsonSerializable(explicitToJson: true)
@JsonSerializable()
class StepEntry extends Entry {
StepEntry({
required this.action,
required this.status,
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<String, dynamic> json) =>
_$StepEntryFromJson(json);
Expand Down
8 changes: 3 additions & 5 deletions packages/patrol_log/lib/src/test_entry.dart
Original file line number Diff line number Diff line change
@@ -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<String, dynamic> json) =>
Expand Down

0 comments on commit 19a77fd

Please sign in to comment.