mapper

package
v0.14.1-dev Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2018 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Unmap

func Unmap(obj interface{}) (map[string]interface{}, error)

Unmap translates an already mapped target object into a raw, unmapped form.

Types

type Decoder

type Decoder func(m Mapper, obj map[string]interface{}) (interface{}, error)

Decoder is a func that knows how to decode into particular type.

type Decoders

type Decoders map[reflect.Type]Decoder

Decoders is a map from type to a decoder func that understands how to decode that type.

type FieldError

type FieldError interface {
	error
	Field() string  // returns the name of the field with a problem.
	Reason() string // returns a full diagnostic string about the error.
}

FieldError represents a failure during decoding of a specific field.

func AsObject

func AsObject(v interface{}, ty reflect.Type, key string) (map[string]interface{}, FieldError)

AsObject attempts to coerce an existing value to an object map, returning a non-nil error if it cannot be done.

func AsString

func AsString(v interface{}, ty reflect.Type, key string) (*string, FieldError)

AsString attempts to coerce an existing value to a string, returning a non-nil error if it cannot be done.

func FieldObject

func FieldObject(obj map[string]interface{}, ty reflect.Type,
	key string, optional bool) (map[string]interface{}, FieldError)

FieldObject looks up a field by name within an object map, coerces it to an object itself, and returns it. If the field exists but is not an object map, or it is missing and optional is false, a non-nil error is returned.

func FieldString

func FieldString(obj map[string]interface{}, ty reflect.Type, key string, optional bool) (*string, FieldError)

FieldString looks up a field by name within an object map, coerces it to a string, and returns it. If the field exists but is not a string, or it is missing and optional is false, a non-nil error is returned.

func NewFieldError

func NewFieldError(ty string, fld string, err error) FieldError

func NewTypeFieldError

func NewTypeFieldError(ty reflect.Type, fld string, err error) FieldError

type Mapper

type Mapper interface {
	// Decode decodes a JSON-like object into the target pointer to a structure.
	Decode(obj map[string]interface{}, target interface{}) MappingError
	// DecodeField decodes a single JSON-like value (with a given type and name) into a target pointer to a structure.
	DecodeValue(obj map[string]interface{}, ty reflect.Type, key string, target interface{}, optional bool) FieldError
	// Encode encodes an object into a JSON-like in-memory object.
	Encode(source interface{}) (map[string]interface{}, MappingError)
	// EncodeValue encodes a value into its JSON-like in-memory value format.
	EncodeValue(v interface{}) (interface{}, MappingError)
}

Mapper can map from weakly typed JSON-like property bags to strongly typed structs, and vice versa.

func New

func New(opts *Opts) Mapper

New allocates a new mapper object with the given options.

type MappingError

type MappingError interface {
	error
	Failures() []error    // the full set of errors (each of one of the below types).
	AddFailure(err error) // registers a new failure.
}

MappingError represents a collection of decoding errors, defined below.

func Map

func Map(obj map[string]interface{}, target interface{}) MappingError

Map decodes an entire map into a target object, using an anonymous decoder and tag-directed mappings.

func MapI

func MapI(obj map[string]interface{}, target interface{}) MappingError

MapI decodes an entire map into a target object, using an anonymous decoder and tag-directed mappings. This variant ignores any missing required fields in the payload in addition to any unrecognized fields.

func MapIM

func MapIM(obj map[string]interface{}, target interface{}) MappingError

MapIM decodes an entire map into a target object, using an anonymous decoder and tag-directed mappings. This variant ignores any missing required fields in the payload.

func MapIU

func MapIU(obj map[string]interface{}, target interface{}) MappingError

MapIU decodes an entire map into a target object, using an anonymous decoder and tag-directed mappings. This variant ignores any unrecognized fields in the payload.

func NewMappingError

func NewMappingError(errs []error) MappingError

type MissingError

type MissingError struct {
	Type    reflect.Type
	Fld     string
	Message string
}

MissingError is used when a required field is missing on an object of a given type.

func NewMissingError

func NewMissingError(ty reflect.Type, fld string) *MissingError

func (*MissingError) Error

func (e *MissingError) Error() string

func (*MissingError) Field

func (e *MissingError) Field() string

func (*MissingError) Reason

func (e *MissingError) Reason() string

type Opts

type Opts struct {
	Tags               []string // the tag names to recognize (`json` and `pulumi` if unspecified).
	OptionalTags       []string // the tags to interpret to mean "optional" (`optional` if unspecified).
	SkipTags           []string // the tags to interpret to mean "skip" (`skip` if unspecified).
	CustomDecoders     Decoders // custom decoders.
	IgnoreMissing      bool     // ignore missing required fields.
	IgnoreUnrecognized bool     // ignore unrecognized fields.
}

Opts controls the way mapping occurs; for default behavior, simply pass an empty struct.

type UnrecognizedError

type UnrecognizedError struct {
	Type    reflect.Type
	Fld     string
	Message string
}

UnrecognizedError is used when a field is unrecognized on the given type.

func NewUnrecognizedError

func NewUnrecognizedError(ty reflect.Type, fld string) *UnrecognizedError

func (*UnrecognizedError) Error

func (e *UnrecognizedError) Error() string

func (*UnrecognizedError) Field

func (e *UnrecognizedError) Field() string

func (*UnrecognizedError) Reason

func (e *UnrecognizedError) Reason() string

type WrongTypeError

type WrongTypeError struct {
	Type    reflect.Type
	Fld     string
	Expect  reflect.Type
	Actual  reflect.Type
	Message string
}

func NewWrongTypeError

func NewWrongTypeError(ty reflect.Type, fld string, expect reflect.Type, actual reflect.Type) *WrongTypeError

func (*WrongTypeError) Error

func (e *WrongTypeError) Error() string

func (*WrongTypeError) Field

func (e *WrongTypeError) Field() string

func (*WrongTypeError) Reason

func (e *WrongTypeError) Reason() string

Jump to

Keyboard shortcuts

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