Skip to content

Commit d95297f

Browse files
committed
Generate EnumMap without as a field of a class
1 parent 975f88f commit d95297f

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

json_serializable/lib/src/json_serializable_generator.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/dart/element/element.dart';
6+
import 'package:analyzer/dart/element/type.dart';
67
import 'package:build/build.dart';
78
import 'package:json_annotation/json_annotation.dart';
89
import 'package:source_gen/source_gen.dart';
@@ -13,6 +14,7 @@ import 'type_helper.dart';
1314
import 'type_helpers/big_int_helper.dart';
1415
import 'type_helpers/date_time_helper.dart';
1516
import 'type_helpers/duration_helper.dart';
17+
import 'type_helpers/enum_helper.dart';
1618
import 'type_helpers/json_helper.dart';
1719
import 'type_helpers/uri_helper.dart';
1820

@@ -76,6 +78,14 @@ class JsonSerializableGenerator
7678
);
7779
}
7880

81+
final targetType = element.thisType;
82+
if (targetType is InterfaceType && targetType.element.isEnum) {
83+
final memberContent = enumValueMapFromType(targetType);
84+
if (memberContent != null) {
85+
return [enumDecodeHelper, enumDecodeHelperNullable, memberContent];
86+
}
87+
}
88+
7989
final helper = GeneratorHelper(_settings, element, annotation);
8090
return helper.generate();
8191
}

json_serializable/lib/src/type_helpers/enum_helper.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class EnumHelper extends TypeHelper<TypeHelperContextWithConfig> {
2020
String expression,
2121
TypeHelperContextWithConfig context,
2222
) {
23-
final memberContent = _enumValueMapFromType(targetType);
23+
final memberContent = enumValueMapFromType(targetType);
2424

2525
if (memberContent == null) {
2626
return null;
@@ -38,18 +38,18 @@ class EnumHelper extends TypeHelper<TypeHelperContextWithConfig> {
3838
TypeHelperContextWithConfig context,
3939
bool defaultProvided,
4040
) {
41-
final memberContent = _enumValueMapFromType(targetType);
41+
final memberContent = enumValueMapFromType(targetType);
4242

4343
if (memberContent == null) {
4444
return null;
4545
}
4646

47-
context.addMember(_enumDecodeHelper);
47+
context.addMember(enumDecodeHelper);
4848

4949
String functionName;
5050
if (targetType.isNullableType || defaultProvided) {
5151
functionName = r'_$enumDecodeNullable';
52-
context.addMember(_enumDecodeHelperNullable);
52+
context.addMember(enumDecodeHelperNullable);
5353
} else {
5454
functionName = r'_$enumDecode';
5555
}
@@ -71,7 +71,7 @@ class EnumHelper extends TypeHelper<TypeHelperContextWithConfig> {
7171
String _constMapName(DartType targetType) =>
7272
'_\$${targetType.element!.name}EnumMap';
7373

74-
String? _enumValueMapFromType(DartType targetType) {
74+
String? enumValueMapFromType(DartType targetType) {
7575
final enumMap = enumFieldsMap(targetType);
7676

7777
if (enumMap == null) {
@@ -86,7 +86,7 @@ String? _enumValueMapFromType(DartType targetType) {
8686
return 'const ${_constMapName(targetType)} = {\n$items\n};';
8787
}
8888

89-
const _enumDecodeHelper = r'''
89+
const enumDecodeHelper = r'''
9090
K _$enumDecode<K, V>(
9191
Map<K, V> enumValues,
9292
Object? source, {
@@ -113,7 +113,7 @@ K _$enumDecode<K, V>(
113113
).key;
114114
}''';
115115

116-
const _enumDecodeHelperNullable = r'''
116+
const enumDecodeHelperNullable = r'''
117117
K? _$enumDecodeNullable<K, V>(
118118
Map<K, V> enumValues,
119119
dynamic source, {

0 commit comments

Comments
 (0)