1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ from enum import IntEnum
1516
1617from objectbox .c import *
1718import flatbuffers .number_types
1819
1920
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+
2154class 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 ):
2356 self ._id = id
2457 self ._uid = uid
25- self ._name = "" # set in Entity.fillProperties ()
58+ self ._name = "" # set in Entity.fill_properties ()
2659
27- self ._fb_type = None # flatbuffers.number_types
2860 self ._py_type = py_type
29- self ._ob_type = OBXPropertyType ( 0 )
30- self .__set_basic_type ()
61+ self ._ob_type = type if type != None else self . __determine_ob_type ( )
62+ self ._fb_type = fb_type_map [ self . _ob_type ]
3163
3264 self ._is_id = isinstance (self , Id )
3365 self ._flags = OBXPropertyFlags (0 )
@@ -37,23 +69,18 @@ def __init__(self, py_type: type, id: int, uid: int):
3769 self ._fb_slot = self ._id - 1
3870 self ._fb_v_offset = 4 + 2 * self ._fb_slot
3971
40- def __set_basic_type (self ) -> OBXPropertyType :
72+ def __determine_ob_type (self ) -> OBXPropertyType :
4173 ts = self ._py_type
4274 if ts == str :
43- self ._ob_type = OBXPropertyType_String
44- self ._fb_type = flatbuffers .number_types .UOffsetTFlags
75+ return OBXPropertyType_String
4576 elif ts == int :
46- self ._ob_type = OBXPropertyType_Long
47- self ._fb_type = flatbuffers .number_types .Int64Flags
77+ return OBXPropertyType_Long
4878 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
5180 elif ts == float :
52- self ._ob_type = OBXPropertyType_Double
53- self ._fb_type = flatbuffers .number_types .Float64Flags
81+ return OBXPropertyType_Double
5482 elif ts == bool :
55- self ._ob_type = OBXPropertyType_Bool
56- self ._fb_type = flatbuffers .number_types .BoolFlags
83+ return OBXPropertyType_Bool
5784 else :
5885 raise Exception ("unknown property type %s" % ts )
5986
0 commit comments