diff --git a/lib/src/crdt.dart b/lib/src/crdt.dart index ef578f7..43f0295 100644 --- a/lib/src/crdt.dart +++ b/lib/src/crdt.dart @@ -65,8 +65,14 @@ abstract mixin class Crdt { // - Update local canonical time if needed changeset.forEach((table, records) { for (final record in records) { + final recordHlc = switch (record['hlc']) { + final Hlc hlc => hlc, + final String hlc => Hlc.parse(hlc), + _ => throw InvalidHlcError( + 'Invalid hlc: ${record['hlc']}', table, record), + }; try { - hlc = hlc.merge(record['hlc'] as Hlc); + hlc = hlc.merge(recordHlc); } catch (e) { throw MergeError(e, table, record); } @@ -90,6 +96,18 @@ abstract mixin class Crdt { } } +/// Thrown on merge errors when the hlc is invalid or not present. +class InvalidHlcError { + final Object? hlc; + final String table; + final Map record; + + InvalidHlcError(this.hlc, this.table, this.record); + + @override + String toString() => 'Invalid hlc: $hlc.\n$table: $record'; +} + /// Thrown on merge errors. Contains the failed payload to help with debugging /// large datasets. class MergeError {