Skip to content

Commit 7654fb0

Browse files
committed
Downgraded dart sdk version to 3.3.3 and corrected some style issues
1 parent 8db00c6 commit 7654fb0

File tree

14 files changed

+99
-145
lines changed

14 files changed

+99
-145
lines changed

_test_yaml/pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: _test_yaml
22
publish_to: none
33

44
environment:
5-
sdk: ^3.5.0
5+
sdk: ^3.3.3
66

77
resolution: workspace
88

@@ -11,8 +11,8 @@ dev_dependencies:
1111
path: ../shared_test
1212
build_runner: ^2.2.1
1313
build_verify: ^3.0.0
14-
checked_yaml: ^2.0.4-wip
15-
json_annotation: ^4.8.1
14+
checked_yaml: ^2.0.3
15+
json_annotation: ^4.9.0
1616
json_serializable: ^6.8.0
1717
path: ^1.8.2
1818
test: ^1.21.6

checked_yaml/pubspec.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ description: >-
66
package:json_serializable and package:yaml.
77
repository: https://github.com/google/json_serializable.dart/tree/master/checked_yaml
88
topics:
9-
- yaml
10-
- json
11-
- build-runner
12-
- json-serializable
13-
- codegen
9+
- yaml
10+
- json
11+
- build-runner
12+
- json-serializable
13+
- codegen
1414

1515
environment:
16-
sdk: ^3.5.0
16+
sdk: ^3.3.3
1717

1818
resolution: workspace
1919

example/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ name: example
22
publish_to: none
33

44
environment:
5-
sdk: ^3.5.0
5+
sdk: ^3.3.3
66

77
resolution: workspace
88

99
dependencies:
10-
json_annotation: ^4.8.1
10+
json_annotation: ^4.9.0
1111

1212
dev_dependencies:
1313
# Used by tests. Not required to use `json_serializable`.

json_annotation/lib/src/json_key.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ class JsonKey {
147147
///
148148
/// To use `null` for unknown values, use [JsonKey.nullForUndefinedEnumValue].
149149
/// This is only valid for nullable enum fields.
150+
///
151+
/// To skip unknown values, use [JsonKey.skipForUndefinedEnumValue].
152+
/// This will remove all `null` values, essentially making the enum non-nullable.
150153
final Enum? unknownEnumValue;
151154

152155
/// Creates a new [JsonKey] instance.
@@ -176,8 +179,13 @@ class JsonKey {
176179
///
177180
/// Read the documentation on [unknownEnumValue] for more details.
178181
static const Enum nullForUndefinedEnumValue = _NullAsDefault.value;
182+
183+
/// Sentinel value for use with [unknownEnumValue].
184+
///
185+
/// Read the documentation on [unknownEnumValue] for more details.
179186
static const Enum skipForUndefinedEnumValue = _SkipAsDefault.value;
180187
}
181188

182189
enum _NullAsDefault { value }
190+
183191
enum _SkipAsDefault { value }

json_annotation/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: json_annotation
2-
version: 4.8.1
2+
version: 4.9.0
33
description: >-
44
Classes and helper functions that support JSON code generation via the
55
`json_serializable` package.
@@ -11,7 +11,7 @@ topics:
1111
- codegen
1212

1313
environment:
14-
sdk: ^3.5.0
14+
sdk: ^3.3.3
1515

1616
resolution: workspace
1717

json_serializable/lib/src/decode_helper.dart

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,23 @@ mixin DecodeHelper implements HelperCore {
5151

5252
final fromJsonLines = <String>[];
5353

54-
String deserializeFun(String paramOrFieldName,
55-
{ParameterElement? ctorParam}) =>
56-
_deserializeForField(accessibleFields[paramOrFieldName]!,
57-
ctorParam: ctorParam);
54+
String deserializeFun(String paramOrFieldName, {ParameterElement? ctorParam}) =>
55+
_deserializeForField(accessibleFields[paramOrFieldName]!, ctorParam: ctorParam);
5856

5957
final data = _writeConstructorInvocation(
6058
element,
6159
config.constructor,
6260
accessibleFields.keys,
6361
accessibleFields.values
64-
.where((fe) =>
65-
element.augmented
66-
.lookUpSetter(name: fe.name, library: element.library) !=
67-
null)
62+
.where((fe) => element.lookUpSetter(fe.name, element.library) != null)
6863
.map((fe) => fe.name)
6964
.toList(),
7065
unavailableReasons,
7166
deserializeFun,
7267
);
7368

7469
final checks = _checkKeys(
75-
accessibleFields.values
76-
.where((fe) => data.usedCtorParamsAndFields.contains(fe.name)),
70+
accessibleFields.values.where((fe) => data.usedCtorParamsAndFields.contains(fe.name)),
7771
).toList();
7872

7973
if (config.checked) {
@@ -162,26 +156,22 @@ mixin DecodeHelper implements HelperCore {
162156
Iterable<String> _checkKeys(Iterable<FieldElement> accessibleFields) sync* {
163157
final args = <String>[];
164158

165-
String constantList(Iterable<FieldElement> things) =>
166-
'const ${jsonLiteralAsDart(things.map(nameAccess).toList())}';
159+
String constantList(Iterable<FieldElement> things) => 'const ${jsonLiteralAsDart(things.map(nameAccess).toList())}';
167160

168161
if (config.disallowUnrecognizedKeys) {
169162
final allowKeysLiteral = constantList(accessibleFields);
170163

171164
args.add('allowedKeys: $allowKeysLiteral');
172165
}
173166

174-
final requiredKeys =
175-
accessibleFields.where((fe) => jsonKeyFor(fe).required).toList();
167+
final requiredKeys = accessibleFields.where((fe) => jsonKeyFor(fe).required).toList();
176168
if (requiredKeys.isNotEmpty) {
177169
final requiredKeyLiteral = constantList(requiredKeys);
178170

179171
args.add('requiredKeys: $requiredKeyLiteral');
180172
}
181173

182-
final disallowNullKeys = accessibleFields
183-
.where((fe) => jsonKeyFor(fe).disallowNullValue)
184-
.toList();
174+
final disallowNullKeys = accessibleFields.where((fe) => jsonKeyFor(fe).disallowNullValue).toList();
185175
if (disallowNullKeys.isNotEmpty) {
186176
final disallowNullKeyLiteral = constantList(disallowNullKeys);
187177

@@ -220,8 +210,7 @@ mixin DecodeHelper implements HelperCore {
220210
if (config.checked) {
221211
value = deserialize('v');
222212
if (!checkedProperty) {
223-
final readValueBit =
224-
readValueFunc == null ? '' : ',readValue: $readValueFunc,';
213+
final readValueBit = readValueFunc == null ? '' : ',readValue: $readValueFunc,';
225214
value = '\$checkedConvert($jsonKeyName, (v) => $value$readValueBit)';
226215
}
227216
} else {
@@ -231,9 +220,7 @@ mixin DecodeHelper implements HelperCore {
231220
);
232221

233222
value = deserialize(
234-
readValueFunc == null
235-
? 'json[$jsonKeyName]'
236-
: '$readValueFunc(json, $jsonKeyName)',
223+
readValueFunc == null ? 'json[$jsonKeyName]' : '$readValueFunc(json, $jsonKeyName)',
237224
);
238225
}
239226
} on UnsupportedTypeError catch (e) // ignore: avoid_catching_errors
@@ -268,8 +255,7 @@ _ConstructorData _writeConstructorInvocation(
268255
Iterable<String> availableConstructorParameters,
269256
Iterable<String> writableFields,
270257
Map<String, String> unavailableReasons,
271-
String Function(String paramOrFieldName, {ParameterElement ctorParam})
272-
deserializeForField,
258+
String Function(String paramOrFieldName, {ParameterElement ctorParam}) deserializeForField,
273259
) {
274260
final className = classElement.name;
275261

@@ -307,8 +293,7 @@ _ConstructorData _writeConstructorInvocation(
307293
}
308294

309295
// fields that aren't already set by the constructor and that aren't final
310-
final remainingFieldsForInvocationBody =
311-
writableFields.toSet().difference(usedCtorParamsAndFields);
296+
final remainingFieldsForInvocationBody = writableFields.toSet().difference(usedCtorParamsAndFields);
312297

313298
final constructorExtra = constructorName.isEmpty ? '' : '.$constructorName';
314299

@@ -322,17 +307,15 @@ _ConstructorData _writeConstructorInvocation(
322307
buffer
323308
..writeln()
324309
..writeAll(constructorArguments.map((paramElement) {
325-
final content =
326-
deserializeForField(paramElement.name, ctorParam: paramElement);
310+
final content = deserializeForField(paramElement.name, ctorParam: paramElement);
327311
return ' $content,\n';
328312
}));
329313
}
330314
if (namedConstructorArguments.isNotEmpty) {
331315
buffer
332316
..writeln()
333317
..writeAll(namedConstructorArguments.map((paramElement) {
334-
final value =
335-
deserializeForField(paramElement.name, ctorParam: paramElement);
318+
final value = deserializeForField(paramElement.name, ctorParam: paramElement);
336319
return ' ${paramElement.name}: $value,\n';
337320
}));
338321
}

json_serializable/lib/src/field_helpers.dart

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ class _FieldSet implements Comparable<_FieldSet> {
1818
final FieldElement field;
1919
final FieldElement sortField;
2020

21-
_FieldSet._(this.field, this.sortField)
22-
: assert(field.name == sortField.name);
21+
_FieldSet._(this.field, this.sortField) : assert(field.name == sortField.name);
2322

2423
factory _FieldSet(FieldElement? classField, FieldElement? superField) {
2524
// At least one of these will != null, perhaps both.
@@ -30,8 +29,7 @@ class _FieldSet implements Comparable<_FieldSet> {
3029

3130
// Prefer the field that's annotated with `JsonKey`, if any.
3231
// If not, use the class field.
33-
final fieldHasJsonKey =
34-
fields.firstWhere(hasJsonKeyAnnotation, orElse: () => fields.first);
32+
final fieldHasJsonKey = fields.firstWhere(hasJsonKeyAnnotation, orElse: () => fields.first);
3533

3634
return _FieldSet._(fieldHasJsonKey, sortField);
3735
}
@@ -52,8 +50,7 @@ class _FieldSet implements Comparable<_FieldSet> {
5250
return -1;
5351
}
5452

55-
final checkerB = TypeChecker.fromStatic(
56-
(b.enclosingElement as InterfaceElement).thisType);
53+
final checkerB = TypeChecker.fromStatic((b.enclosingElement as InterfaceElement).thisType);
5754

5855
if (checkerB.isAssignableFrom(a.enclosingElement)) {
5956
return 1;
@@ -79,8 +76,8 @@ class _FieldSet implements Comparable<_FieldSet> {
7976
List<FieldElement> createSortedFieldSet(ClassElement element) {
8077
// Get all of the fields that need to be assigned
8178
// TODO: support overriding the field set with an annotation option
82-
final elementInstanceFields = Map.fromEntries(
83-
element.fields.where((e) => !e.isStatic).map((e) => MapEntry(e.name, e)));
79+
final elementInstanceFields =
80+
Map.fromEntries(element.fields.where((e) => !e.isStatic).map((e) => MapEntry(e.name, e)));
8481

8582
final inheritedFields = <String, FieldElement>{};
8683
final manager = InheritanceManager3();
@@ -92,21 +89,17 @@ List<FieldElement> createSortedFieldSet(ClassElement element) {
9289
}
9390

9491
if (v is PropertyAccessorElement && v.isGetter) {
95-
assert(v.variable2 is FieldElement);
96-
final variable = v.variable2 as FieldElement;
92+
assert(v.variable is FieldElement);
93+
final variable = v.variable as FieldElement;
9794
assert(!inheritedFields.containsKey(variable.name));
9895
inheritedFields[variable.name] = variable;
9996
}
10097
}
10198

10299
// Get the list of all fields for `element`
103-
final allFields =
104-
elementInstanceFields.keys.toSet().union(inheritedFields.keys.toSet());
100+
final allFields = elementInstanceFields.keys.toSet().union(inheritedFields.keys.toSet());
105101

106-
final fields = allFields
107-
.map((e) => _FieldSet(elementInstanceFields[e], inheritedFields[e]))
108-
.toList()
109-
..sort();
102+
final fields = allFields.map((e) => _FieldSet(elementInstanceFields[e], inheritedFields[e])).toList()..sort();
110103

111104
return fields.map((fs) => fs.field).toList(growable: false);
112105
}

json_serializable/lib/src/type_helpers/enum_helper.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ class EnumHelper extends TypeHelper<TypeHelperContextWithConfig> {
2929

3030
context.addMember(memberContent);
3131

32-
if (targetType.isNullableType ||
33-
enumFieldWithNullInEncodeMap(targetType) == true) {
32+
if (targetType.isNullableType || enumFieldWithNullInEncodeMap(targetType) == true) {
3433
return '${constMapName(targetType)}[$expression]';
3534
} else {
3635
return '${constMapName(targetType)}[$expression]!';
@@ -52,8 +51,7 @@ class EnumHelper extends TypeHelper<TypeHelperContextWithConfig> {
5251

5352
final jsonKey = jsonKeyForField(context.fieldElement, context.config);
5453

55-
if (!targetType.isNullableType &&
56-
jsonKey.unknownEnumValue == jsonKeyNullForUndefinedEnumValueFieldName) {
54+
if (!targetType.isNullableType && jsonKey.unknownEnumValue == jsonKeyNullForUndefinedEnumValueFieldName) {
5755
// If the target is not nullable,
5856
throw InvalidGenerationSourceError(
5957
'`$jsonKeyNullForUndefinedEnumValueFieldName` cannot be used with '
@@ -65,6 +63,8 @@ class EnumHelper extends TypeHelper<TypeHelperContextWithConfig> {
6563
String functionName;
6664
if (targetType.isNullableType || defaultProvided) {
6765
functionName = r'$enumDecodeNullable';
66+
} else if (jsonKey.unknownEnumValue == r'JsonKey.skipForUndefinedEnumValue') {
67+
functionName = r'$enumDecodeNullable';
6868
} else {
6969
functionName = r'$enumDecode';
7070
}
@@ -74,8 +74,7 @@ class EnumHelper extends TypeHelper<TypeHelperContextWithConfig> {
7474
final args = [
7575
constMapName(targetType),
7676
expression,
77-
if (jsonKey.unknownEnumValue != null)
78-
'unknownValue: ${jsonKey.unknownEnumValue}',
77+
if (jsonKey.unknownEnumValue != null) 'unknownValue: ${jsonKey.unknownEnumValue}',
7978
];
8079

8180
return '$functionName(${args.join(', ')})';

json_serializable/lib/src/type_helpers/iterable_helper.dart

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

55
import 'package:analyzer/dart/element/type.dart';
6-
import 'package:json_annotation/json_annotation.dart';
76
import 'package:source_gen/source_gen.dart' show TypeChecker;
87
import 'package:source_helper/source_helper.dart';
98

@@ -104,10 +103,9 @@ class IterableHelper extends TypeHelper<TypeHelperContextWithConfig> {
104103

105104
if (_coreListChecker.isExactlyType(targetType)) {
106105
final jsonKey = jsonKeyForField(context.fieldElement, context.config);
107-
final shouldRemoveNulls =
108-
targetType.isDartCoreEnum && jsonKey.unknownEnumValue == JsonKey.skipForUndefinedEnumValue.name;
106+
final shouldRemoveNulls = jsonKey.unknownEnumValue == jsonKeyNullForUndefinedEnumValueFieldName;
109107

110-
output += shouldRemoveNulls ? '$optionalQuestion.nonNulls.toList()' : '$optionalQuestion.toList()';
108+
output += '$optionalQuestion${shouldRemoveNulls ? '.nonNulls' : ''}.toList()';
111109
} else if (_coreSetChecker.isExactlyType(targetType)) {
112110
output += '$optionalQuestion.toSet()';
113111
}

0 commit comments

Comments
 (0)