Skip to content

Commit f00f368

Browse files
committed
managed simple conversions
1 parent 1b7480e commit f00f368

18 files changed

+821
-11
lines changed

generator/lib/src/dartstruct_generator.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'package:analyzer/dart/element/element.dart';
2-
import 'package:analyzer/dart/element/type_system.dart';
32
import 'package:build/build.dart';
43
import 'package:build/src/builder/build_step.dart';
54
import 'package:code_builder/code_builder.dart';
@@ -191,11 +190,15 @@ class DartStructGenerator extends GeneratorForAnnotation<Mapper> {
191190
}
192191

193192

194-
if (mapper.returnType != outputField.type && _conversions.canConvert(mapper.returnType, outputField.type)) {
193+
if (_conversions.canConvert(mapper.returnType, outputField.type)) {
195194
mapper = _conversions.convert(mapper.returnType, outputField.type, mapper);
196195
return mapper.expression;
197196
}
198197

198+
if (mapper.returnType != outputField.type) {
199+
return null;
200+
}
201+
199202
return mapper.expression;
200203

201204
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import 'package:analyzer/dart/element/type.dart';
2+
import 'package:code_builder/code_builder.dart';
3+
import 'package:code_builder/src/specs/expression.dart';
4+
import 'package:dartstruct_generator/src/mappers/mappers.dart';
5+
6+
class BoolToNumMapper implements MapperAdapter {
7+
8+
final MapperAdapter _mapper;
9+
final DartType _numType;
10+
11+
BoolToNumMapper(this._mapper, this._numType);
12+
13+
@override
14+
Expression get expression {
15+
16+
final mapperExpression = _mapper.expression;
17+
18+
return _nullableBoolToNum(mapperExpression);
19+
20+
}
21+
22+
Expression _nullableBoolToNum(Expression variable) {
23+
24+
final nullValue = refer('null');
25+
return variable.equalTo(nullValue).conditional(nullValue, _boolToNum(variable));
26+
27+
}
28+
29+
Expression _boolToNum(Expression variable) {
30+
31+
final trueValue = refer('true');
32+
return variable.equalTo(trueValue).conditional(refer('1'), refer('0'));
33+
34+
}
35+
36+
@override
37+
DartType get returnType => _numType;
38+
39+
}

generator/lib/src/mappers/conversions/conversions.dart

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import 'dart:collection';
22

33
import 'package:analyzer/dart/element/element.dart';
44
import 'package:analyzer/dart/element/type.dart';
5-
import 'package:analyzer/dart/element/type_provider.dart';
5+
import 'package:dartstruct_generator/src/mappers/conversions/bool_to_num_mapper.dart';
66
import 'package:dartstruct_generator/src/mappers/conversions/primitive_to_string_mapper.dart';
7+
import 'package:dartstruct_generator/src/mappers/conversions/string_to_bool_mapper.dart';
78
import 'package:dartstruct_generator/src/mappers/conversions/string_to_num_mapper.dart';
89
import 'package:dartstruct_generator/src/mappers/mappers.dart';
910
import 'package:equatable/equatable.dart';
11+
import 'num_to_bool_mapper.dart';
1012
import 'num_to_num_mapper.dart';
1113

1214

@@ -22,26 +24,35 @@ class Conversions {
2224
DartType numType = dartCoreLibrary.getType('num').thisType;
2325
DartType stringType = dartCoreLibrary.getType('String').thisType;
2426

25-
2627
// to string
2728
_addConvertion(intType, stringType, (parent) => PrimitiveToStringMapper(parent, stringType));
2829
_addConvertion(doubleType, stringType, (parent) => PrimitiveToStringMapper(parent, stringType));
2930
_addConvertion(boolType, stringType, (parent) => PrimitiveToStringMapper(parent, stringType));
31+
_addConvertion(numType, stringType, (parent) => PrimitiveToStringMapper(parent, stringType));
3032

3133
// to int
3234
_addConvertion(numType, intType, (parent) => NumToNumMapper(parent, intType));
3335
_addConvertion(doubleType, intType, (parent) => NumToNumMapper(parent, intType));
3436
_addConvertion(stringType, intType, (parent) => StringToNumMapper(parent, intType));
37+
_addConvertion(boolType, intType, (parent) => BoolToNumMapper(parent, intType));
3538

3639
// to double
3740
_addConvertion(numType, doubleType, (parent) => NumToNumMapper(parent, doubleType));
3841
_addConvertion(intType, doubleType, (parent) => NumToNumMapper(parent, doubleType));
3942
_addConvertion(stringType, doubleType, (parent) => StringToNumMapper(parent, doubleType));
43+
_addConvertion(boolType, doubleType, (parent) => BoolToNumMapper(parent, doubleType));
4044

4145
// to num
4246
_addConvertion(intType, numType, (parent) => NumToNumMapper(parent, numType));
4347
_addConvertion(doubleType, numType, (parent) => NumToNumMapper(parent, numType));
4448
_addConvertion(stringType, numType, (parent) => StringToNumMapper(parent, numType));
49+
_addConvertion(boolType, numType, (parent) => BoolToNumMapper(parent, numType));
50+
51+
// to bool
52+
_addConvertion(intType, boolType, (parent) => NumToBoolMapper(parent, boolType));
53+
_addConvertion(doubleType, boolType, (parent) => NumToBoolMapper(parent, numType));
54+
_addConvertion(numType, boolType, (parent) => NumToBoolMapper(parent, numType));
55+
_addConvertion(stringType, boolType, (parent) => StringToBoolMapper(parent, numType));
4556

4657
}
4758

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import 'package:analyzer/dart/element/type.dart';
2+
import 'package:code_builder/code_builder.dart';
3+
import 'package:code_builder/src/specs/expression.dart';
4+
import 'package:dartstruct_generator/src/mappers/mappers.dart';
5+
6+
class NumToBoolMapper implements MapperAdapter {
7+
8+
final MapperAdapter _mapper;
9+
final DartType _boolType;
10+
11+
NumToBoolMapper(this._mapper, this._boolType);
12+
13+
@override
14+
Expression get expression {
15+
16+
final mapperExpression = _mapper.expression;
17+
18+
return _nullableNumToBool(mapperExpression);
19+
20+
}
21+
22+
Expression _nullableNumToBool(Expression variable) {
23+
24+
final nullValue = refer('null');
25+
return variable.equalTo(nullValue).conditional(nullValue, _numToBool(variable));
26+
27+
}
28+
29+
Expression _numToBool(Expression variable) {
30+
31+
return variable.greaterOrEqualTo(refer('1'));
32+
33+
}
34+
35+
@override
36+
DartType get returnType => _boolType;
37+
38+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import 'package:analyzer/dart/element/type.dart';
2+
import 'package:code_builder/code_builder.dart';
3+
import 'package:code_builder/src/specs/expression.dart';
4+
import 'package:dartstruct_generator/src/mappers/mappers.dart';
5+
6+
class StringToBoolMapper implements MapperAdapter {
7+
8+
final MapperAdapter _mapper;
9+
final DartType _numType;
10+
11+
StringToBoolMapper(this._mapper, this._numType);
12+
13+
@override
14+
Expression get expression {
15+
16+
final mapperExpression = _mapper.expression;
17+
18+
return _nullableStringToBoolExpression(mapperExpression);
19+
20+
}
21+
22+
Expression _nullableStringToBoolExpression(Expression variable) {
23+
24+
final nullValue = refer('null');
25+
return variable.equalTo(nullValue).conditional(nullValue, variable.nullSafeProperty('toLowerCase').call([]).equalTo(refer('\'true\'')));
26+
27+
}
28+
29+
@override
30+
DartType get returnType => _numType;
31+
32+
}

generator/lib/src/mappers/conversions/string_to_num_mapper.dart

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,32 @@ class StringToNumMapper implements MapperAdapter {
1616
final mapperExpression = _mapper.expression;
1717

1818
if (_numType.isDartCoreInt) {
19-
return _parseExpression(mapperExpression, 'int');
19+
return _nullableParseExpression(mapperExpression, 'int');
2020
}
2121

2222
if (_numType.isDartCoreDouble) {
23-
return _parseExpression(mapperExpression, 'double');
23+
return _nullableParseExpression(mapperExpression, 'double');
2424
}
2525

2626
if (_numType.isDartCoreNum) {
27-
return _parseExpression(mapperExpression, 'num');
27+
return _nullableParseExpression(mapperExpression, 'num');
2828
}
2929

3030
return _mapper.expression;
3131

3232
}
3333

34-
Expression _parseExpression(Expression variable, String parseTo) {
34+
Expression _nullableParseExpression(Expression variable, String parseTo) {
35+
3536
final nullValue = refer('null');
36-
return variable.equalTo(nullValue).conditional(nullValue, refer(parseTo).property('parse').call([variable]));
37+
return variable.equalTo(nullValue).conditional(nullValue, _parseExpression(variable, parseTo));
38+
39+
}
40+
41+
Expression _parseExpression(Expression variable, String parseTo) {
42+
43+
return refer(parseTo).property('parse').call([variable]);
44+
3745
}
3846

3947
@override
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
3+
import 'package:dartstruct/dartstruct.dart';
4+
5+
part 'bool_mapper.g.dart';
6+
7+
class Model {
8+
int trueInteger$;
9+
int falseInteger$;
10+
int nullInteger$;
11+
String trueString$;
12+
String falseString$;
13+
String nullString$;
14+
num trueNumber$;
15+
num falseNumber$;
16+
num nullNumber$;
17+
double trueDouble$;
18+
double falseDouble$;
19+
double nullDouble$;
20+
bool true$;
21+
bool false$;
22+
bool nullBool$;
23+
}
24+
25+
class Dto {
26+
bool trueInteger$;
27+
bool falseInteger$;
28+
bool nullInteger$;
29+
bool trueString$;
30+
bool falseString$;
31+
bool nullString$;
32+
bool trueNumber$;
33+
bool falseNumber$;
34+
bool nullNumber$;
35+
bool trueDouble$;
36+
bool falseDouble$;
37+
bool nullDouble$;
38+
bool true$;
39+
bool false$;
40+
bool nullBool$;
41+
}
42+
43+
44+
@Mapper()
45+
abstract class BoolMapper {
46+
47+
static BoolMapper get INSTANCE => BoolMapperImpl();
48+
49+
Dto modelToDto(Model model);
50+
51+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
2+
import 'package:test/test.dart';
3+
4+
import 'bool_mapper.dart';
5+
6+
7+
8+
9+
void main() {
10+
group('Bool Mapper', () {
11+
12+
final model = Model()
13+
..trueString$ = 'true'
14+
..falseString$ = 'string'
15+
..nullString$ = null
16+
..trueDouble$ = 1.1
17+
..falseDouble$ = 0
18+
..nullDouble$ = null
19+
..trueInteger$ = 100
20+
..falseInteger$ = 0
21+
..trueNumber$ = 1
22+
..falseNumber$ = 0
23+
..nullNumber$ = null
24+
..true$ = true
25+
..false$ = false
26+
..nullBool$ = null;
27+
28+
29+
test('Should be instantiated', () {
30+
expect(BoolMapper.INSTANCE, isNotNull);
31+
});
32+
33+
test('Should assign field when input is number', () {
34+
35+
final actual = BoolMapper.INSTANCE.modelToDto(model);
36+
expect(actual.trueNumber$, equals(true));
37+
expect(actual.falseNumber$, equals(false));
38+
39+
});
40+
41+
test('Should assign field when input is null number', () {
42+
43+
final actual = BoolMapper.INSTANCE.modelToDto(model);
44+
expect(actual.nullNumber$, equals(null));
45+
46+
});
47+
48+
test('Should assign field when input is integer', () {
49+
50+
final actual = BoolMapper.INSTANCE.modelToDto(model);
51+
expect(actual.trueInteger$, equals(true));
52+
expect(actual.falseInteger$, equals(false));
53+
54+
});
55+
56+
test('Should assign field when input is null integer', () {
57+
58+
final actual = BoolMapper.INSTANCE.modelToDto(model);
59+
expect(actual.nullInteger$, equals(null));
60+
61+
});
62+
63+
test('Should assign field when input is double', () {
64+
65+
final actual = BoolMapper.INSTANCE.modelToDto(model);
66+
expect(actual.trueDouble$, equals(true));
67+
expect(actual.falseDouble$, equals(false));
68+
69+
});
70+
71+
test('Should assign field when input is null double', () {
72+
73+
final actual = BoolMapper.INSTANCE.modelToDto(model);
74+
expect(actual.nullDouble$, equals(null));
75+
76+
});
77+
78+
test('Should assign field when input is string', () {
79+
80+
final actual = BoolMapper.INSTANCE.modelToDto(model);
81+
expect(actual.trueString$, equals(true));
82+
expect(actual.falseString$, equals(false));
83+
84+
});
85+
86+
test('Should assign field when input is null string', () {
87+
88+
final actual = BoolMapper.INSTANCE.modelToDto(model);
89+
expect(actual.nullString$, equals(null));
90+
91+
});
92+
93+
test('Should assign field when input is bool', () {
94+
95+
final actual = BoolMapper.INSTANCE.modelToDto(model);
96+
expect(actual.true$, equals(true));
97+
expect(actual.false$, equals(false));
98+
99+
});
100+
101+
test('Should assign field when input is null bool', () {
102+
103+
final actual = BoolMapper.INSTANCE.modelToDto(model);
104+
expect(actual.nullBool$, equals(null));
105+
106+
});
107+
108+
});
109+
}

0 commit comments

Comments
 (0)