diff --git a/generator/integration-tests/part-partof/.gitignore b/generator/integration-tests/part-partof/.gitignore new file mode 100644 index 000000000..502e6ee4f --- /dev/null +++ b/generator/integration-tests/part-partof/.gitignore @@ -0,0 +1,3 @@ +# start with an empty project, without a objectbox-model.json +objectbox-model.json +*.freezed.dart \ No newline at end of file diff --git a/generator/integration-tests/part-partof/0.dart b/generator/integration-tests/part-partof/0.dart new file mode 100644 index 000000000..805fc4046 --- /dev/null +++ b/generator/integration-tests/part-partof/0.dart @@ -0,0 +1,12 @@ +import 'dart:io'; +import 'package:test/test.dart'; +import '../test_env.dart'; + +void main() { + // this is actually a meta test - that `git clean -fX` is run + test('project must be clean before generating the code', () { + expect(TestEnv.dir.existsSync(), false); + expect(File('lib/objectbox.g.dart').existsSync(), false); + expect(File('lib/objectbox-model.json').existsSync(), false); + }); +} diff --git a/generator/integration-tests/part-partof/1.dart b/generator/integration-tests/part-partof/1.dart new file mode 100644 index 000000000..34eceb608 --- /dev/null +++ b/generator/integration-tests/part-partof/1.dart @@ -0,0 +1,34 @@ +import 'dart:io'; + +import 'package:test/test.dart'; + +import '../test_env.dart'; +import '../common.dart'; +import 'lib/json.dart'; +import 'lib/frozen.dart'; +import 'lib/objectbox.g.dart'; + +void main() { + commonModelTests(getObjectBoxModel(), readModelJson('lib')); + + test('project must be generated properly', () { + expect(File('lib/objectbox.g.dart').existsSync(), true); + expect(File('lib/objectbox-model.json').existsSync(), true); + }); + + setupTestsFor(JsonEntity(id: 0, str: 'foo', date: DateTime.now())); + setupTestsFor(FrozenEntity(id: 1, str: 'foo', date: DateTime.now())); +} + +void setupTestsFor(EntityT newObject) { + group('${EntityT}', () { + late TestEnv env; + setUp(() => env = TestEnv(getObjectBoxModel())); + tearDown(() => env.close()); + + test('read/write', () { + env.box.put(newObject); + env.box.get(1); + }); + }); +} diff --git a/generator/integration-tests/part-partof/lib/frozen.dart b/generator/integration-tests/part-partof/lib/frozen.dart new file mode 100644 index 000000000..5523f3b5a --- /dev/null +++ b/generator/integration-tests/part-partof/lib/frozen.dart @@ -0,0 +1,13 @@ +import 'package:objectbox/objectbox.dart'; +import 'package:freezed_annotation/freezed_annotation.dart'; + +part 'frozen.freezed.dart'; + +@freezed +class FrozenEntity with _$FrozenEntity { + @Entity(realClass: FrozenEntity) + factory FrozenEntity( + {@Id(assignable: true) required int id, + required String str, + required DateTime date}) = _FrozenEntity; +} diff --git a/generator/integration-tests/part-partof/lib/json.dart b/generator/integration-tests/part-partof/lib/json.dart new file mode 100644 index 000000000..df4cd6ffa --- /dev/null +++ b/generator/integration-tests/part-partof/lib/json.dart @@ -0,0 +1,19 @@ +import 'package:objectbox/objectbox.dart'; +import 'package:json_annotation/json_annotation.dart'; + +part 'json.g.dart'; + +@Entity() +@JsonSerializable() +class JsonEntity { + int id; + final String str; + final DateTime? date; + + JsonEntity({required this.id, required this.str, required this.date}); + + factory JsonEntity.fromJson(Map json) => + _$JsonEntityFromJson(json); + + Map toJson() => _$JsonEntityToJson(this); +} diff --git a/generator/integration-tests/part-partof/pubspec.yaml b/generator/integration-tests/part-partof/pubspec.yaml new file mode 100644 index 000000000..a9de25e70 --- /dev/null +++ b/generator/integration-tests/part-partof/pubspec.yaml @@ -0,0 +1,24 @@ +name: objectbox_generator_test + +environment: + sdk: ">=2.12.0 <3.0.0" + +dependencies: + objectbox: + json_serializable: + freezed_annotation: + +dev_dependencies: + freezed: + objectbox_generator: + test: + build_runner: + build_test: + io: + path: + +dependency_overrides: + objectbox: + path: ../../../objectbox + objectbox_generator: + path: ../../ \ No newline at end of file diff --git a/generator/lib/src/entity_resolver.dart b/generator/lib/src/entity_resolver.dart index a4467c051..d18a170a2 100644 --- a/generator/lib/src/entity_resolver.dart +++ b/generator/lib/src/entity_resolver.dart @@ -59,9 +59,12 @@ class EntityResolver extends Builder { // process basic entity (note that allModels.createEntity is not used, as the entity will be merged) final entityUid = annotation.read('uid'); + final entityRealClass = annotation.read('realClass'); final entity = ModelEntity.create( IdUid(0, entityUid.isNull ? 0 : entityUid.intValue), - element.name, + entityRealClass.isNull + ? element.name + : entityRealClass.typeValue.element!.name!, null); if (_syncChecker.hasAnnotationOfExact(element)) { diff --git a/objectbox/CHANGELOG.md b/objectbox/CHANGELOG.md index 85b2b5023..37ff4df3c 100644 --- a/objectbox/CHANGELOG.md +++ b/objectbox/CHANGELOG.md @@ -1,5 +1,6 @@ ## latest +* New `@Entity()` annotation field `type realClass` to support some custom code generators. ## 1.0.0 (2021-05-18) diff --git a/objectbox/lib/src/annotations.dart b/objectbox/lib/src/annotations.dart index 38d60d258..0979e998e 100644 --- a/objectbox/lib/src/annotations.dart +++ b/objectbox/lib/src/annotations.dart @@ -17,8 +17,21 @@ class Entity { /// Entity instead of creating a new one. final int? uid; + /// Actual type this entity represents. ObjectBox will use it instead of the + /// `@Entity()`-annotated class's name. For example: + /// ```dart + /// @freezed + /// class Person with _$Person { + /// @Entity(realClass: Person) + /// factory Person( + /// {@Id(assignable: true) required int id, + /// required String name}) = _Person; + /// } + /// ``` + final Type? realClass; + /// Create an Entity annotation. - const Entity({this.uid}); + const Entity({this.uid, this.realClass}); } /// Property annotation enables you to explicitly configure some details about