44import re
55from dataclasses import dataclass , field
66from enum import Enum , auto
7- from typing import List , Set , Dict
7+ from typing import List , Set , Dict , ClassVar
88
99import yaml
1010
1111root_class_name = "Element"
1212
1313
14- class Cardinality (Enum ):
15- """ The cardinality of a property
16-
17- `ONE` is the default, `OPTIONAL` are the fields denoted by `?`, `MANY` are those denoted by `*`
18- """
19- ONE = auto ()
20- OPTIONAL = auto ()
21- MANY = auto ()
22-
23-
2414@dataclass
2515class Property :
16+ is_single : ClassVar = False
17+ is_optional : ClassVar = False
18+ is_repeated : ClassVar = False
19+
2620 name : str
2721 type : str
28- cardinality : Cardinality = Cardinality .ONE
2922
30- @property
31- def is_single (self ):
32- return self .cardinality == Cardinality .ONE
3323
34- @property
35- def is_optional (self ):
36- return self .cardinality == Cardinality .OPTIONAL
24+ @dataclass
25+ class SingleProperty (Property ):
26+ is_single : ClassVar = True
27+
28+
29+ @dataclass
30+ class OptionalProperty (Property ):
31+ is_optional : ClassVar = True
3732
38- @property
39- def is_repeated (self ):
40- return self .cardinality == Cardinality .MANY
33+
34+ @dataclass
35+ class RepeatedProperty (Property ):
36+ is_repeated : ClassVar = True
4137
4238
4339@dataclass
@@ -57,14 +53,14 @@ class Schema:
5753
5854def _parse_property (name , type ):
5955 if type .endswith ("*" ):
60- cardinality = Cardinality . MANY
56+ cls = RepeatedProperty
6157 type = type [:- 1 ]
6258 elif type .endswith ("?" ):
63- cardinality = Cardinality . OPTIONAL
59+ cls = OptionalProperty
6460 type = type [:- 1 ]
6561 else :
66- cardinality = Cardinality . ONE
67- return Property (name , type , cardinality )
62+ cls = SingleProperty
63+ return cls (name , type )
6864
6965
7066class _DirSelector :
0 commit comments