refmt: github.com/polydawn/refmt Index | Examples | Files | Directories

package refmt

import "github.com/polydawn/refmt"

Refmt is a serialization and object-mapping library.

Look to the README on github for more high-level information.

This top-level package exposes simple helper methods for most common operations. You can also compose custom marshallers/unmarshallers and serializer/deserializers by constructing a `refmt.TokenPump` with components from the packages beneath this one. For example, the `refmt.JsonEncode` helper method can be replicated by combining an `obj.Marshaller` with a `json.Encoder`.

Index

Examples

Package Files

cloneHelpers.go doc.go marshalHelpers.go unmarshalHelpers.go

func Clone Uses

func Clone(src, dst interface{}) error

func CloneAtlased Uses

func CloneAtlased(src, dst interface{}, atl atlas.Atlas) error

func Marshal Uses

func Marshal(opts EncodeOptions, v interface{}) ([]byte, error)

func MarshalAtlased Uses

func MarshalAtlased(opts EncodeOptions, v interface{}, atl atlas.Atlas) ([]byte, error)

func MustClone Uses

func MustClone(src, dst interface{})

func MustCloneAtlased Uses

func MustCloneAtlased(src, dst interface{}, atl atlas.Atlas)

func Unmarshal Uses

func Unmarshal(opts DecodeOptions, data []byte, v interface{}) error

func UnmarshalAtlased Uses

func UnmarshalAtlased(opts DecodeOptions, data []byte, v interface{}, atl atlas.Atlas) error

type Cloner Uses

type Cloner interface {
    Clone(src, dst interface{}) error
}

func NewCloner Uses

func NewCloner(atl atlas.Atlas) Cloner

type DecodeOptions Uses

type DecodeOptions interface {
    IsDecodeOptions() // marker method.
}

type EncodeOptions Uses

type EncodeOptions interface {
    IsEncodeOptions() // marker method.
}

type Marshaller Uses

type Marshaller interface {
    Marshal(v interface{}) error
}

func NewMarshaller Uses

func NewMarshaller(opts EncodeOptions, wr io.Writer) Marshaller

Code:

var buf bytes.Buffer
encoder := refmt.NewMarshaller(json.EncodeOptions{}, &buf)
err := encoder.Marshal(map[string]interface{}{
    "x": "a",
    "y": 1,
})
fmt.Println(buf.String())
fmt.Printf("%v\n", err)

Output:

{"x":"a","y":1}
<nil>

func NewMarshallerAtlased Uses

func NewMarshallerAtlased(opts EncodeOptions, wr io.Writer, atl atlas.Atlas) Marshaller

Code:

type MyType struct {
    X   string
    Y   int
}

MyType_AtlasEntry := atlas.BuildEntry(MyType{}).
    StructMap().Autogenerate().
    Complete()

atl := atlas.MustBuild(
    MyType_AtlasEntry,
    // this is a vararg... stack more entries here!
)

var buf bytes.Buffer
encoder := refmt.NewMarshallerAtlased(json.EncodeOptions{}, &buf, atl)
err := encoder.Marshal(MyType{"a", 1})
fmt.Println(buf.String())
fmt.Printf("%v\n", err)

Output:

{"x":"a","y":1}
<nil>

Code:

type MyType struct {
    X   string
    Y   int
}

MyType_AtlasEntry := atlas.BuildEntry(MyType{}).
    StructMap().
    AddField("X", atlas.StructMapEntry{SerialName: "overrideName"}).
    // and no "Y" mapping at all!
    Complete()

atl := atlas.MustBuild(
    MyType_AtlasEntry,
    // this is a vararg... stack more entries here!
)

var buf bytes.Buffer
encoder := refmt.NewMarshallerAtlased(json.EncodeOptions{}, &buf, atl)
err := encoder.Marshal(MyType{"a", 1})
fmt.Println(buf.String())
fmt.Printf("%v\n", err)

Output:

{"overrideName":"a"}
<nil>

Code:

type MyType struct {
    X   string
    Y   string
    Z   string
}

MyType_AtlasEntry := atlas.BuildEntry(MyType{}).
    Transform().
    TransformMarshal(atlas.MakeMarshalTransformFunc(
        func(x MyType) (string, error) {
            return string(x.X) + ":" + string(x.Y) + ":" + string(x.Z), nil
        })).
    TransformUnmarshal(atlas.MakeUnmarshalTransformFunc(
        func(x string) (MyType, error) {
            ss := strings.Split(x, ":")
            if len(ss) != 3 {
                return MyType{}, fmt.Errorf("parsing MyType: string must have 3 parts, separated by colon")
            }
            return MyType{ss[0], ss[1], ss[2]}, nil
        })).
    Complete()

atl := atlas.MustBuild(
    MyType_AtlasEntry,
    // this is a vararg... stack more entries here!
)

var buf bytes.Buffer
encoder := refmt.NewMarshallerAtlased(json.EncodeOptions{}, &buf, atl)

err := encoder.Marshal(MyType{"serializes", "as", "string!"})
fmt.Println(buf.String())
fmt.Printf("%v\n", err)

Output:

"serializes:as:string!"
<nil>

type Unmarshaller Uses

type Unmarshaller interface {
    Unmarshal(v interface{}) error
}

func NewUnmarshaller Uses

func NewUnmarshaller(opts DecodeOptions, r io.Reader) Unmarshaller

func NewUnmarshallerAtlased Uses

func NewUnmarshallerAtlased(opts DecodeOptions, r io.Reader, atl atlas.Atlas) Unmarshaller

Directories

PathSynopsis
cborPackage implementing the CBOR -- Concise Binary Object Notation -- http://cbor.io/ -- spec.
jsonPackage implementing the JSON -- http://json.org/ -- spec.
misc
objThe `obj` package defines Marshaller and Unmarshaller types, which can be used to convert in-memory values to token streams, and token streams to unpack in-memory values.
obj/atlasAtlas types are used to define how to map Go values into refmt token streams.
obj/atlas/commoncommonatlases is a package full of `atlas.Entry` definions for common types in the standard library.
pretty
sharedThe `shared` package defines helper types and functions used internally by all the other refmt packages; it is not user-facing.
testutilTesting helper functions.
tokPackage containing Token struct and TokenType info.
tok/fixturesToken stream test fixtures.

Package refmt imports 6 packages (graph) and is imported by 4 packages. Updated 2019-08-10. Refresh now. Tools for package owners.