Skip to content

Commit

Permalink
refactor(dynamite)!: use a separate value field for the enum value
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolas Rimikis <[email protected]>
  • Loading branch information
Leptopoda committed Dec 20, 2023
1 parent d136b79 commit 8701763
Show file tree
Hide file tree
Showing 24 changed files with 3,596 additions and 2,083 deletions.
77 changes: 40 additions & 37 deletions packages/dynamite/dynamite/lib/src/builder/resolve_enum.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,22 @@ TypeResult resolveEnum(
enumType = 'Object';
}

final values = <String, Object?>{};
final values = <({String dartName, Object? value, String name})>[];
for (final enumValue in schema.$enum!) {
final dartName = toDartName(enumValue.toString());
final name = enumValue.toString();
final dartName = toDartName(name);
var value = enumValue.value;
if (enumValue.isString) {
value = "r'$value'";
}

values[dartName] = value;
values.add((dartName: dartName, value: value, name: name));
}

final $class = Class(
(final b) => b
..name = identifier
..extend = refer('DynamiteEnum<$enumType>')
..extend = refer('EnumClass')
..constructors.add(
Constructor(
(final b) => b
Expand All @@ -49,60 +50,62 @@ TypeResult resolveEnum(
),
)
..fields.addAll(
values.entries.map(
values.map(
(final enumValue) => Field(
(final b) {
b
..name = enumValue.key
..name = enumValue.dartName
..static = true
..modifier = FieldModifier.constant
..type = refer(identifier)
..assignment = Code('$identifier._(${enumValue.value})');
..assignment = Code(
'_\$${toCamelCase('$identifier${enumValue.dartName.capitalize()}')}',
);

if (enumValue.name != enumValue.dartName) {
b.annotations.add(
refer('BuiltValueEnumConst').call([], {
'wireName': refer("r'${enumValue.name}'"),
}),
);
}
},
),
),
)
..methods.addAll([
Method((final b) {
b
Method(
(final b) => b
..name = 'values'
..returns = refer('BuiltSet<$identifier>')
..lambda = true
..static = true
..type = MethodType.getter;

final buffer = StringBuffer()
..writeln('BuiltSet<$identifier>(const <$identifier>[')
..writeAll(values.entries.map((final enumValue) => '$identifier.${enumValue.key}'), ',\n')
..writeln(',')
..write('])');

b.body = Code(buffer.toString());
}),
Method((final b) {
b
..body = Code('_\$${toCamelCase(identifier)}Values')
..type = MethodType.getter,
),
Method(
(final b) => b
..name = 'valueOf'
..returns = refer(identifier)
..lambda = true
..static = true
..requiredParameters.add(
Parameter(
(final b) => b
..name = 'name'
..type = refer(enumType),
..type = refer('String'),
),
);
final buffer = StringBuffer()..writeln('switch (name) {');

for (final enumValue in values.entries) {
buffer
..writeln('case ${enumValue.value}:')
..writeln('return $identifier.${enumValue.key};');
}

buffer.writeln('default: throw ArgumentError(name); }');

b.body = Code(buffer.toString());
}),
)
..body = Code('_\$valueOf$identifier(name)'),
),
Method(
(final b) => b
..returns = refer(enumType)
..name = 'value'
..type = MethodType.getter
..lambda = true
..body = Code('jsonSerializers.deserializeWith(serializer, this)! as $enumType'),
),
buildSerializer(identifier, isCustom: true),
]),
);
Expand All @@ -126,7 +129,7 @@ TypeResult resolveEnum(
final buffer = StringBuffer()
..writeln('<$identifier, $enumType>{')
..writeAll(
values.entries.map((final enumValue) => '$identifier.${enumValue.key}:${enumValue.value}'),
values.map((final enumValue) => '$identifier.${enumValue.dartName}: ${enumValue.value}'),
',\n',
)
..writeln(',')
Expand All @@ -143,7 +146,7 @@ TypeResult resolveEnum(
final buffer = StringBuffer()
..writeln('<$enumType, $identifier>{')
..writeAll(
values.entries.map((final enumValue) => '${enumValue.value}:$identifier.${enumValue.key}'),
values.map((final enumValue) => '${enumValue.value}: $identifier.${enumValue.dartName}'),
',\n',
)
..writeln(',')
Expand Down
2 changes: 1 addition & 1 deletion packages/dynamite/dynamite/lib/src/builder/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class State {
/// Wether the state contains resolved types that need the built_value generator.
bool get hasResolvedBuiltTypes => resolvedTypes
.where(
(final type) => type is TypeResultObject && type.className != 'ContentString',
(final type) => type is TypeResultEnum || type is TypeResultObject && type.className != 'ContentString',
)
.isNotEmpty;
}
Loading

0 comments on commit 8701763

Please sign in to comment.