15
15
# specific language governing permissions and limitations
16
16
# under the License.
17
17
18
+ from datetime import date , datetime
18
19
from fnmatch import fnmatch
19
20
20
21
from .exceptions import ValidationException
21
- from .field import Field
22
+ from .field import Field , Integer , Float , Boolean , Text , Binary , Date
22
23
from .mapping import Mapping
23
24
from .utils import DOC_META_FIELDS , ObjectBase
24
25
@@ -36,12 +37,30 @@ def __new__(cls, name, bases, attrs):
36
37
37
38
38
39
class DocumentOptions :
40
+ type_annotation_map = {
41
+ int : (Integer , {}),
42
+ float : (Float , {}),
43
+ bool : (Boolean , {}),
44
+ str : (Text , {}),
45
+ bytes : (Binary , {}),
46
+ datetime : (Date , {}),
47
+ date : (Date , {"format" : "yyyy-MM-dd" }),
48
+ }
49
+
39
50
def __init__ (self , name , bases , attrs ):
40
51
meta = attrs .pop ("Meta" , None )
41
52
42
53
# create the mapping instance
43
54
self .mapping = getattr (meta , "mapping" , Mapping ())
44
55
56
+ for name , type_ in attrs .get ('__annotations__' , {}).items ():
57
+ if name not in attrs :
58
+ if type_ in self .type_annotation_map :
59
+ field , field_args = self .type_annotation_map [type_ ]
60
+ self .mapping .field (name , field (** field_args ))
61
+ elif issubclass (type_ , Field ):
62
+ self .mapping .field (name , type_ ())
63
+
45
64
# register all declared fields into the mapping
46
65
for name , value in list (attrs .items ()):
47
66
if isinstance (value , Field ):
0 commit comments