Revert back to dsd format constant typing

This commit is contained in:
Daniel 2021-11-23 11:24:32 +01:00
parent 6cde860324
commit 560b96a825
4 changed files with 40 additions and 48 deletions

View file

@ -9,9 +9,9 @@ import (
)
// DumpAndCompress stores the interface as a dsd formatted data structure and compresses the resulting data.
func DumpAndCompress(t interface{}, format SerializationFormat, compression CompressionFormat) ([]byte, error) {
func DumpAndCompress(t interface{}, format uint8, compression uint8) ([]byte, error) {
// Check if compression format is valid.
compression, ok := compression.ValidateCompressionFormat()
compression, ok := ValidateCompressionFormat(compression)
if !ok {
return nil, ErrIncompatibleFormat
}
@ -23,7 +23,7 @@ func DumpAndCompress(t interface{}, format SerializationFormat, compression Comp
}
// prepare writer
packetFormat := varint.Pack8(uint8(compression))
packetFormat := varint.Pack8(compression)
buf := bytes.NewBuffer(nil)
buf.Write(packetFormat)
@ -58,9 +58,9 @@ func DumpAndCompress(t interface{}, format SerializationFormat, compression Comp
}
// DecompressAndLoad decompresses the data using the specified compression format and then loads the resulting data blob into the interface.
func DecompressAndLoad(data []byte, compression CompressionFormat, t interface{}) (format SerializationFormat, err error) {
func DecompressAndLoad(data []byte, compression uint8, t interface{}) (format uint8, err error) {
// Check if compression format is valid.
compression, ok := compression.ValidateCompressionFormat()
_, ok := ValidateCompressionFormat(compression)
if !ok {
return 0, ErrIncompatibleFormat
}
@ -95,14 +95,9 @@ func DecompressAndLoad(data []byte, compression CompressionFormat, t interface{}
// assign decompressed data
data = buf.Bytes()
formatID, read, err := loadFormat(data)
format, read, err := loadFormat(data)
if err != nil {
return 0, err
}
format, ok = SerializationFormat(formatID).ValidateSerializationFormat()
if !ok {
return 0, ErrIncompatibleFormat
}
return format, LoadAsFormat(data[read:], format, t)
}

View file

@ -16,21 +16,21 @@ import (
)
// Load loads an dsd structured data blob into the given interface.
func Load(data []byte, t interface{}) (format SerializationFormat, err error) {
formatID, read, err := loadFormat(data)
func Load(data []byte, t interface{}) (format uint8, err error) {
format, read, err := loadFormat(data)
if err != nil {
return 0, err
}
format, ok := SerializationFormat(formatID).ValidateSerializationFormat()
_, ok := ValidateSerializationFormat(format)
if ok {
return format, LoadAsFormat(data[read:], format, t)
}
return DecompressAndLoad(data[read:], CompressionFormat(format), t)
return DecompressAndLoad(data[read:], format, t)
}
// LoadAsFormat loads a data blob into the interface using the specified format.
func LoadAsFormat(data []byte, format SerializationFormat, t interface{}) (err error) {
func LoadAsFormat(data []byte, format uint8, t interface{}) (err error) {
switch format {
case RAW:
return ErrIsRaw
@ -80,18 +80,18 @@ func loadFormat(data []byte) (format uint8, read int, err error) {
}
// Dump stores the interface as a dsd formatted data structure.
func Dump(t interface{}, format SerializationFormat) ([]byte, error) {
func Dump(t interface{}, format uint8) ([]byte, error) {
return DumpIndent(t, format, "")
}
// DumpIndent stores the interface as a dsd formatted data structure with indentation, if available.
func DumpIndent(t interface{}, format SerializationFormat, indent string) ([]byte, error) {
format, ok := format.ValidateSerializationFormat()
func DumpIndent(t interface{}, format uint8, indent string) ([]byte, error) {
format, ok := ValidateSerializationFormat(format)
if !ok {
return nil, ErrIncompatibleFormat
}
f := varint.Pack8(uint8(format))
f := varint.Pack8(format)
var data []byte
var err error
switch format {

View file

@ -120,8 +120,8 @@ var (
func TestConversion(t *testing.T) {
t.Parallel()
compressionFormats := []CompressionFormat{AutoCompress, GZIP}
formats := []SerializationFormat{JSON, CBOR, MsgPack}
compressionFormats := []uint8{AUTO, GZIP}
formats := []uint8{JSON, CBOR, MsgPack}
for _, compression := range compressionFormats {
for _, format := range formats {
@ -129,7 +129,7 @@ func TestConversion(t *testing.T) {
// simple
var b []byte
var err error
if compression != AutoCompress {
if compression != AUTO {
b, err = DumpAndCompress(simpleSubject, format, compression)
} else {
b, err = Dump(simpleSubject, format)
@ -151,7 +151,7 @@ func TestConversion(t *testing.T) {
}
// complex
if compression != AutoCompress {
if compression != AUTO {
b, err = DumpAndCompress(complexSubject, format, compression)
} else {
b, err = Dump(complexSubject, format)
@ -233,14 +233,14 @@ func TestConversion(t *testing.T) {
}
// test all formats
simplifiedFormatTesting := []SerializationFormat{JSON, CBOR, MsgPack, GenCode}
simplifiedFormatTesting := []uint8{JSON, CBOR, MsgPack, GenCode}
for _, format := range simplifiedFormatTesting {
// simple
var b []byte
var err error
if compression != AutoCompress {
if compression != AUTO {
b, err = DumpAndCompress(simpleSubject, format, compression)
} else {
b, err = Dump(simpleSubject, format)

View file

@ -9,39 +9,36 @@ var (
ErrUnknownFormat = errors.New("dsd: format is unknown")
)
type SerializationFormat uint8
// Format types.
const (
AUTO SerializationFormat = 0
RAW SerializationFormat = 1
CBOR SerializationFormat = 67 // C
GenCode SerializationFormat = 71 // G
JSON SerializationFormat = 74 // J
MsgPack SerializationFormat = 77 // M
)
AUTO = 0
type CompressionFormat uint8
// Serialization types.
RAW = 1
CBOR = 67 // C
GenCode = 71 // G
JSON = 74 // J
MsgPack = 77 // M
const (
AutoCompress CompressionFormat = 0
GZIP CompressionFormat = 90 // Z
)
// Compression types.
GZIP = 90 // Z
type SpecialFormat uint8
// Special types.
LIST = 76 // L
const (
LIST SpecialFormat = 76 // L
// Deprecated: NONE is deprecated, please use RAW instead.
NONE = 1
)
var (
DefaultSerializationFormat = JSON
DefaultCompressionFormat = GZIP
DefaultSerializationFormat uint8 = JSON
DefaultCompressionFormat uint8 = GZIP
)
// ValidateSerializationFormat validates if the format is for serialization,
// and returns the validated format as well as the result of the validation.
// If called on the AUTO format, it returns the default serialization format.
func (format SerializationFormat) ValidateSerializationFormat() (validated SerializationFormat, ok bool) {
func ValidateSerializationFormat(format uint8) (validatedFormat uint8, ok bool) {
switch format {
case AUTO:
return DefaultSerializationFormat, true
@ -63,9 +60,9 @@ func (format SerializationFormat) ValidateSerializationFormat() (validated Seria
// ValidateCompressionFormat validates if the format is for compression,
// and returns the validated format as well as the result of the validation.
// If called on the AUTO format, it returns the default compression format.
func (format CompressionFormat) ValidateCompressionFormat() (validated CompressionFormat, ok bool) {
func ValidateCompressionFormat(format uint8) (validatedFormat uint8, ok bool) {
switch format {
case AutoCompress:
case AUTO:
return DefaultCompressionFormat, true
case GZIP:
return format, true