Documentation
¶
Overview ¶
Package encoding provides bounded encoding and decoding helpers.
Index ¶
- Variables
- func DecodeBase64(input string, opts ...Base64Option) ([]byte, error)
- func DecodeHex(input string, opts ...HexOption) ([]byte, error)
- func DecodeJSON(data []byte, value any, opts ...JSONOption) error
- func DecodeJSONReader(reader io.Reader, value any, opts ...JSONOption) error
- func EncodeBase64(data []byte, opts ...Base64Option) (string, error)
- func EncodeHex(data []byte, opts ...HexOption) (string, error)
- func EncodeJSON(value any, opts ...JSONOption) ([]byte, error)
- type Base64Encoding
- type Base64Option
- type HexOption
- type JSONOption
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidBase64Config indicates an invalid base64 configuration. ErrInvalidBase64Config = ewrap.New("invalid base64 config") // ErrBase64Empty indicates the base64 input is empty. ErrBase64Empty = ewrap.New("base64 input is empty") // ErrBase64TooLong indicates the base64 input exceeds the configured max length. ErrBase64TooLong = ewrap.New("base64 input too long") // ErrBase64Invalid indicates the base64 input is invalid. ErrBase64Invalid = ewrap.New("base64 input is invalid") // ErrInvalidHexConfig indicates an invalid hex configuration. ErrInvalidHexConfig = ewrap.New("invalid hex config") // ErrHexEmpty indicates the hex input is empty. ErrHexEmpty = ewrap.New("hex input is empty") // ErrHexTooLong indicates the hex input exceeds the configured max length. ErrHexTooLong = ewrap.New("hex input too long") // ErrHexInvalid indicates the hex input is invalid. ErrHexInvalid = ewrap.New("hex input is invalid") // ErrInvalidJSONConfig indicates an invalid JSON configuration. ErrInvalidJSONConfig = ewrap.New("invalid json config") // ErrJSONTooLarge indicates the JSON input exceeds the configured max length. ErrJSONTooLarge = ewrap.New("json input too large") // ErrJSONInvalid indicates the JSON input is invalid. ErrJSONInvalid = ewrap.New("json input is invalid") // ErrJSONTrailingData indicates trailing JSON data was found. ErrJSONTrailingData = ewrap.New("json trailing data detected") )
Functions ¶
func DecodeBase64 ¶
func DecodeBase64(input string, opts ...Base64Option) ([]byte, error)
DecodeBase64 decodes a base64-encoded string.
func DecodeJSON ¶
func DecodeJSON(data []byte, value any, opts ...JSONOption) error
DecodeJSON decodes JSON from a byte slice with size bounds.
func DecodeJSONReader ¶
func DecodeJSONReader(reader io.Reader, value any, opts ...JSONOption) error
DecodeJSONReader decodes JSON from a reader with size bounds.
func EncodeBase64 ¶
func EncodeBase64(data []byte, opts ...Base64Option) (string, error)
EncodeBase64 encodes data using the configured base64 encoding.
func EncodeJSON ¶
func EncodeJSON(value any, opts ...JSONOption) ([]byte, error)
EncodeJSON marshals a value using go-json with size bounds.
Types ¶
type Base64Encoding ¶
type Base64Encoding int
Base64Encoding identifies a base64 encoding variant.
const ( // Base64EncodingRawURL uses URL-safe base64 without padding. Base64EncodingRawURL Base64Encoding = iota // Base64EncodingRawStd uses standard base64 without padding. Base64EncodingRawStd // Base64EncodingURL uses URL-safe base64 with padding. Base64EncodingURL // Base64EncodingStd uses standard base64 with padding. Base64EncodingStd )
type Base64Option ¶
type Base64Option func(*base64Options) error
Base64Option configures base64 encoding and decoding.
func WithBase64Encoding ¶
func WithBase64Encoding(encoding Base64Encoding) Base64Option
WithBase64Encoding sets the base64 encoding variant.
func WithBase64MaxLength ¶
func WithBase64MaxLength(maxLength int) Base64Option
WithBase64MaxLength sets the maximum accepted base64 string length.
type HexOption ¶
type HexOption func(*hexOptions) error
HexOption configures hex encoding and decoding.
func WithHexMaxLength ¶
WithHexMaxLength sets the maximum accepted hex string length.
type JSONOption ¶
type JSONOption func(*jsonOptions) error
JSONOption configures JSON encoding and decoding.
func WithJSONAllowUnknownFields ¶
func WithJSONAllowUnknownFields(allow bool) JSONOption
WithJSONAllowUnknownFields allows unknown fields during decode.
func WithJSONMaxBytes ¶
func WithJSONMaxBytes(maxBytes int) JSONOption
WithJSONMaxBytes sets the maximum JSON payload size.
func WithJSONUseNumber ¶
func WithJSONUseNumber(useNumber bool) JSONOption
WithJSONUseNumber enables json.Number decoding for numbers.