@@ -27,73 +27,78 @@ String? enumValueMapFromType(
27
27
return null ;
28
28
}
29
29
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 ;
68
60
69
- final valueReader = reader. read ( 'value' );
61
+ final e = fieldElementType. getField (jsonEnum.valueField ! );
70
62
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! );
71
74
if (valueReader.validValueType) {
72
- fieldValue = valueReader.literalValue;
75
+ return valueReader.literalValue;
73
76
} else {
74
- final targetTypeCode = typeToCode (targetType);
75
77
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 ,
79
81
);
80
82
}
83
+ } else {
84
+ return encodedFieldName (jsonEnum.fieldRename, field.name);
81
85
}
86
+ } else {
87
+ final reader = ConstantReader (annotation);
82
88
83
- final entry = MapEntry (fe, fieldValue );
89
+ final valueReader = reader. read ( 'value' );
84
90
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
+ }
86
101
}
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 };' ;
97
102
}
98
103
99
104
const _jsonEnumChecker = TypeChecker .fromRuntime (JsonEnum );
0 commit comments