11import 'dart:async' ;
22import 'dart:convert' ;
33
4+ import 'package:analyzer/dart/constant/value.dart' ;
45import 'package:analyzer/dart/element/element.dart' ;
56import 'package:analyzer/dart/element/type.dart' ;
67import 'package:build/build.dart' ;
78import 'package:objectbox/objectbox.dart' as obx;
89import 'package:objectbox/src/bindings/bindings.dart' ;
10+ import 'package:objectbox/src/bindings/helpers.dart' ;
911import 'package:objectbox/src/modelinfo/index.dart' ;
1012import 'package:source_gen/source_gen.dart' ;
1113
@@ -67,8 +69,7 @@ class EntityResolver extends Builder {
6769 entity.flags | = OBXEntityFlags .SYNC_ENABLED ;
6870 }
6971
70- log.info ('entity ${entity .name }(${entity .id }), sync=' +
71- (entity.hasFlag (OBXEntityFlags .SYNC_ENABLED ) ? 'ON' : 'OFF' ));
72+ log.info (entity);
7273
7374 // getters, ... (anything else?)
7475 final readOnlyFields = < String , bool > {};
@@ -91,7 +92,8 @@ class EntityResolver extends Builder {
9192 continue ;
9293 }
9394
94- int fieldType, flags = 0 ;
95+ int fieldType;
96+ var flags = 0 ;
9597 int propUid;
9698
9799 if (_idChecker.hasAnnotationOfExact (f)) {
@@ -114,7 +116,8 @@ class EntityResolver extends Builder {
114116 } else if (_propertyChecker.hasAnnotationOfExact (f)) {
115117 final _propertyAnnotation = _propertyChecker.firstAnnotationOfExact (f);
116118 propUid = _propertyAnnotation.getField ('uid' ).toIntValue ();
117- fieldType = _propertyAnnotation.getField ('type' ).toIntValue ();
119+ fieldType =
120+ propertyTypeFromAnnotation (_propertyAnnotation.getField ('type' ));
118121 flags = _propertyAnnotation.getField ('flag' ).toIntValue () ?? 0 ;
119122 }
120123
@@ -147,14 +150,12 @@ class EntityResolver extends Builder {
147150 flags: flags, entity: entity);
148151
149152 // Index and unique annotation.
150- final indexTypeStr =
151- processAnnotationIndexUnique (f, fieldType, elementBare, prop);
153+ processAnnotationIndexUnique (f, fieldType, elementBare, prop);
152154
153155 if (propUid != null ) prop.id.uid = propUid;
154156 entity.properties.add (prop);
155157
156- log.info (
157- ' property ${prop .name }(${prop .id }) type:${prop .type } flags:${prop .flags } ${prop .hasIndexFlag () ? "index:${indexTypeStr }" : "" }' );
158+ log.info (' ${prop }' );
158159 }
159160
160161 // some checks on the entity's integrity
@@ -166,7 +167,7 @@ class EntityResolver extends Builder {
166167 return entity;
167168 }
168169
169- String processAnnotationIndexUnique (FieldElement f, int fieldType,
170+ void processAnnotationIndexUnique (FieldElement f, int fieldType,
170171 Element elementBare, obx.ModelProperty prop) {
171172 obx.IndexType indexType;
172173
@@ -233,16 +234,35 @@ class EntityResolver extends Builder {
233234 switch (indexType) {
234235 case obx.IndexType .value:
235236 prop.flags | = OBXPropertyFlags .INDEXED ;
236- return 'value' ;
237+ break ;
237238 case obx.IndexType .hash:
238239 prop.flags | = OBXPropertyFlags .INDEX_HASH ;
239- return 'hash' ;
240+ break ;
240241 case obx.IndexType .hash64:
241242 prop.flags | = OBXPropertyFlags .INDEX_HASH64 ;
242- return 'hash64' ;
243+ break ;
243244 default :
244245 throw InvalidGenerationSourceError (
245246 'in target ${elementBare .name }: invalid index type: $indexType ' );
246247 }
247248 }
249+
250+ // find out @Property(type:) field value - its an enum PropertyType
251+ int /*?*/ propertyTypeFromAnnotation (DartObject typeField) {
252+ if (typeField.isNull) return null ;
253+ final enumValues = (typeField.type as InterfaceType )
254+ .element
255+ .fields
256+ .where ((f) => f.isEnumConstant)
257+ .toList ();
258+
259+ // Find the index of the matching enum constant.
260+ for (var i = 0 ; i < enumValues.length; i++ ) {
261+ if (enumValues[i].computeConstantValue () == typeField) {
262+ return propertyTypeToOBXPropertyType (obx.PropertyType .values[i]);
263+ }
264+ }
265+
266+ return null ;
267+ }
248268}
0 commit comments