Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
#7. Considering done. We have all data inside the EmittedEntity
Browse files Browse the repository at this point in the history
  • Loading branch information
matei-tm committed Mar 13, 2019
1 parent 0c70d93 commit 67555f4
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 11 deletions.
22 changes: 22 additions & 0 deletions src/lib/exceptions/field_parse_exception.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class FieldParseException implements Exception {
dynamic inner;

String fieldName;

String message;

StackTrace trace;

FieldParseException(this.fieldName, this.inner, this.trace, {this.message});

String toString() {
StringBuffer stringBuffer = StringBuffer();
stringBuffer.writeln('Exception while parsing field: $fieldName!');
if (message != null) {
stringBuffer.writeln("Message: $message");
}
stringBuffer.writeln(inner);
stringBuffer.writeln(trace);
return stringBuffer.toString();
}
}
9 changes: 8 additions & 1 deletion src/lib/generator/emitted_entity.dart
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
class EmittedEntity {}
import 'package:flutter_sqlite_m8_generator/generator/utils/utils.dart';

class EmittedEntity {
final String modelName;
final String entityName;
final Map<String, EntityAttribute> attributes;
EmittedEntity(this.modelName, this.entityName, this.attributes);
}
62 changes: 53 additions & 9 deletions src/lib/generator/model_parser.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
import 'package:analyzer/dart/constant/value.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:flutter_sqlite_m8_generator/exceptions/field_parse_exception.dart';
import 'package:flutter_sqlite_m8_generator/generator/emitted_entity.dart';
import 'package:flutter_sqlite_m8_generator/generator/utils/utils.dart';
import 'package:source_gen/source_gen.dart';

class ModelParser {
String modelName;
final ClassElement modelClassElement;
EntityType entityType;

ConstantReader reader;

ModelParser(this.modelClassElement) {
final ClassElement modelClassElement;
final String entityName;

final Map<String, EntityAttribute> entityAttributes =
<String, EntityAttribute>{};

ModelParser(this.modelClassElement, this.entityName) {
modelName = this.modelClassElement.name;
}

EmittedEntity getEmittedEntity() {
_extractEntityMeta();
_extractEntityAttributes();

final EmittedEntity resultEntity =
EmittedEntity(modelName, entityName, entityAttributes);

return resultEntity;
}

void _extractEntityMeta() {
/// Guard only DbEntity implementations
if (!isDbEntity.isAssignableFromType(modelClassElement.type)) {
throw Exception("Database models must implement `DbEntity` interface!");
}
Expand All @@ -26,7 +41,7 @@ class ModelParser {
(meta) =>
isDataTable.isExactlyType(meta.computeConstantValue().type),
orElse: () => throw Exception(
"Cannot find or parse `DataTable` annotation!"));
"The DbEntity implementations must be annotated with `@DataTable`!"));

reader = ConstantReader(elementAnnotationMetadata.computeConstantValue());

Expand All @@ -42,11 +57,40 @@ class ModelParser {
}
}

EmittedEntity getEmittedEntity() {
_extractEntityMeta();
void _extractEntityAttributes() {
modelClassElement.fields.forEach((f) {
_parseModelField(f);
});
}

final resultEntity = EmittedEntity();
void _parseModelField(FieldElement field) {
try {
if (field.displayName == 'hashCode' ||
field.displayName == 'runtimeType' ||
field.isStatic ||
field.getter == null) return;
//todo final or synthetic

return resultEntity;
List<EntityAttribute> rawEntityAttributes = field.metadata
.map((ElementAnnotation annot) => annot.computeConstantValue())
.where((DartObject d) => isDataColumn.isExactlyType(d.type))
.map((DartObject obj) => EntityAttribute(
field.type.name, field.name, obj.getField('name').toStringValue(),
metadataLevel: obj.getField('metadataLevel').toIntValue()))
.toList();

if (rawEntityAttributes.length > 1) {
throw Exception('Only one Column annotation is allowed on a Field!');
}

if (rawEntityAttributes.length == 0) return;

var firstField = rawEntityAttributes.first;
if (firstField is EntityAttribute) {
entityAttributes[firstField.modelName] = firstField;
}
} catch (exception, stack) {
throw FieldParseException(field.name, exception, stack);
}
}
}
2 changes: 1 addition & 1 deletion src/lib/generator/orm_m8_generator_for_annotation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class OrmM8GeneratorForAnnotation extends GeneratorForAnnotation<DataTable> {
annotation.objectValue.getField('name').toStringValue();
print("Generating entity for $modelName ... $entityName");

ModelParser modelParser = ModelParser(element);
ModelParser modelParser = ModelParser(element, entityName);
final EmittedEntity emittedEntity = modelParser.getEmittedEntity();

final entityWriter = EntityWriter(emittedEntity);
Expand Down
9 changes: 9 additions & 0 deletions src/lib/generator/utils/entity_attribute.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class EntityAttribute {
final String modelTypeName;
final String modelName;

final String attributeName;

EntityAttribute(this.modelTypeName, this.modelName, this.attributeName,
{int metadataLevel});
}
4 changes: 4 additions & 0 deletions src/lib/generator/utils/type_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ bool isIndexed(int value) {
bool isUnique(int value) {
return value & ColumnMetadata.Unique == ColumnMetadata.Unique;
}

bool mustIgnore(int value) {
return value & ColumnMetadata.Ignore == ColumnMetadata.Ignore;
}
1 change: 1 addition & 0 deletions src/lib/generator/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ library orm_m8_generator.core.utils;

export 'package:flutter_sqlite_m8_generator/generator/utils/type_utils.dart';
export 'package:flutter_sqlite_m8_generator/generator/utils/db_entity_type_enum.dart';
export 'package:flutter_sqlite_m8_generator/generator/utils/entity_attribute.dart';

0 comments on commit 67555f4

Please sign in to comment.