-
Notifications
You must be signed in to change notification settings - Fork 408
/
Copy pathenum_helpers.dart
110 lines (96 loc) · 3.09 KB
/
enum_helpers.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'json_key.dart';
/// Compare an enum value against a source using case-insensitive.
///
/// Exposed only for code generated by `package:json_serializable`.
/// Not meant to be used directly by user code.
bool $enumCompareCaseInsensitive<V>(V arg1, Object arg2) =>
((arg1 is String) && (arg2 is String))
? (arg1.toLowerCase() == arg2.toLowerCase())
: arg1 == arg2;
/// Compare an enum value against a source.
///
/// Exposed only for code generated by `package:json_serializable`.
/// Not meant to be used directly by user code.
bool $enumCompareStandard<V>(V arg1, Object arg2) => arg1 == arg2;
/// Returns the key associated with value [source] from [enumValues], if one
/// exists.
///
/// If [unknownValue] is not `null` and [source] is not a value in [enumValues],
/// [unknownValue] is returned. Otherwise, an [ArgumentError] is thrown.
///
/// If [source] is `null`, `null` is returned.
///
/// Exposed only for code generated by `package:json_serializable`.
/// Not meant to be used directly by user code.
K? $enumDecodeNullable<K extends Enum, V>(
Map<K, V> enumValues,
Object? source, {
Enum? unknownValue,
bool Function(V arg1, Object arg2)? comparator,
}) {
if (source == null) {
return null;
}
comparator ??= $enumCompareStandard;
for (var entry in enumValues.entries) {
if (comparator(entry.value, source)) {
return entry.key;
}
}
if (unknownValue == JsonKey.nullForUndefinedEnumValue) {
return null;
}
if (unknownValue == null) {
throw ArgumentError(
'`$source` is not one of the supported values: '
'${enumValues.values.join(', ')}',
);
}
if (unknownValue is! K) {
throw ArgumentError.value(
unknownValue,
'unknownValue',
'Must by of type `$K` or `JsonKey.nullForUndefinedEnumValue`.',
);
}
return unknownValue;
}
/// Returns the key associated with value [source] from [enumValues], if one
/// exists.
///
/// If [unknownValue] is not `null` and [source] is not a value in [enumValues],
/// [unknownValue] is returned. Otherwise, an [ArgumentError] is thrown.
///
/// If [source] is `null`, an [ArgumentError] is thrown.
///
/// Exposed only for code generated by `package:json_serializable`.
/// Not meant to be used directly by user code.
K $enumDecode<K extends Enum, V>(
Map<K, V> enumValues,
Object? source, {
K? unknownValue,
bool Function(V arg1, Object arg2)? comparator,
}) {
if (source == null) {
throw ArgumentError(
'A value must be provided. Supported values: '
'${enumValues.values.join(', ')}',
);
}
comparator ??= $enumCompareStandard;
for (var entry in enumValues.entries) {
if (comparator(entry.value, source)) {
return entry.key;
}
}
if (unknownValue == null) {
throw ArgumentError(
'`$source` is not one of the supported values: '
'${enumValues.values.join(', ')}',
);
}
return unknownValue;
}