@@ -26,55 +26,27 @@ def create(
26
26
raise NotImplementedError ()
27
27
28
28
@property
29
- def _attributes_read_model (self ) -> typing .Dict [str , typing .Any ]:
29
+ def attributes (self ) -> typing .Dict [str , typing .Any ]:
30
30
"""
31
31
:return: Attributes of this event.
32
32
33
33
You MUST NOT mutate this dict.
34
34
Implementation MAY assume the dict will not be mutated.
35
-
36
- The reason this is only a read model and not a write model is
37
- because you MAY not have an easy write access to the data.
38
-
39
- For example - a database row or a cached value.
40
-
41
- We don't wont to restrict our future selves.
42
-
43
- When a write model will be needed, it will be implemented
44
-
45
- The we don't have an `attributes` property is to prevent API confusion
46
- Examples of confusion:
47
- * What is the difference between `event.get("myattr")` and
48
- `event.attributes.get("myattr")`
49
- * What SHOULD I use `event["myattr"]` or `event.attributes["myattr"]` ?
50
35
"""
51
36
raise NotImplementedError ()
52
37
53
38
@property
54
- def _data_read_model (self ) -> typing .Optional [typing .Any ]:
39
+ def data (self ) -> typing .Optional [typing .Any ]:
55
40
"""
56
41
:return: Data value of the event.
57
42
You MUST NOT mutate this dict.
58
43
Implementation MAY assume the dict will not be mutated.
59
-
60
- The reason this is only a read model and not a write model is
61
- because you MAY not have an easy write access to the data.
62
-
63
- For example - a database row or a cached value.
64
-
65
- We don't wont to restrict our future selves.
66
-
67
- When a write model will be needed, it will be implemented
68
-
69
44
"""
70
45
raise NotImplementedError ()
71
46
72
47
def __eq__ (self , other : typing .Any ) -> bool :
73
48
if isinstance (other , CloudEvent ):
74
- return (
75
- self ._data_read_model == other ._data_read_model
76
- and self ._attributes_read_model == other ._attributes_read_model
77
- )
49
+ return self .data == other .data and self .attributes == other .attributes
78
50
return False
79
51
80
52
def __getitem__ (self , key : str ) -> typing .Any :
@@ -84,7 +56,7 @@ def __getitem__(self, key: str) -> typing.Any:
84
56
:param key: The event attribute name.
85
57
:return: The event attribute value.
86
58
"""
87
- return self ._attributes_read_model [key ]
59
+ return self .attributes [key ]
88
60
89
61
def get (
90
62
self , key : str , default : typing .Optional [typing .Any ] = None
@@ -100,21 +72,19 @@ def get(
100
72
no attribute with the given key exists.
101
73
:returns: The event attribute value if exists, default value otherwise.
102
74
"""
103
- return self ._attributes_read_model .get (key , default )
75
+ return self .attributes .get (key , default )
104
76
105
77
def __iter__ (self ) -> typing .Iterator [typing .Any ]:
106
- return iter (self ._attributes_read_model )
78
+ return iter (self .attributes )
107
79
108
80
def __len__ (self ) -> int :
109
- return len (self ._attributes_read_model )
81
+ return len (self .attributes )
110
82
111
83
def __contains__ (self , key : str ) -> bool :
112
- return key in self ._attributes_read_model
84
+ return key in self .attributes
113
85
114
86
def __repr__ (self ) -> str :
115
- return str (
116
- {"attributes" : self ._attributes_read_model , "data" : self ._data_read_model }
117
- )
87
+ return str ({"attributes" : self .attributes , "data" : self .data })
118
88
119
89
120
90
AnyCloudEvent = TypeVar ("AnyCloudEvent" , bound = CloudEvent )
0 commit comments