trphoenix
2018-11-12 29fbfc5dd1d55d189f23eb6d32f000252f92985f
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (c) 2018, 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/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/dart/constant/value.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:source_gen/source_gen.dart';
 
import 'json_literal_generator.dart';
import 'utils.dart';
 
final _jsonKeyExpando = Expando<JsonKey>();
 
JsonKey jsonKeyForField(FieldElement field, JsonSerializable classAnnotation) =>
    _jsonKeyExpando[field] ??= _from(field, classAnnotation);
 
JsonKey _from(FieldElement element, JsonSerializable classAnnotation) {
  // If an annotation exists on `element` the source is a 'real' field.
  // If the result is `null`, check the getter – it is a property.
  // TODO(kevmoo) setters: github.com/dart-lang/json_serializable/issues/24
  final obj = jsonKeyAnnotation(element);
 
  if (obj == null) {
    return _populateJsonKey(classAnnotation, element);
  }
 
  Object _getLiteral(DartObject dartObject, Iterable<String> things) {
    if (dartObject.isNull) {
      return null;
    }
 
    final reader = ConstantReader(dartObject);
 
    String badType;
    if (reader.isSymbol) {
      badType = 'Symbol';
    } else if (reader.isType) {
      badType = 'Type';
    } else if (dartObject.type is FunctionType) {
      // TODO(kevmoo): Support calling function for the default value?
      badType = 'Function';
    } else if (!reader.isLiteral) {
      badType = dartObject.type.name;
    }
 
    if (badType != null) {
      badType = things.followedBy([badType]).join(' > ');
      throwUnsupported(
          element, '`defaultValue` is `$badType`, it must be a literal.');
    }
 
    final literal = reader.literalValue;
 
    if (literal is num || literal is String || literal is bool) {
      return literal;
    } else if (literal is List<DartObject>) {
      return literal
          .map((e) => _getLiteral(e, things.followedBy(['List'])))
          .toList();
    } else if (literal is Map<DartObject, DartObject>) {
      final mapThings = things.followedBy(['Map']);
      return literal.map((k, v) =>
          MapEntry(_getLiteral(k, mapThings), _getLiteral(v, mapThings)));
    }
 
    badType = things.followedBy(['$dartObject']).join(' > ');
 
    throwUnsupported(
        element,
        'The provided value is not supported: $badType. '
        'This may be an error in package:json_serializable. '
        'Please rerun your build with `--verbose` and file an issue.');
  }
 
  final defaultValueObject = obj.getField('defaultValue');
 
  Object defaultValueLiteral;
 
  final enumFields = iterateEnumFields(defaultValueObject.type);
  if (enumFields != null) {
    final allowedValues = enumFields.map((p) => p.name).toList();
    final enumValueIndex = defaultValueObject.getField('index').toIntValue();
    defaultValueLiteral =
        '${defaultValueObject.type.name}.${allowedValues[enumValueIndex]}';
  } else {
    defaultValueLiteral = _getLiteral(defaultValueObject, []);
    if (defaultValueLiteral != null) {
      defaultValueLiteral = jsonLiteralAsDart(defaultValueLiteral);
    }
  }
 
  final disallowNullValue = obj.getField('disallowNullValue').toBoolValue();
  final includeIfNull = obj.getField('includeIfNull').toBoolValue();
 
  if (disallowNullValue == true) {
    if (includeIfNull == true) {
      throwUnsupported(
          element,
          'Cannot set both `disallowNullvalue` and `includeIfNull` to `true`. '
          'This leads to incompatible `toJson` and `fromJson` behavior.');
    }
  }
 
  return _populateJsonKey(
    classAnnotation,
    element,
    name: obj.getField('name').toStringValue(),
    nullable: obj.getField('nullable').toBoolValue(),
    includeIfNull: includeIfNull,
    ignore: obj.getField('ignore').toBoolValue(),
    defaultValue: defaultValueLiteral,
    required: obj.getField('required').toBoolValue(),
    disallowNullValue: disallowNullValue,
  );
}
 
JsonKey _populateJsonKey(
  JsonSerializable classAnnotation,
  FieldElement fieldElement, {
  String name,
  bool nullable,
  bool includeIfNull,
  bool ignore,
  Object defaultValue,
  bool required,
  bool disallowNullValue,
}) {
  final jsonKey = JsonKey(
      name: _encodedFieldName(classAnnotation, name, fieldElement),
      nullable: nullable ?? classAnnotation.nullable,
      includeIfNull: _includeIfNull(
          includeIfNull, disallowNullValue, classAnnotation.includeIfNull),
      ignore: ignore ?? false,
      defaultValue: defaultValue,
      required: required ?? false,
      disallowNullValue: disallowNullValue ?? false);
 
  return jsonKey;
}
 
String _encodedFieldName(JsonSerializable classAnnotation,
    String jsonKeyNameValue, FieldElement fieldElement) {
  if (jsonKeyNameValue != null) {
    return jsonKeyNameValue;
  }
 
  switch (classAnnotation.fieldRename) {
    case FieldRename.none:
      // noop
      break;
    case FieldRename.snake:
      return snakeCase(fieldElement.name);
    case FieldRename.kebab:
      return kebabCase(fieldElement.name);
  }
 
  return fieldElement.name;
}
 
bool _includeIfNull(
    bool keyIncludeIfNull, bool keyDisallowNullValue, bool classIncludeIfNull) {
  if (keyDisallowNullValue == true) {
    assert(keyIncludeIfNull != true);
    return false;
  }
  return keyIncludeIfNull ?? classIncludeIfNull;
}