Skip to content

Commit

Permalink
Remove extra if and fix dynamic check
Browse files Browse the repository at this point in the history
  • Loading branch information
getBoolean committed Oct 30, 2023
1 parent 31ae895 commit 93371e4
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 16 deletions.
7 changes: 1 addition & 6 deletions packages/envied_generator/lib/src/generate_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Iterable<Field> generateFields(

late final Expression result;

if (optional && value == null) {
if (value == null) {
// Early return if null, so need to check for allowed types
if (!field.type.isDartCoreInt &&
!field.type.isDartCoreDouble &&
Expand All @@ -46,11 +46,6 @@ Iterable<Field> generateFields(
..assignment = Code('null'),
),
];
} else if (value == null) {
throw InvalidGenerationSourceError(
'Environment variable not found for field `${field.name}`.',
element: field,
);
}

if (field.type.isDartCoreInt ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Iterable<Field> generateFieldsEncrypted(
? '?'
: '';

if (optional && value == null) {
if (value == null) {
// Early return if null, so need to check for allowed types
if (!field.type.isDartCoreInt &&
!field.type.isDartCoreBool &&
Expand All @@ -51,11 +51,6 @@ Iterable<Field> generateFieldsEncrypted(
..assignment = Code('null'),
),
];
} else if (value == null) {
throw InvalidGenerationSourceError(
'Environment variable not found for field `${field.name}`.',
element: field,
);
}

if (field.type.isDartCoreInt) {
Expand Down
7 changes: 3 additions & 4 deletions packages/envied_generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,9 @@ final class EnviedGenerator extends GeneratorForAnnotation<Envied> {
config.allowOptionalFields;

// Throw if value is null but the field is not nullable
if (varValue == null &&
field.type is! DynamicType &&
!(optional &&
field.type.nullabilitySuffix == NullabilitySuffix.question)) {
bool isNullable = field.type is DynamicType ||
field.type.nullabilitySuffix == NullabilitySuffix.question;
if (varValue == null && !(optional && isNullable)) {
throw InvalidGenerationSourceError(
'Environment variable not found for field `${field.name}`.',
element: field,
Expand Down

0 comments on commit 93371e4

Please sign in to comment.