Skip to content

Commit 1beea1b

Browse files
authored
Merge pull request #4 from domtom1126/flutter-data-upgrade
Flutter data upgrade
2 parents 5af6c88 + 25624cd commit 1beea1b

File tree

10 files changed

+352
-549
lines changed

10 files changed

+352
-549
lines changed

lib/flutter_data_json_api_adapter.dart

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,30 @@ import 'package:json_api/document.dart';
1313
///
1414
/// ```
1515
/// @JsonSerializable()
16-
/// @DataRepository([JSONAPIAdapter, MyJSONAPIAdapter])
16+
/// @DataAdapter([JSONAPIAdapter, MyJSONAPIAdapter])
1717
/// class User with DataModel<User> {
1818
/// @override
1919
/// final int id;
2020
/// final String name;
2121
/// User({this.id, this.name});
2222
/// }
2323
///
24-
/// mixin MyJSONAPIAdapter on RemoteAdapter<User> {
24+
/// mixin MyJSONAPIAdapter on Adapter<User> {
2525
/// @override
2626
/// String get baseUrl => "https://my.jsonapi.server/api";
2727
///
2828
/// // other customizations
2929
/// }
3030
/// ```
31-
mixin JSONAPIAdapter<T extends DataModel<T>> on RemoteAdapter<T> {
31+
mixin JSONAPIAdapter<T extends DataModelMixin<T>> on Adapter<T> {
3232
@override
3333
FutureOr<Map<String, String>> get defaultHeaders async {
34-
return await super.defaultHeaders &
35-
{
36-
'Content-Type': 'application/vnd.api+json',
37-
'Accept': 'application/vnd.api+json',
38-
};
34+
final superHeaders = await super.defaultHeaders;
35+
return {
36+
...superHeaders,
37+
'Content-Type': 'application/vnd.api+json',
38+
'Accept': 'application/vnd.api+json',
39+
};
3940
}
4041

4142
/// Transforms native format into JSON:API
@@ -49,7 +50,7 @@ mixin JSONAPIAdapter<T extends DataModel<T>> on RemoteAdapter<T> {
4950
// relationships
5051
final relationships = <String, NewRelationship>{};
5152

52-
for (final relEntry in localAdapter.relationshipMetas.entries) {
53+
for (final relEntry in relationshipMetas.entries) {
5354
final key = relEntry.key;
5455
final type = _typeFor(relEntry.value.type);
5556

@@ -68,12 +69,12 @@ mixin JSONAPIAdapter<T extends DataModel<T>> on RemoteAdapter<T> {
6869
final id = map.remove('id');
6970

7071
// attributes
71-
final attributes = Map.fromEntries(
72+
final attributes = Map<String, Object?>.fromEntries(
7273
map.entries.map((e) => MapEntry(e.key, e.value)),
7374
);
7475

7576
// assemble type, id, attributes, relationships in `Resource`
76-
final resource = NewResource(_typeFor(internalType), id: id?.toString());
77+
final resource = NewResource(_typeFor(this.type), id: id?.toString());
7778
resource.attributes.addAll(attributes);
7879
resource.relationships.addAll(relationships);
7980

@@ -87,7 +88,7 @@ mixin JSONAPIAdapter<T extends DataModel<T>> on RemoteAdapter<T> {
8788

8889
/// Transforms JSON:API into native format (with included resources)
8990
@override
90-
Future<DeserializedData<T>> deserialize(Object? data, {String? key}) async {
91+
DeserializedData<T> deserialize(Object? data, {String? key}) {
9192
final result = DeserializedData<T>([], included: []);
9293
final collection = <Resource>[];
9394

@@ -102,18 +103,9 @@ mixin JSONAPIAdapter<T extends DataModel<T>> on RemoteAdapter<T> {
102103
collection.addAll(inbound.dataAsCollection());
103104
}
104105

105-
// group by type
106-
final grouped = groupBy<Resource, String>(
107-
inbound.included(), (r) => _internalTypeFor(r.type));
108-
109-
for (final e in grouped.entries) {
110-
final internalType = e.key;
111-
if (adapters.containsKey(internalType)) {
112-
final data = await adapters[internalType]!.deserialize(e.value);
113-
result.included
114-
.addAll(List<DataModel<DataModel>>.from(data.models));
115-
}
116-
}
106+
// for included resources, we'll just skip since we can't
107+
// easily access other adapters in the new version
108+
// we'll process the main collection
117109
} catch (err, stack) {
118110
throw DataException('Invalid JSON:API, $err', stackTrace: stack);
119111
}
@@ -148,29 +140,23 @@ mixin JSONAPIAdapter<T extends DataModel<T>> on RemoteAdapter<T> {
148140
mapOut[attrEntry.key] = attrEntry.value;
149141
}
150142

151-
final data = await super.deserialize(mapOut);
143+
final data = super.deserialize(mapOut);
152144
result.models.addAll(data.models);
153145
}
154146

155147
return result;
156148
}
157149

158150
String _internalTypeFor(String type) {
159-
final adapterForType = adapters.values
160-
.where((adapter) =>
161-
adapter.type == type ||
162-
adapter.type == DataHelpers.internalTypeFor(type))
163-
// ignore: invalid_use_of_visible_for_testing_member
164-
.safeFirst;
165-
return adapterForType?.internalType ?? DataHelpers.internalTypeFor(type);
151+
// In v2, we don't have direct access to other adapters
152+
// so we'll just return the type
153+
return type.toLowerCase();
166154
}
167155

168156
String _typeFor(String internalType) {
169-
final adapterForType = adapters.values
170-
.where((adapter) => adapter.internalType == internalType)
171-
// ignore: invalid_use_of_visible_for_testing_member
172-
.safeFirst;
173-
return adapterForType?.type ?? internalType;
157+
// In v2, we don't have direct access to other adapters
158+
// so we'll just return the type
159+
return internalType;
174160
}
175161
}
176162

0 commit comments

Comments
 (0)