Skip to content

Commit 1423a68

Browse files
committed
Remove Resource.key, refactor Resource(id)
1 parent 459690d commit 1423a68

File tree

4 files changed

+24
-16
lines changed

4 files changed

+24
-16
lines changed

lib/src/document/identifier.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ class Identifier {
77
static Identifier of(Resource resource) =>
88
Identifier(resource.type, resource.id);
99

10+
/// Resource type.
1011
final String type;
12+
13+
/// Resource id.
1114
final String id;
1215

1316
/// Identifier meta-data.

lib/src/document/inbound_document.dart

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ class _Parser {
8686
..links.addAll(links(json))
8787
..meta.addAll(meta(json));
8888

89-
NewResource newResource(Map json) => NewResource(json.get<String>('type'),
90-
json.containsKey('id') ? json.get<String>('id') : null)
91-
..attributes.addAll(_getAttributes(json))
92-
..relationships.addAll(_getRelationships(json))
93-
..meta.addAll(meta(json));
89+
NewResource newResource(Map json) =>
90+
NewResource(json.get<String>('type'), id: json.getIfDefined('id'))
91+
..attributes.addAll(_getAttributes(json))
92+
..relationships.addAll(_getRelationships(json))
93+
..meta.addAll(meta(json));
9494

9595
/// Decodes Identifier from [json]. Returns the decoded object.
9696
/// If the [json] has incorrect format, throws [FormatException].
@@ -141,13 +141,19 @@ class _Parser {
141141

142142
extension _TypedGeter on Map {
143143
T get<T>(String key, {T Function()? orGet}) {
144-
if (containsKey(key)) {
145-
final val = this[key];
146-
if (val is T) return val;
147-
throw FormatException(
148-
'Key "$key": expected $T, found ${val.runtimeType}');
149-
}
144+
if (containsKey(key)) return _get(key);
150145
if (orGet != null) return orGet();
151146
throw FormatException('Key "$key" does not exist');
152147
}
148+
149+
T? getIfDefined<T>(String key, {T Function()? orGet}) {
150+
if (containsKey(key)) return _get(key);
151+
return null;
152+
}
153+
154+
T _get<T>(String key) {
155+
final val = this[key];
156+
if (val is T) return val;
157+
throw FormatException('Key "$key": expected $T, found ${val.runtimeType}');
158+
}
153159
}

lib/src/document/new_resource.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import 'package:json_api/src/document/resource_properties.dart';
22

33
/// A set of properties for a to-be-created resource which does not have the id yet.
44
class NewResource with ResourceProperties {
5-
NewResource(this.type, [this.id]) {
6-
ArgumentError.checkNotNull(type);
7-
}
5+
NewResource(this.type, {this.id});
86

97
/// Resource type
108
final String type;

lib/src/document/resource.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import 'package:json_api/src/document/resource_properties.dart';
44
class Resource with ResourceProperties {
55
Resource(this.type, this.id);
66

7+
/// Resource type.
78
final String type;
8-
final String id;
99

10-
String get key => '$type:$id';
10+
/// Resource id.
11+
final String id;
1112

1213
/// Resource links
1314
final links = <String, Link>{};

0 commit comments

Comments
 (0)