dsd

package
v0.19.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2024 License: GPL-3.0 Imports: 13 Imported by: 29

Documentation

Index

Constants

View Source
const (
	AUTO = 0

	// Serialization types.
	RAW     = 1
	CBOR    = 67 // C
	GenCode = 71 // G
	JSON    = 74 // J
	MsgPack = 77 // M
	YAML    = 89 // Y

	// Compression types.
	GZIP = 90 // Z

	// Special types.
	LIST = 76 // L
)

Format types.

Variables

View Source
var (
	ErrIncompatibleFormat = errors.New("dsd: format is incompatible with operation")
	ErrIsRaw              = errors.New("dsd: given data is in raw format")
	ErrUnknownFormat      = errors.New("dsd: format is unknown")
)

Errors.

View Source
var (
	DefaultSerializationFormat uint8 = JSON
	DefaultCompressionFormat   uint8 = GZIP
)

Default Formats.

View Source
var (
	ErrMissingBody        = errors.New("dsd: missing http body")
	ErrMissingContentType = errors.New("dsd: missing http content type")
)

HTTP Related Errors.

View Source
var (
	FormatToMimeType = map[uint8]string{
		CBOR:    "application/cbor",
		JSON:    "application/json",
		MsgPack: "application/msgpack",
		YAML:    "application/yaml",
	}
	MimeTypeToFormat = map[string]uint8{
		"cbor":    CBOR,
		"json":    JSON,
		"msgpack": MsgPack,
		"yaml":    YAML,
		"yml":     YAML,
	}
)

Format and MimeType mappings.

Functions

func DecompressAndLoad added in v0.4.1

func DecompressAndLoad(data []byte, compression uint8, t interface{}) (format uint8, err error)

DecompressAndLoad decompresses the data using the specified compression format and then loads the resulting data blob into the interface.

func Dump

func Dump(t interface{}, format uint8) ([]byte, error)

Dump stores the interface as a dsd formatted data structure.

func DumpAndCompress added in v0.4.1

func DumpAndCompress(t interface{}, format uint8, compression uint8) ([]byte, error)

DumpAndCompress stores the interface as a dsd formatted data structure and compresses the resulting data.

func DumpIndent added in v0.4.1

func DumpIndent(t interface{}, format uint8, indent string) ([]byte, error)

DumpIndent stores the interface as a dsd formatted data structure with indentation, if available.

func DumpToHTTPRequest added in v0.13.0

func DumpToHTTPRequest(r *http.Request, t interface{}, format uint8) error

DumpToHTTPRequest dumps the given data to the HTTP request using the given format. It also sets the Accept header to the same format.

func DumpToHTTPResponse added in v0.13.0

func DumpToHTTPResponse(w http.ResponseWriter, r *http.Request, t interface{}) error

DumpToHTTPResponse dumpts the given data to the HTTP response, using the format defined in the request's Accept header.

func FormatFromAccept added in v0.18.3

func FormatFromAccept(accept string) (format uint8)

FormatFromAccept returns the format for the given accept definition. The accept parameter matches the format of the HTTP Accept header. Special cases, in this order: - If accept is an empty string: returns default serialization format. - If accept contains no supported format, but a wildcard: returns default serialization format. - If accept contains no supported format, and no wildcard: returns AUTO format.

func Load

func Load(data []byte, t interface{}) (format uint8, err error)

Load loads an dsd structured data blob into the given interface.

func LoadAsFormat

func LoadAsFormat(data []byte, format uint8, t interface{}) (err error)

LoadAsFormat loads a data blob into the interface using the specified format.

func LoadFromHTTPRequest added in v0.13.0

func LoadFromHTTPRequest(r *http.Request, t interface{}) (format uint8, err error)

LoadFromHTTPRequest loads the data from the body into the given interface.

func LoadFromHTTPResponse added in v0.13.0

func LoadFromHTTPResponse(resp *http.Response, t interface{}) (format uint8, err error)

LoadFromHTTPResponse loads the data from the body into the given interface. Closing the body is left to the caller.

func MimeDump added in v0.18.2

func MimeDump(t any, accept string) (data []byte, mimeType string, format uint8, err error)

MimeDump dumps the given interface based on the given mime type accept header.

func MimeLoad added in v0.18.2

func MimeLoad(data []byte, accept string, t interface{}) (format uint8, err error)

MimeLoad loads the given data into the interface based on the given mime type accept header.

func RequestHTTPResponseFormat added in v0.13.0

func RequestHTTPResponseFormat(r *http.Request, format uint8) (mimeType string, err error)

RequestHTTPResponseFormat sets the Accept header to the given format.

func ValidateCompressionFormat added in v0.13.0

func ValidateCompressionFormat(format uint8) (validatedFormat uint8, ok bool)

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 ValidateSerializationFormat added in v0.13.0

func ValidateSerializationFormat(format uint8) (validatedFormat uint8, ok bool)

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.

Types

type GenCodeCompatible added in v0.3.0

type GenCodeCompatible interface {
	// GenCodeMarshal gencode marshalls the struct into the given byte array, or a new one if its too small.
	GenCodeMarshal(buf []byte) ([]byte, error)
	// GenCodeUnmarshal gencode unmarshalls the struct and returns the bytes read.
	GenCodeUnmarshal(buf []byte) (uint64, error)
}

GenCodeCompatible is an interface to identify and use gencode compatible structs.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL