Skip to content

Commit

Permalink
add type mismatch check in binding
Browse files Browse the repository at this point in the history
  • Loading branch information
codekeyz committed May 9, 2024
1 parent 52950d0 commit 7f73235
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions lib/src/builder/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -252,18 +252,20 @@ final class ParsedEntityClass {

/// Check the field we're binding onto. If provided, validate that if exists
/// if not, use the related class primary key
Symbol? fieldToBind = field.reader.peek('on')?.symbolValue;

if (fieldToBind != null) {
final fields = parsedRelatedClass.allFields.map((e) => Symbol(e.name));
if (!fields.contains(fieldToBind)) {
throw InvalidGenerationSource(
'Field $fieldToBind used in Binding does not exist on ${parsedRelatedClass.className} Entity',
element: field.field,
);
}
} else {
fieldToBind = Symbol(parsedRelatedClass.primaryKey.field.name);
final fieldToBind = field.reader.peek('on')?.symbolValue ?? Symbol(parsedRelatedClass.primaryKey.field.name);
final referencedField = parsedRelatedClass.allFields.firstWhereOrNull((e) => Symbol(e.name) == fieldToBind);
if (referencedField == null) {
throw InvalidGenerationSource(
'Field $fieldToBind used in Binding does not exist on ${parsedRelatedClass.className} Entity',
element: field.field,
);
}

if (referencedField.type != field.field.type) {
throw InvalidGenerationSource(
'Type-mismatch between fields $className.${field.field.name}(${field.field.type}) and ${parsedRelatedClass.className}.${referencedField.name}(${referencedField.type})',
element: field.field,
);
}

bindings[Symbol(field.field.name)] = (entity: parsedRelatedClass, field: fieldToBind, reader: field.reader);
Expand Down

0 comments on commit 7f73235

Please sign in to comment.