generateForAnnotatedElement method
- Element element,
- ConstantReader annotation,
- BuildStep buildStep
Implement to return source code to generate for element.
This method is invoked based on finding elements annotated with an
instance of T. The annotation is provided as a ConstantReader.
Supported return values include a single String or multiple String instances within an Iterable or Stream. It is also valid to return a Future of String, Iterable, or Stream. When multiple values are returned through an iterable or stream they will be deduplicated. Typically each value will be an independent unit of code and the deduplication prevents re-defining the same member multiple times. For example if multiple annotated elements may need a specific utility method available it can be output for each one, and the single deduplicated definition can be shared.
Implementations should return null when no content is generated. Empty
or whitespace-only String instances are also ignored.
Implementation
@override
Future<String> generateForAnnotatedElement(
Element element,
ConstantReader annotation,
BuildStep buildStep,
) async {
if (element is! ClassElement) {
throw InvalidGenerationSourceError(
'@Entity can only be applied to classes',
element: element,
);
}
final className = element.name;
final tableName =
annotation.peek('tableName')?.stringValue ??
_tableNameFromClass(className!);
final dbType = _getDatabaseType(annotation);
// Validate that @Id and @PrimaryKey are not used together
_validatePrimaryKeyAnnotations(element);
final fields = <Map<String, dynamic>>[];
final relationships = <Map<String, dynamic>>[];
for (final field in element.fields) {
if (field.isStatic) continue;
final ignoreAnnotation = _getAnnotation(field, 'Ignore');
if (ignoreAnnotation != null) continue;
final columnAnnotation = _getAnnotation(field, 'Column');
final idAnnotation = _getAnnotation(field, 'Id');
final oneToOne = _getAnnotation(field, 'OneToOne');
final oneToMany = _getAnnotation(field, 'OneToMany');
final manyToOne = _getAnnotation(field, 'ManyToOne');
final manyToMany = _getAnnotation(field, 'ManyToMany');
if (oneToOne != null ||
oneToMany != null ||
manyToOne != null ||
manyToMany != null) {
relationships.add({
'fieldName': field.name,
'type': _getRelationType(field),
'annotation': oneToOne ?? oneToMany ?? manyToOne ?? manyToMany,
});
continue;
}
// Process all fields that are not relationships or ignored
final columnName = _getColumnName(field, columnAnnotation);
final sqlType = _getSqlType(field.type, dbType, columnAnnotation);
final isPrimaryKey =
idAnnotation != null ||
(columnAnnotation != null && _isPrimaryKey(columnAnnotation));
fields.add({
'name': field.name,
'type': field.type.getDisplayString(),
'columnName': columnName,
'sqlType': sqlType,
'isPrimaryKey': isPrimaryKey,
'isNullable': field.type.nullabilitySuffix.toString().contains(
'question',
),
'column': columnAnnotation,
'id': idAnnotation,
});
}
// Get source file path for import calculation
final sourceFilePath = buildStep.inputId.path;
// Detect constructor parameters
final constructorParams = _getConstructorParameters(element);
final code = _generateRepositoryCode(
className!,
tableName,
fields,
relationships,
dbType,
sourceFilePath,
constructorParams,
);
// Return the generated code
return code;
}