|
| 1 | +import "dart:async"; |
| 2 | +import "package:analyzer/dart/element/element.dart"; |
| 3 | +import "package:build/src/builder/build_step.dart"; |
| 4 | +import "package:source_gen/source_gen.dart"; |
| 5 | + |
| 6 | +import "package:objectbox/objectbox.dart"; |
| 7 | +import "package:objectbox/src/bindings/constants.dart"; |
| 8 | + |
| 9 | +class EntityGenerator extends GeneratorForAnnotation<Entity> { |
| 10 | + @override |
| 11 | + FutureOr<String> generateForAnnotatedElement(Element elementBare, ConstantReader annotation, BuildStep buildStep) { |
| 12 | + if(elementBare is! ClassElement) |
| 13 | + throw InvalidGenerationSourceError("in target ${elementBare.name}: annotated element isn't a class"); |
| 14 | + |
| 15 | + // get basic entity info |
| 16 | + var entity = Entity(id: annotation.read('id').intValue, uid: annotation.read('uid').intValue); |
| 17 | + var element = elementBare as ClassElement; |
| 18 | + var ret = """ |
| 19 | + const _${element.name}_OBXModel = { |
| 20 | + "entity": { |
| 21 | + "name": "${element.name}", |
| 22 | + "id": ${entity.id}, |
| 23 | + "uid": ${entity.uid} |
| 24 | + }, |
| 25 | + "properties": [ |
| 26 | + """; |
| 27 | + |
| 28 | + // read all suitable annotated properties |
| 29 | + var props = []; |
| 30 | + String idPropertyName; |
| 31 | + for(var f in element.fields) { |
| 32 | + if(f.metadata == null || f.metadata.length != 1) // skip unannotated fields |
| 33 | + continue; |
| 34 | + var annotElmt = f.metadata[0].element as ConstructorElement; |
| 35 | + var annotType = annotElmt.returnType.toString(); |
| 36 | + var annotVal = f.metadata[0].computeConstantValue(); |
| 37 | + var fieldTypeObj = annotVal.getField("type"); |
| 38 | + int fieldType = fieldTypeObj == null ? null : fieldTypeObj.toIntValue(); |
| 39 | + |
| 40 | + var prop = { |
| 41 | + "name": f.name, |
| 42 | + "id": annotVal.getField("id").toIntValue(), |
| 43 | + "uid": annotVal.getField("uid").toIntValue(), |
| 44 | + "flags": 0, |
| 45 | + }; |
| 46 | + |
| 47 | + if(annotType == "Id") { |
| 48 | + if(idPropertyName != null) |
| 49 | + throw InvalidGenerationSourceError("in target ${elementBare.name}: has more than one properties annotated with @Id"); |
| 50 | + if(fieldType != null) |
| 51 | + throw InvalidGenerationSourceError("in target ${elementBare.name}: programming error: @Id property may not specify a type"); |
| 52 | + if(f.type.toString() != "int") |
| 53 | + throw InvalidGenerationSourceError("in target ${elementBare.name}: field with @Id property has type '${f.type.toString()}', but it must be 'int'"); |
| 54 | + |
| 55 | + fieldType = OBXPropertyType.Long; |
| 56 | + prop["flags"] = OBXPropertyFlag.ID; |
| 57 | + idPropertyName = f.name; |
| 58 | + } else if(annotType == "Property") { |
| 59 | + // nothing special here |
| 60 | + } else { |
| 61 | + // skip unknown annotations |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + if(fieldType == null) { |
| 66 | + var fieldTypeStr = f.type.toString(); |
| 67 | + if(fieldTypeStr == "int") |
| 68 | + fieldType = OBXPropertyType.Int; |
| 69 | + else if(fieldTypeStr == "String") |
| 70 | + fieldType = OBXPropertyType.String; |
| 71 | + else { |
| 72 | + print("warning: skipping field '${f.name}' in entity '${element.name}', as it has the unsupported type '$fieldTypeStr'"); |
| 73 | + continue; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + prop["type"] = fieldType; |
| 78 | + props.add(prop); |
| 79 | + ret += """ |
| 80 | + { |
| 81 | + "name": "${prop['name']}", |
| 82 | + "id": ${prop['id']}, |
| 83 | + "uid": ${prop['uid']}, |
| 84 | + "type": ${prop['type']}, |
| 85 | + "flags": ${prop['flags']}, |
| 86 | + "flatbuffers_id": ${(prop['id'] as int) - 1}, |
| 87 | + }, |
| 88 | + """; |
| 89 | + } |
| 90 | + |
| 91 | + // some checks on the entity's integrity |
| 92 | + if(idPropertyName == null) |
| 93 | + throw InvalidGenerationSourceError("in target ${elementBare.name}: has no properties annotated with @Id"); |
| 94 | + |
| 95 | + // main code for instance builders and readers |
| 96 | + ret += """ |
| 97 | + ], |
| 98 | + "idPropertyName": "${idPropertyName}", |
| 99 | + }; |
| 100 | +
|
| 101 | + ${element.name} _${element.name}_OBXBuilder(Map<String, dynamic> members) { |
| 102 | + ${element.name} r = new ${element.name}(); |
| 103 | + ${props.map((p) => "r.${p['name']} = members[\"${p['name']}\"];").join()} |
| 104 | + return r; |
| 105 | + } |
| 106 | +
|
| 107 | + Map<String, dynamic> _${element.name}_OBXReader(${element.name} inst) { |
| 108 | + Map<String, dynamic> r = {}; |
| 109 | + ${props.map((p) => "r[\"${p['name']}\"] = inst.${p['name']};").join()} |
| 110 | + return r; |
| 111 | + } |
| 112 | +
|
| 113 | + const ${element.name}_OBXDefs = { |
| 114 | + "model": _${element.name}_OBXModel, |
| 115 | + "builder": _${element.name}_OBXBuilder, |
| 116 | + "reader": _${element.name}_OBXReader, |
| 117 | + }; |
| 118 | + """; |
| 119 | + |
| 120 | + return ret; |
| 121 | + } |
| 122 | +} |
0 commit comments