Skip to content

Commit 116069a

Browse files
committed
index annotation cleanup
1 parent 80769f5 commit 116069a

File tree

7 files changed

+35
-55
lines changed

7 files changed

+35
-55
lines changed

generator/lib/src/code_builder.dart

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,10 @@ class CodeBuilder extends Builder {
135135
log.info('Found new property ${entity.name}.${prop.name}');
136136
entity.addProperty(prop);
137137
} else {
138-
if (propInModel.name != prop.name) {
139-
log.warning('The name of the property(${prop.name}) changed.');
140-
}
141-
if (propInModel.flags != prop.flags) {
142-
log.warning('The flags of the property(${prop.name}) changed.');
143-
}
144-
if (propInModel.type != prop.type) {
145-
log.warning('The type of the property(${prop.name}) changed.');
146-
}
138+
propInModel.name = prop.name;
139+
propInModel.type = prop.type;
140+
propInModel.flags = prop.flags;
141+
propInModel.index = prop.indexId;
147142
}
148143
}
149144

generator/lib/src/entity_resolver.dart

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ class EntityResolver extends Builder {
9191
}
9292

9393
int fieldType, flags = 0;
94-
int propUid, indexUid;
94+
int propUid;
95+
var indexFlag = 0;
9596

9697
if (_idChecker.hasAnnotationOfExact(f)) {
9798
if (hasIdProperty) {
@@ -146,22 +147,22 @@ class EntityResolver extends Builder {
146147
final hasUniqueAnnotation = _uniqueChecker.hasAnnotationOfExact(f);
147148
if (indexAnnotation != null || hasUniqueAnnotation) {
148149
// Throw if property type does not support any index.
149-
if (fieldType == OBXPropertyType.Float || fieldType == OBXPropertyType.Double || fieldType == OBXPropertyType.ByteVector) {
150+
if (fieldType == OBXPropertyType.Float ||
151+
fieldType == OBXPropertyType.Double ||
152+
fieldType == OBXPropertyType.ByteVector) {
150153
throw InvalidGenerationSourceError(
151154
"in target ${elementBare.name}: @Index/@Unique is not supported for type '${f.type.toString()}' of field '${f.name}'");
152155
}
153156

154157
// TODO Throw if index used on Id property.
155158

156-
157159
// If available use index type from annotation.
158-
var indexFlag;
159160
if (indexAnnotation != null) {
160161
indexFlag = indexAnnotation.getField('flag').toIntValue();
161162
if (indexFlag != null &&
162-
(indexFlag != OBXPropertyFlag.INDEXED
163-
|| indexFlag != OBXPropertyFlag.INDEX_HASH
164-
|| indexFlag != OBXPropertyFlag.INDEX_HASH64)) {
163+
(indexFlag != OBXPropertyFlag.INDEXED ||
164+
indexFlag != OBXPropertyFlag.INDEX_HASH ||
165+
indexFlag != OBXPropertyFlag.INDEX_HASH64)) {
165166
throw InvalidGenerationSourceError(
166167
'in target ${elementBare.name}: @Index flag must be one of [OBXPropertyFlag.INDEXED, OBXPropertyFlag.INDEX_HASH, OBXPropertyFlag.INDEX_HASH64] or none for auto-detection');
167168
}
@@ -195,18 +196,13 @@ class EntityResolver extends Builder {
195196
final prop =
196197
ModelProperty(IdUid.empty(), f.name, fieldType, flags, entity);
197198

198-
// TODO test on @Property(...) that uses the proper flags for index
199-
final isIndexer = flags.isIndexer;
200-
201-
if (isIndexer) {
202-
prop.indexId = indexUid == null ? IdUid.empty() : IdUid(0, indexUid);
203-
}
199+
if (indexFlag != 0) prop.indexId = IdUid.empty();
204200

205201
if (propUid != null) prop.id.uid = propUid;
206202
entity.properties.add(prop);
207203

208204
log.info(
209-
' ${isIndexer ? "index " : ""}property ${prop.name}(${prop.id}) type:${prop.type} flags:${prop.flags}');
205+
' property ${prop.name}(${prop.id}) type:${prop.type} flags:${prop.flags} index:${prop.indexId == null ? "yes" : "no"}');
210206
}
211207

212208
// some checks on the entity's integrity

lib/integration_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class IntegrationTest {
1919
// create a model with a single entity and a single property
2020
final modelInfo = ModelInfo();
2121
final property = ModelProperty(
22-
IdUid(1, int64_max - 1), 'id', OBXPropertyType.Long, 0, null);
22+
IdUid(1, int64_max - 1), 'id', OBXPropertyType.Long);
2323
final entity = ModelEntity(IdUid(1, int64_max), 'entity', modelInfo);
2424
property.entity = entity;
2525
entity.properties.add(property);

lib/src/modelinfo/modelentity.dart

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class ModelEntity {
142142
}
143143
final uniqueUid = uid == 0 ? model.generateUid() : uid;
144144

145-
final property = ModelProperty(IdUid(id, uniqueUid), name, 0, 0, this);
145+
final property = ModelProperty(IdUid(id, uniqueUid), name, 0, entity: this);
146146
properties.add(property);
147147
lastPropertyId = property.id;
148148

@@ -153,18 +153,7 @@ class ModelEntity {
153153
final modelProp = createProperty(prop.name, prop.id.uid);
154154
modelProp.type = prop.type;
155155
modelProp.flags = prop.flags;
156-
157-
if (flags.isIndexer) {
158-
final indexUid = prop.indexId.uid;
159-
if (indexUid != 0 && model.containsUid(indexUid)) {
160-
throw Exception('index uid already exists: $uid');
161-
}
162-
modelProp.indexId = IdUid(_model.lastIndexId.id + 1,
163-
indexUid == 0 ? model.generateUid() : indexUid);
164-
165-
_model.lastIndexId = modelProp.indexId;
166-
}
167-
156+
modelProp.indexId = prop.indexId;
168157
return modelProp;
169158
}
170159

@@ -177,7 +166,7 @@ class ModelEntity {
177166
_properties.remove(foundProp);
178167
model.retiredPropertyUids.add(prop.id.uid);
179168

180-
if (prop.flags.isIndexer) {
169+
if (prop.indexId != null) {
181170
model.retiredIndexUids.add(prop.indexId.uid);
182171
}
183172
}

lib/src/modelinfo/modelinfo.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,10 @@ class ModelInfo {
208208
if (listContains(retiredRelationUids, uid)) return true;
209209
return false;
210210
}
211+
212+
IdUid createIndexId() {
213+
var id = lastIndexId.isEmpty ? 1 : lastIndexId.id + 1;
214+
lastIndexId = IdUid(id, generateUid());
215+
return lastIndexId;
216+
}
211217
}

lib/src/modelinfo/modelproperty.dart

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@ import 'iduid.dart';
44
/// ModelProperty describes a single property of an entity, i.e. its id, name, type and flags.
55
class ModelProperty {
66
IdUid id;
7-
8-
/*late*/
9-
String _name;
10-
11-
/*late*/
12-
int _type, _flags;
7+
/*late*/ String _name;
8+
/*late*/ int _type, _flags;
139
IdUid /*?*/ _indexId;
1410
ModelEntity /*?*/ entity;
1511

@@ -40,25 +36,29 @@ class ModelProperty {
4036
_flags = value /*!*/;
4137
}
4238

43-
ModelProperty(this.id, String /*?*/ name, int /*?*/ type, int /*?*/ flags,
44-
String indexId, this.entity) {
39+
IdUid/*?*/ get indexId => _indexId;
40+
41+
set indexId(IdUid value) => _indexId = value;
42+
43+
ModelProperty(this.id, String /*?*/ name, int /*?*/ type,
44+
{int flags = 0, String/*?*/ indexId, this.entity}) {
4545
this.name = name;
4646
this.type = type;
4747
this.flags = flags;
48-
if (indexId != null) this._indexId = IdUid.fromString(indexId);
48+
this.indexId = indexId == null ? null : IdUid.fromString(indexId);
4949
}
5050

5151
ModelProperty.fromMap(Map<String, dynamic> data, ModelEntity /*?*/ entity)
5252
: this(IdUid.fromString(data['id']), data['name'], data['type'],
53-
data['flags'] ?? 0, data['indexId'], entity);
53+
flags: data['flags'] ?? 0, indexId: data['indexId'], entity: entity);
5454

5555
Map<String, dynamic> toMap() {
5656
final ret = <String, dynamic>{};
5757
ret['id'] = id.toString();
5858
ret['name'] = name;
5959
ret['type'] = type;
6060
if (flags != 0) ret['flags'] = flags;
61-
if (_indexId != null) ret['indexId'] = _indexId /*!*/ .toString();
61+
if (indexId != null) ret['indexId'] = indexId /*!*/ .toString();
6262
return ret;
6363
}
6464

lib/src/util.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,3 @@ class SyncOrObserversExclusive {
6262
}
6363

6464
final syncOrObserversExclusive = SyncOrObserversExclusive();
65-
66-
extension Indexer on int {
67-
bool get isIndexer =>
68-
this & OBXPropertyFlag.INDEXED == 8 ||
69-
this & OBXPropertyFlag.UNIQUE == 32;
70-
}

0 commit comments

Comments
 (0)