Type System
Last updated
Last updated
The TEN framework type system is the system used within the TEN framework to define the data types of values. Developers can declare the types of message or extension properties through the TEN schema.
The TEN framework type system includes basic types and composite types. The basic types include the following:
Type | Description | C++ Type | Go Type | Python Type |
---|---|---|---|---|
Composite types include the following:
Type | Description | C++ Type | Go Type | Python Type |
---|---|---|---|---|
For basic types, properties can be accessed or set using methods such as get_property()
/ set_property()
. For example:
For composite types, it is typically necessary to use related serialization methods. For example:
If a TEN schema is specified, the property type will be determined according to the corresponding TEN schema. If no TEN schema is specified, the property type will be determined based on the type of the initial value assignment. For example, if the initial assignment is int32_t
, the property type will be int32_t
; if the initial assignment is done using JSON, the type will be determined according to the JSON processing rules.
The TEN framework supports flexible automatic conversion between different types of values. As long as the conversion does not result in a loss of value, the TEN framework will automatically perform the type conversion. However, if the conversion leads to a loss of value, such as converting a type from int32_t
to int8_t
when the value exceeds the range that int8_t
can represent, the TEN framework will report an error. For example, the TEN framework will return an error for a send_<foo>
action.
Converting a lower precision type to a higher precision type is always safe and guaranteed to succeed, as the higher precision type can fully accommodate the value of the lower precision type without data loss. This automatic conversion is called Safe Conversion. For example, when trying to retrieve an int8
type property as int32
, the TEN type system will automatically convert the property type to int32
.
In the TEN Type System, the Safe Conversion rules are as follows:
Within the int
types, from lower to higher precision.
Within the uint
types, from lower to higher precision.
Within the float
types, from lower to higher precision.
For example:
Converting a higher precision type to a lower precision type is unsafe because the value of the higher precision type may exceed the range of the lower precision type, leading to data loss. This conversion is called Unsafe Conversion. When performing Unsafe Conversion, the TEN runtime checks for overflow. If an overflow occurs, the TEN Type System will throw an error.
In the TEN framework type system, the Unsafe Conversion rules are as follows:
Converting int64
to a lower precision int
.
Converting int64
to any precision uint
.
Converting float64
to float32
.
It is important to note that TEN runtime only performs Unsafe Conversion when deserializing a JSON document into a TEN property and the TEN property has a defined TEN schema. For example:
When loading property.json
.
For integers, they will be parsed as int64
by default; for floating-point numbers, they will be parsed as float64
by default. The TEN framework type system will perform Unsafe Conversion according to the rules mentioned above.
When calling methods such as set_property_from_json()
.
When passing a serialized JSON string, the TEN framework type system will also perform Unsafe Conversion according to the rules mentioned above.
From | To (Allowed) |
---|---|
From | To | Correct Value Range of From |
---|---|---|
int8
An 8-bit signed integer.
int8_t
int8
int
int16
A 16-bit signed integer.
int16_t
int16
int
int32
A 32-bit signed integer.
int32_t
int32
int
int64
A 64-bit signed integer.
int64_t
int64
int
uint8
An 8-bit unsigned integer.
uint8_t
uint8
int
uint16
A 16-bit unsigned integer.
uint16_t
uint16
int
uint32
A 32-bit unsigned integer.
uint32_t
uint32
int
uint64
A 64-bit unsigned integer.
uint64_t
uint64
int
float32
A single precision (32-bit) IEEE 754 floating-point number.
float
float32
float
float64
A double-precision (64-bit) IEEE 754 floating-point number.
double
float64
float
string
A Unicode character sequence.
std::string / char*
string
str
buf
A sequence of 8-bit unsigned bytes.
uint8_t*
[]byte
bytearray / memoryview
bool
A binary value, either true or false.
bool
bool
bool
ptr
A pointer to a memory address.
void*
unsafe.Pointer
array
A collection of elements of the same type.
object
Represents a complex key/value pair. The key type is always a string.
int8
int16 / int32 / int64
int16
int32 / int64
int32
int64
uint8
uint16 / uint32 / uint64
uint16
uint32 / uint64
uint32
uint64
float32
float64
int64
int8
[-2^7, 2^7 - 1]
int64
int16
[-2^15, 2^15 - 1]
int64
int32
[-2^31, 2^31 - 1]
int64
uint8
[0, 2^7 - 1]
int64
uint16
[0, 2^15 - 1]
int64
uint32
[0, 2^31 - 1]
int64
uint64
[0, 2^63 - 1]
float64
float32
[-3.4028234663852886e+38, 3.4028234663852886e+38]