Skip to content

Commit 49b766e

Browse files
committed
Remove Identity
1 parent 7d26a58 commit 49b766e

File tree

9 files changed

+54
-54
lines changed

9 files changed

+54
-54
lines changed

example/server/in_memory_repo.dart

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ class InMemoryRepo implements Repository {
2828
}
2929

3030
@override
31-
Stream<Identity> addMany(
32-
String type, String id, String rel, Iterable<Identity> ids) {
31+
Stream<Identifier> addMany(
32+
String type, String id, String rel, Iterable<Identifier> ids) {
3333
final many = _many(type, id, rel);
3434
many.addAll(ids.map(Reference.of));
35-
return Stream.fromIterable(many);
35+
return Stream.fromIterable(many).map((e) => e.toIdentifier());
3636
}
3737

3838
@override
@@ -47,21 +47,25 @@ class InMemoryRepo implements Repository {
4747

4848
@override
4949
Future<void> replaceOne(
50-
String type, String id, String rel, Identity? one) async {
50+
String type, String id, String rel, Identifier? one) async {
5151
_model(type, id).one[rel] = nullable(Reference.of)(one);
5252
}
5353

5454
@override
55-
Stream<Identity> deleteMany(
56-
String type, String id, String rel, Iterable<Identity> many) =>
57-
Stream.fromIterable(_many(type, id, rel)..removeAll(many.map(Reference.of)));
55+
Stream<Identifier> deleteMany(
56+
String type, String id, String rel, Iterable<Identifier> many) =>
57+
Stream.fromIterable(
58+
_many(type, id, rel)..removeAll(many.map(Reference.of)))
59+
.map((it) => it.toIdentifier());
5860

5961
@override
60-
Stream<Identity> replaceMany(
61-
String type, String id, String rel, Iterable<Identity> many) =>
62-
Stream.fromIterable(_many(type, id, rel)
63-
..clear()
64-
..addAll(many.map(Reference.of)));
62+
Stream<Identifier> replaceMany(
63+
String type, String id, String rel, Iterable<Identifier> many) {
64+
final set = _many(type, id, rel);
65+
set.clear();
66+
set.addAll(many.map(Reference.of));
67+
return Stream.fromIterable(set).map((it) => it.toIdentifier());
68+
}
6569

6670
Map<String, Model> _collection(String type) =>
6771
(_storage[type] ?? (throw CollectionNotFound()));

example/server/repository.dart

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,26 @@ abstract class Repository {
1616
/// Throws [CollectionNotFound].
1717
/// Throws [ResourceNotFound].
1818
/// Throws [RelationshipNotFound].
19-
Stream<Identity> addMany(
20-
String type, String id, String rel, Iterable<Identity> refs);
19+
Stream<Identifier> addMany(
20+
String type, String id, String rel, Iterable<Identifier> many);
2121

2222
/// Delete the resource
2323
Future<void> delete(String type, String id);
2424

2525
/// Updates the model
2626
Future<void> update(String type, String id, ModelProps props);
2727

28-
Future<void> replaceOne(String type, String id, String rel, Identity? ref);
28+
Future<void> replaceOne(String type, String id, String rel, Identifier? one);
2929

3030
/// Deletes refs from the to-many relationship.
3131
/// Returns the new actual refs.
32-
Stream<Identity> deleteMany(
33-
String type, String id, String rel, Iterable<Identity> refs);
32+
Stream<Identifier> deleteMany(
33+
String type, String id, String rel, Iterable<Identifier> many);
3434

3535
/// Replaces refs in the to-many relationship.
3636
/// Returns the new actual refs.
37-
Stream<Identity> replaceMany(
38-
String type, String id, String rel, Iterable<Identity> refs);
37+
Stream<Identifier> replaceMany(
38+
String type, String id, String rel, Iterable<Identifier> many);
3939
}
4040

4141
class CollectionNotFound implements Exception {}
@@ -44,11 +44,12 @@ class ResourceNotFound implements Exception {}
4444

4545
class RelationshipNotFound implements Exception {}
4646

47-
class Reference with Identity {
47+
class Reference {
4848
Reference(this.type, this.id);
4949

50-
static Reference of(Identity identity) => Reference(identity.type, identity.id);
50+
static Reference of(Identifier id) => Reference(id.type, id.id);
5151

52+
Identifier toIdentifier() => Identifier(type, id);
5253
@override
5354
final String type;
5455
@override
@@ -111,10 +112,10 @@ class Model extends ModelProps {
111112
});
112113
one.forEach((key, value) {
113114
res.relationships[key] =
114-
(value == null ? ToOne.empty() : ToOne(Identifier.of(value)));
115+
(value == null ? ToOne.empty() : ToOne(value.toIdentifier()));
115116
});
116117
many.forEach((key, value) {
117-
res.relationships[key] = ToMany(value.map(Identifier.of));
118+
res.relationships[key] = ToMany(value.map((it) => it.toIdentifier()));
118119
});
119120
return res;
120121
}

example/server/repository_controller.dart

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ class RepositoryController implements Controller {
6868
final refs = await repo
6969
.addMany(target.type, target.id, target.relationship, many)
7070
.toList();
71-
return Response.ok(
72-
OutboundDataDocument.many(ToMany(refs.map(Identifier.of))));
71+
return Response.ok(OutboundDataDocument.many(ToMany(refs)));
7372
}
7473

7574
@override
@@ -94,13 +93,12 @@ class RepositoryController implements Controller {
9493
if (rel is ToOne) {
9594
final ref = rel.identifier;
9695
await repo.replaceOne(target.type, target.id, target.relationship, ref);
97-
return Response.ok(OutboundDataDocument.one(
98-
ref == null ? ToOne.empty() : ToOne(Identifier.of(ref))));
96+
return Response.ok(
97+
OutboundDataDocument.one(ref == null ? ToOne.empty() : ToOne(ref)));
9998
}
10099
if (rel is ToMany) {
101100
final ids = await repo
102101
.replaceMany(target.type, target.id, target.relationship, rel)
103-
.map(Identifier.of)
104102
.toList();
105103
return Response.ok(OutboundDataDocument.many(ToMany(ids)));
106104
}
@@ -113,7 +111,6 @@ class RepositoryController implements Controller {
113111
final rel = (await _decode(request)).asToMany();
114112
final ids = await repo
115113
.deleteMany(target.type, target.id, target.relationship, rel)
116-
.map(Identifier.of)
117114
.toList();
118115
return Response.ok(OutboundDataDocument.many(ToMany(ids)));
119116
}
@@ -125,11 +122,12 @@ class RepositoryController implements Controller {
125122

126123
if (model.one.containsKey(target.relationship)) {
127124
return Response.ok(OutboundDataDocument.one(
128-
ToOne(nullable(Identifier.of)(model.one[target.relationship]))));
125+
ToOne(model.one[target.relationship]?.toIdentifier())));
129126
}
130-
final many = model.many[target.relationship];
127+
final many =
128+
model.many[target.relationship]?.map((it) => it.toIdentifier());
131129
if (many != null) {
132-
final doc = OutboundDataDocument.many(ToMany(many.map(Identifier.of)));
130+
final doc = OutboundDataDocument.many(ToMany(many));
133131
return Response.ok(doc);
134132
}
135133
throw RelationshipNotFound();

lib/document.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ library document;
33

44
export 'package:json_api/src/document/error_object.dart';
55
export 'package:json_api/src/document/identifier.dart';
6-
export 'package:json_api/src/document/identity.dart';
76
export 'package:json_api/src/document/inbound_document.dart';
87
export 'package:json_api/src/document/link.dart';
98
export 'package:json_api/src/document/many.dart';

lib/src/document/identifier.dart

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
1-
import 'package:json_api/src/document/identity.dart';
1+
import 'package:json_api/src/document/resource.dart';
22

33
/// A Resource Identifier object
4-
class Identifier with Identity {
4+
class Identifier {
55
Identifier(this.type, this.id);
66

7-
static Identifier of(Identity identity) =>
8-
Identifier(identity.type, identity.id);
7+
static Identifier of(Resource resource) =>
8+
Identifier(resource.type, resource.id);
99

10-
@override
1110
final String type;
12-
@override
1311
final String id;
1412

13+
String get key => '$type:$id';
14+
15+
// /// True if the [other] identifier references the same object.
16+
// bool hasSameReference(Identifier other) =>
17+
// type == other.type && id == other.id;
18+
19+
/// True if this identifier identifies the [resource].
20+
bool identifies(Resource resource) =>
21+
type == resource.type && id == resource.id;
22+
1523
/// Identifier meta-data.
1624
final meta = <String, Object?>{};
1725

lib/src/document/identity.dart

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/src/document/resource.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
import 'package:json_api/src/document/identity.dart';
21
import 'package:json_api/src/document/link.dart';
32
import 'package:json_api/src/document/resource_properties.dart';
43

5-
class Resource with ResourceProperties, Identity {
4+
class Resource with ResourceProperties {
65
Resource(this.type, this.id);
76

8-
@override
97
final String type;
10-
@override
118
final String id;
9+
String get key => '$type:$id';
1210

1311
/// Resource links
1412
final links = <String, Link>{};

lib/src/nullable.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
/// Nullable function application.
12
U? Function(V? v) nullable<V, U>(U Function(V v) f) =>
23
(v) => v == null ? null : f(v);

test/contract/crud_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@ void main() {
9595

9696
test('Fetch a to-one relationship', () async {
9797
await client.fetchToOne(post.type, post.id, 'author').then((r) {
98-
expect(Identity.same(r.relationship.identifier!, alice), isTrue);
98+
expect(r.relationship.identifier!.identifies(alice), isTrue);
9999
});
100100
});
101101

102102
test('Fetch a to-many relationship', () async {
103103
await client.fetchToMany(post.type, post.id, 'comments').then((r) {
104-
expect(Identity.same(r.relationship.single, comment), isTrue);
104+
expect(r.relationship.single.identifies(comment), isTrue);
105105
});
106106
});
107107

0 commit comments

Comments
 (0)