Documentation
¶
Index ¶
- Constants
- Variables
- func DecompressAndLoad(data []byte, compression uint8, t interface{}) (format uint8, err error)
- func Dump(t interface{}, format uint8) ([]byte, error)
- func DumpAndCompress(t interface{}, format uint8, compression uint8) ([]byte, error)
- func DumpIndent(t interface{}, format uint8, indent string) ([]byte, error)
- func DumpToHTTPRequest(r *http.Request, t interface{}, format uint8) error
- func DumpToHTTPResponse(w http.ResponseWriter, r *http.Request, t interface{}) error
- func DumpWithoutIdentifier(t interface{}, format uint8, indent string) ([]byte, error)
- func FormatFromAccept(accept string) (format uint8)
- func Load(data []byte, t interface{}) (format uint8, err error)
- func LoadAsFormat(data []byte, format uint8, t interface{}) (err error)
- func LoadFromHTTPRequest(r *http.Request, t interface{}) (format uint8, err error)
- func LoadFromHTTPResponse(resp *http.Response, t interface{}) (format uint8, err error)
- func MimeDump(t any, accept string) (data []byte, mimeType string, format uint8, err error)
- func MimeLoad(data []byte, accept string, t interface{}) (format uint8, err error)
- func RequestHTTPResponseFormat(r *http.Request, format uint8) (mimeType string, err error)
- func ValidateCompressionFormat(format uint8) (validatedFormat uint8, ok bool)
- func ValidateSerializationFormat(format uint8) (validatedFormat uint8, ok bool)
- type GenCodeCompatible
Constants ¶
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 ¶
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.
var ( DefaultSerializationFormat uint8 = JSON DefaultCompressionFormat uint8 = GZIP )
Default Formats.
var ( ErrMissingBody = errors.New("dsd: missing http body") ErrMissingContentType = errors.New("dsd: missing http content type") )
HTTP Related Errors.
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 ¶
DecompressAndLoad decompresses the data using the specified compression format and then loads the resulting data blob into the interface.
func DumpAndCompress ¶
DumpAndCompress stores the interface as a dsd formatted data structure and compresses the resulting data.
func DumpIndent ¶
DumpIndent stores the interface as a dsd formatted data structure with indentation, if available.
func DumpToHTTPRequest ¶
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 ¶
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 DumpWithoutIdentifier ¶ added in v1.2.0
DumpWithoutIdentifier stores the interface as a data structure, without format identifier, but with indentation, if specified and available.
func FormatFromAccept ¶
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 LoadAsFormat ¶
LoadAsFormat loads a data blob into the interface using the specified format.
func LoadFromHTTPRequest ¶
LoadFromHTTPRequest loads the data from the body into the given interface.
func LoadFromHTTPResponse ¶
LoadFromHTTPResponse loads the data from the body into the given interface. Closing the body is left to the caller.
func MimeLoad ¶
MimeLoad loads the given data into the interface based on the given mime type accept header.
func RequestHTTPResponseFormat ¶
RequestHTTPResponseFormat sets the Accept header to the given format.
func ValidateCompressionFormat ¶
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 ¶
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 ¶
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.