Skip to content

Commit 5d9cf22

Browse files
Model: extract map keys to constants to avoid mistakes
1 parent 953ee98 commit 5d9cf22

File tree

3 files changed

+105
-59
lines changed

3 files changed

+105
-59
lines changed

objectbox/lib/src/modelinfo/modelentity.dart

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -90,39 +90,41 @@ class ModelEntity {
9090
ModelEntity.fromMap(Map<String, dynamic> data,
9191
{ModelInfo? model, bool check = true})
9292
: _model = model,
93-
id = IdUid.fromString(data['id'] as String?),
94-
lastPropertyId = IdUid.fromString(data['lastPropertyId'] as String?),
95-
uidRequest = data['uidRequest'] as bool? ?? false,
93+
id = IdUid.fromString(data[ModelEntityKey.id] as String?),
94+
lastPropertyId =
95+
IdUid.fromString(data[ModelEntityKey.lastPropertyId] as String?),
96+
uidRequest = data[ModelEntityKey.uidRequest] as bool? ?? false,
9697
_properties = [],
9798
_relations = [],
9899
_backlinks = [] {
99-
name = data['name'] as String?;
100-
externalName = data['externalName'] as String?;
101-
flags = data['flags'] as int? ?? 0;
100+
name = data[ModelEntityKey.name] as String?;
101+
externalName = data[ModelEntityKey.externalName] as String?;
102+
flags = data[ModelEntityKey.flags] as int? ?? 0;
102103

103-
final properties = data['properties'] as List;
104+
final properties = data[ModelEntityKey.properties] as List;
104105
for (final p in properties) {
105106
_properties.add(ModelProperty.fromMap(p as Map<String, dynamic>, this));
106107
}
107108

108-
final relations = data['relations'] as List?;
109+
final relations = data[ModelEntityKey.relations] as List?;
109110
if (relations != null) {
110111
for (final p in relations) {
111112
_relations.add(ModelRelation.fromMap(p as Map<String, dynamic>));
112113
}
113114
}
114115

115-
final backlinks = data['backlinks'] as List?;
116+
final backlinks = data[ModelEntityKey.backlinks] as List?;
116117
if (backlinks != null) {
117118
for (final p in backlinks) {
118119
_backlinks.add(ModelBacklink.fromMap(p as Map<String, dynamic>));
119120
}
120121
}
121122

122-
if (data['constructorParams'] != null) {
123-
constructorParams = (data['constructorParams'] as List<dynamic>)
124-
.map((dynamic e) => e as String)
125-
.toList(growable: false);
123+
if (data[ModelEntityKey.constructorParams] != null) {
124+
constructorParams =
125+
(data[ModelEntityKey.constructorParams] as List<dynamic>)
126+
.map((dynamic e) => e as String)
127+
.toList(growable: false);
126128
}
127129

128130
if (check) validate();
@@ -183,19 +185,19 @@ class ModelEntity {
183185
/// JSON.
184186
Map<String, dynamic> toMap({bool forModelJson = false}) {
185187
final ret = <String, dynamic>{};
186-
ret['id'] = id.toString();
187-
ret['lastPropertyId'] = lastPropertyId.toString();
188-
ret['name'] = name;
189-
if (externalName != null) ret['externalName'] = externalName;
190-
if (flags != 0) ret['flags'] = flags;
191-
ret['properties'] =
188+
ret[ModelEntityKey.id] = id.toString();
189+
ret[ModelEntityKey.lastPropertyId] = lastPropertyId.toString();
190+
ret[ModelEntityKey.name] = name;
191+
if (externalName != null) ret[ModelEntityKey.externalName] = externalName;
192+
if (flags != 0) ret[ModelEntityKey.flags] = flags;
193+
ret[ModelEntityKey.properties] =
192194
properties.map((p) => p.toMap(forModelJson: forModelJson)).toList();
193-
ret['relations'] =
195+
ret[ModelEntityKey.relations] =
194196
relations.map((r) => r.toMap(forModelJson: forModelJson)).toList();
195197
if (!forModelJson) {
196-
ret['backlinks'] = backlinks.map((r) => r.toMap()).toList();
197-
ret['constructorParams'] = constructorParams;
198-
ret['uidRequest'] = uidRequest;
198+
ret[ModelEntityKey.backlinks] = backlinks.map((r) => r.toMap()).toList();
199+
ret[ModelEntityKey.constructorParams] = constructorParams;
200+
ret[ModelEntityKey.uidRequest] = uidRequest;
199201
}
200202
return ret;
201203
}
@@ -316,3 +318,17 @@ class ModelEntity {
316318
return result;
317319
}
318320
}
321+
322+
/// Map keys for properties of this entity.
323+
class ModelEntityKey {
324+
static const String id = 'id';
325+
static const String lastPropertyId = 'lastPropertyId';
326+
static const String name = 'name';
327+
static const String externalName = 'externalName';
328+
static const String flags = 'flags';
329+
static const String properties = 'properties';
330+
static const String relations = 'relations';
331+
static const String backlinks = 'backlinks';
332+
static const String constructorParams = 'constructorParams';
333+
static const String uidRequest = 'uidRequest';
334+
}

objectbox/lib/src/modelinfo/modelproperty.dart

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,18 @@ class ModelProperty {
132132
uidRequest = false;
133133

134134
ModelProperty.fromMap(Map<String, dynamic> data, this.entity)
135-
: id = IdUid.fromString(data['id'] as String?),
136-
relationTarget = data['relationTarget'] as String?,
137-
_dartFieldType = data['dartFieldType'] as String?,
138-
uidRequest = data['uidRequest'] as bool? ?? false,
135+
: id = IdUid.fromString(data[ModelPropertyKey.id] as String?),
136+
relationTarget = data[ModelPropertyKey.relationTarget] as String?,
137+
_dartFieldType = data[ModelPropertyKey.dartFieldType] as String?,
138+
uidRequest = data[ModelPropertyKey.uidRequest] as bool? ?? false,
139139
hnswParams = ModelHnswParams.fromMap(
140-
data['hnswParams'] as Map<String, dynamic>?),
141-
externalName = data['externalName'] as String?,
142-
externalType = data['externalType'] as int? {
143-
name = data['name'] as String?;
144-
type = data['type'] as int?;
145-
flags = data['flags'] as int? ?? 0;
146-
final indexId = data['indexId'] as String?;
140+
data[ModelPropertyKey.hnswParams] as Map<String, dynamic>?),
141+
externalName = data[ModelPropertyKey.externalName] as String?,
142+
externalType = data[ModelPropertyKey.externalType] as int? {
143+
name = data[ModelPropertyKey.name] as String?;
144+
type = data[ModelPropertyKey.type] as int?;
145+
flags = data[ModelPropertyKey.flags] as int? ?? 0;
146+
final indexId = data[ModelPropertyKey.indexId] as String?;
147147
this.indexId = indexId == null ? null : IdUid.fromString(indexId);
148148
}
149149

@@ -153,21 +153,23 @@ class ModelProperty {
153153
/// generated model JSON.
154154
Map<String, dynamic> toMap({bool forModelJson = false}) {
155155
final ret = <String, dynamic>{};
156-
ret['id'] = id.toString();
157-
ret['name'] = name;
158-
if (indexId != null) ret['indexId'] = indexId!.toString();
159-
ret['type'] = type;
160-
if (externalName != null) ret['externalName'] = externalName;
161-
if (externalType != null) ret['externalType'] = externalType;
162-
if (flags != 0) ret['flags'] = flags;
163-
if (relationTarget != null) ret['relationTarget'] = relationTarget;
156+
ret[ModelPropertyKey.id] = id.toString();
157+
ret[ModelPropertyKey.name] = name;
158+
if (indexId != null) ret[ModelPropertyKey.indexId] = indexId!.toString();
159+
ret[ModelPropertyKey.type] = type;
160+
if (externalName != null) ret[ModelPropertyKey.externalName] = externalName;
161+
if (externalType != null) ret[ModelPropertyKey.externalType] = externalType;
162+
if (flags != 0) ret[ModelPropertyKey.flags] = flags;
163+
if (relationTarget != null) {
164+
ret[ModelPropertyKey.relationTarget] = relationTarget;
165+
}
164166
if (!forModelJson) {
165167
if (_dartFieldType != null) {
166-
ret['dartFieldType'] = _dartFieldType;
168+
ret[ModelPropertyKey.dartFieldType] = _dartFieldType;
167169
}
168-
ret['uidRequest'] = uidRequest;
170+
ret[ModelPropertyKey.uidRequest] = uidRequest;
169171
if (hnswParams != null) {
170-
ret['hnswParams'] = hnswParams!.toMap();
172+
ret[ModelPropertyKey.hnswParams] = hnswParams!.toMap();
171173
}
172174
}
173175
return ret;
@@ -221,3 +223,18 @@ class ModelProperty {
221223
return result;
222224
}
223225
}
226+
227+
/// Map keys for properties of this property.
228+
class ModelPropertyKey {
229+
static const String id = 'id';
230+
static const String name = 'name';
231+
static const String indexId = 'indexId';
232+
static const String type = 'type';
233+
static const String flags = 'flags';
234+
static const String relationTarget = 'relationTarget';
235+
static const String dartFieldType = 'dartFieldType';
236+
static const String uidRequest = 'uidRequest';
237+
static const String hnswParams = 'hnswParams';
238+
static const String externalName = 'externalName';
239+
static const String externalType = 'externalType';
240+
}

objectbox/lib/src/modelinfo/modelrelation.dart

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,28 +74,30 @@ class ModelRelation {
7474
uidRequest = false;
7575

7676
ModelRelation.fromMap(Map<String, dynamic> data)
77-
: this.create(
78-
IdUid.fromString(data['id'] as String?), data['name'] as String?,
79-
targetId: data['targetId'] as String?,
80-
targetName: data['targetName'] as String?,
81-
uidRequest: data['uidRequest'] as bool? ?? false,
82-
externalName: data['externalName'] as String?,
83-
externalType: data['externalType'] as int?);
77+
: this.create(IdUid.fromString(data[ModelRelationKey.id] as String?),
78+
data[ModelRelationKey.name] as String?,
79+
targetId: data[ModelRelationKey.targetId] as String?,
80+
targetName: data[ModelRelationKey.targetName] as String?,
81+
uidRequest: data[ModelRelationKey.uidRequest] as bool? ?? false,
82+
externalName: data[ModelRelationKey.externalName] as String?,
83+
externalType: data[ModelRelationKey.externalType] as int?);
8484

8585
/// See [ModelEntity.toMap] for important details.
8686
///
8787
/// Note that the order in which keys are written defines the order in the
8888
/// generated model JSON.
8989
Map<String, dynamic> toMap({bool forModelJson = false}) {
9090
final ret = <String, dynamic>{};
91-
ret['id'] = id.toString();
92-
ret['name'] = name;
93-
if (externalName != null) ret['externalName'] = externalName;
94-
if (externalType != null) ret['externalType'] = externalType;
95-
if (_targetId != null) ret['targetId'] = _targetId.toString();
91+
ret[ModelRelationKey.id] = id.toString();
92+
ret[ModelRelationKey.name] = name;
93+
if (externalName != null) ret[ModelRelationKey.externalName] = externalName;
94+
if (externalType != null) ret[ModelRelationKey.externalType] = externalType;
95+
if (_targetId != null) {
96+
ret[ModelRelationKey.targetId] = _targetId.toString();
97+
}
9698
if (!forModelJson) {
97-
ret['targetName'] = _targetName;
98-
ret['uidRequest'] = uidRequest;
99+
ret[ModelRelationKey.targetName] = _targetName;
100+
ret[ModelRelationKey.uidRequest] = uidRequest;
99101
}
100102
return ret;
101103
}
@@ -104,3 +106,14 @@ class ModelRelation {
104106
String toString() =>
105107
'standalone relation $name($id) relTarget: $_targetName($_targetId)';
106108
}
109+
110+
/// Map keys for properties of this relation.
111+
class ModelRelationKey {
112+
static const String id = 'id';
113+
static const String name = 'name';
114+
static const String targetId = 'targetId';
115+
static const String targetName = 'targetName';
116+
static const String uidRequest = 'uidRequest';
117+
static const String externalType = 'externalType';
118+
static const String externalName = 'externalName';
119+
}

0 commit comments

Comments
 (0)