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

eng: deprecate FieldSerializable#nullable #520 #521

Merged
merged 6 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion docs/data/fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ As providers are ultimately responsible for converting raw data into model data,
| `ignore:` | Do not deserialize (from) or serialize (to) this field to a provider | `@Rest(ignore: true)` |
| `name:` | Stored key for this field according to the provider. In SQLite, for example, this would be the column name. | `@Sqlite(name: "address_2")` |
| `defaultValue:` | Value to use in absence or `null` of the instance. It does not dictate what the Dart model will hold on empty instantiation. **Recommended to use Dart's default constructor assignment instead**. | `@Rest(defaultValue: '[]')` |
| `nullable:` | `null` fields are handled gracefully when serializing and deserializing. | `@Rest(nullable: true)` |
| `fromGenerator` | A stringified function with access to [placeholders](#placeholders); replaces adapter's generated deserialize code for the field. Do not include trailing semicolons or function body wrapping symbols (`{}` or `=>`) in the definition. | `@Rest(fromGenerator: "int.tryParse(%DATA_PROPERTY%.toString())")` |
| `toGenerator` | A stringified function with access to [placeholders](#placeholders); replaces adapter's generated serialize code for the field. Do not include trailing semicolons or function body wrapping symbols (`{}` or `=>`) in the definition. | `@Sqlite(toGenerator: "%INSTANCE_PROPERTY% > 1 ? 1 : 0")` |

Expand Down
5 changes: 5 additions & 0 deletions packages/brick_build/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Unreleased

## 3.3.1

- Remove `FieldSerializable#nullable` requirement on `deserializerNullableClause`
- Update minimum `brick_core` to `1.4.0`

## 3.3.0

- Add documentation to increase pub.dev score
Expand Down
4 changes: 2 additions & 2 deletions packages/brick_build/example/file_fields.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:analyzer/dart/element/element.dart';
import 'package:brick_core/field_serializable.dart';
import 'package:brick_build/generators.dart';
import 'package:brick_core/field_serializable.dart';

// in a real-world equivalent, this is an annotation
class File implements FieldSerializable {
Expand Down Expand Up @@ -52,7 +52,7 @@ class _FileSerdesFinder extends AnnotationFinder<File> {
_FileSerdesFinder();

@override
File from(element) {
File from(FieldElement element) {
final obj = objectForField(element);

if (obj == null) {
Expand Down
2 changes: 1 addition & 1 deletion packages/brick_build/lib/src/serdes_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ abstract class SerdesGenerator<FieldAnnotation extends FieldSerializable,
required FieldAnnotation fieldAnnotation,
required String name,
}) =>
fieldAnnotation.nullable && field.type.nullabilitySuffix != NullabilitySuffix.none
field.type.nullabilitySuffix != NullabilitySuffix.none
? "data['$name'] == null ? null :"
: '';

Expand Down
4 changes: 2 additions & 2 deletions packages/brick_build/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ homepage: https://github.com/GetDutchie/brick/tree/main/packages/brick_build
issue_tracker: https://github.com/GetDutchie/brick/issues
repository: https://github.com/GetDutchie/brick

version: 3.3.0
version: 3.3.1

environment:
sdk: ">=2.18.0 <4.0.0"

dependencies:
analyzer: ">=6.0.0 <7.0.0"
brick_core: ^1.3.0
brick_core: ">=1.4.0 <2.0.0"
build: ^2.3.0
dart_style: ">=2.0.0 <3.0.0"
glob: ">=2.1.0 <3.0.0"
Expand Down
4 changes: 4 additions & 0 deletions packages/brick_core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Unreleased

## 1.4.0

- **DEPRECATION** remove `FieldSerializable#nullable`. Builders should evaluate the nullable suffix of the field instead

## 1.3.1

- `const`antize `Where.exactly` and `OrderBy.{desc|asc}`
Expand Down
1 change: 1 addition & 0 deletions packages/brick_core/lib/field_serializable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ abstract class FieldSerializable {

/// When `true`, `null` fields are handled gracefully when serializing and deserializing.
/// For Dart >=2.12, the member type must also be nullable (i.e. `bool?`).
@Deprecated('Use a nullable type instead')
bool get nullable;

/// Manipulates output for the field in the serialize generator.
Expand Down
2 changes: 1 addition & 1 deletion packages/brick_core/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_core
issue_tracker: https://github.com/GetDutchie/brick/issues
repository: https://github.com/GetDutchie/brick

version: 1.3.1
version: 1.4.0

environment:
sdk: ">=3.0.0 <4.0.0"
Expand Down
4 changes: 4 additions & 0 deletions packages/brick_graphql/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Unreleased

## 3.3.0

- **DEPRECATION** remove `Graphql#nullable`. Builders should evaluate the nullable suffix of the field instead

## 3.2.0

- **DEPRECATION** `Query(providerArgs: {'context':})` is now `Query(forProviders: [GraphqlProviderQuery(context:)])`
Expand Down
3 changes: 2 additions & 1 deletion packages/brick_graphql/lib/src/annotations/graphql.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Graphql implements FieldSerializable {
/// This indicates that the payload from GraphQL could be `null` and is not related to
/// Dart nullability. Defaults to `false`.
@override
@Deprecated('Use a nullable type instead')
final bool nullable;

/// Supply subfields that should be requested from the server.
Expand All @@ -58,7 +59,7 @@ class Graphql implements FieldSerializable {
bool? ignoreFrom,
bool? ignoreTo,
this.name,
bool? nullable,
@Deprecated('Use a nullable type instead') bool? nullable,
this.subfields,
this.toGenerator,
}) : enumAsString = enumAsString ?? false,
Expand Down
4 changes: 2 additions & 2 deletions packages/brick_graphql/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ homepage: https://github.com/GetDutchie/brick/tree/main/packages/brick_graphql
issue_tracker: https://github.com/GetDutchie/brick/issues
repository: https://github.com/GetDutchie/brick

version: 3.2.0
version: 3.3.0

environment:
sdk: ">=2.18.0 <4.0.0"

dependencies:
brick_core: ^1.3.0
brick_core: ">=1.4.0 <2.0.0"
collection: ">=1.15.0 <2.0.0"
gql: ">=0.13.0 <2.0.0"
gql_exec: ">=0.3.0 <2.0.0"
Expand Down
6 changes: 6 additions & 0 deletions packages/brick_graphql_generators/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## Unreleased

## 3.3.1

- Upgrade `brick_core` to `1.4.0`
- Upgrade `brick_build` to `3.3.1`
- Ignore `Graphql#nullable`

## 3.3.0

- Upgrade `brick_core` to `1.3.0`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class GraphqlAnnotationFinder extends AnnotationFinder<Graphql>
config?.fieldRename,
GraphqlSerializable.defaults.fieldRename,
),
nullable: Graphql.defaults.nullable,
enumAsString: Graphql.defaults.enumAsString,
);
}
Expand All @@ -43,7 +42,6 @@ class GraphqlAnnotationFinder extends AnnotationFinder<Graphql>
ignoreTo: obj.getField('ignoreTo')?.toBoolValue() ?? Graphql.defaults.ignoreTo,
name: obj.getField('name')?.toStringValue() ??
renameField(element.name, config?.fieldRename, GraphqlSerializable.defaults.fieldRename),
nullable: obj.getField('nullable')?.toBoolValue() ?? Graphql.defaults.nullable,
subfields: _convertMapToMap(obj.getField('subfields')?.toMapValue()),
toGenerator: obj.getField('toGenerator')!.toStringValue(),
);
Expand Down
8 changes: 4 additions & 4 deletions packages/brick_graphql_generators/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ homepage: https://github.com/GetDutchie/brick/tree/main/packages/brick_graphql_g
issue_tracker: https://github.com/GetDutchie/brick/issues
repository: https://github.com/GetDutchie/brick

version: 3.3.0
version: 3.3.1+1

environment:
sdk: ">=2.18.0 <4.0.0"

dependencies:
analyzer: ">=6.0.0 <7.0.0"
brick_build: ">=3.2.0 <4.0.0"
brick_core: ">=1.3.0 <2.0.0"
brick_build: ">=3.3.1 <4.0.0"
brick_core: ">=1.4.0 <2.0.0"
brick_graphql: ">=3.0.0 <4.0.0"
brick_json_generators: ">=3.0.0 <4.0.0"
brick_json_generators: ">=3.2.0 <4.0.0"
build: ">=2.0.0 <3.0.0"
dart_style: ">=2.0.0 <3.0.0"
source_gen: ">=1.2.2 <2.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class EnumAsString {
@Graphql(enumAsString: true)
final Hat hat;

@Graphql(enumAsString: true, nullable: true)
@Graphql(enumAsString: true)
final Hat? nullableHat;

@Graphql(enumAsString: true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ Future<ToFromJson> _$ToFromJsonFromGraphql(Map<String, dynamic> data,
GraphqlFirstRepository? repository}) async {
return ToFromJson(
assoc: ToFromJsonAssoc.fromJson(data['assoc'] as String),
assocNullable: data['assocNullable'] != null
? ToFromJsonAssoc.fromJson(data['assocNullable'] as String)
: null,
assocNullable: data['assocNullable'] == null
? null
: ToFromJsonAssoc.fromJson(data['assocNullable'] as String),
assocIterable: data['assocIterable']
.map((d) => ToFromJsonAssoc.fromJson(d as String))
.toList()
.cast<ToFromJsonAssoc>(),
assocIterableNullable: data['assocIterableNullable']
?.map((d) => ToFromJsonAssoc.fromJson(d as String))
.toList()
.cast<ToFromJsonAssoc>());
assocIterableNullable: data['assocIterableNullable'] == null
? null
: data['assocIterableNullable']
?.map((d) => ToFromJsonAssoc.fromJson(d as String))
.toList()
.cast<ToFromJsonAssoc>());
}

Future<Map<String, dynamic>> _$ToFromJsonToGraphql(ToFromJson instance,
Expand Down
5 changes: 4 additions & 1 deletion packages/brick_json_generators/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
## Unreleased

## 3.2.0

- (test) remove analysis options override for non-standard library prefixes
- Revert `.getDisplayString()` change due to Flutter 3.22 being restricted to analyzer <6.4.1. `meta` is pinned to `1.12` in this version of Flutter, and `analyzer >=6.5.0`, where the change was made, requires `meta >= 1.15`. This change will eventually be re-reverted.
- Upgrade `brick_core` to `1.3.0`
- Upgrade `brick_core` to `1.4.0`
- Update analysis to modern lints
- Remove redundant nullability checks for siblings and iterable siblings

## 3.1.1

Expand Down
8 changes: 2 additions & 6 deletions packages/brick_json_generators/lib/json_deserialize.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,8 @@ mixin JsonDeserialize<TModel extends Model, Annotation extends FieldSerializable
// sibling
} else if (checker.isSibling) {
final shouldAwait = wrappedInFuture ? '' : 'await ';
final nullableSuffix = checker.isNullable ? '$fieldValue == null ? null : ' : '';

return '''$nullableSuffix$shouldAwait${SharedChecker.withoutNullability(checker.unFuturedType)}Adapter().from$providerName(
return '''$shouldAwait${SharedChecker.withoutNullability(checker.unFuturedType)}Adapter().from$providerName(
$fieldValue, provider: provider, repository: repository
)''';

Expand All @@ -143,10 +142,7 @@ mixin JsonDeserialize<TModel extends Model, Annotation extends FieldSerializable
final klass = checker.targetType.element! as ClassElement;
final parameterType = checker.fromJsonConstructor!.parameters.first.type;

final output =
'${klass.displayName}.fromJson($fieldValue as ${parameterType.getDisplayString()})';
if (checker.isNullable) return '$fieldValue != null ? $output : null';
return output;
return '${klass.displayName}.fromJson($fieldValue as ${parameterType.getDisplayString()})';
}

return null;
Expand Down
6 changes: 3 additions & 3 deletions packages/brick_json_generators/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ homepage: https://github.com/GetDutchie/brick/tree/main/packages/brick_json_gene
issue_tracker: https://github.com/GetDutchie/brick/issues
repository: https://github.com/GetDutchie/brick

version: 3.1.1
version: 3.2.0

environment:
sdk: ">=2.18.0 <4.0.0"

dependencies:
analyzer: ">=6.0.0 <7.0.0"
brick_build: ">=3.0.0 <4.0.0"
brick_core: ^1.3.0
brick_build: ">=3.3.1 <4.0.0"
brick_core: ">=1.4.0 <2.0.0"
build: ">=2.0.0 <3.0.0"
dart_style: ">=2.0.0 <3.0.0"
source_gen: ">=1.2.2 <2.0.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import 'package:brick_core/core.dart';
import 'package:brick_offline_first_with_rest/brick_offline_first_with_rest.dart';
import 'package:brick_offline_first/brick_offline_first.dart';
import 'package:brick_rest/brick_rest.dart';
import 'package:brick_sqlite/brick_sqlite.dart';
import 'package:brick_offline_first_with_rest/brick_offline_first_with_rest.dart';
import 'package:brick_offline_first_with_rest_example/brick/models/hat.dart';
import 'package:brick_offline_first_with_rest_example/brick/models/mounty.model.dart';
import 'package:brick_rest/brick_rest.dart';
import 'package:brick_sqlite/brick_sqlite.dart';

class KitchenSinkRequest extends RestRequestTransformer {
final get = RestRequest(url: '/my-path', topLevelKey: 'kitchen_sinks');
@override
final get = const RestRequest(url: '/my-path', topLevelKey: 'kitchen_sinks');

final upsert = RestRequest(url: '/my-path', topLevelKey: 'kitchen_sink');
@override
final upsert = const RestRequest(url: '/my-path', topLevelKey: 'kitchen_sink');

KitchenSinkRequest(Query? query, Model? instance) : super(query, instance);
KitchenSinkRequest(super.query, super.instance);
}

@ConnectOfflineFirstWithRest(
Expand Down Expand Up @@ -58,7 +60,6 @@ class KitchenSink extends OfflineFirstWithRestModel {
@Rest(defaultValue: "'a default value'")
final String? restAnnotationDefaultValue;

@Rest(nullable: true)
final String? restAnnotationNullable;

@Rest(ignore: true)
Expand All @@ -79,7 +80,6 @@ class KitchenSink extends OfflineFirstWithRestModel {
@Rest(enumAsString: true)
final AnyEnum? enumFromString;

@Sqlite(nullable: true)
final String? sqliteAnnotationNullable;

@Sqlite(defaultValue: "'default value'")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// ignore_for_file: always_use_package_imports

import 'package:brick_offline_first/brick_offline_first.dart';
import 'package:brick_offline_first_with_supabase/brick_offline_first_with_supabase.dart';
import 'package:brick_offline_first_with_supabase_example/models/mounty.model.dart';
import 'package:brick_sqlite/brick_sqlite.dart';
import 'package:brick_supabase/brick_supabase.dart';

import 'hat.dart';
import 'mounty.model.dart';

@ConnectOfflineFirstWithSupabase()
class KitchenSink extends OfflineFirstWithSupabaseModel {
final String? anyString;
Expand Down Expand Up @@ -44,7 +48,6 @@ class KitchenSink extends OfflineFirstWithSupabaseModel {
@Supabase(defaultValue: "'a default value'")
final String? restAnnotationDefaultValue;

@Supabase(nullable: true)
final String? restAnnotationNullable;

@Supabase(ignore: true)
Expand All @@ -65,7 +68,6 @@ class KitchenSink extends OfflineFirstWithSupabaseModel {
@Supabase(enumAsString: true)
final AnyEnum? enumFromString;

@Sqlite(nullable: true)
final String? sqliteAnnotationNullable;

@Sqlite(defaultValue: "'default value'")
Expand Down
4 changes: 4 additions & 0 deletions packages/brick_rest/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Unreleased

## 3.2.0

- **DEPRECATION** remove `Rest#nullable`. Builders should evaluate the nullable suffix of the field instead

## 3.1.0

- **DEPRECATION** `Query(providerArgs: {'request':})` is now `Query(forProviders: [RestProviderQuery(request:)])`.
Expand Down
3 changes: 2 additions & 1 deletion packages/brick_rest/lib/src/annotations/rest.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Rest implements FieldSerializable {
/// This indicates that the payload from REST could be `null` and is not related to
/// Dart nullability. Defaults to `false`.
@override
@Deprecated('Use a nullable type instead')
final bool nullable;

@override
Expand All @@ -60,7 +61,7 @@ class Rest implements FieldSerializable {
bool? ignoreFrom,
bool? ignoreTo,
this.name,
bool? nullable,
@Deprecated('Use a nullable type instead') bool? nullable,
this.toGenerator,
}) : enumAsString = enumAsString ?? false,
ignore = ignore ?? false,
Expand Down
4 changes: 2 additions & 2 deletions packages/brick_rest/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ homepage: https://github.com/GetDutchie/brick/tree/main/packages/brick_rest
issue_tracker: https://github.com/GetDutchie/brick/issues
repository: https://github.com/GetDutchie/brick

version: 3.1.0
version: 3.2.0

environment:
sdk: ">=2.18.0 <4.0.0"

dependencies:
brick_core: ^1.3.0
brick_core: ">=1.4.0 <2.0.0"
http: ">=1.0.0 <2.0.0"
logging: ">=1.0.0 <2.0.0"
meta: ">=1.3.0 <2.0.0"
Expand Down
4 changes: 4 additions & 0 deletions packages/brick_rest_generators/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Unreleased

## 3.3.1

- Upgrade `brick_core` to `1.4.0`

## 3.3.0

- Upgrade `brick_core` to `1.3.0`
Expand Down
Loading
Loading