Add support for RAW dsd format.

This commit is contained in:
Daniel 2021-11-22 14:46:57 +01:00
parent 601dbffa4f
commit 8813102b7b
2 changed files with 12 additions and 0 deletions
formats/dsd

View file

@ -31,6 +31,8 @@ func Load(data []byte, t interface{}) (format SerializationFormat, err error) {
// LoadAsFormat loads a data blob into the interface using the specified format.
func LoadAsFormat(data []byte, format SerializationFormat, t interface{}) (err error) {
switch format {
case RAW:
return ErrIsRaw
case JSON:
err = json.Unmarshal(data, t)
if err != nil {
@ -86,6 +88,12 @@ func DumpIndent(t interface{}, format SerializationFormat, indent string) ([]byt
var data []byte
var err error
switch format {
case RAW:
var ok bool
data, ok = t.([]byte)
if !ok {
return nil, ErrIncompatibleFormat
}
case JSON:
// TODO: use SetEscapeHTML(false)
if indent != "" {

View file

@ -4,6 +4,7 @@ import "errors"
var (
ErrIncompatibleFormat = errors.New("dsd: format is incompatible with operation")
ErrIsRaw = errors.New("dsd: given data is in raw format")
ErrNoMoreSpace = errors.New("dsd: no more space left after reading dsd type")
ErrUnknownFormat = errors.New("dsd: format is unknown")
)
@ -12,6 +13,7 @@ type SerializationFormat uint8
const (
AUTO SerializationFormat = 0
RAW SerializationFormat = 1
CBOR SerializationFormat = 67 // C
GenCode SerializationFormat = 71 // G
JSON SerializationFormat = 74 // J
@ -43,6 +45,8 @@ func (format SerializationFormat) ValidateSerializationFormat() (validated Seria
switch format {
case AUTO:
return DefaultSerializationFormat, true
case RAW:
return format, true
case CBOR:
return format, true
case GenCode: