@@ -13,6 +13,9 @@ Table of Contents:
1313- [ About] ( #about )
1414- [ Contribute] ( #contribute )
1515- [ Usage] ( #usage )
16+ - [ Installation] ( #installation )
17+ - [ Basic] ( #basic )
18+ - [ Map dictionary source to target object] ( #map-dictionary-source-to-target-object )
1619 - [ Different field names] ( #different-field-names )
1720 - [ Overwrite field value in mapping] ( #overwrite-field-value-in-mapping )
1821 - [ Disable Deepcopy] ( #disable-deepcopy )
@@ -37,25 +40,27 @@ The major advantage of py-automapper is its extensibility, that allows it to map
3740Read [ CONTRIBUTING.md] ( /CONTRIBUTING.md ) guide.
3841
3942# Usage
43+ ## Installation
4044Install package:
4145``` bash
4246pip install py-automapper
4347```
4448
45- Let's say we have domain model ` UserInfo ` and its API representation ` PublicUserInfo ` with ` age ` field missing:
49+ ## Basic
50+ Let's say we have domain model ` UserInfo ` and its API representation ` PublicUserInfo ` without exposing user ` age ` :
4651``` python
4752class UserInfo :
48- def __init__ (self , name : str , age : int , profession : str ):
53+ def __init__ (self , name : str , profession : str , age : int ):
4954 self .name = name
50- self .age = age
5155 self .profession = profession
56+ self .age = age
5257
5358class PublicUserInfo :
5459 def __init__ (self , name : str , profession : str ):
5560 self .name = name
5661 self .profession = profession
5762
58- user_info = UserInfo(" John Malkovich" , 35 , " engineer" )
63+ user_info = UserInfo(" John Malkovich" , " engineer" , 35 )
5964```
6065To create ` PublicUserInfo ` object:
6166``` python
@@ -77,6 +82,19 @@ print(vars(public_user_info))
7782# {'name': 'John Malkovich', 'profession': 'engineer'}
7883```
7984
85+ ## Map dictionary source to target object
86+ If source object is dictionary:
87+ ``` python
88+ source = {
89+ " name" : " John Carter" ,
90+ " profession" : " hero"
91+ }
92+ public_info = mapper.to(PublicUserInfo).map(source)
93+
94+ print (vars (public_info))
95+ # {'name': 'John Carter', 'profession': 'hero'}
96+ ```
97+
8098## Different field names
8199If your target class field name is different from source class.
82100``` python
0 commit comments