Skip to content

Commit 04fa1ca

Browse files
external types: drop property prefix from external field names #139
1 parent 4af4d68 commit 04fa1ca

File tree

7 files changed

+67
-70
lines changed

7 files changed

+67
-70
lines changed

generator/lib/src/code_builder.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ class CodeBuilder extends Builder {
213213
propInModel.dartFieldType = prop.dartFieldType;
214214
propInModel.relationTarget = prop.relationTarget;
215215
propInModel.hnswParams = prop.hnswParams;
216-
propInModel.externalPropertyType = prop.externalPropertyType;
217-
propInModel.externalPropertyName = prop.externalPropertyName;
216+
propInModel.externalType = prop.externalType;
217+
propInModel.externalName = prop.externalName;
218218

219219
if (!prop.hasIndexFlag()) {
220220
propInModel.removeIndex();
@@ -244,8 +244,8 @@ class CodeBuilder extends Builder {
244244

245245
relInModel.name = rel.name;
246246
relInModel.targetName = rel.targetName;
247-
relInModel.externalPropertyType = rel.externalPropertyType;
248-
relInModel.externalPropertyName = rel.externalPropertyName;
247+
relInModel.externalType = rel.externalType;
248+
relInModel.externalName = rel.externalName;
249249
}
250250

251251
IdUid mergeEntity(ModelInfo modelInfo, ModelEntity entity) {

generator/lib/src/code_chunks.dart

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,11 @@ class CodeChunks {
154154
additionalArgs +=
155155
", hnswParams: ${property.hnswParams!.toCodeString(obxInt)}";
156156
}
157-
if (property.externalPropertyType != null) {
158-
additionalArgs +=
159-
", externalPropertyType: ${property.externalPropertyType!}";
157+
if (property.externalType != null) {
158+
additionalArgs += ", externalType: ${property.externalType!}";
160159
}
161-
if (property.externalPropertyName != null) {
162-
additionalArgs +=
163-
", externalPropertyName: '${property.externalPropertyName!}'";
160+
if (property.externalName != null) {
161+
additionalArgs += ", externalName: '${property.externalName!}'";
164162
}
165163
return '''
166164
$obxInt.ModelProperty(
@@ -175,11 +173,11 @@ class CodeChunks {
175173

176174
static String createModelRelation(ModelRelation relation) {
177175
var additionalArgs = '';
178-
if (relation.externalPropertyType != null) {
179-
additionalArgs += ", externalType: ${relation.externalPropertyType!}";
176+
if (relation.externalType != null) {
177+
additionalArgs += ", externalType: ${relation.externalType!}";
180178
}
181-
if (relation.externalPropertyName != null) {
182-
additionalArgs += ", externalName: '${relation.externalPropertyName!}'";
179+
if (relation.externalName != null) {
180+
additionalArgs += ", externalName: '${relation.externalName!}'";
183181
}
184182
return '''
185183
$obxInt.ModelRelation(

generator/lib/src/entity_resolver.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -189,28 +189,28 @@ class EntityResolver extends Builder {
189189
// Handles standalone (non backlink) ToMany relation
190190

191191
// @ExternalType
192-
int? externalPropertyType;
192+
int? externalType;
193193
_externalTypeChecker.runIfMatches(annotated, (annotation) {
194194
final externalTypeId = _readExternalTypeParams(annotation);
195-
externalPropertyType = externalTypeId;
195+
externalType = externalTypeId;
196196
});
197197

198198
// @ExternalName
199-
String? externalPropertyName;
199+
String? externalName;
200200
_externalNameChecker.runIfMatches(annotated, (annotation) {
201-
if (externalPropertyType == null) {
201+
if (externalType == null) {
202202
throw InvalidGenerationSourceError(
203203
"@ExternalName annotation requires @ExternalType annotation to be present");
204204
}
205-
externalPropertyName = _readExternalNameParams(annotation);
205+
externalName = _readExternalNameParams(annotation);
206206
});
207207

208208
// create relation
209209
final rel = ModelRelation.create(IdUid(0, propUid ?? 0), f.name,
210210
targetName: relTargetName,
211211
uidRequest: propUid != null && propUid == 0,
212-
externalPropertyName: externalPropertyName,
213-
externalPropertyType: externalPropertyType);
212+
externalName: externalName,
213+
externalType: externalType);
214214

215215
entity.relations.add(rel);
216216

@@ -255,17 +255,17 @@ class EntityResolver extends Builder {
255255
// @ExternalType
256256
_externalTypeChecker.runIfMatches(annotated, (annotation) {
257257
final externalTypeId = _readExternalTypeParams(annotation);
258-
prop.externalPropertyType = externalTypeId;
258+
prop.externalType = externalTypeId;
259259
});
260260

261261
// @ExternalName
262262
_externalNameChecker.runIfMatches(annotated, (annotation) {
263-
if (prop.externalPropertyType == null) {
263+
if (prop.externalType == null) {
264264
throw InvalidGenerationSourceError(
265265
"@ExternalName annotation requires @ExternalType annotation to be present");
266266
}
267267
final externalName = _readExternalNameParams(annotation);
268-
prop.externalPropertyName = externalName;
268+
prop.externalName = externalName;
269269
});
270270

271271
// for code generation

generator/test/code_builder_test.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,12 @@ void main() {
405405

406406
final property1 = testEnv.model.entities[0].properties
407407
.firstWhere((element) => element.name == "mongoId");
408-
expect(property1.externalPropertyType, OBXExternalPropertyType.MongoId);
408+
expect(property1.externalType, OBXExternalPropertyType.MongoId);
409409

410410
final property2 = testEnv.model.entities[0].properties
411411
.firstWhere((element) => element.name == "mongoUuid");
412-
expect(property2.externalPropertyType, OBXExternalPropertyType.Uuid);
413-
expect(property2.externalPropertyName, "my-mongo-uuid");
412+
expect(property2.externalType, OBXExternalPropertyType.Uuid);
413+
expect(property2.externalName, "my-mongo-uuid");
414414
});
415415

416416
test('annotations work on ToMany (standalone) relations', () async {
@@ -436,12 +436,12 @@ void main() {
436436

437437
final relation1 = testEnv.model.entities[0].relations
438438
.firstWhere((element) => element.name == "rel1");
439-
expect(relation1.externalPropertyType, OBXExternalPropertyType.MongoId);
439+
expect(relation1.externalType, OBXExternalPropertyType.MongoId);
440440

441441
final relation2 = testEnv.model.entities[0].relations
442442
.firstWhere((element) => element.name == "rel2");
443-
expect(relation2.externalPropertyType, OBXExternalPropertyType.Uuid);
444-
expect(relation2.externalPropertyName, "my-courses-rel");
443+
expect(relation2.externalType, OBXExternalPropertyType.Uuid);
444+
expect(relation2.externalName, "my-courses-rel");
445445
});
446446
});
447447

objectbox/lib/src/modelinfo/modelproperty.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class ModelProperty {
1616
ModelEntity? entity;
1717
String? relationTarget;
1818
ModelHnswParams? hnswParams;
19-
int? externalPropertyType;
20-
String? externalPropertyName;
19+
int? externalType;
20+
String? externalName;
2121

2222
/// Type used in the source dart code - used by the code generator.
2323
/// Starts with [_fieldReadOnlyPrefix] if the field (currently IDs only) is
@@ -117,8 +117,8 @@ class ModelProperty {
117117
IdUid? indexId,
118118
this.relationTarget,
119119
this.hnswParams,
120-
this.externalPropertyName,
121-
this.externalPropertyType})
120+
this.externalName,
121+
this.externalType})
122122
: _name = name,
123123
_type = type,
124124
_flags = flags,
@@ -132,8 +132,8 @@ class ModelProperty {
132132
uidRequest = data['uidRequest'] as bool? ?? false,
133133
hnswParams = ModelHnswParams.fromMap(
134134
data['hnswParams'] as Map<String, dynamic>?),
135-
externalPropertyType = data['externalType'] as int?,
136-
externalPropertyName = data['externalName'] as String? {
135+
externalType = data['externalType'] as int?,
136+
externalName = data['externalName'] as String? {
137137
name = data['name'] as String?;
138138
type = data['type'] as int?;
139139
flags = data['flags'] as int? ?? 0;
@@ -157,11 +157,11 @@ class ModelProperty {
157157
if (hnswParams != null) {
158158
ret['hnswParams'] = hnswParams!.toMap();
159159
}
160-
if (externalPropertyType != null) {
161-
ret['externalType'] = externalPropertyType;
160+
if (externalType != null) {
161+
ret['externalType'] = externalType;
162162
}
163-
if (externalPropertyName != null) {
164-
ret['externalName'] = externalPropertyName;
163+
if (externalName != null) {
164+
ret['externalName'] = externalName;
165165
}
166166
}
167167
return ret;

objectbox/lib/src/modelinfo/modelrelation.dart

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class ModelRelation {
1212

1313
String? _targetName;
1414

15-
int? externalPropertyType;
16-
String? externalPropertyName;
15+
int? externalType;
16+
String? externalName;
1717

1818
// whether the user requested UID information (started a rename process)
1919
final bool uidRequest;
@@ -49,17 +49,17 @@ class ModelRelation {
4949
ModelRelation.create(this.id, String? name,
5050
{String? targetId,
5151
String? targetName,
52-
int? externalPropertyType,
53-
String? externalPropertyName,
52+
int? externalType,
53+
String? externalName,
5454
this.uidRequest = false}) {
5555
this.name = name;
5656
if (targetId != null) this.targetId = IdUid.fromString(targetId);
5757
if (targetName != null) this.targetName = targetName;
58-
if (externalPropertyType != null) {
59-
this.externalPropertyType = externalPropertyType;
58+
if (externalType != null) {
59+
this.externalType = externalType;
6060
}
61-
if (externalPropertyName != null) {
62-
this.externalPropertyName = externalPropertyName;
61+
if (externalName != null) {
62+
this.externalName = externalName;
6363
}
6464
}
6565

@@ -80,18 +80,20 @@ class ModelRelation {
8080
targetId: data['targetId'] as String?,
8181
targetName: data['targetName'] as String?,
8282
uidRequest: data['uidRequest'] as bool? ?? false,
83-
externalPropertyType: data['externalType'] as int?,
84-
externalPropertyName: data['externalName'] as String?);
83+
externalType: data['externalType'] as int?,
84+
externalName: data['externalName'] as String?);
8585

8686
Map<String, dynamic> toMap({bool forModelJson = false}) {
8787
final ret = <String, dynamic>{};
8888
ret['id'] = id.toString();
8989
ret['name'] = name;
9090
if (_targetId != null) ret['targetId'] = _targetId.toString();
91-
if (externalPropertyType != null)
92-
ret['externalType'] = externalPropertyType;
93-
if (externalPropertyName != null)
94-
ret['externalName'] = externalPropertyName;
91+
if (externalType != null) {
92+
ret['externalType'] = externalType;
93+
}
94+
if (externalName != null) {
95+
ret['externalName'] = externalName;
96+
}
9597
if (!forModelJson) {
9698
ret['targetName'] = _targetName;
9799
ret['uidRequest'] = uidRequest;

objectbox/lib/src/native/model.dart

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -116,34 +116,31 @@ class Model {
116116
}
117117
}
118118

119-
final externalPropertyType = prop.externalPropertyType;
120-
if (externalPropertyType != null) {
121-
_check(C.model_property_external_type(_cModel, externalPropertyType));
119+
final externalType = prop.externalType;
120+
if (externalType != null) {
121+
_check(C.model_property_external_type(_cModel, externalType));
122122
}
123-
final externalPropertyName = prop.externalPropertyName?.toNativeUtf8();
124-
if (externalPropertyName != null) {
123+
final externalName = prop.externalName?.toNativeUtf8();
124+
if (externalName != null) {
125125
try {
126-
_check(C.model_property_external_name(
127-
_cModel, externalPropertyName.cast()));
126+
_check(C.model_property_external_name(_cModel, externalName.cast()));
128127
} finally {
129-
calloc.free(externalPropertyName);
128+
calloc.free(externalName);
130129
}
131130
}
132131
}
133132

134133
void addRelation(ModelRelation rel) {
135134
_check(C.model_relation(
136135
_cModel, rel.id.id, rel.id.uid, rel.targetId.id, rel.targetId.uid));
137-
if (rel.externalPropertyType != null) {
138-
_check(
139-
C.model_relation_external_type(_cModel, rel.externalPropertyType!));
140-
if (rel.externalPropertyName != null) {
141-
final externalPropertyName = rel.externalPropertyName!.toNativeUtf8();
136+
if (rel.externalType != null) {
137+
_check(C.model_relation_external_type(_cModel, rel.externalType!));
138+
if (rel.externalName != null) {
139+
final externalName = rel.externalName!.toNativeUtf8();
142140
try {
143-
_check(C.model_relation_external_name(
144-
_cModel, externalPropertyName.cast()));
141+
_check(C.model_relation_external_name(_cModel, externalName.cast()));
145142
} finally {
146-
calloc.free(externalPropertyName);
143+
calloc.free(externalName);
147144
}
148145
}
149146
}

0 commit comments

Comments
 (0)