Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Codec ¶
Codec bundles an Encoder and Decoder for S ↔ T round-trips.
func PipeCodec ¶ added in v0.2.0
PipeCodec chains two codecs: Codec[A,B] + Codec[B,C] → Codec[A,C].
Example ¶
package main
import (
"fmt"
"strconv"
goencode "github.com/foomo/goencode"
)
func main() {
intStr := goencode.Codec[int, string]{
Encode: func(i int) (string, error) {
return strconv.Itoa(i), nil
},
Decode: func(s string, i *int) error {
v, err := strconv.Atoi(s)
if err != nil {
return err
}
*i = v
return nil
},
}
strBytes := goencode.Codec[string, []byte]{
Encode: func(s string) ([]byte, error) {
return []byte(s), nil
},
Decode: func(b []byte, s *string) error {
*s = string(b)
return nil
},
}
piped := goencode.PipeCodec(intStr, strBytes)
encoded, err := piped.Encode(42)
if err != nil {
fmt.Printf("Encode failed: %v\n", err)
return
}
var decoded int
if err := piped.Decode(encoded, &decoded); err != nil {
fmt.Printf("Decode failed: %v\n", err)
return
}
fmt.Printf("Encoded: %s\n", string(encoded))
fmt.Printf("Decoded: %d\n", decoded)
}
Output: Encoded: 42 Decoded: 42
type Decoder ¶
Decoder decodes target T back into source S.
func PipeDecoder ¶ added in v0.2.0
PipeDecoder chains two decoders in reverse: decodes C → B via second, then B → A via first.
Example ¶
package main
import (
"fmt"
"strconv"
goencode "github.com/foomo/goencode"
)
func main() {
strToInt := goencode.Decoder[int, string](func(s string, i *int) error {
v, err := strconv.Atoi(s)
if err != nil {
return err
}
*i = v
return nil
})
bytesToStr := goencode.Decoder[string, []byte](func(b []byte, s *string) error {
*s = string(b)
return nil
})
piped := goencode.PipeDecoder(strToInt, bytesToStr)
var got int
if err := piped([]byte("42"), &got); err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Printf("Result: %d\n", got)
}
Output: Result: 42
type Encoder ¶
Encoder encodes source S to target T.
func PipeEncoder ¶ added in v0.2.0
PipeEncoder chains two encoders: A → B → C.
Example ¶
package main
import (
"fmt"
"strconv"
goencode "github.com/foomo/goencode"
)
func main() {
intToStr := goencode.Encoder[int, string](func(i int) (string, error) {
return strconv.Itoa(i), nil
})
strToBytes := goencode.Encoder[string, []byte](func(s string) ([]byte, error) {
return []byte(s), nil
})
piped := goencode.PipeEncoder(intToStr, strToBytes)
got, err := piped(42)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Printf("Result: %s\n", string(got))
}
Output: Result: 42
type StreamCodec ¶
type StreamCodec[S any] struct { Encode StreamEncoder[S] Decode StreamDecoder[S] }
StreamCodec bundles streaming encode/decode for S.
type StreamDecoder ¶
StreamDecoder decodes S from an io.Reader.
Click to show internal directories.
Click to hide internal directories.