-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change ForeignKeyAction to enum in the generator (#359)
- Loading branch information
Showing
16 changed files
with
222 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 24 additions & 3 deletions
27
floor_generator/lib/misc/extension/dart_object_extension.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,36 @@ | ||
import 'package:analyzer/dart/constant/value.dart'; | ||
import 'package:analyzer/dart/element/type.dart'; | ||
import 'package:floor_annotation/floor_annotation.dart'; | ||
import 'package:meta/meta.dart'; | ||
|
||
extension DartObjectExtension on DartObject { | ||
String toEnumValueString() { | ||
/// get the String representation of the enum value, or the result of | ||
/// [orElse] if the enum was not valid. | ||
String toEnumValueString({@required String orElse()}) { | ||
final interfaceType = type as InterfaceType; | ||
final enumName = interfaceType.getDisplayString(withNullability: false); | ||
final enumValue = interfaceType.element.fields | ||
.where((element) => element.isEnumConstant) | ||
.map((fieldElement) => fieldElement.name) | ||
.singleWhere((valueName) => getField(valueName) != null); | ||
.singleWhere((valueName) => getField(valueName) != null, | ||
orElse: () => null); | ||
if (enumValue == null) { | ||
return orElse(); | ||
} else { | ||
return '$enumName.$enumValue'; | ||
} | ||
} | ||
|
||
return '$enumName.$enumValue'; | ||
/// get the ForeignKeyAction this enum represents, or the result of | ||
/// [orElse] if the enum did not contain a valid value. | ||
ForeignKeyAction toForeignKeyAction({@required ForeignKeyAction orElse()}) { | ||
final enumValueString = toEnumValueString(orElse: () => null); | ||
if (enumValueString == null) { | ||
return orElse(); | ||
} else { | ||
return ForeignKeyAction.values.singleWhere( | ||
(foreignKeyAction) => foreignKeyAction.toString() == enumValueString, | ||
orElse: orElse); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
floor_generator/test/misc/extension/foreign_key_action_extension_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import 'package:floor_annotation/floor_annotation.dart' as annotations; | ||
import 'package:floor_generator/misc/extension/foreign_key_action_extension.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('foreign key action strings', () { | ||
test('NO ACTION', () { | ||
final actual = annotations.ForeignKeyAction.noAction.toSql(); | ||
expect(actual, equals('NO ACTION')); | ||
}); | ||
|
||
test('RESTRICT', () { | ||
final actual = annotations.ForeignKeyAction.restrict.toSql(); | ||
expect(actual, equals('RESTRICT')); | ||
}); | ||
|
||
test('SET NULL', () { | ||
final actual = annotations.ForeignKeyAction.setNull.toSql(); | ||
expect(actual, equals('SET NULL')); | ||
}); | ||
|
||
test('SET DEFAULT', () { | ||
final actual = annotations.ForeignKeyAction.setDefault.toSql(); | ||
expect(actual, equals('SET DEFAULT')); | ||
}); | ||
|
||
test('CASCADE', () { | ||
final actual = annotations.ForeignKeyAction.cascade.toSql(); | ||
expect(actual, equals('CASCADE')); | ||
}); | ||
|
||
test('null throws ArgumentError', () { | ||
expect(() => null.toSql(), throwsArgumentError); | ||
}); | ||
}); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.