Skip to content

Commit 52236b0

Browse files
authored
json_serializable: refactor enum logic (#1204)
Refactor a function into to functions Simplify the return value
1 parent 9143b83 commit 52236b0

File tree

1 file changed

+61
-56
lines changed

1 file changed

+61
-56
lines changed

json_serializable/lib/src/enum_utils.dart

Lines changed: 61 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -27,73 +27,78 @@ String? enumValueMapFromType(
2727
return null;
2828
}
2929

30-
MapEntry<FieldElement, dynamic> generateEntry(FieldElement fe) {
31-
final annotation =
32-
const TypeChecker.fromRuntime(JsonValue).firstAnnotationOfExact(fe);
33-
34-
dynamic fieldValue;
35-
if (annotation == null) {
36-
if (jsonEnum.valueField != null) {
37-
// TODO: fieldRename is pointless here!!! At least log a warning!
38-
39-
final fieldElementType = fe.type.element2 as EnumElement;
40-
41-
final e = fieldElementType.getField(jsonEnum.valueField!);
42-
43-
if (e == null || e.isStatic) {
44-
throw InvalidGenerationSourceError(
45-
'`JsonEnum.valueField` was set to "${jsonEnum.valueField}", but '
46-
'that is not a valid, instance field on '
47-
'`${typeToCode(targetType)}`.',
48-
element: targetType.element2,
49-
);
50-
}
51-
52-
final reader = ConstantReader(fe.computeConstantValue());
53-
final valueReader = reader.read(jsonEnum.valueField!);
54-
if (valueReader.validValueType) {
55-
fieldValue = valueReader.literalValue;
56-
} else {
57-
throw InvalidGenerationSourceError(
58-
'`JsonEnum.valueField` was set to "${jsonEnum.valueField}", but '
59-
'that field does not have a type of String, int, or null.',
60-
element: targetType.element2,
61-
);
62-
}
63-
} else {
64-
fieldValue = encodedFieldName(jsonEnum.fieldRename, fe.name);
65-
}
66-
} else {
67-
final reader = ConstantReader(annotation);
30+
final enumMap = {
31+
for (var field in enumFields)
32+
field: generateEntry(
33+
field: field,
34+
jsonEnum: jsonEnum,
35+
targetType: targetType,
36+
),
37+
};
38+
39+
final items = enumMap.entries
40+
.map((e) => ' ${targetType.element2!.name}.${e.key.name}: '
41+
'${jsonLiteralAsDart(e.value)},')
42+
.join();
43+
44+
return 'const ${constMapName(targetType)} = {\n$items\n};';
45+
}
46+
47+
Object? generateEntry({
48+
required FieldElement field,
49+
required JsonEnum jsonEnum,
50+
required DartType targetType,
51+
}) {
52+
final annotation =
53+
const TypeChecker.fromRuntime(JsonValue).firstAnnotationOfExact(field);
54+
55+
if (annotation == null) {
56+
if (jsonEnum.valueField != null) {
57+
// TODO: fieldRename is pointless here!!! At least log a warning!
58+
59+
final fieldElementType = field.type.element2 as EnumElement;
6860

69-
final valueReader = reader.read('value');
61+
final e = fieldElementType.getField(jsonEnum.valueField!);
7062

63+
if (e == null || e.isStatic) {
64+
throw InvalidGenerationSourceError(
65+
'`JsonEnum.valueField` was set to "${jsonEnum.valueField}", but '
66+
'that is not a valid, instance field on '
67+
'`${typeToCode(targetType)}`.',
68+
element: targetType.element2,
69+
);
70+
}
71+
72+
final reader = ConstantReader(field.computeConstantValue());
73+
final valueReader = reader.read(jsonEnum.valueField!);
7174
if (valueReader.validValueType) {
72-
fieldValue = valueReader.literalValue;
75+
return valueReader.literalValue;
7376
} else {
74-
final targetTypeCode = typeToCode(targetType);
7577
throw InvalidGenerationSourceError(
76-
'The `JsonValue` annotation on `$targetTypeCode.${fe.name}` does '
77-
'not have a value of type String, int, or null.',
78-
element: fe,
78+
'`JsonEnum.valueField` was set to "${jsonEnum.valueField}", but '
79+
'that field does not have a type of String, int, or null.',
80+
element: targetType.element2,
7981
);
8082
}
83+
} else {
84+
return encodedFieldName(jsonEnum.fieldRename, field.name);
8185
}
86+
} else {
87+
final reader = ConstantReader(annotation);
8288

83-
final entry = MapEntry(fe, fieldValue);
89+
final valueReader = reader.read('value');
8490

85-
return entry;
91+
if (valueReader.validValueType) {
92+
return valueReader.literalValue;
93+
} else {
94+
final targetTypeCode = typeToCode(targetType);
95+
throw InvalidGenerationSourceError(
96+
'The `JsonValue` annotation on `$targetTypeCode.${field.name}` does '
97+
'not have a value of type String, int, or null.',
98+
element: field,
99+
);
100+
}
86101
}
87-
88-
final enumMap =
89-
Map<FieldElement, dynamic>.fromEntries(enumFields.map(generateEntry));
90-
91-
final items = enumMap.entries
92-
.map((e) => ' ${targetType.element2!.name}.${e.key.name}: '
93-
'${jsonLiteralAsDart(e.value)},')
94-
.join();
95-
96-
return 'const ${constMapName(targetType)} = {\n$items\n};';
97102
}
98103

99104
const _jsonEnumChecker = TypeChecker.fromRuntime(JsonEnum);

0 commit comments

Comments
 (0)