/
config_types.dart
126 lines (111 loc) · 4.06 KB
/
config_types.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// 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 'package:analyzer/dart/constant/value.dart';
import 'package:json_annotation/json_annotation.dart';
/// Represents values from [JsonKey] when merged with local configuration.
class KeyConfig {
final String? defaultValue;
final bool disallowNullValue;
final bool ignore;
final bool includeIfNull;
final String name;
final bool required;
final String? unknownEnumValue;
final String? readValueFunctionName;
KeyConfig({
required this.defaultValue,
required this.disallowNullValue,
required this.ignore,
required this.includeIfNull,
required this.name,
required this.readValueFunctionName,
required this.required,
required this.unknownEnumValue,
});
}
/// Represents values from [JsonSerializable] when merged with local
/// configuration.
///
/// Values are all known, so types are non-nullable.
class ClassConfig {
final bool anyMap;
final bool checked;
final String constructor;
final bool createFactory;
final bool createToJson;
final bool disallowUnrecognizedKeys;
final bool explicitToJson;
final FieldRename fieldRename;
final bool genericArgumentFactories;
final bool ignoreUnannotated;
final bool includeIfNull;
final Map<String, String> ctorParamDefaults;
final List<DartObject> converters;
const ClassConfig({
required this.anyMap,
required this.checked,
required this.constructor,
required this.createFactory,
required this.createToJson,
required this.disallowUnrecognizedKeys,
required this.explicitToJson,
required this.fieldRename,
required this.genericArgumentFactories,
required this.ignoreUnannotated,
required this.includeIfNull,
this.converters = const [],
this.ctorParamDefaults = const {},
});
factory ClassConfig.fromJsonSerializable(JsonSerializable config) =>
// #CHANGE WHEN UPDATING json_annotation
ClassConfig(
checked: config.checked ?? ClassConfig.defaults.checked,
anyMap: config.anyMap ?? ClassConfig.defaults.anyMap,
constructor: config.constructor ?? ClassConfig.defaults.constructor,
createFactory:
config.createFactory ?? ClassConfig.defaults.createFactory,
createToJson: config.createToJson ?? ClassConfig.defaults.createToJson,
ignoreUnannotated:
config.ignoreUnannotated ?? ClassConfig.defaults.ignoreUnannotated,
explicitToJson:
config.explicitToJson ?? ClassConfig.defaults.explicitToJson,
includeIfNull:
config.includeIfNull ?? ClassConfig.defaults.includeIfNull,
genericArgumentFactories: config.genericArgumentFactories ??
ClassConfig.defaults.genericArgumentFactories,
fieldRename: config.fieldRename ?? ClassConfig.defaults.fieldRename,
disallowUnrecognizedKeys: config.disallowUnrecognizedKeys ??
ClassConfig.defaults.disallowUnrecognizedKeys,
// TODO typeConverters = []
);
/// An instance of [JsonSerializable] with all fields set to their default
/// values.
static const defaults = ClassConfig(
anyMap: false,
checked: false,
constructor: '',
createFactory: true,
createToJson: true,
disallowUnrecognizedKeys: false,
explicitToJson: false,
fieldRename: FieldRename.none,
genericArgumentFactories: false,
ignoreUnannotated: false,
includeIfNull: true,
);
JsonSerializable toJsonSerializable() => JsonSerializable(
checked: checked,
anyMap: anyMap,
constructor: constructor,
createFactory: createFactory,
createToJson: createToJson,
ignoreUnannotated: ignoreUnannotated,
explicitToJson: explicitToJson,
includeIfNull: includeIfNull,
genericArgumentFactories: genericArgumentFactories,
fieldRename: fieldRename,
disallowUnrecognizedKeys: disallowUnrecognizedKeys,
// TODO typeConverters = []
);
}