Skip to content

Data Type Annotations

deng_ran edited this page Aug 12, 2021 · 2 revisions

English | 中文

Data Type Annotations

FastProto supports multiple data types, in addition to Java primitive data types and their wrapper classes, FastProto also supports Timestamp, String and byte array, all above type annotations can be replaced by @AutoType. Taking into account cross-language data sharing, unsigned types, int8, int16 are also introduced.

Annotation Java C/C++ Size AutoType
@BooleanType Boolean / boolean bool 1 bit
@CharacterType Character / char -- 2 bytes
@ByteType Byte / byte char 1 byte
@ShortType Short / short short 2 bytes
@IntegerType Integer / int int 4 bytes
@LongType Long / long long long 8 bytes
@FloatType Float / float float 4 bytes
@DoubleType Double / double double 8 bytes
@Integer8Type Integer / int char 1 byte ×
@Integer16Type Integer / int short 2 bytes ×
@UInteger8Type Integer / int unsigned char 1 byte ×
@UInteger16Type Integer / int unsigned short 2 bytes ×
@UInteger32Type Long / long unsigned long 4 bytes ×
@UInteger64Type BigInteger unsigned long long 8 bytes
@BinaryType byte[] char[] variable length
@StringType java.lang.String -- variable length
@TimestampType java.sql.Timestamp -- 4 / 8 bytes
@Date java.util.Date -- 4 / 8 bytes
@ArrayType array of primitive data types array of primitive data types variable length
@ListType list of primitive data types -- variable length
@EnumType enum enum 1 / 2 bytes

@BooleanType

Boolean type annotation,can be used to annotate Boolean and boolean field directly,or annotate any data type field by specifying encoding formula and decoding formula.

Field Data Type Description
value int byte offset
bitOffset int bit offset
afterDecode Class<? extend Function>[] decoding formula
beforeEncode Class<? extend Function>[] encoding formula

@CharacterType

Character type annotation,can be used to annotate Character and char field directly,or annotate any data type field by specifying encoding formula and decoding formula.

Field Data Type Description
value int byte offset
afterDecode Class<? extend Function>[] decoding formula
beforeEncode Class<? extend Function>[] encoding formula

@ByteType

Byte type annotation,can be used to annotate Byte and byte field directly,or annotate any data type field by specifying encoding formula and decoding formula.

Field Data Type Description
value int byte offset
afterDecode Class<? extend Function>[] decoding formula
beforeEncode Class<? extend Function>[] encoding formula

@ShortType

Short type annotation,can be used to annotate Short and short field directly,or annotate any data type field by specifying encoding formula and decoding formula.

Field Data Type Description
value int byte offset
afterDecode Class<? extend Function>[] decoding formula
beforeEncode Class<? extend Function>[] encoding formula

@IntegerType

Integer type annotation,can be used to annotate Integer and int field directly,or annotate any data type field by specifying encoding formula and decoding formula.

Field Data Type Description
value int byte offset
afterDecode Class<? extend Function>[] decoding formula
beforeEncode Class<? extend Function>[] encoding formula

@LongType

Long type annotation,can be used to annotate Long and long field directly,or annotate any data type field by specifying encoding formula and decoding formula.

Field Data Type Description
value int byte offset
afterDecode Class<? extend Function>[] decoding formula
beforeEncode Class<? extend Function>[] encoding formula

@FloatType

Float type annotation,can be used to annotate Float and float field directly,or annotate any data type field by specifying encoding formula and decoding formula.

Field Data Type Description
value int byte offset
afterDecode Class<? extend Function>[] decoding formula
beforeEncode Class<? extend Function>[] encoding formula

@DoubleType

Double type annotation,can be used to annotate Double and double field directly,or annotate any data type field by specifying encoding formula and decoding formula.

Field Data Type Description
value int byte offset
afterDecode Class<? extend Function>[] decoding formula
beforeEncode Class<? extend Function>[] encoding formula

@Integer8Type

Singed 8-bit Integer type annotation,can be used to annotate Integer and int field directly,or annotate any data type field by specifying encoding formula and decoding formula.

Field Data Type Description
value int byte offset
afterDecode Class<? extend Function>[] decoding formula
beforeEncode Class<? extend Function>[] encoding formula

@Integer16Type

Singed 16-bit Integer type annotation,can be used to annotate Integer and int field directly,or annotate any data type field by specifying encoding formula and decoding formula.

Field Data Type Description
value int byte offset
afterDecode Class<? extend Function>[] decoding formula
beforeEncode Class<? extend Function>[] encoding formula

@UInteger8Type

Unsigned 8-bit Integer type annotation,can be used to annotate Integer and int field directly,or annotate any data type field by specifying encoding formula and decoding formula.

Field Data Type Description
value int byte offset
afterDecode Class<? extend Function>[] decoding formula
beforeEncode Class<? extend Function>[] encoding formula

@UInteger16Type

Unsigned 8-bit Integer type annotation,can be used to annotate Integer and int field directly,or annotate any data type field by specifying encoding formula and decoding formula.

Field Data Type Description
value int byte offset
afterDecode Class<? extend Function>[] decoding formula
beforeEncode Class<? extend Function>[] encoding formula

@UInteger32Type

Unsigned 32-bit Integer type annotation,can be used to annotate Long and long field directly,or annotate any data type field by specifying encoding formula and decoding formula.

Field Data Type Description
value int byte offset
afterDecode Class<? extend Function>[] decoding formula
beforeEncode Class<? extend Function>[] encoding formula

@UInteger64Type

Unsigned 64-bit Integer type annotation,can be used to annotate BigInteger type field directly,or annotate any data type field by specifying encoding formula and decoding formula.

Field Data Type Description
value int byte offset
afterDecode Class<? extend Function>[] decoding formula
beforeEncode Class<? extend Function>[] encoding formula

@BinaryType

Binary type annotation,can be used to annotate byte[] field directly,or annotate any data type field by specifying encoding formula and decoding formula.

Field Data Type Description
value int byte offset
length int length
afterDecode Class<? extend Function> decoding formula
beforeEncode Class<? extend Function> encoding formula

@StringType

String type annotation,can be used to annotate String field directly,or annotate any data type field by specifying encoding formula and decoding formula.

Field Data Type Description
value int byte offset
length int length
charsetName String character set
afterDecode Class<? extend Function> decoding formula
beforeEncode Class<? extend Function> encoding formula

@TimestampType

Timestamp type annotation,can be used to annotate java.sql.Timestamp field directly,or annotate any data type field by specifying encoding formula and decoding formula.

Field Data Type Description
value int byte offset
protocolType ProtocolType protocol type
unit TimeUnit time unit
afterDecode Class<? extend Function> decoding formula
beforeEncode Class<? extend Function> encoding formula

@Date

Date type annotation,can be used to annotate java.util.Date field directly,or annotate any data type field by specifying encoding formula and decoding formula.

Field Data Type Description
value int byte offset
protocolType ProtocolType protocol type
unit TimeUnit time unit
afterDecode Class<? extend Function> decoding formula
beforeEncode Class<? extend Function> encoding formula

@ArrayType

Array type annotation,can be used to annotate any array of Java primitive type field directly,or annotate any data type field by specifying encoding formula and decoding formula.

Field Data Type Description
value int byte offset
protocolType ProtocolType protocol type
unit TimeUnit time unit
afterDecode Class<? extend Function> decoding formula
beforeEncode Class<? extend Function> encoding formula

@ListType

List type annotation,can be used to annotate any list of Java primitive type field directly,or annotate any data type field by specifying encoding formula and decoding formula.

Field Data Type Description
value int byte offset
protocolType ProtocolType protocol type
length int length
afterDecode Class<? extend Function> decoding formula
beforeEncode Class<? extend Function> encoding formula

@EnumType

Enum type annotation,can be used to annotate enum field directly,or annotate any data type field by specifying encoding formula and decoding formula.

Field Data Type Description
value int byte offset
protocolType ProtocolType protocol type
afterDecode Class<? extend Function> decoding formula
beforeEncode Class<? extend Function> encoding formula

What is AutoType

When the data type of the protocol is Java's native data type, developers can directly use the AutoType annotation to annotate, and FastProto can infer the protocol type based on the field type.

Reverse Addressing

In order to resolve indefinite length data, FastProto supports reverse addressing:

  • If the byte offset is equal to -1, then address to the end of the message. Similarly, if the byte offset is equal to -2, then the second-to-last byte of the data is addressed
  • The length of StringType and BianryType also supports reverse addressing. If the length is equal to -1, then the address is to the end of the message. Similarly, if the length is equal to -2, then the second-to-last byte of the data is addressed