Skip to content

Commit

Permalink
fix(sqlite) : nullable maps are not cast to default values #531 (#532)
Browse files Browse the repository at this point in the history
tshedor authored Jan 27, 2025
1 parent 9a41b95 commit 331ddef
Showing 7 changed files with 47 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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
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
2 changes: 1 addition & 1 deletion packages/brick_sqlite/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 4 additions & 0 deletions packages/brick_sqlite_generators/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Unreleased

## 3.3.1

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

## 3.3.0

- Upgrade `brick_core` to `1.3.0`
Original file line number Diff line number Diff line change
@@ -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())';
2 changes: 1 addition & 1 deletion packages/brick_sqlite_generators/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ homepage: https://github.com/GetDutchie/brick/tree/main/packages/brick_sqlite_ge
issue_tracker: https://github.com/GetDutchie/brick/issues
repository: https://github.com/GetDutchie/brick

version: 3.3.0+1
version: 3.3.1

environment:
sdk: ">=2.18.0 <4.0.0"
Original file line number Diff line number Diff line change
@@ -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;
@@ -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())
};
@@ -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,
@@ -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,
});
@@ -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;
}

0 comments on commit 331ddef

Please sign in to comment.