4
4
import re
5
5
from dataclasses import dataclass , field
6
6
from enum import Enum , auto
7
- from typing import List , Set , Dict
7
+ from typing import List , Set , Dict , ClassVar
8
8
9
9
import yaml
10
10
11
11
root_class_name = "Element"
12
12
13
13
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
-
24
14
@dataclass
25
15
class Property :
16
+ is_single : ClassVar = False
17
+ is_optional : ClassVar = False
18
+ is_repeated : ClassVar = False
19
+
26
20
name : str
27
21
type : str
28
- cardinality : Cardinality = Cardinality .ONE
29
22
30
- @property
31
- def is_single (self ):
32
- return self .cardinality == Cardinality .ONE
33
23
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
37
32
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
41
37
42
38
43
39
@dataclass
@@ -57,14 +53,14 @@ class Schema:
57
53
58
54
def _parse_property (name , type ):
59
55
if type .endswith ("*" ):
60
- cardinality = Cardinality . MANY
56
+ cls = RepeatedProperty
61
57
type = type [:- 1 ]
62
58
elif type .endswith ("?" ):
63
- cardinality = Cardinality . OPTIONAL
59
+ cls = OptionalProperty
64
60
type = type [:- 1 ]
65
61
else :
66
- cardinality = Cardinality . ONE
67
- return Property (name , type , cardinality )
62
+ cls = SingleProperty
63
+ return cls (name , type )
68
64
69
65
70
66
class _DirSelector :
0 commit comments