Documentation
¶
Overview ¶
Package encode provides a collection of functions for safe serialization and deserialization of data between different systems, such as databases, queues, and caches.
Index ¶
- func Base64EncodeString(s string) string
- func Base64Encoder(w io.Writer) io.WriteCloser
- func BufferDecode(reader io.Reader, data any) error
- func BufferDeserialize(reader io.Reader, data any) error
- func BufferEncode(data any) (*bytes.Buffer, error)
- func BufferSerialize(data any) (*bytes.Buffer, error)
- func ByteDecode(msg []byte, data any) error
- func ByteDeserialize(msg []byte, data any) error
- func ByteEncode(data any) ([]byte, error)
- func ByteSerialize(data any) ([]byte, error)
- func Decode(msg string, data any) error
- func Deserialize(msg string, data any) error
- func Encode(data any) (string, error)
- func GobEncoder(enc io.WriteCloser, data any) error
- func JsonEncoder(enc io.WriteCloser, data any) error
- func Serialize(data any) (string, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Base64EncodeString ¶ added in v1.121.0
Base64EncodeString encodes a string in Base64.
func Base64Encoder ¶ added in v1.121.0
func Base64Encoder(w io.Writer) io.WriteCloser
Base64Encoder wraps an io.Writer with a base64 encoder.
func BufferDecode ¶ added in v1.121.0
BufferDecode decodes gob+base64 data from the provided io.Reader into the provided data object.
func BufferDeserialize ¶ added in v1.121.0
BufferDeserialize decodes JSON+base64 data from the provided io.Reader into the provided data object.
func BufferEncode ¶ added in v1.121.0
BufferEncode encodes the input data to gob+base64 and returns it as a bytes.Buffer.
func BufferSerialize ¶ added in v1.121.0
BufferSerialize encodes the input data to JSON+base64 and returns it as a bytes.Buffer.
func ByteDecode ¶
ByteDecode decodes a byte slice message encoded with the ByteEncode function to the provided data object. The value underlying data must be a pointer to the correct type for the next data item received.
func ByteDeserialize ¶
ByteDeserialize decodes a string message encoded with the Serialize function to the provided data object. The value underlying data must be a pointer to the correct type for the next data item received.
func ByteEncode ¶
ByteEncode encodes the input data to gob+base64 byte slice.
func ByteSerialize ¶
ByteSerialize encodes the input data to JSON+base64 byte slice.
func Decode ¶
Decode decodes a string message encoded with the Encode function to the provided data object. The value underlying data must be a pointer to the correct type for the next data item received.
Example ¶
package main
import (
"fmt"
"log"
"github.com/tecnickcom/gogen/pkg/encode"
)
func main() {
type TestData struct {
Alpha string
Beta int
}
var data TestData
msg := "Kf+BAwEBCFRlc3REYXRhAf+CAAECAQVBbHBoYQEMAAEEQmV0YQEEAAAAD/+CAQZhYmMxMjMB/gLtAA=="
err := encode.Decode(msg, &data)
if err != nil {
log.Fatal(err)
}
fmt.Println(data)
}
Output: {abc123 -375}
func Deserialize ¶
Deserialize decodes a byte slice message encoded with the Serialize function to the provided data object. The value underlying data must be a pointer to the correct type for the next data item received.
Example ¶
package main
import (
"fmt"
"log"
"github.com/tecnickcom/gogen/pkg/encode"
)
func main() {
type TestData struct {
Alpha string
Beta int
}
var data TestData
msg := "eyJBbHBoYSI6ImFiYzEyMyIsIkJldGEiOi0zNzV9Cg=="
err := encode.Deserialize(msg, &data)
if err != nil {
log.Fatal(err)
}
fmt.Println(data)
}
Output: {abc123 -375}
func Encode ¶
Encode encodes the input data to gob+base64 string.
Example ¶
package main
import (
"fmt"
"log"
"github.com/tecnickcom/gogen/pkg/encode"
)
func main() {
type TestData struct {
Alpha string
Beta int
}
data := &TestData{Alpha: "test_string", Beta: -9876}
v, err := encode.Encode(data)
if err != nil {
log.Fatal(err)
}
fmt.Println(v)
}
func GobEncoder ¶ added in v1.121.0
func GobEncoder(enc io.WriteCloser, data any) error
GobEncoder encodes data using gob encoding and writes it to the provided io.WriteCloser.
func JsonEncoder ¶ added in v1.121.0
func JsonEncoder(enc io.WriteCloser, data any) error
JsonEncoder encodes data using JSON encoding and writes it to the provided io.WriteCloser.
func Serialize ¶
Serialize encodes the input data to JSON+base64 string.
Example ¶
package main
import (
"fmt"
"log"
"github.com/tecnickcom/gogen/pkg/encode"
)
func main() {
type TestData struct {
Alpha string
Beta int
}
data := &TestData{Alpha: "test_string", Beta: -9876}
v, err := encode.Serialize(data)
if err != nil {
log.Fatal(err)
}
fmt.Println(v)
}
Output: eyJBbHBoYSI6InRlc3Rfc3RyaW5nIiwiQmV0YSI6LTk4NzZ9Cg==
Types ¶
This section is empty.