Fix `JSON KEY` not working

This commit is contained in:
Simon Binder 2022-12-17 21:36:24 +01:00
parent 425878b712
commit 0e395bbef3
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
6 changed files with 39 additions and 5 deletions

View File

@ -124,7 +124,7 @@ class WithDefault extends DataClass implements Insertable<WithDefault> {
{ValueSerializer? serializer}) {
serializer ??= driftRuntimeOptions.defaultSerializer;
return WithDefault(
a: serializer.fromJson<String?>(json['a']),
a: serializer.fromJson<String?>(json['customJsonName']),
b: serializer.fromJson<int?>(json['b']),
);
}
@ -137,7 +137,7 @@ class WithDefault extends DataClass implements Insertable<WithDefault> {
Map<String, dynamic> toJson({ValueSerializer? serializer}) {
serializer ??= driftRuntimeOptions.defaultSerializer;
return <String, dynamic>{
'a': serializer.toJson<String?>(a),
'customJsonName': serializer.toJson<String?>(a),
'b': serializer.toJson<int?>(b),
};
}

View File

@ -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
);

View File

@ -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);
});
}

View File

@ -52,6 +52,7 @@ class DriftTableResolver extends LocalElementResolver<DiscoveredDriftTable> {
final constraints = <DriftColumnConstraint>[];
AppliedTypeConverter? converter;
AnnotatedDartCode? defaultArgument;
String? overriddenJsonName;
final typeName = column.definition?.typeName;
@ -102,6 +103,9 @@ class DriftTableResolver extends LocalElementResolver<DiscoveredDriftTable> {
}
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<DiscoveredDriftTable> {
nullable: nullable,
nameInSql: column.name,
nameInDart: overriddenDartName ?? ReCase(column.name).camelCase,
overriddenJsonName: overriddenJsonName,
constraints: constraints,
typeConverter: converter,
defaultArgument: defaultArgument,

View File

@ -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:

View File

@ -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<DriftColumn>().having(
(e) => e.overriddenJsonName, 'overriddenJsonName', 'parentDoc'));
});
}