Documentation
¶
Index ¶
- func NewCodec(opts ...Option) encoding.Codec[[]byte, []byte]
- func NewDecoder(opts ...Option) encoding.Decoder[[]byte, []byte]
- func NewEncoder(opts ...Option) encoding.Encoder[[]byte, []byte]
- func NewStreamCodec(opts ...Option) encoding.StreamCodec[[]byte]
- func NewStreamDecoder(opts ...Option) encoding.StreamDecoder[[]byte]
- func NewStreamEncoder(opts ...Option) encoding.StreamEncoder[[]byte]
- type Option
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewCodec ¶
NewCodec returns a gzip compression codec. It is safe for concurrent use.
Example ¶
package main
import (
"fmt"
goencode "github.com/foomo/goencode"
"github.com/foomo/goencode/gzip"
json "github.com/foomo/goencode/json/v1"
)
func main() {
type Data struct {
Name string
}
c := goencode.PipeCodec(json.NewCodec[Data](), gzip.NewCodec())
encoded, err := c.Encode(Data{Name: "example-123"})
if err != nil {
fmt.Printf("Encode failed: %v\n", err)
return
}
var decoded Data
if err := c.Decode(encoded, &decoded); err != nil {
fmt.Printf("Decode failed: %v\n", err)
return
}
fmt.Printf("Decoded Name: %s\n", decoded.Name)
}
Output: Decoded Name: example-123
func NewDecoder ¶ added in v0.2.0
NewDecoder returns a gzip decompression decoder.
func NewEncoder ¶ added in v0.2.0
NewEncoder returns a gzip compression encoder.
func NewStreamCodec ¶
func NewStreamCodec(opts ...Option) encoding.StreamCodec[[]byte]
NewStreamCodec returns a gzip compression stream codec. It is safe for concurrent use.
Example ¶
package main
import (
"bytes"
"fmt"
"github.com/foomo/goencode/gzip"
)
func main() {
c := gzip.NewStreamCodec()
input := []byte("hello gzip stream")
var buf bytes.Buffer
if err := c.Encode(&buf, input); err != nil {
fmt.Printf("Encode failed: %v\n", err)
return
}
var decoded []byte
if err := c.Decode(&buf, &decoded); err != nil {
fmt.Printf("Decode failed: %v\n", err)
return
}
fmt.Printf("Decoded: %s\n", string(decoded))
}
Output: Decoded: hello gzip stream
func NewStreamDecoder ¶ added in v0.2.0
func NewStreamDecoder(opts ...Option) encoding.StreamDecoder[[]byte]
NewStreamDecoder returns a gzip decompression stream decoder.
func NewStreamEncoder ¶ added in v0.2.0
func NewStreamEncoder(opts ...Option) encoding.StreamEncoder[[]byte]
NewStreamEncoder returns a gzip compression stream encoder.
Types ¶
type Option ¶
type Option func(o *options)
Option configures a gzip Codec.
func WithLevel ¶
WithLevel sets the gzip compression level. Use compress/gzip constants: gzip.NoCompression, gzip.BestSpeed, gzip.BestCompression, gzip.DefaultCompression, gzip.HuffmanOnly.
func WithMaxDecodedSize ¶
WithMaxDecodedSize sets the maximum allowed size of decompressed data in bytes. If the decompressed data exceeds this limit, Decode returns an error. A value of 0 (the default) means no limit.