parseParam method
Expression
parseParam(
- FormalParameterElement param
Implementation
Expression parseParam(FormalParameterElement param) {
return switch (param.type) {
DartType type when type.getDisplayString() == 'DateTime' =>
refer('DateTime').property('fromMillisecondsSinceEpoch').call([
refer('body').index(literalString(param.name!)).asA(refer('int')),
]),
DartType type
when type is InterfaceType &&
type.allSupertypes.any((e) => e.isDartCoreEnum) =>
refer(
type.getDisplayString(),
).property('values').property('byName').call([
refer('body').index(literalString(param.name!)).asA(refer('String')),
]),
DartType type when type.isDartCoreList => () {
final typeNonNullable = type.getDisplayString().replaceAll('?', '');
final internalType = (type as InterfaceType).typeArguments.first;
var list = refer(
'body',
).index(literalString(param.name!)).asA(refer('List'));
if (internalType is InterfaceType &&
internalType.allSupertypes.any(
(e) => e.getDisplayString().contains('Mappable'),
)) {
list = list.property('map').call([
CodeExpression(
Code(
'(e) => ${internalType.getDisplayString()}Mapper.fromJson(e)',
),
),
]);
}
if (type.nullabilitySuffix == NullabilitySuffix.question) {
return refer('body')
.index(literalString(param.name!))
.asA(refer('List?'))
.equalTo(literalNull)
.conditional(
literalNull,
refer(typeNonNullable).property('from').call([list]),
);
}
return refer(typeNonNullable).property('from').call([list]);
}(),
DartType type
when type is InterfaceType &&
type.allSupertypes.any(
(e) => e.getDisplayString().contains('Mappable'),
) =>
() {
final typeNonNullable = type.getDisplayString().replaceAll('?', '');
if (type.nullabilitySuffix == NullabilitySuffix.question) {
return refer('body')
.index(literalString(param.name!))
.asA(refer('Map<String, dynamic>?'))
.equalTo(literalNull)
.conditional(
literalNull,
refer('${typeNonNullable}Mapper').property('fromJson').call([
refer('body')
.index(literalString(param.name!))
.asA(refer('Map<String, dynamic>')),
]),
);
}
return refer('${typeNonNullable}Mapper').property('fromJson').call([
refer('body')
.index(literalString(param.name!))
.asA(refer('Map<String, dynamic>')),
]);
}(),
_ =>
refer('body')
.index(literalString(param.name!))
.asA(refer(param.type.getDisplayString())),
};
}