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. // 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. // Check if compression format is valid.
compression, ok := compression.ValidateCompressionFormat() compression, ok := ValidateCompressionFormat(compression)
if !ok { if !ok {
return nil, ErrIncompatibleFormat return nil, ErrIncompatibleFormat
} }
@ -23,7 +23,7 @@ func DumpAndCompress(t interface{}, format SerializationFormat, compression Comp
} }
// prepare writer // prepare writer
packetFormat := varint.Pack8(uint8(compression)) packetFormat := varint.Pack8(compression)
buf := bytes.NewBuffer(nil) buf := bytes.NewBuffer(nil)
buf.Write(packetFormat) 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. // 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. // Check if compression format is valid.
compression, ok := compression.ValidateCompressionFormat() _, ok := ValidateCompressionFormat(compression)
if !ok { if !ok {
return 0, ErrIncompatibleFormat return 0, ErrIncompatibleFormat
} }
@ -95,14 +95,9 @@ func DecompressAndLoad(data []byte, compression CompressionFormat, t interface{}
// assign decompressed data // assign decompressed data
data = buf.Bytes() data = buf.Bytes()
formatID, read, err := loadFormat(data) format, read, err := loadFormat(data)
if err != nil { if err != nil {
return 0, err return 0, err
} }
format, ok = SerializationFormat(formatID).ValidateSerializationFormat()
if !ok {
return 0, ErrIncompatibleFormat
}
return format, LoadAsFormat(data[read:], format, t) 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. // Load loads an dsd structured data blob into the given interface.
func Load(data []byte, t interface{}) (format SerializationFormat, err error) { func Load(data []byte, t interface{}) (format uint8, err error) {
formatID, read, err := loadFormat(data) format, read, err := loadFormat(data)
if err != nil { if err != nil {
return 0, err return 0, err
} }
format, ok := SerializationFormat(formatID).ValidateSerializationFormat() _, ok := ValidateSerializationFormat(format)
if ok { if ok {
return format, LoadAsFormat(data[read:], format, t) 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. // 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 { switch format {
case RAW: case RAW:
return ErrIsRaw 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. // 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, "") return DumpIndent(t, format, "")
} }
// DumpIndent stores the interface as a dsd formatted data structure with indentation, if available. // DumpIndent stores the interface as a dsd formatted data structure with indentation, if available.
func DumpIndent(t interface{}, format SerializationFormat, indent string) ([]byte, error) { func DumpIndent(t interface{}, format uint8, indent string) ([]byte, error) {
format, ok := format.ValidateSerializationFormat() format, ok := ValidateSerializationFormat(format)
if !ok { if !ok {
return nil, ErrIncompatibleFormat return nil, ErrIncompatibleFormat
} }
f := varint.Pack8(uint8(format)) f := varint.Pack8(format)
var data []byte var data []byte
var err error var err error
switch format { switch format {

View file

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

View file

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