github.com/alloyzeus/go-azfl@v0.0.0-20231220071816-9740126a2d07/azid/bin.go (about) 1 package azid 2 3 // BinMarshalable is an interface definition for objects which able to 4 // provide an azid-bin representation of itself. 5 // 6 // This interface is for top-level object. If the object is part of other 7 // object, i.e., a field, see BinFieldMarshalable. 8 type BinMarshalable interface { 9 // AZIDBin returns an azid-bin representation of the instance. 10 AZIDBin() []byte 11 } 12 13 // BinUnmarshalable is and interface definition for objects which able 14 // to load an azid-bin representation into itself. 15 type BinUnmarshalable interface { 16 UnmarshalAZIDBin(b []byte) (readLen int, err error) 17 } 18 19 // BinFieldMarshalable is an interface definition for objects which 20 // able to provide an azid-bin representation of itself as a field 21 // of other azid object. 22 type BinFieldMarshalable interface { 23 // AZIDBinField returns an azid-bin representation of the instance 24 // which was designed to be part of the larger object. 25 // The data type is returned along with the data bytes instead of included 26 // in them as this method was designed for constructing an azid-bin 27 // structure where this object is part of. 28 AZIDBinField() ([]byte, BinDataType) 29 } 30 31 // BinFieldUnmarshalable is and interface definition for objects which able 32 // to load an azid-bin representation into itself. 33 type BinFieldUnmarshalable interface { 34 UnmarshalAZIDBinField(b []byte, typeHint BinDataType) (readLen int, err error) 35 } 36 37 //region BinDataType 38 39 // BinDataType represents a type supported by azid-bin. 40 type BinDataType uint8 41 42 // Supported data types. 43 // 44 // MSB is reserved 45 const ( 46 // BinDataTypeUnspecified is used for the zero value of BinDataType. 47 BinDataTypeUnspecified BinDataType = 0b0 48 49 BinDataTypeInt16 BinDataType = 0b_0001_0010 50 BinDataTypeInt32 BinDataType = 0b_0001_0011 51 BinDataTypeInt64 BinDataType = 0b_0001_0100 52 53 // BinDataTypeArray is used to indicate an array data type. 54 BinDataTypeArray BinDataType = 0b_0100_0000 55 ) 56 57 // BinDataTypeFromByte parses a byte to its respective BinDataType. 58 func BinDataTypeFromByte(b byte) (BinDataType, error) { 59 //TODO: accept only valid value 60 return BinDataType(b), nil 61 } 62 63 // Byte returns a representation of BinDataType as a byte. The byte then can 64 // be used as the byte mark in the resulting encoded data. 65 func (typ BinDataType) Byte() byte { return byte(typ) } 66 67 //endregion