encoding

package module
Version: v0.0.0-...-4a810d6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2019 License: Apache-2.0 Imports: 4 Imported by: 8

README

GoDoc codecov Build Status Go Report Card License Mentioned in Awesome Go

encoding

Package provides a generic interface to encoders and decoders

Example

package main
  
import (
        "flag"
        "fmt"
        "log"
        "strings"

        "github.com/mickep76/encoding"
        _ "github.com/mickep76/encoding/json"
        _ "github.com/mickep76/encoding/toml"
        _ "github.com/mickep76/encoding/yaml"
)

type Message struct {
        Name, Text string
}

type Messages struct {
        Messages []*Message
}

func main() {
        codec := flag.String("codec", "json", fmt.Sprintf("Codecs: [%s].", strings.Join(encoding.Codecs(), ", ")))
        indent := flag.String("indent", "", "Indent encoding (only supported by JSON codec)")
        flag.Parse()

        in := Messages{
                Messages: []*Message{
                        &Message{Name: "Ed", Text: "Knock knock."},
                        &Message{Name: "Sam", Text: "Who's there?"},
                        &Message{Name: "Ed", Text: "Go fmt."},
                        &Message{Name: "Sam", Text: "Go fmt who?"},
                        &Message{Name: "Ed", Text: "Go fmt yourself!"},
                },
        }

        var opts []encoding.Option
        if *indent != "" {
                opts = append(opts, encoding.WithIndent(*indent))
        }
        c, err := encoding.NewCodec(*codec, opts...)
        if err != nil {
                log.Fatal(err)
        }

        b, err := c.Encode(in)
        if err != nil {
                log.Fatal(err)
        }

        fmt.Printf("Codec: %s\n", *codec)
        fmt.Printf("Encoded:\n%s\n", string(b))

        out := Messages{}
        if err := c.Decode(b, &out); err != nil {
                log.Fatal(err)
        }

        fmt.Println("Decoded:")
        for _, m := range out.Messages {
                fmt.Printf("%s: %s\n", m.Name, m.Text)
        }
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnsupportedOption unsupported option
	ErrUnsupportedOption = errors.New("unsupported option")
)

Functions

func Codecs

func Codecs() []string

Codecs registered.

func Decode

func Decode(c Codec, encoded []byte, v interface{}) error

Decode method.

func Encode

func Encode(c Codec, v interface{}) ([]byte, error)

Encode method.

func Register

func Register(name string, codec Codec)

Register codec.

func Registered

func Registered(name string) error

Registered is the algorithm registered.

Types

type Codec

type Codec interface {
	NewCodec() Codec
	NewEncoder(w io.Writer) (Encoder, error)
	NewDecoder(r io.Reader) (Decoder, error)
	Encode(v interface{}) ([]byte, error)
	Decode(b []byte, v interface{}) error
	SetIndent(indent string) error
	SetMapString() error
}

Codec interface.

func NewCodec

func NewCodec(name string, opts ...Option) (Codec, error)

NewCodec variadic constructor.

type Decoder

type Decoder interface {
	Decode(v interface{}) error
}

Decoder interface.

type Encoder

type Encoder interface {
	Encode(v interface{}) error
}

Encoder interface.

type Option

type Option func(Codec) error

Option variadic function.

func WithIndent

func WithIndent(indent string) Option

WithIndent indent output. Supported by JSON.

func WithMapString

func WithMapString() Option

WithMapString convert map[interface{}]interface{} to map[string]interface{}. Suppored by YAML.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto