mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-28 01:45:55 +08:00
Port Serializers to handle NoteData
This completely breaks the app, as it is part of a large refactor to make Note loading asyncrhnous and not block. Additionally this removed JSON serialization for Notes as that isn't something we care about any more.
This commit is contained in:
@ -1,30 +1,38 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:fimber/fimber.dart';
|
||||
import 'package:journal/note.dart';
|
||||
import 'package:yaml/yaml.dart';
|
||||
|
||||
abstract class NoteSerializer {
|
||||
String encode(Note note);
|
||||
Note decode(String str);
|
||||
class NoteData {
|
||||
String body;
|
||||
Map<String, dynamic> props = {};
|
||||
|
||||
NoteData(this.body, this.props);
|
||||
|
||||
@override
|
||||
int get hashCode => body.hashCode ^ props.hashCode;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
other is NoteData &&
|
||||
runtimeType == other.runtimeType &&
|
||||
body == other.body &&
|
||||
_equalMaps(props, other.props);
|
||||
|
||||
static bool _equalMaps(Map a, Map b) {
|
||||
if (a.length != b.length) return false;
|
||||
return a.keys
|
||||
.every((dynamic key) => b.containsKey(key) && a[key] == b[key]);
|
||||
}
|
||||
}
|
||||
|
||||
class JsonNoteSerializer implements NoteSerializer {
|
||||
@override
|
||||
Note decode(String str) {
|
||||
final json = JsonDecoder().convert(str);
|
||||
return Note.fromJson(json);
|
||||
}
|
||||
|
||||
@override
|
||||
String encode(Note note) {
|
||||
return JsonEncoder().convert(note.toJson());
|
||||
}
|
||||
abstract class NoteSerializer {
|
||||
String encode(NoteData note);
|
||||
NoteData decode(String str);
|
||||
}
|
||||
|
||||
class MarkdownYAMLSerializer implements NoteSerializer {
|
||||
@override
|
||||
Note decode(String str) {
|
||||
NoteData decode(String str) {
|
||||
if (str.startsWith("---\n")) {
|
||||
var parts = str.split("---\n");
|
||||
var map = <String, dynamic>{};
|
||||
@ -41,27 +49,21 @@ class MarkdownYAMLSerializer implements NoteSerializer {
|
||||
Fimber.d(
|
||||
'MarkdownYAMLSerializer::decode("$yamlText") -> ${err.toString()}');
|
||||
}
|
||||
map['body'] = parts[2].trimLeft();
|
||||
var body = parts[2].trimLeft();
|
||||
|
||||
return Note.fromJson(map);
|
||||
return NoteData(body, map);
|
||||
}
|
||||
|
||||
var map = <String, dynamic>{};
|
||||
map['body'] = str;
|
||||
return Note.fromJson(map);
|
||||
return NoteData(str, <String, dynamic>{});
|
||||
}
|
||||
|
||||
@override
|
||||
String encode(Note note) {
|
||||
String encode(NoteData note) {
|
||||
const serparator = '---\n';
|
||||
var str = "";
|
||||
str += serparator;
|
||||
|
||||
var metadata = note.toJson();
|
||||
metadata.remove('body');
|
||||
metadata.remove('filePath');
|
||||
|
||||
str += toYAML(metadata);
|
||||
str += toYAML(note.props);
|
||||
str += serparator;
|
||||
str += '\n';
|
||||
str += note.body;
|
||||
|
@ -1,5 +1,5 @@
|
||||
import 'package:journal/note.dart';
|
||||
import 'package:journal/storage/serializers.dart';
|
||||
import 'package:journal/datetime_utils.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
DateTime nowWithoutMicro() {
|
||||
@ -9,25 +9,14 @@ DateTime nowWithoutMicro() {
|
||||
|
||||
void main() {
|
||||
group('Serializers', () {
|
||||
var note = Note(
|
||||
filePath: "2", body: "This is the body", created: nowWithoutMicro());
|
||||
|
||||
test('JSON Serializer', () {
|
||||
var serializer = JsonNoteSerializer();
|
||||
var str = serializer.encode(note);
|
||||
var note2 = serializer.decode(str);
|
||||
|
||||
expect(note2, note);
|
||||
});
|
||||
var created = toIso8601WithTimezone(nowWithoutMicro());
|
||||
var note = NoteData("This is the body", {"created": created});
|
||||
|
||||
test('Markdown Serializer', () {
|
||||
var serializer = MarkdownYAMLSerializer();
|
||||
var str = serializer.encode(note);
|
||||
var note2 = serializer.decode(str);
|
||||
|
||||
// The YAML seriazlier loses the fileName by design
|
||||
note2.filePath = note.filePath;
|
||||
|
||||
expect(note2, note);
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user