Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sqlite) : nullable maps are not cast to default values #531 #532

Merged
merged 3 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ Future<Map<String, dynamic>> _$PrimitiveFieldsToSqlite(PrimitiveFields instance,
'nullable_set': instance.nullableSet == null
? null
: jsonEncode(instance.nullableSet.toList()),
'nullable_map': jsonEncode(instance.nullableMap ?? {}),
'nullable_map':
instance.nullableMap != null ? jsonEncode(instance.nullableMap) : null,
'nullable_longer_camelized_variable':
instance.nullableLongerCamelizedVariable,
'nullable_casing': instance.nullableCasing != null
Expand Down
4 changes: 4 additions & 0 deletions packages/brick_sqlite/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Unreleased

## 3.2.2

- Fix: nullable maps are not cast to a default value on serialization (#531)

## 3.2.1

- Remove `dart:io` dependency
Expand Down
2 changes: 1 addition & 1 deletion packages/brick_sqlite/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ homepage: https://github.com/GetDutchie/brick/tree/main/packages/brick_sqlite
issue_tracker: https://github.com/GetDutchie/brick/issues
repository: https://github.com/GetDutchie/brick

version: 3.2.1
version: 3.2.2

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,10 @@ class SqliteSerialize<_Model extends SqliteModel> extends SqliteSerdesGenerator<

// Map
} else if (checker.isMap) {
final nullableSuffix = checker.isNullable ? ' ?? {}' : '';
return 'jsonEncode($fieldValue$nullableSuffix)';
if (checker.isNullable) {
return '$fieldValue != null ? jsonEncode($fieldValue) : null';
}
return 'jsonEncode($fieldValue)';
} else if (checker.toJsonMethod != null) {
final nullableSuffix = checker.isNullable ? '!' : '';
final output = 'jsonEncode($fieldValue$nullableSuffix.toJson())';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ Future<AllFieldTypes> _$AllFieldTypesFromSqlite(Map<String, dynamic> data,
.whereType<Casing>()
.toList()
.cast<Casing>(),
longerCamelizedVariable: data['longer_camelized_variable'] == null
? null
: data['longer_camelized_variable'] as String?,
map: jsonDecode(data['map']),
nullableList: data['nullable_list'] == null
? null
: jsonDecode(data['nullable_list']).toList().cast<int>(),
longerCamelizedVariable: data['longer_camelized_variable'] == null
nullableMap: data['nullable_map'] == null
? null
: data['longer_camelized_variable'] as String?,
: jsonDecode(data['nullable_map']),
string: data['string'] == null ? null : data['string'] as String?,
stringSet: jsonDecode(data['string_set']).toSet().cast<String>())
..primaryKey = data['_brick_id'] as int;
Expand All @@ -46,10 +50,13 @@ Future<Map<String, dynamic>> _$AllFieldTypesToSqlite(AllFieldTypes instance,
: null,
'enum_list': jsonEncode(
instance.enumList.map((s) => Casing.values.indexOf(s)).toList()),
'longer_camelized_variable': instance.longerCamelizedVariable,
'map': jsonEncode(instance.map),
'nullable_list': instance.nullableList == null
? null
: jsonEncode(instance.nullableList),
'longer_camelized_variable': instance.longerCamelizedVariable,
'nullable_map':
instance.nullableMap != null ? jsonEncode(instance.nullableMap) : null,
'string': instance.string,
'string_set': jsonEncode(instance.stringSet.toList())
};
Expand Down Expand Up @@ -97,17 +104,29 @@ class AllFieldTypesAdapter extends SqliteAdapter<AllFieldTypes> {
iterable: true,
type: Casing,
),
'longerCamelizedVariable': const RuntimeSqliteColumnDefinition(
association: false,
columnName: 'longer_camelized_variable',
iterable: false,
type: String,
),
'map': const RuntimeSqliteColumnDefinition(
association: false,
columnName: 'map',
iterable: false,
type: Map,
),
'nullableList': const RuntimeSqliteColumnDefinition(
association: false,
columnName: 'nullable_list',
iterable: true,
type: int,
),
'longerCamelizedVariable': const RuntimeSqliteColumnDefinition(
'nullableMap': const RuntimeSqliteColumnDefinition(
association: false,
columnName: 'longer_camelized_variable',
columnName: 'nullable_map',
iterable: false,
type: String,
type: Map,
),
'string': const RuntimeSqliteColumnDefinition(
association: false,
Expand Down Expand Up @@ -154,8 +173,10 @@ class AllFieldTypes {
this.dub,
this.enumField,
required this.enumList,
this.nullableList,
this.longerCamelizedVariable,
this.nullableList,
this.nullableMap,
required this.map,
this.string,
required this.stringSet,
});
Expand All @@ -165,8 +186,10 @@ class AllFieldTypes {
final double? dub;
final Casing? enumField;
final List<Casing> enumList;
final List<int>? nullableList;
final String? longerCamelizedVariable;
final Map<String, dynamic> map;
final List<int>? nullableList;
final Map<String, dynamic>? nullableMap;
final String? string;
final Set<String> stringSet;
}
Loading