Documentation
¶
Overview ¶
Package codec provides a set of utilities for encoding and decoding data in Go.
Index ¶
- Constants
- func Register(contentType string, readerWriter ReaderWriter)
- type BaseCodec
- func (bc *BaseCodec) DecodeBytes(b []byte, v interface{}) error
- func (bc *BaseCodec) DecodeString(s string, v interface{}) error
- func (bc *BaseCodec) EncodeToBytes(v interface{}) ([]byte, error)
- func (bc *BaseCodec) EncodeToString(v interface{}) (string, error)
- func (bc *BaseCodec) MimeTypes() []string
- func (bc *BaseCodec) Read(r io.Reader, v interface{}) (err error)
- func (bc *BaseCodec) SetOption(key string, value interface{})
- func (bc *BaseCodec) Write(v interface{}, w io.Writer) (err error)
- type BaseConstraints
- type BoolConstraints
- type BytesDecoder
- type BytesEncoder
- type Codec
- type Decoder
- type Encoder
- type F32Constraints
- type F64Constraints
- type IntConstraints
- type ReaderWriter
- type StrConstraints
- type StringDecoder
- type StringEncoder
- type StructMeta
- type UIntConstraints
- type Validator
Constants ¶
const ( ValidateOnRead = "ValidateOnRead" ValidateBefWrite = "ValidateBefWrite" Charset = "charset" JsonEscapeHTML = "JsonEscapeHTML" PrettyPrint = "PrettyPrint" )
Variables ¶
This section is empty.
Functions ¶
func Register ¶ added in v1.1.2
func Register(contentType string, readerWriter ReaderWriter)
Types ¶
type BaseCodec ¶
type BaseCodec struct {
// contains filtered or unexported fields
}
BaseCodec is a struct that encapsulates a ReaderWriter interface, a set of options, and a sync.Once instance to ensure that certain operations are only performed once. It is designed to handle encoding and decoding operations with customizable options.
func (*BaseCodec) DecodeBytes ¶
func (*BaseCodec) DecodeString ¶
func (*BaseCodec) EncodeToBytes ¶
EncodeToBytes :
func (*BaseCodec) EncodeToString ¶
type BaseConstraints ¶
type BaseConstraints struct {
// Name of the field
Name string
// Dimension holds the field dimension
Dimension int
// Required flag indicating if the field is a required field.
Required bool
// TargetNames stores map for known format types. This allows
TargetNames map[string]string
// TargetConfig stores configuration that is required by the target format . for Eg. Attribute config for XML etc.
TargetConfig map[string]string
// Sequence specifies the order of the fields in the source/target format
Sequence int
//SkipField indicates that if the value of the field is absent/nil then skip the field while writing to data
//This is similar to omitempty
SkipField bool
}
BaseConstraints struct captures the basic information for a field
type BoolConstraints ¶
type BoolConstraints struct {
BaseConstraints
DefaultVal *bool
}
BoolConstraints Struct
type BytesDecoder ¶
type BytesDecoder interface {
//DecodeBytes will decode a type from an array of bytes
DecodeBytes(b []byte, v interface{}) error
}
BytesDecoder Interface
type BytesEncoder ¶
type BytesEncoder interface {
// EncodeToBytes will encode the provided type to []byte
EncodeToBytes(v interface{}) ([]byte, error)
}
BytesEncoder Interface
type Codec ¶
type Codec interface {
Decoder
Encoder
ReaderWriter
//SetOption sets an option to the reader and writer
SetOption(key string, value interface{})
}
Codec Interface
func Get ¶
Get returns a Codec based on the provided content type and options. It supports JSON, XML, and YAML content types. If the content type contains a charset, it is added to the options but not used by the known JSON, XML, and YAML Read Writers.
Parameters:
- contentType: A string representing the MIME type of the content.
- options: A map of options to configure the Codec.
Returns:
- c: A Codec configured for the specified content type.
- err: An error if the content type is unsupported.
func GetDefault ¶
GetDefault function creates an instance of codec based on the contentType and defaultOptions
func JsonCodec ¶ added in v1.1.2
func JsonCodec() Codec
JsonCodec Provides a JSONCodec JsonCodec returns a Codec for handling JSON data. It retrieves the default Codec for the MIME type "application/json". If there is an error during retrieval, it is ignored and the default Codec is returned.
type F32Constraints ¶
type F32Constraints struct {
BaseConstraints
DefaultVal *float32
Min *float32 //The value is inclusive
Max *float32 //The value is inclusive
}
F32Constraints Struct
type F64Constraints ¶
type F64Constraints struct {
BaseConstraints
DefaultVal *float64
Min *float64 //The value is inclusive
Max *float64 //The value is inclusive
}
F64Constraints Struct
type IntConstraints ¶
type IntConstraints struct {
BaseConstraints
DefaultVal *int
Min *int //The value is inclusive
Max *int //The value is inclusive
}
IntConstraints Struct
type ReaderWriter ¶
type ReaderWriter interface {
//Write a type to writer
Write(v interface{}, w io.Writer) error
//Read a type from a reader
Read(r io.Reader, v interface{}) error
//MimeTypes returns a slice of strings representing the MIME types
MimeTypes() []string
}
ReaderWriter is an interface that defines methods for writing and reading data to and from an io.Writer and io.Reader, respectively.
Write writes the given value to the provided writer. It takes an interface{} value and an io.Writer, and returns an error if the write operation fails.
Read reads data from the provided reader into the given value. It takes an io.Reader and an interface{} value, and returns an error if the read operation fails.
type StrConstraints ¶
type StrConstraints struct {
BaseConstraints
DefaultVal *string
Pattern *string
Format *string
MinLength *int
MaxLength *int
}
StrConstraints Struct
type StringDecoder ¶
type StringDecoder interface {
//DecodeString will decode a type from string
DecodeString(s string, v interface{}) error
}
StringDecoder Interface
type StringEncoder ¶
type StringEncoder interface {
//EncodeToString will encode a type to string
EncodeToString(v interface{}) (string, error)
}
StringEncoder Interface
type UIntConstraints ¶
type UIntConstraints struct {
BaseConstraints
DefaultVal *uint
Min *uint //The value is inclusive
Max *uint //The value is inclusive
}
UIntConstraints Struct