package tmc
import "github.com/bep/tmc"
Example¶
Code:play
package main import ( "fmt" "log" "github.com/bep/tmc" ) func main() { m1 := map[string]interface{}{"num": 42} c, err := tmc.New() if err != nil { log.Fatal(err) } data, err := c.Marshal(m1) if err != nil { log.Fatal(err) } m2 := make(map[string]interface{}) err = c.Unmarshal(data, &m2) if err != nil { log.Fatal(err) } num := m2["num"] fmt.Printf("%v (%T)", num, num) }
Output:
42 (int)
Index ¶
- Variables
- func WithMarshalUnmarshaler(marshaler MarshalUnmarshaler) func(c *Codec) error
- func WithTypeAdapters(typeAdapters []Adapter) func(c *Codec) error
- func WithTypeSep(sep string) func(c *Codec) error
- type Adapter
- type Codec
- func New(opts ...Option) (*Codec, error)
- func (c *Codec) Marshal(v interface{}) ([]byte, error)
- func (c *Codec) Unmarshal(data []byte, v interface{}) error
- type MarshalUnmarshaler
- type Option
Examples ¶
Variables ¶
var ( // DefaultTypeAdapters contains the default set of type adapters. DefaultTypeAdapters = []Adapter{ NewAdapter(time.Now(), nil, nil), NewAdapter( 3*time.Hour, func(s string) (interface{}, error) { return time.ParseDuration(s) }, func(v interface{}) (string, error) { return v.(time.Duration).String(), nil }, ), NewAdapter(big.NewRat(1, 2), nil, nil), NewAdapter( int(32), func(s string) (interface{}, error) { return strconv.Atoi(s) }, func(v interface{}) (string, error) { return strconv.Itoa(v.(int)), nil }, ), } )
var JSONMarshaler = new(jsonMarshaler)
JSONMarshaler encodes and decodes JSON and is the default used in this codec.
Functions ¶
func WithMarshalUnmarshaler ¶
func WithMarshalUnmarshaler(marshaler MarshalUnmarshaler) func(c *Codec) error
WithMarshalUnmarshaler sets the MarshalUnmarshaler to use.
Default is JSONMarshaler.
Code:play
Output:Example¶
package main
import (
"fmt"
"log"
"github.com/bep/tmc"
yaml "gopkg.in/yaml.v2"
)
func main() {
m1 := map[string]interface{}{"num": 42}
c, err := tmc.New(tmc.WithMarshalUnmarshaler(new(yamlMarshaler)))
if err != nil {
log.Fatal(err)
}
data, err := c.Marshal(m1)
if err != nil {
log.Fatal(err)
}
m2 := make(map[string]interface{})
err = c.Unmarshal(data, &m2)
if err != nil {
log.Fatal(err)
}
num := m2["num"]
fmt.Printf("%v (%T)", num, num)
}
type yamlMarshaler int
func (yamlMarshaler) Marshal(v interface{}) ([]byte, error) {
return yaml.Marshal(v)
}
func (yamlMarshaler) Unmarshal(b []byte, v interface{}) error {
return yaml.Unmarshal(b, v)
}
42 (int)
func WithTypeAdapters ¶
WithTypeAdapters sets the type adapters to use. Note that if more than one adapter exists for the same type, the last one will win. This means that if you want to use the default adapters, but override some of them, you can do:
adapters := append(typedmapcodec.DefaultTypeAdapters, mycustomAdapters ...) codec := typedmapcodec.New(WithTypeAdapters(adapters))
func WithTypeSep ¶
WithTypeSep sets the separator to use before the type information encoded in the key field. Default is "|".
Types ¶
type Adapter ¶
type Adapter interface { FromString(s string) (interface{}, error) MarshalText() (text []byte, err error) Type() reflect.Type Wrap(v interface{}) Adapter }
Adapter wraps a type to preserve type information when encoding and decoding a map.
The simples way to create new adapters is via the NewAdapter function.
func NewAdapter ¶
func NewAdapter( target interface{}, fromString func(s string) (interface{}, error), toString func(v interface{}) (string, error)) Adapter
NewAdapter creates a new adapter that wraps the target type.
fromString can be omitted if target implements encoding.TextUnmarshaler. toString can be omitted if target implements encoding.TextMarshaler.
It will panic if it can not be created.
type Codec ¶
type Codec struct {
// contains filtered or unexported fields
}
Codec provides methods to marshal and unmarshal a Go map while preserving type information.
func New ¶
New creates a new Coded with some optional options.
func (*Codec) Marshal ¶
Marshal accepts a Go map and marshals it to the configured marshaler anntated with type information.
func (*Codec) Unmarshal ¶
Unmarshal unmarshals the given data to the given Go map, using any annotated type information found to preserve the type information stored in Marshal.
type MarshalUnmarshaler ¶
type MarshalUnmarshaler interface { Marshal(v interface{}) ([]byte, error) Unmarshal(b []byte, v interface{}) error }
MarshalUnmarshaler is the interface that must be implemented if you want to add support for more than JSON to this codec.
type Option ¶
Option configures the Codec.
Source Files ¶
- Version
- v0.5.1 (latest)
- Published
- Sep 2, 2019
- Platform
- darwin/amd64
- Imports
- 9 packages
- Last checked
- 1 minute ago –
Tools for package owners.