trphoenix
2018-11-29 25f4612acc6885d3f977c16252e2185b874b3394
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
// 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/type.dart';
import 'package:source_gen/source_gen.dart' show TypeChecker;
 
import '../type_helper.dart';
 
class UriHelper extends TypeHelper {
  const UriHelper();
 
  @override
  String serialize(
      DartType targetType, String expression, TypeHelperContext context) {
    if (!_matchesType(targetType)) {
      return null;
    }
 
    final buffer = StringBuffer(expression);
 
    if (context.nullable) {
      buffer.write('?');
    }
 
    buffer.write('.toString()');
 
    return buffer.toString();
  }
 
  @override
  String deserialize(
      DartType targetType, String expression, TypeHelperContext context) {
    if (!_matchesType(targetType)) {
      return null;
    }
 
    return commonNullPrefix(
            context.nullable, expression, 'Uri.parse($expression as String)')
        .toString();
  }
}
 
bool _matchesType(DartType type) =>
    const TypeChecker.fromUrl('dart:core#Uri').isExactlyType(type);