|
| 1 | +import 'dart:collection'; |
| 2 | + |
| 3 | +import 'package:analyzer/dart/element/element.dart'; |
| 4 | +import 'package:analyzer/dart/element/type.dart'; |
| 5 | +import 'package:analyzer/dart/element/type_provider.dart'; |
| 6 | +import 'package:dartstruct_generator/src/mappers/conversions/primitive_to_string_mapper.dart'; |
| 7 | +import 'package:dartstruct_generator/src/mappers/conversions/string_to_num_mapper.dart'; |
| 8 | +import 'package:dartstruct_generator/src/mappers/mappers.dart'; |
| 9 | +import 'package:equatable/equatable.dart'; |
| 10 | +import 'num_to_num_mapper.dart'; |
| 11 | + |
| 12 | + |
| 13 | +class Conversions { |
| 14 | + |
| 15 | + final Map<_ConvertionKey, _MapperFactory> _convertions = HashMap<_ConvertionKey, _MapperFactory>(); |
| 16 | + |
| 17 | + Conversions(LibraryElement dartCoreLibrary) { |
| 18 | + |
| 19 | + DartType intType = dartCoreLibrary.getType('int').thisType; |
| 20 | + DartType doubleType = dartCoreLibrary.getType('double').thisType; |
| 21 | + DartType boolType = dartCoreLibrary.getType('bool').thisType; |
| 22 | + DartType numType = dartCoreLibrary.getType('num').thisType; |
| 23 | + DartType stringType = dartCoreLibrary.getType('String').thisType; |
| 24 | + |
| 25 | + |
| 26 | + // to string |
| 27 | + _addConvertion(intType, stringType, (parent) => PrimitiveToStringMapper(parent, stringType)); |
| 28 | + _addConvertion(doubleType, stringType, (parent) => PrimitiveToStringMapper(parent, stringType)); |
| 29 | + _addConvertion(boolType, stringType, (parent) => PrimitiveToStringMapper(parent, stringType)); |
| 30 | + |
| 31 | + // to int |
| 32 | + _addConvertion(numType, intType, (parent) => NumToNumMapper(parent, intType)); |
| 33 | + _addConvertion(doubleType, intType, (parent) => NumToNumMapper(parent, intType)); |
| 34 | + _addConvertion(stringType, intType, (parent) => StringToNumMapper(parent, intType)); |
| 35 | + |
| 36 | + // to double |
| 37 | + _addConvertion(numType, doubleType, (parent) => NumToNumMapper(parent, doubleType)); |
| 38 | + _addConvertion(intType, doubleType, (parent) => NumToNumMapper(parent, doubleType)); |
| 39 | + _addConvertion(stringType, doubleType, (parent) => StringToNumMapper(parent, doubleType)); |
| 40 | + |
| 41 | + // to num |
| 42 | + _addConvertion(intType, numType, (parent) => NumToNumMapper(parent, numType)); |
| 43 | + _addConvertion(doubleType, numType, (parent) => NumToNumMapper(parent, numType)); |
| 44 | + _addConvertion(stringType, numType, (parent) => StringToNumMapper(parent, numType)); |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + bool canConvert(DartType from, DartType to) => _convertions.containsKey(_ConvertionKey(from, to)); |
| 49 | + |
| 50 | + MapperAdapter convert(DartType from, DartType to, MapperAdapter parent) => _convertions[_ConvertionKey(from, to)](parent); |
| 51 | + |
| 52 | + void _addConvertion(DartType from, DartType to, _MapperFactory mapperFactory) { |
| 53 | + final key = _ConvertionKey(from, to); |
| 54 | + _convertions.putIfAbsent(key, () => mapperFactory); |
| 55 | + } |
| 56 | + |
| 57 | +} |
| 58 | + |
| 59 | +class _ConvertionKey extends Equatable { |
| 60 | + |
| 61 | + final DartType from; |
| 62 | + final DartType to; |
| 63 | + |
| 64 | + _ConvertionKey(this.from, this.to); |
| 65 | + |
| 66 | + @override |
| 67 | + List<Object> get props => [from, to]; |
| 68 | + |
| 69 | +} |
| 70 | + |
| 71 | +typedef _MapperFactory = MapperAdapter Function(MapperAdapter parent); |
0 commit comments