12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+ from enum import IntEnum
15
16
16
17
from objectbox .c import *
17
18
import flatbuffers .number_types
18
19
19
20
20
- # base property
21
+ class PropertyType (IntEnum ):
22
+ bool = OBXPropertyType_Bool
23
+ byte = OBXPropertyType_Byte
24
+ short = OBXPropertyType_Short
25
+ char = OBXPropertyType_Char
26
+ int = OBXPropertyType_Int
27
+ long = OBXPropertyType_Long
28
+ float = OBXPropertyType_Float
29
+ double = OBXPropertyType_Double
30
+ string = OBXPropertyType_String
31
+ # date = OBXPropertyType_Date
32
+ # relation = OBXPropertyType_Relation
33
+ byteVector = OBXPropertyType_ByteVector
34
+ # stringVector = OBXPropertyType_StringVector
35
+
36
+
37
+ fb_type_map = {
38
+ PropertyType .bool : flatbuffers .number_types .BoolFlags ,
39
+ PropertyType .byte : flatbuffers .number_types .Int8Flags ,
40
+ PropertyType .short : flatbuffers .number_types .Int16Flags ,
41
+ PropertyType .char : flatbuffers .number_types .Int8Flags ,
42
+ PropertyType .int : flatbuffers .number_types .Int32Flags ,
43
+ PropertyType .long : flatbuffers .number_types .Int64Flags ,
44
+ PropertyType .float : flatbuffers .number_types .Float32Flags ,
45
+ PropertyType .double : flatbuffers .number_types .Float64Flags ,
46
+ PropertyType .string : flatbuffers .number_types .UOffsetTFlags ,
47
+ # PropertyType.date: flatbuffers.number_types.Int64Flags,
48
+ # PropertyType.relation: flatbuffers.number_types.Int64Flags,
49
+ PropertyType .byteVector : flatbuffers .number_types .UOffsetTFlags ,
50
+ # PropertyType.stringVector: flatbuffers.number_types.UOffsetTFlags,
51
+ }
52
+
53
+
21
54
class Property :
22
- def __init__ (self , py_type : type , id : int , uid : int ):
55
+ def __init__ (self , py_type : type , id : int , uid : int , type : PropertyType = None ):
23
56
self ._id = id
24
57
self ._uid = uid
25
- self ._name = "" # set in Entity.fillProperties ()
58
+ self ._name = "" # set in Entity.fill_properties ()
26
59
27
- self ._fb_type = None # flatbuffers.number_types
28
60
self ._py_type = py_type
29
- self ._ob_type = OBXPropertyType ( 0 )
30
- self .__set_basic_type ()
61
+ self ._ob_type : OBXPropertyType = type if type != None else self . __determine_ob_type ( )
62
+ self ._fb_type = fb_type_map [ self . _ob_type ]
31
63
32
64
self ._is_id = isinstance (self , Id )
33
65
self ._flags = OBXPropertyFlags (0 )
@@ -37,23 +69,18 @@ def __init__(self, py_type: type, id: int, uid: int):
37
69
self ._fb_slot = self ._id - 1
38
70
self ._fb_v_offset = 4 + 2 * self ._fb_slot
39
71
40
- def __set_basic_type (self ) -> OBXPropertyType :
72
+ def __determine_ob_type (self ) -> OBXPropertyType :
41
73
ts = self ._py_type
42
74
if ts == str :
43
- self ._ob_type = OBXPropertyType_String
44
- self ._fb_type = flatbuffers .number_types .UOffsetTFlags
75
+ return OBXPropertyType_String
45
76
elif ts == int :
46
- self ._ob_type = OBXPropertyType_Long
47
- self ._fb_type = flatbuffers .number_types .Int64Flags
77
+ return OBXPropertyType_Long
48
78
elif ts == bytes : # or ts == bytearray: might require further tests on read objects due to mutability
49
- self ._ob_type = OBXPropertyType_ByteVector
50
- self ._fb_type = flatbuffers .number_types .UOffsetTFlags
79
+ return OBXPropertyType_ByteVector
51
80
elif ts == float :
52
- self ._ob_type = OBXPropertyType_Double
53
- self ._fb_type = flatbuffers .number_types .Float64Flags
81
+ return OBXPropertyType_Double
54
82
elif ts == bool :
55
- self ._ob_type = OBXPropertyType_Bool
56
- self ._fb_type = flatbuffers .number_types .BoolFlags
83
+ return OBXPropertyType_Bool
57
84
else :
58
85
raise Exception ("unknown property type %s" % ts )
59
86
0 commit comments