Skip to content

@Entity(realClass: ) to support some custom code generators #255

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 1 commit into from
Jun 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions generator/integration-tests/part-partof/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# start with an empty project, without a objectbox-model.json
objectbox-model.json
*.freezed.dart
12 changes: 12 additions & 0 deletions generator/integration-tests/part-partof/0.dart
Original file line number Diff line number Diff line change
@@ -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);
});
}
34 changes: 34 additions & 0 deletions generator/integration-tests/part-partof/1.dart
Original file line number Diff line number Diff line change
@@ -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>(EntityT newObject) {
group('${EntityT}', () {
late TestEnv<EntityT> env;
setUp(() => env = TestEnv(getObjectBoxModel()));
tearDown(() => env.close());

test('read/write', () {
env.box.put(newObject);
env.box.get(1);
});
});
}
13 changes: 13 additions & 0 deletions generator/integration-tests/part-partof/lib/frozen.dart
Original file line number Diff line number Diff line change
@@ -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;
}
19 changes: 19 additions & 0 deletions generator/integration-tests/part-partof/lib/json.dart
Original file line number Diff line number Diff line change
@@ -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<String, dynamic> json) =>
_$JsonEntityFromJson(json);

Map<String, dynamic> toJson() => _$JsonEntityToJson(this);
}
24 changes: 24 additions & 0 deletions generator/integration-tests/part-partof/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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: ../../
5 changes: 4 additions & 1 deletion generator/lib/src/entity_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
1 change: 1 addition & 0 deletions objectbox/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## latest

* New `@Entity()` annotation field `type realClass` to support some custom code generators.

## 1.0.0 (2021-05-18)

Expand Down
15 changes: 14 additions & 1 deletion objectbox/lib/src/annotations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down