Documentation
¶
Overview ¶
Package marshaling implements marshaling, unmarshaling and diff functionality for structs and maps.
Index ¶
- Constants
- func ByteDiff(new, old map[string][]byte) (diff map[string][]byte)
- func BytesToType(b []byte, typ reflect.Type) (interface{}, error)
- func Diff(new, old map[string]interface{}) (diff map[string]interface{})
- func DiffFields(a, b interface{}) (fields []string, err error)
- func Flattened(m map[string]interface{}) (out map[string]interface{})
- func FlattenedValue(m map[string]reflect.Value) (out map[string]reflect.Value)
- func IsNillableKind(k reflect.Kind) bool
- func IsNillableType(t reflect.Type) bool
- func MarshalByteMap(v interface{}) (bm map[string][]byte, err error)
- func MarshalMap(v interface{}) (m map[string]interface{}, err error)
- func ToBytes(v interface{}) (b []byte, err error)
- func ToBytesValue(v reflect.Value) (b []byte, err error)
- func ToValueMap(m map[string]interface{}) map[string]reflect.Value
- func Unflattened(m map[string]interface{}) map[string]interface{}
- func UnmarshalByteMap(bm map[string][]byte, v interface{}, hooks ...mapstructure.DecodeHookFunc) error
- func UnmarshalMap(m map[string]interface{}, v interface{}, hooks ...mapstructure.DecodeHookFunc) error
- type ByteMapMarshaler
- type ByteMapUnmarshaler
- type Encoding
- type MapMarshaler
- type MapUnmarshaler
- type Version
Constants ¶
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 BytesToType ¶
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 DiffFields ¶
DiffFields returns the fieldpaths of fields of a and b that differ in value.
func Flattened ¶
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 ¶
FlattenedValue is like Flattened, but it operates on maps containing reflect.Value.
func IsNillableKind ¶
IsNillableKind reports whether k represents a nillable reflect.Kind.
func IsNillableType ¶
IsNillableType reports whether t represents a nillable reflect.Type.
func MarshalByteMap ¶
MarshalByteMap returns the byte map encoding of v.
MarshalByteMap traverses map returned by Marshal and converts all values to bytes.
func MarshalMap ¶
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 ¶
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 ¶
ToBytesValue is like ToBytes, but operates on values of type reflect.Value.
func ToValueMap ¶
ToValueMap converts the input map m into a map[string]reflect.Value by calling reflect.ValueOf with each value in m.
func Unflattened ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.