Skip to content

Automatically generate IDs and UIDs for entities and properties #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 42 commits into from
Oct 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
59486e5
Update gitignore to include generated files
nalenz-objectbox Sep 17, 2019
33f93fa
Adjust library for new generator output format
nalenz-objectbox Sep 17, 2019
3772fa3
Adjust model generator to load separate JSON file
nalenz-objectbox Sep 17, 2019
8593d0e
Use global objectbox_models.json
nalenz-objectbox Sep 17, 2019
233650b
Remove explicit ID and UID annotations from model definitions
nalenz-objectbox Sep 17, 2019
14185d0
Rename objectbox_models.json to objectbox-models.json
nalenz-objectbox Sep 18, 2019
4dfb7e0
Add full support for new JSON structure for models
nalenz-objectbox Sep 19, 2019
3090ec0
Refactor generator modelinfo
nalenz-objectbox Sep 19, 2019
b95f1d6
Implement remaining generator modelinfo functions
nalenz-objectbox Sep 19, 2019
2f19f8e
Fix 63 bit random number generation
nalenz-objectbox Sep 19, 2019
dddaa2e
Move long generated code chunks to separate file
nalenz-objectbox Sep 19, 2019
980be8a
Make generator be based fully on modelinfo classes
nalenz-objectbox Sep 19, 2019
bc02ced
Add possibility to manually specify UIDs for entities and properties
nalenz-objectbox Sep 19, 2019
b78909b
Add merge functionality for new entities
nalenz-objectbox Sep 23, 2019
a9af161
Fix handling of duplicate entity or property names
nalenz-objectbox Sep 23, 2019
021ba63
Implement entity merging involving added or removed properties
nalenz-objectbox Sep 23, 2019
315e468
Correctly merge properties in generator
nalenz-objectbox Sep 24, 2019
77f10dd
Remove Note class from test.dart
nalenz-objectbox Sep 24, 2019
3fde1b2
Edit generated model version to be 1
nalenz-objectbox Sep 24, 2019
00ac886
Adjust generator to treat unannotated fields as normal properties
nalenz-objectbox Sep 24, 2019
92c824c
Add test field TestEntity.number
nalenz-objectbox Sep 24, 2019
4cdfec0
Remove .g.dart files
nalenz-objectbox Sep 24, 2019
f7cf7c4
Add generated .g.dart files to gitignore
nalenz-objectbox Sep 24, 2019
05cdfac
Upgrade Flutter demo and adjust it to work with automatic ID generation
nalenz-objectbox Sep 24, 2019
7a2dead
Rename objectbox-models.json to objectbox-model.json
nalenz-objectbox Sep 26, 2019
f96d051
Edit generated model version to be 5
nalenz-objectbox Sep 26, 2019
d7195f2
Add all fields to generator ModelInfo
nalenz-objectbox Sep 26, 2019
6dbd200
Omit entity property flags when none are present
nalenz-objectbox Sep 26, 2019
11e26e8
Remove option to have custom property types
nalenz-objectbox Sep 26, 2019
3d2f294
Move class annotation definitions to separate file
nalenz-objectbox Oct 1, 2019
931c0d0
Move generator modelinfo to objectbox package
nalenz-objectbox Oct 1, 2019
169c3dc
Refactor to use as few maps as possible
nalenz-objectbox Oct 2, 2019
8d043d7
Move id property name search to ModelEntity
nalenz-objectbox Oct 2, 2019
6fa4333
move model validation from box to store
vaind Oct 2, 2019
d0cce26
make OBXDefs typeful and simplify usage
vaind Oct 2, 2019
ff5a3bf
Rename test.dart to box_test.dart
nalenz-objectbox Oct 2, 2019
77dedd0
Add tests for objectbox_model_generator
nalenz-objectbox Oct 2, 2019
7f58f65
Divide main tests into separate files
nalenz-objectbox Oct 2, 2019
ebf1e6c
Update Flutter demo to use new models
nalenz-objectbox Oct 2, 2019
e07ee2e
Remove database directory from Flutter demo
nalenz-objectbox Oct 2, 2019
7f14318
Merge branch 'dev' into 18-automatic-id-generation
vaind Oct 3, 2019
956f5a8
execute generator tests in CI
vaind Oct 3, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,31 @@ name: Dart CI
on: [push, pull_request]

jobs:
build:
generator:
runs-on: ubuntu-latest
container:
image: google/dart:latest
steps:
- uses: actions/checkout@v1
- name: Install dependencies
working-directory: bin/objectbox_model_generator
run: pub get
- name: Run tests
working-directory: bin/objectbox_model_generator
run: pub run test

lib:
needs: generator
runs-on: ubuntu-latest

container:
image: google/dart:latest

steps:
- uses: actions/checkout@v1
- name: Install ObjectBox C-API
run: ./install.sh
- name: Install dependencies
run: pub get
- name: Generate ObjectBox models
run: pub run build_runner build
- name: Run tests
run: pub run test
- uses: actions/checkout@v1
- name: Install ObjectBox C-API
run: ./install.sh
- name: Install dependencies
run: pub get
- name: Generate ObjectBox models
run: pub run build_runner build
- name: Run tests
run: pub run test
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
**/pubspec.lock
misc/
.idea/
**/*.g.dart
download/
lib/*.dll
lib/*.dylib
lib/*.so
lib/*.a
.vscode/
**/*.g.dart
46 changes: 46 additions & 0 deletions bin/objectbox_model_generator/lib/src/code_chunks.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import "package:objectbox/src/modelinfo/index.dart";

class CodeChunks {
static String modelInfoLoader(String allModelsJsonFilename) => """
Map<int, ModelEntity> _allOBXModelEntities = null;

void _loadOBXModelEntities() {
if (FileSystemEntity.typeSync("objectbox-model.json") == FileSystemEntityType.notFound)
throw Exception("objectbox-model.json not found");

_allOBXModelEntities = {};
ModelInfo modelInfo = ModelInfo.fromMap(json.decode(new File("objectbox-model.json").readAsStringSync()));
modelInfo.entities.forEach((e) => _allOBXModelEntities[e.id.uid] = e);
}

ModelEntity _getOBXModelEntity(int entityUid) {
if (_allOBXModelEntities == null) _loadOBXModelEntities();
if (!_allOBXModelEntities.containsKey(entityUid))
throw Exception("entity uid missing in objectbox-model.json: \$entityUid");
return _allOBXModelEntities[entityUid];
}
""";

static String instanceBuildersReaders(ModelEntity readEntity) {
String name = readEntity.name;
return """
ModelEntity _${name}_OBXModelGetter() {
return _getOBXModelEntity(${readEntity.id.uid});
}

$name _${name}_OBXBuilder(Map<String, dynamic> members) {
$name r = new $name();
${readEntity.properties.map((p) => "r.${p.name} = members[\"${p.name}\"];").join()}
return r;
}

Map<String, dynamic> _${name}_OBXReader($name inst) {
Map<String, dynamic> r = {};
${readEntity.properties.map((p) => "r[\"${p.name}\"] = inst.${p.name};").join()}
return r;
}

const ${name}_OBXDefs = EntityDefinition<${name}>(_${name}_OBXModelGetter, _${name}_OBXReader, _${name}_OBXBuilder);
""";
}
}
211 changes: 107 additions & 104 deletions bin/objectbox_model_generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
@@ -1,125 +1,128 @@
import "dart:async";
import "dart:convert";
import "dart:io";
import "package:analyzer/dart/element/element.dart";
import "package:build/src/builder/build_step.dart";
import "package:source_gen/source_gen.dart";

import "package:objectbox/objectbox.dart";
import "package:objectbox/objectbox.dart" as obx;
import "package:objectbox/src/bindings/constants.dart";

class EntityGenerator extends GeneratorForAnnotation<Entity> {
import "code_chunks.dart";
import "merge.dart";
import "package:objectbox/src/modelinfo/index.dart";

class EntityGenerator extends GeneratorForAnnotation<obx.Entity> {
static const ALL_MODELS_JSON = "objectbox-model.json";

// each .g.dart file needs to get a header with functions to load the ALL_MODELS_JSON file exactly once. Store the input .dart file ids this has already been done for here
List<String> entityHeaderDone = [];

Future<ModelInfo> _loadModelInfo() async {
if ((await FileSystemEntity.type(ALL_MODELS_JSON)) == FileSystemEntityType.notFound)
return ModelInfo.createDefault();
return ModelInfo.fromMap(json.decode(await (new File(ALL_MODELS_JSON).readAsString())));
}

@override
FutureOr<String> generateForAnnotatedElement(Element elementBare, ConstantReader annotation, BuildStep buildStep) {
if (elementBare is! ClassElement)
throw InvalidGenerationSourceError("in target ${elementBare.name}: annotated element isn't a class");

// get basic entity info
var entity = Entity(id: annotation.read('id').intValue, uid: annotation.read('uid').intValue);
var element = elementBare as ClassElement;
var ret = """
const _${element.name}_OBXModel = {
"entity": {
"name": "${element.name}",
"id": ${entity.id},
"uid": ${entity.uid}
},
"properties": [
""";

// read all suitable annotated properties
var props = [];
String idPropertyName;
for (var f in element.fields) {
if (f.metadata == null || f.metadata.length != 1) // skip unannotated fields
continue;
var annotElmt = f.metadata[0].element as ConstructorElement;
var annotType = annotElmt.returnType.toString();
var annotVal = f.metadata[0].computeConstantValue();
var fieldTypeObj = annotVal.getField("type");
int fieldType = fieldTypeObj == null ? null : fieldTypeObj.toIntValue();

var prop = {
"name": f.name,
"id": annotVal.getField("id").toIntValue(),
"uid": annotVal.getField("uid").toIntValue(),
"flags": 0,
};

if (annotType == "Id") {
if (idPropertyName != null)
throw InvalidGenerationSourceError(
"in target ${elementBare.name}: has more than one properties annotated with @Id");
if (fieldType != null)
throw InvalidGenerationSourceError(
"in target ${elementBare.name}: programming error: @Id property may not specify a type");
if (f.type.toString() != "int")
throw InvalidGenerationSourceError(
"in target ${elementBare.name}: field with @Id property has type '${f.type.toString()}', but it must be 'int'");

fieldType = OBXPropertyType.Long;
prop["flags"] = OBXPropertyFlag.ID;
idPropertyName = f.name;
} else if (annotType == "Property") {
// nothing special here
} else {
// skip unknown annotations
continue;
}
Future<String> generateForAnnotatedElement(
Element elementBare, ConstantReader annotation, BuildStep buildStep) async {
try {
if (elementBare is! ClassElement)
throw InvalidGenerationSourceError("in target ${elementBare.name}: annotated element isn't a class");
var element = elementBare as ClassElement;

if (fieldType == null) {
var fieldTypeStr = f.type.toString();
if (fieldTypeStr == "int")
fieldType = OBXPropertyType.Int;
else if (fieldTypeStr == "String")
fieldType = OBXPropertyType.String;
else {
print(
"warning: skipping field '${f.name}' in entity '${element.name}', as it has the unsupported type '$fieldTypeStr'");
continue;
}
// load existing model from JSON file if possible
String inputFileId = buildStep.inputId.toString();
ModelInfo allModels = await _loadModelInfo();

// optionally add header for loading the .g.json file
var ret = "";
if (entityHeaderDone.indexOf(inputFileId) == -1) {
ret += CodeChunks.modelInfoLoader(ALL_MODELS_JSON);
entityHeaderDone.add(inputFileId);
}

prop["type"] = fieldType;
props.add(prop);
ret += """
{
"name": "${prop['name']}",
"id": ${prop['id']},
"uid": ${prop['uid']},
"type": ${prop['type']},
"flags": ${prop['flags']},
},
""";
}
// process basic entity (note that allModels.createEntity is not used, as the entity will be merged)
ModelEntity readEntity = new ModelEntity(IdUid.empty(), null, element.name, [], allModels);
var entityUid = annotation.read("uid");
if (entityUid != null && !entityUid.isNull) readEntity.id.uid = entityUid.intValue;

// read all suitable annotated properties
bool hasIdProperty = false;
for (var f in element.fields) {
int fieldType, flags = 0;
int propUid;

// some checks on the entity's integrity
if (idPropertyName == null)
throw InvalidGenerationSourceError("in target ${elementBare.name}: has no properties annotated with @Id");
if (f.metadata != null && f.metadata.length == 1) {
var annotElmt = f.metadata[0].element as ConstructorElement;
var annotType = annotElmt.returnType.toString();
var annotVal = f.metadata[0].computeConstantValue();
var fieldTypeAnnot = null; // for the future, with custom type sizes allowed: annotVal.getField("type");
fieldType = fieldTypeAnnot == null ? null : fieldTypeAnnot.toIntValue();
propUid = annotVal.getField("uid").toIntValue();

// main code for instance builders and readers
ret += """
],
"idPropertyName": "${idPropertyName}",
};
// find property flags
if (annotType == "Id") {
if (hasIdProperty)
throw InvalidGenerationSourceError(
"in target ${elementBare.name}: has more than one properties annotated with @Id");
if (fieldType != null)
throw InvalidGenerationSourceError(
"in target ${elementBare.name}: programming error: @Id property may not specify a type");
if (f.type.toString() != "int")
throw InvalidGenerationSourceError(
"in target ${elementBare.name}: field with @Id property has type '${f.type.toString()}', but it must be 'int'");

${element.name} _${element.name}_OBXBuilder(Map<String, dynamic> members) {
${element.name} r = new ${element.name}();
${props.map((p) => "r.${p['name']} = members[\"${p['name']}\"];").join()}
return r;
fieldType = OBXPropertyType.Long;
flags |= OBXPropertyFlag.ID;
hasIdProperty = true;
} else if (annotType == "Property") {
// nothing special
} else {
// skip unknown annotations
print(
"warning: skipping field '${f.name}' in entity '${element.name}', as it has the unknown annotation type '$annotType'");
continue;
}
}

Map<String, dynamic> _${element.name}_OBXReader(${element.name} inst) {
Map<String, dynamic> r = {};
${props.map((p) => "r[\"${p['name']}\"] = inst.${p['name']};").join()}
return r;
if (fieldType == null) {
var fieldTypeStr = f.type.toString();
if (fieldTypeStr == "int")
fieldType = OBXPropertyType.Int;
else if (fieldTypeStr == "String")
fieldType = OBXPropertyType.String;
else {
print(
"warning: skipping field '${f.name}' in entity '${element.name}', as it has the unsupported type '$fieldTypeStr'");
continue;
}
}

const ${element.name}_OBXDefs = {
"model": _${element.name}_OBXModel,
"builder": _${element.name}_OBXBuilder,
"reader": _${element.name}_OBXReader,
};
""";
// create property (do not use readEntity.createProperty in order to avoid generating new ids)
ModelProperty prop = new ModelProperty(IdUid.empty(), f.name, fieldType, flags, readEntity);
if (propUid != null) prop.id.uid = propUid;
readEntity.properties.add(prop);
}

// some checks on the entity's integrity
if (!hasIdProperty)
throw InvalidGenerationSourceError("in target ${elementBare.name}: has no properties annotated with @Id");

return ret;
// merge existing model and annotated model that was just read, then write new final model to file
mergeEntity(allModels, readEntity);
new File(ALL_MODELS_JSON).writeAsString(new JsonEncoder.withIndent(" ").convert(allModels.toMap()));
readEntity = allModels.findEntityByName(element.name);
if (readEntity == null) return ret;

// main code for instance builders and readers
ret += CodeChunks.instanceBuildersReaders(readEntity);

return ret;
} catch (e, s) {
print(s);
rethrow;
}
}
}
30 changes: 30 additions & 0 deletions bin/objectbox_model_generator/lib/src/merge.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import "package:objectbox/src/modelinfo/index.dart";

void _mergeProperty(ModelEntity entity, ModelProperty prop) {
ModelProperty propInModel = entity.findSameProperty(prop);
if (propInModel == null) {
entity.createCopiedProperty(prop);
} else {
propInModel.type = prop.type;
propInModel.flags = prop.flags;
}
}

void mergeEntity(ModelInfo modelInfo, ModelEntity readEntity) {
// "readEntity" only contains the entity info directly read from the annotations and Dart source (i.e. with missing ID, lastPropertyId etc.)
// "entityInModel" is the entity from the model with all correct id/uid, lastPropertyId etc.
ModelEntity entityInModel = modelInfo.findSameEntity(readEntity);

if (entityInModel == null) {
// in case the entity is created (i.e. when its given UID or name that does not yet exist), we are done, as nothing needs to be merged
modelInfo.createCopiedEntity(readEntity);
} else {
// here, the entity was found already and entityInModel and readEntity might differ, i.e. conflicts need to be resolved, so merge all properties first
readEntity.properties.forEach((p) => _mergeProperty(entityInModel, p));

// them remove all properties not present anymore in readEntity
entityInModel.properties
.where((p) => readEntity.findSameProperty(p) == null)
.forEach((p) => entityInModel.removeProperty(p));
}
}
7 changes: 4 additions & 3 deletions bin/objectbox_model_generator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ version: 0.3.0
description: >-
A preprocessor for Dart source files containing ObjectBox entity definitions.
environment:
sdk: '>=2.5.0-dev.2.0 <3.0.0'
sdk: ">=2.5.0-dev.2.0 <3.0.0"
dependencies:
build: '>=0.12.0 <2.0.0'
build: ">=0.12.0 <2.0.0"
source_gen: ^0.9.0
objectbox:
path: ../..
dev_dependencies:
build_runner: '>=0.9.0 <0.11.0'
build_runner: ">=0.9.0 <0.11.0"
test: ^1.0.0
Loading