Skip to content

Commit

Permalink
fix: Fixed list of enum not parsing correctly #329
Browse files Browse the repository at this point in the history
  • Loading branch information
Seven Du committed Jan 27, 2024
1 parent 80add34 commit fe14c50
Showing 1 changed file with 40 additions and 12 deletions.
52 changes: 40 additions & 12 deletions bin/src/generate_output.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:code_builder/code_builder.dart';
import 'package:orm/dmmf.dart' as dmmf;

import 'generate_enum.dart';
import 'generate_helpers.dart';
import 'generate_type.dart';
import 'generator.dart';
Expand Down Expand Up @@ -106,20 +107,47 @@ extension on Generator {
}

Expression generateFromJsonEnumField(dmmf.OutputField field) {
Method generateClosure(String parameterName, Expression value) {
return Method((builder) {
builder.lambda = true;
builder.requiredParameters.add(Parameter((builder) {
builder.name = parameterName;
}));
builder.body =
refer(parameterName).property('name').equalTo(value).code;
});
}

Expression firstWhereCall(Expression enum$, Expression closure) {
return enum$.property('values').property('firstWhere').call([closure]);
}

Expression makeReurns(Expression value, Expression when) {
return value.notEqualTo(literalNull).conditional(when, literalNull);
}

final enum$ =
generateEnum(field.outputType.type, field.outputType.namespace, false);
final value = refer('json').index(literalString(field.name));
final method = Method((builder) {
builder.lambda = true;
builder.requiredParameters.add(Parameter((builder) {
builder.name = 'e';
}));
builder.body = refer('e').property('name').equalTo(value).code;
});
final deserialize = generateType(field.outputType)
.property('values')
.property('firstWhere')
.call([method.closure]);

return value.notEqualTo(literalNull).conditional(deserialize, literalNull);
if (field.outputType.isList) {
final wrapper = Method((builder) {
builder.lambda = true;
builder.requiredParameters.add(Parameter((builder) {
builder.name = 'child';
}));
builder.body =
firstWhereCall(enum$, generateClosure('e', refer('child')).closure)
.code;
});
final deserialize =
value.asA(refer('Iterable')).property('map').call([wrapper.closure]);

return makeReurns(value, deserialize);
}

return makeReurns(
value, firstWhereCall(enum$, generateClosure('e', value).closure));
}

Constructor generateDefaultConstructor(dmmf.OutputType output) {
Expand Down

1 comment on commit fe14c50

@vercel
Copy link

@vercel vercel bot commented on fe14c50 Jan 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.