Skip to content

Commit 3c54ccd

Browse files
committed
Allow repeating identifiers in ToMany
1 parent 16b37f3 commit 3c54ccd

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

lib/src/document/many.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@ import 'package:json_api/src/document/resource_collection.dart';
55

66
class ToMany extends Relationship {
77
ToMany(Iterable<Identifier> identifiers) {
8-
for (var id in identifiers) {
9-
_map[id.key] = id;
10-
}
8+
_ids.addAll(identifiers);
119
}
1210

13-
final _map = <String, Identifier>{};
11+
final _ids = <Identifier>[];
1412

1513
@override
16-
Map<String, Object> toJson() =>
17-
{'data': _map.values.toList(), ...super.toJson()};
14+
Map<String, Object> toJson() => {
15+
'data': [..._ids],
16+
...super.toJson()
17+
};
1818

1919
@override
20-
Iterator<Identifier> get iterator => _map.values.iterator;
20+
Iterator<Identifier> get iterator => _ids.iterator;
2121

2222
/// Finds the referenced elements which are found in the [collection].
2323
/// The resulting [Iterable] may contain fewer elements than referred by the
2424
/// relationship if the [collection] does not have all of them.
25-
Iterable<Resource> findIn(ResourceCollection collection) =>
26-
_map.keys.map((key) => collection[key]).whereType();
25+
Iterable<Resource> findIn(ResourceCollection collection) => collection.where(
26+
(resource) => any((identifier) => identifier.identifies(resource)));
2727
}

0 commit comments

Comments
 (0)