mirror of
https://github.com/safing/portbase
synced 2025-09-02 02:29:59 +00:00
Add support for RAW dsd format.
This commit is contained in:
parent
601dbffa4f
commit
8813102b7b
2 changed files with 12 additions and 0 deletions
|
@ -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.
|
// 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 SerializationFormat, t interface{}) (err error) {
|
||||||
switch format {
|
switch format {
|
||||||
|
case RAW:
|
||||||
|
return ErrIsRaw
|
||||||
case JSON:
|
case JSON:
|
||||||
err = json.Unmarshal(data, t)
|
err = json.Unmarshal(data, t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -86,6 +88,12 @@ func DumpIndent(t interface{}, format SerializationFormat, indent string) ([]byt
|
||||||
var data []byte
|
var data []byte
|
||||||
var err error
|
var err error
|
||||||
switch format {
|
switch format {
|
||||||
|
case RAW:
|
||||||
|
var ok bool
|
||||||
|
data, ok = t.([]byte)
|
||||||
|
if !ok {
|
||||||
|
return nil, ErrIncompatibleFormat
|
||||||
|
}
|
||||||
case JSON:
|
case JSON:
|
||||||
// TODO: use SetEscapeHTML(false)
|
// TODO: use SetEscapeHTML(false)
|
||||||
if indent != "" {
|
if indent != "" {
|
||||||
|
|
|
@ -4,6 +4,7 @@ import "errors"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrIncompatibleFormat = errors.New("dsd: format is incompatible with operation")
|
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")
|
ErrNoMoreSpace = errors.New("dsd: no more space left after reading dsd type")
|
||||||
ErrUnknownFormat = errors.New("dsd: format is unknown")
|
ErrUnknownFormat = errors.New("dsd: format is unknown")
|
||||||
)
|
)
|
||||||
|
@ -12,6 +13,7 @@ type SerializationFormat uint8
|
||||||
|
|
||||||
const (
|
const (
|
||||||
AUTO SerializationFormat = 0
|
AUTO SerializationFormat = 0
|
||||||
|
RAW SerializationFormat = 1
|
||||||
CBOR SerializationFormat = 67 // C
|
CBOR SerializationFormat = 67 // C
|
||||||
GenCode SerializationFormat = 71 // G
|
GenCode SerializationFormat = 71 // G
|
||||||
JSON SerializationFormat = 74 // J
|
JSON SerializationFormat = 74 // J
|
||||||
|
@ -43,6 +45,8 @@ func (format SerializationFormat) ValidateSerializationFormat() (validated Seria
|
||||||
switch format {
|
switch format {
|
||||||
case AUTO:
|
case AUTO:
|
||||||
return DefaultSerializationFormat, true
|
return DefaultSerializationFormat, true
|
||||||
|
case RAW:
|
||||||
|
return format, true
|
||||||
case CBOR:
|
case CBOR:
|
||||||
return format, true
|
return format, true
|
||||||
case GenCode:
|
case GenCode:
|
||||||
|
|
Loading…
Add table
Reference in a new issue