Documentation
¶
Overview ¶
Package encode provides helpers for serialization and deserialization across system boundaries such as databases, queues, caches, and RPC payloads.
The package supports two main modes:
- Gob + Base64 encoding for arbitrary Go values - JSON + Base64 encoding for interoperable text-based payloads
Caveats:
- Decoding reads a single encoded value; any trailing bytes are ignored.
- The reader- and buffer-based decoders do not bound their input. When decoding from an untrusted source, wrap the reader with io.LimitReader.
- Gob is not designed to decode adversarial input; prefer the JSON family (Serialize/Deserialize) for untrusted, interoperable payloads.
- The JSON encoding appends a trailing newline, which is included in the encoded output.
Index ¶
- func Base64DecodeString(s string) ([]byte, error)
- func Base64Decoder(r io.Reader) io.Reader
- 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 Base64DecodeString ¶
Base64DecodeString decodes the Base64 string s into its raw bytes.
It is the inverse of Base64EncodeString. The decoded bytes need not be valid UTF-8, so they are returned as a byte slice.
func Base64Decoder ¶
Base64Decoder wraps r with a streaming Base64 decoder.
It is the read-side counterpart to Base64Encoder.
func Base64EncodeString ¶
Base64EncodeString returns the Base64 representation of s.
Use it when text-safe transport is required for arbitrary bytes.
func Base64Encoder ¶
func Base64Encoder(w io.Writer) io.WriteCloser
Base64Encoder wraps w with a streaming Base64 encoder.
The caller should close the returned writer to flush buffered output.
func BufferDecode ¶
BufferDecode reads gob+Base64 content from reader into data.
data must be a pointer to the destination type. Only the first encoded value is read; any trailing bytes are ignored. When reading from an untrusted source, wrap reader with io.LimitReader to bound the input.
func BufferDeserialize ¶
BufferDeserialize reads JSON+Base64 content from reader into data.
data must be a pointer to the destination type. Only the first encoded value is read; any trailing bytes are ignored. When reading from an untrusted source, wrap reader with io.LimitReader to bound the input.
func BufferEncode ¶
BufferEncode encodes data as gob+Base64 and returns an in-memory buffer.
It is a single-buffer building block reused by Encode and ByteEncode.
func BufferSerialize ¶
BufferSerialize encodes data as JSON+Base64 and returns an in-memory buffer.
It is a single-buffer building block reused by Serialize and ByteSerialize.
func ByteDecode ¶
ByteDecode decodes gob+Base64 bytes into data.
data must be a pointer to the destination type.
func ByteDeserialize ¶
ByteDeserialize decodes JSON+Base64 bytes into data.
data must be a pointer to the destination type.
func ByteEncode ¶
ByteEncode encodes data as gob+Base64 bytes.
This format is convenient for binary channels while still staying text-safe.
func ByteSerialize ¶
ByteSerialize encodes data as JSON+Base64 bytes.
func Decode ¶
Decode decodes a gob+Base64 string into data.
data must be a pointer to the destination type. Only the first encoded value is read; any trailing bytes are ignored.
Example ¶
package main
import (
"fmt"
"log"
"github.com/tecnickcom/nurago/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 JSON+Base64 string into data.
data must be a pointer to the destination type. Only the first encoded value is read; any trailing bytes are ignored.
Example ¶
package main
import (
"fmt"
"log"
"github.com/tecnickcom/nurago/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 data as gob+Base64 string.
It is useful for storing typed payloads in text-only fields.
Example ¶
The gob wire format embeds process-global type IDs, so the encoded output is not stable across runs; this example is intentionally not output-testable.
package main
import (
"fmt"
"log"
"github.com/tecnickcom/nurago/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)
}
Output:
func GobEncoder ¶
func GobEncoder(enc io.WriteCloser, data any) error
GobEncoder gob-encodes data into enc and closes enc.
It centralizes gob encoding and close handling for stream-based pipelines.
func JSONEncoder ¶
func JSONEncoder(enc io.WriteCloser, data any) error
JSONEncoder JSON-encodes data into enc and closes enc.
It is useful for stream-friendly JSON pipelines with explicit close semantics.
func Serialize ¶
Serialize encodes data as JSON+Base64 string.
Choose this over Encode when interoperability with non-Go systems matters.
Example ¶
package main
import (
"fmt"
"log"
"github.com/tecnickcom/nurago/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.