diff --git a/drift/test/generated/custom_tables.g.dart b/drift/test/generated/custom_tables.g.dart index 18fef15d..6f4e0c36 100644 --- a/drift/test/generated/custom_tables.g.dart +++ b/drift/test/generated/custom_tables.g.dart @@ -124,7 +124,7 @@ class WithDefault extends DataClass implements Insertable { {ValueSerializer? serializer}) { serializer ??= driftRuntimeOptions.defaultSerializer; return WithDefault( - a: serializer.fromJson(json['a']), + a: serializer.fromJson(json['customJsonName']), b: serializer.fromJson(json['b']), ); } @@ -137,7 +137,7 @@ class WithDefault extends DataClass implements Insertable { Map toJson({ValueSerializer? serializer}) { serializer ??= driftRuntimeOptions.defaultSerializer; return { - 'a': serializer.toJson(a), + 'customJsonName': serializer.toJson(a), 'b': serializer.toJson(b), }; } diff --git a/drift/test/generated/tables.drift b/drift/test/generated/tables.drift index a8819a40..97c16772 100644 --- a/drift/test/generated/tables.drift +++ b/drift/test/generated/tables.drift @@ -6,7 +6,7 @@ CREATE TABLE no_ids ( ) WITHOUT ROWID WITH NoIdRow; CREATE TABLE with_defaults ( - a TEXT DEFAULT 'something', + a TEXT JSON KEY customJsonName DEFAULT 'something', b INT UNIQUE ); diff --git a/drift/test/integration_tests/drift_files_test.dart b/drift/test/integration_tests/drift_files_test.dart index e7a29416..fa215c56 100644 --- a/drift/test/integration_tests/drift_files_test.dart +++ b/drift/test/integration_tests/drift_files_test.dart @@ -247,4 +247,12 @@ void main() { '"sync_state" IS NOT NULL AND "sync_state" = ?;', [ConfigTable.$convertersyncState.toSql(SyncType.synchronized)])); }); + + test('respects `JSON KEY` option', () { + const entry = WithDefault(a: 'foo'); + const asJson = {'customJsonName': 'foo', 'b': null}; + + expect(entry.toJson(), asJson); + expect(WithDefault.fromJson(asJson), entry); + }); } diff --git a/drift_dev/lib/src/analysis/resolver/drift/table.dart b/drift_dev/lib/src/analysis/resolver/drift/table.dart index ae7a9a95..59e0a556 100644 --- a/drift_dev/lib/src/analysis/resolver/drift/table.dart +++ b/drift_dev/lib/src/analysis/resolver/drift/table.dart @@ -52,6 +52,7 @@ class DriftTableResolver extends LocalElementResolver { final constraints = []; AppliedTypeConverter? converter; AnnotatedDartCode? defaultArgument; + String? overriddenJsonName; final typeName = column.definition?.typeName; @@ -102,6 +103,9 @@ class DriftTableResolver extends LocalElementResolver { } converter = await _readTypeConverter(type, nullable, constraint); + } else if (constraint is sql.JsonKey) { + writeIntoTable = false; + overriddenJsonName = constraint.jsonKey; } else if (constraint is ForeignKeyColumnConstraint) { // Note: Warnings about whether the referenced column exists or not // are reported later, we just need to know dependencies before the @@ -162,6 +166,7 @@ class DriftTableResolver extends LocalElementResolver { nullable: nullable, nameInSql: column.name, nameInDart: overriddenDartName ?? ReCase(column.name).camelCase, + overriddenJsonName: overriddenJsonName, constraints: constraints, typeConverter: converter, defaultArgument: defaultArgument, diff --git a/drift_dev/lib/src/analysis/serializer.dart b/drift_dev/lib/src/analysis/serializer.dart index e759081c..f2834587 100644 --- a/drift_dev/lib/src/analysis/serializer.dart +++ b/drift_dev/lib/src/analysis/serializer.dart @@ -133,7 +133,7 @@ class ElementSerializer { ], 'queries': element.declaredQueries, if (element is DatabaseAccessor) ...{ - 'type': element.ownType.toJson(), + 'dart_type': element.ownType.toJson(), 'database': element.databaseClass.toJson(), }, if (element is DriftDatabase) ...{ @@ -673,7 +673,7 @@ class ElementDeserializer { declaredIncludes: includes, declaredQueries: queries, databaseClass: AnnotatedDartCode.fromJson(json['database'] as Map), - ownType: AnnotatedDartCode.fromJson(json['type'] as Map), + ownType: AnnotatedDartCode.fromJson(json['dart_type'] as Map), ); } default: diff --git a/drift_dev/test/analysis/resolver/drift/table_test.dart b/drift_dev/test/analysis/resolver/drift/table_test.dart index fbe07f30..1aa29345 100644 --- a/drift_dev/test/analysis/resolver/drift/table_test.dart +++ b/drift_dev/test/analysis/resolver/drift/table_test.dart @@ -199,4 +199,25 @@ CREATE TABLE b ( contains(isDriftError('Not an enum: `NotAnEnum`')), ); }); + + test('supports JSON KEY annotation', () async { + final state = TestBackend.inTest({ + 'a|lib/a.drift': ''' +CREATE TABLE waybills ( + parent INT JSON KEY parentDoc NULL, + id INT NOT NULL, + dataType TEXT NOT NULL +); +''', + }); + + final file = await state.analyze('package:a/a.drift'); + state.expectNoErrors(); + + final table = file.analyzedElements.single as DriftTable; + expect( + table.columnBySqlName['parent'], + isA().having( + (e) => e.overriddenJsonName, 'overriddenJsonName', 'parentDoc')); + }); }