-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathfunction_mapping_static.dart
More file actions
50 lines (36 loc) · 1.07 KB
/
function_mapping_static.dart
File metadata and controls
50 lines (36 loc) · 1.07 KB
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
import 'package:smartstruct/smartstruct.dart';
part 'function_mapping_static.mapper.g.dart';
// TARGET
class Dog {
final String name;
final String breed;
final int age;
late AgeHolderTarget? model;
Dog(this.name, this.breed, this.age);
}
// SOURCE
class DogModel {
final String name;
final int age;
DogModel(this.name, this.age);
}
class AgeHolderSource {
final int age;
AgeHolderSource(this.age);
}
class AgeHolderTarget {
late int age;
}
// This example uses static helper methods to map inner fields
@Mapper(useStaticMapping: false)
abstract class DogMapper {
static String breedCustom(DogModel model) => 'customBreed';
static AgeHolderSource? toAgeHolderSource(DogModel model) =>
AgeHolderSource(model.age);
static String fullNameWithAge(DogModel model) => model.name + '${model.age}';
@Mapping(source: fullNameWithAge, target: 'name')
@Mapping(source: breedCustom, target: 'breed')
@Mapping(source: toAgeHolderSource, target: 'model')
Dog fromDogModel(DogModel model);
AgeHolderTarget fromAgeHolderSource(AgeHolderSource model);
}