marshaling

package
v3.0.0-pre.0...-f1e8a59 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2018 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package marshaling implements marshaling, unmarshaling and diff functionality for structs and maps.

Index

Constants

View Source
const (
	// Separator is used to separate the flattened struct fields.
	Separator = "."

	// DefaultVersion is the encoding version used by marshaling package.
	DefaultVersion Version = 1

	// ZeroEncoding represents case when the encode value is zero value of it's type.
	ZeroEncoding Encoding = 0
	// BigEndianEncoding represents case when big endian binary encoding was used to encode value.
	BigEndianEncoding Encoding = 1
	// LittleEndianEncoding represents case when little endian binary encoding was used to encode value.
	LittleEndianEncoding Encoding = 2
	// JSONEncoding represents case when MarshalJSON() method was used to encode value.
	JSONEncoding Encoding = 3
	// ProtoEncoding represents case when Proto() method was used to encode value.
	ProtoEncoding Encoding = 4
	// GobEncoding represents case when Gob was used to encode value.
	GobEncoding Encoding = 5
	// MsgPackEncoding represents case when MsgPack was used to encode value.
	MsgPackEncoding Encoding = 6
)

Variables

This section is empty.

Functions

func ByteDiff

func ByteDiff(new, old map[string][]byte) (diff map[string][]byte)

ByteDiff the new and the old value and return the changed fields

func BytesToType

func BytesToType(b []byte, typ reflect.Type) (interface{}, error)

BytesToType decodes []byte value in b into a new value of type typ. BytesToType expects the first byte in b to represent the encoding version used to encode the value and the second byte to represent the encoding and attempts to decode accordingly.

func Diff

func Diff(new, old map[string]interface{}) (diff map[string]interface{})

Diff the new and the old value and return the changed fields

func DiffFields

func DiffFields(a, b interface{}) (fields []string, err error)

DiffFields returns the fieldpaths of fields of a and b that differ in value.

func Flattened

func Flattened(m map[string]interface{}) (out map[string]interface{})

Flattened returns a copy of m with keys 'Flattened'. If the map contains sub-maps, the values of these sub-maps are set under the root map, each level separated by Separator.

func FlattenedValue

func FlattenedValue(m map[string]reflect.Value) (out map[string]reflect.Value)

FlattenedValue is like Flattened, but it operates on maps containing reflect.Value.

func IsNillableKind

func IsNillableKind(k reflect.Kind) bool

IsNillableKind reports whether k represents a nillable reflect.Kind.

func IsNillableType

func IsNillableType(t reflect.Type) bool

IsNillableType reports whether t represents a nillable reflect.Type.

func MarshalByteMap

func MarshalByteMap(v interface{}) (bm map[string][]byte, err error)

MarshalByteMap returns the byte map encoding of v.

MarshalByteMap traverses map returned by Marshal and converts all values to bytes.

func MarshalMap

func MarshalMap(v interface{}) (m map[string]interface{}, err error)

MarshalMap returns the map encoding of v, where v is either a struct or a map with string keys.

MarshalMap traverses the value v recursively. If v implements the MapMarshaler interface, MarshalMap calls its MarshalMap method to produce map[string]interface{}. Otherwise, MarshalMap first encodes the value v as a map[string]interface{}. Default marshaler marshals slices as maps with string keys, where all keys represent integers. The map produced by any of the methods will be flattened by joining sub-map values with a dot(note that slices produced by custom MarshalMap implementations won't be flattened).

func ToBytes

func ToBytes(v interface{}) (b []byte, err error)

ToBytes marshals v into a []byte value and returns the result. Slices and arrays of bytes, strings, booleans and numeric types are stored in a human-readable format, if value implements proto.Marshaler, result of Marshal() method is stored, otherwise encoding/gob is used. Encoded values have the according Encoding byte prepended.

func ToBytesValue

func ToBytesValue(v reflect.Value) (b []byte, err error)

ToBytesValue is like ToBytes, but operates on values of type reflect.Value.

func ToValueMap

func ToValueMap(m map[string]interface{}) map[string]reflect.Value

ToValueMap converts the input map m into a map[string]reflect.Value by calling reflect.ValueOf with each value in m.

func Unflattened

func Unflattened(m map[string]interface{}) map[string]interface{}

Unflattened unflattens m and returns the result

func UnmarshalByteMap

func UnmarshalByteMap(bm map[string][]byte, v interface{}, hooks ...mapstructure.DecodeHookFunc) error

UnmarshalByteMap parses the byte map-encoded data and stores the result in the value pointed to by v. UnmarshalByteMap uses the inverse of the encodings that MarshalByteMap uses.

func UnmarshalMap

func UnmarshalMap(m map[string]interface{}, v interface{}, hooks ...mapstructure.DecodeHookFunc) error

UnmarshalMap parses the map-encoded data and stores the result in the value pointed to by v.

UnmarshalMap uses the inverse of the encodings that Marshal uses.

Types

type ByteMapMarshaler

type ByteMapMarshaler interface {
	MarshalByteMap() (map[string][]byte, error)
}

ByteMapMarshaler is the interface implemented by an object that can marshal itself into a map[string][]byte.

MarshalByteMap encodes the receiver into map[string][]byte and returns the result.

type ByteMapUnmarshaler

type ByteMapUnmarshaler interface {
	UnmarshalByteMap(map[string][]byte) error
}

ByteMapUnmarshaler is the interface implemented by an object that can unmarshal a map[string][]byte representation of itself.

UnmarshalByteMap must be able to decode the form generated by MarshalByteMap. UnmarshalByteMap must deep copy the data if it wishes to retain the data after returning.

type Encoding

type Encoding byte

Encoding represents the encoding used to encode value into []byte representation. This is used as the first byte in the encoded []byte representation and allows for consistent decoding.

type MapMarshaler

type MapMarshaler interface {
	MarshalMap() (map[string]interface{}, error)
}

MapMarshaler is the interface implemented by an object that can marshal itself into a flattened map[string]interface{}

MarshalMap encodes the receiver into map[string]interface{} and returns the result.

type MapUnmarshaler

type MapUnmarshaler interface {
	UnmarshalMap(map[string]interface{}) error
}

MapUnmarshaler is the interface implemented by an object that can unmarshal a map[string]interface{} representation of itself.

UnmarshalMap must be able to decode the form generated by MarshalMap. UnmarshalMap must deep copy the data if it wishes to retain the data after returning.

type Version

type Version byte

Version represents the version of the encoding used to encode value into []byte representation. This is used as the second byte in the encoded []byte representation and allows for consistent decoding.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址