Skip to content

Commit

Permalink
Use ejson in realm
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed Jun 6, 2023
1 parent 8f380d6 commit 04f579e
Show file tree
Hide file tree
Showing 91 changed files with 5,149 additions and 3,468 deletions.
10 changes: 3 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -580,20 +580,16 @@ jobs:

- name: Delete generated files
# exclude generator and ejson folders
run: find . -name "*.g.dart" -not -path "./generator/*" -not -path "./ejson/*" -delete
run: |
find . -name "*.g.dart" -not -path "./generator/*" -not -path "./ejson/*" -delete
find . -name "*.realm.dart" -not -path "./generator/*" -delete
shell: bash

- name: Run generator in realm-dart repo
run: |
dart pub get
dart run build_runner build --delete-conflicting-outputs
- name: Run generator in realm-dart/example
run: |
dart pub get
dart run build_runner build --delete-conflicting-outputs
working-directory: ./example/

- name: Run generator in realm_flutter/example
run: |
dart pub get
Expand Down
21 changes: 19 additions & 2 deletions build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,34 @@ targets:
generate_for:
- lib/src/cli/metrics/flutter_info.dart
- lib/src/cli/metrics/metrics.dart
realm_generator|realm_object_builder:
realm_generator:
generate_for:
include:
- test/**.dart
- example/**.dart
ejson_generator:
generate_for:
include:
- test/**.dart
- example/**.dart
source_gen|combining_builder:
options:
ignore_for_file:
- "type=lint"

builders:
realm_generator:
import: "package:realm_generator/realm_generator.dart"
builder_factories: ["generateRealmObjects"]
build_extensions: { ".dart": ["realm_objects.g.part"] }
build_extensions: { ".dart": [".realm.dart"] }
auto_apply: dependents
build_to: source
runs_before: ["ejson_generator|ejson_generator"]
ejson_generator:
import: "package:ejson_generator/ejson_generator.dart"
required_inputs: [".realm.dart"]
builder_factories: ["getEJsonGenerator"]
build_extensions: { ".dart": ["ejson.g.part"] }
auto_apply: dependents
build_to: cache
applies_builders: ["source_gen|combining_builder"]
2 changes: 1 addition & 1 deletion ejson/packages/ejson/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ environment:
sdk: ^3.0.1

dependencies:
collection: ^1.17.2
collection: ^1.17.0
ejson_annotation: ^0.1.0
type_plus: ^2.0.0

Expand Down
3 changes: 2 additions & 1 deletion ejson/packages/ejson_generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ class EJsonGenerator extends Generator {
if (getter == null) {
EJsonError.missingGetter.raise();
}
if (getter.returnType != p.type) {
if (!TypeChecker.fromStatic(p.type)
.isAssignableFromType(getter.returnType)) {
EJsonError.mismatchedGetterType.raise();
}
}
Expand Down
4 changes: 2 additions & 2 deletions ejson/packages/ejson_generator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ environment:
dependencies:
analyzer: ^5.13.0
build: ^2.4.0
collection: ^1.17.2
collection: ^1.17.0
ejson_annotation: ^0.1.0
ejson_analyzer: ^0.1.0
source_gen: ^1.3.2
source_span: ^1.10.0
source_span: ^1.9.0

dev_dependencies:
build_runner: ^2.4.4
Expand Down
6 changes: 5 additions & 1 deletion example/bin/myapp.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import 'package:ejson_annotation/ejson_annotation.dart';
import 'package:ejson/ejson.dart';

import 'dart:async';
import 'dart:io';
import 'package:realm_dart/realm.dart';

part 'myapp.realm.dart';
part 'myapp.g.dart';

@RealmModel()
Expand Down Expand Up @@ -57,7 +61,7 @@ void main(List<String> arguments) async {
print("Getting all Tesla cars from the Realm.");
var filteredCars = realm.all<Car>().query("make == 'Tesla'");
print('Found ${filteredCars.length} Tesla cars');

//allow changes event to fire
await Future<void>.delayed(Duration(milliseconds: 1));

Expand Down
145 changes: 42 additions & 103 deletions example/bin/myapp.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

120 changes: 120 additions & 0 deletions example/bin/myapp.realm.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'myapp.dart';

// **************************************************************************
// RealmObjectGenerator
// **************************************************************************

class Car extends _Car with RealmEntity, RealmObjectBase, RealmObject {
static var _defaultsSet = false;

@ejson
Car(
String make, {
String? model,
int? kilometers = 500,
Person? owner,
}) {
if (!_defaultsSet) {
_defaultsSet = RealmObjectBase.setDefaults<Car>({
'kilometers': 500,
});
}
RealmObjectBase.set(this, 'make', make);
RealmObjectBase.set(this, 'model', model);
RealmObjectBase.set(this, 'kilometers', kilometers);
RealmObjectBase.set(this, 'owner', owner);
}

Car._();

@override
String get make => RealmObjectBase.get<String>(this, 'make') as String;
@override
set make(String value) => RealmObjectBase.set(this, 'make', value);

@override
String? get model => RealmObjectBase.get<String>(this, 'model') as String?;
@override
set model(String? value) => RealmObjectBase.set(this, 'model', value);

@override
int? get kilometers => RealmObjectBase.get<int>(this, 'kilometers') as int?;
@override
set kilometers(int? value) => RealmObjectBase.set(this, 'kilometers', value);

@override
Person? get owner => RealmObjectBase.get<Person>(this, 'owner') as Person?;
@override
set owner(covariant Person? value) =>
RealmObjectBase.set(this, 'owner', value);

@override
Stream<RealmObjectChanges<Car>> get changes =>
RealmObjectBase.getChanges<Car>(this);

@override
Car freeze() => RealmObjectBase.freezeObject<Car>(this);

static SchemaObject get schema => _schema ??= _initSchema();
static SchemaObject? _schema;
static SchemaObject _initSchema() {
RealmObjectBase.registerFactory(Car._);
return const SchemaObject(ObjectType.realmObject, Car, 'Car', [
SchemaProperty('make', RealmPropertyType.string),
SchemaProperty('model', RealmPropertyType.string, optional: true),
SchemaProperty('kilometers', RealmPropertyType.int, optional: true),
SchemaProperty('owner', RealmPropertyType.object,
optional: true, linkTarget: 'Person'),
]);
}
}

class Person extends _Person with RealmEntity, RealmObjectBase, RealmObject {
static var _defaultsSet = false;

@ejson
Person(
String name, {
int age = 1,
}) {
if (!_defaultsSet) {
_defaultsSet = RealmObjectBase.setDefaults<Person>({
'age': 1,
});
}
RealmObjectBase.set(this, 'name', name);
RealmObjectBase.set(this, 'age', age);
}

Person._();

@override
String get name => RealmObjectBase.get<String>(this, 'name') as String;
@override
set name(String value) => RealmObjectBase.set(this, 'name', value);

@override
int get age => RealmObjectBase.get<int>(this, 'age') as int;
@override
set age(int value) => RealmObjectBase.set(this, 'age', value);

@override
Stream<RealmObjectChanges<Person>> get changes =>
RealmObjectBase.getChanges<Person>(this);

@override
Person freeze() => RealmObjectBase.freezeObject<Person>(this);

static SchemaObject get schema => _schema ??= _initSchema();
static SchemaObject? _schema;
static SchemaObject _initSchema() {
RealmObjectBase.registerFactory(Person._);
return const SchemaObject(ObjectType.realmObject, Person, 'Person', [
SchemaProperty('name', RealmPropertyType.string),
SchemaProperty('age', RealmPropertyType.int),
]);
}
}
Loading

0 comments on commit 04f579e

Please sign in to comment.