stick

package
v0.29.5 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2020 License: MIT Imports: 12 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Backoff

func Backoff(min, max time.Duration, factor float64, attempt int) time.Duration

Backoff will calculate the exponential delay.

func Contains

func Contains(list []string, str string) bool

Contains return whether the list contains the item.

func Get

func Get(acc Accessible, name string) (interface{}, bool)

Get will lookup the specified field on the accessible and return its value and whether the field was found at all.

func GetType added in v0.29.5

func GetType(v interface{}) reflect.Type

GetType will get the underlying reflect type for the specified value. It will unwrap pointers automatically.

func Includes

func Includes(all, subset []string) bool

Includes returns whether a list includes another list.

func InternalBSONValue added in v0.29.1

func InternalBSONValue(typ bsontype.Type, bytes []byte) []byte

InternalBSONValue will return a byte sequence for an internal BSON value.

func Intersect

func Intersect(listA, listB []string) []string

Intersect will return a list with items that are not part of both lists.

func MustGet

func MustGet(acc Accessible, name string) interface{}

MustGet will call Get and panic if the operation failed.

func MustSet

func MustSet(acc Accessible, name string, value interface{})

MustSet will call Set and panic if the operation failed.

func Set

func Set(acc Accessible, name string, value interface{}) bool

Set will set the specified field on the accessible with the provided value and return whether the field has been found and the value has been set.

func Subtract added in v0.29.1

func Subtract(listA, listB []string) []string

Subtract will return a list with items that are only part of the first list.

func Union

func Union(lists ...[]string) []string

Union will will merge all list and remove duplicates.

func Unique

func Unique(list []string) []string

Unique will return a new list with unique strings.

Types

type Accessible

type Accessible interface {
	GetAccessor(interface{}) *Accessor
}

Accessible is a type that has dynamically accessible fields.

type Accessor

type Accessor struct {
	Name   string
	Fields map[string]*Field
}

Accessor provides dynamic access to a structs fields.

func BuildAccessor

func BuildAccessor(v interface{}, ignore ...string) *Accessor

BuildAccessor will build an accessor for the provided type.

func GetAccessor added in v0.29.5

func GetAccessor(acc Accessible) *Accessor

GetAccessor is a short-hand to retrieve the accessor of an accessible.

type BasicAccess added in v0.29.5

type BasicAccess struct{}

BasicAccess may be embedded in a struct to provide basic accessibility.

func (*BasicAccess) GetAccessor added in v0.29.5

func (a *BasicAccess) GetAccessor(v interface{}) *Accessor

GetAccessor implements the Accessible interface.

type Coding

type Coding string

Coding defines an encoding, decoding and transfer scheme.

const (
	JSON Coding = "json"
	BSON Coding = "bson"
)

The available coding schemes.

func (Coding) GetKey added in v0.29.1

func (c Coding) GetKey(field reflect.StructField) string

GetKey will return the coding key for the specified struct field.

func (Coding) Marshal

func (c Coding) Marshal(in interface{}) ([]byte, error)

Marshal will encode the specified value into a byte sequence.

Note: When marshalling a non document compatible type to BSON the result is a custom byte sequence that can only be unmarshalled by this codec.

func (Coding) SafeUnmarshal added in v0.29.1

func (c Coding) SafeUnmarshal(in []byte, out interface{}) error

SafeUnmarshal will decode the specified value from the provided byte sequence. It will preserve JSON numbers when decoded into an interface{} value.

func (Coding) Transfer

func (c Coding) Transfer(in, out interface{}) error

Transfer will transfer data from one value to another using.

func (Coding) Unmarshal

func (c Coding) Unmarshal(in []byte, out interface{}) error

Unmarshal will decode the specified value from the provided byte sequence.

func (Coding) UnmarshalKeyedList added in v0.29.1

func (c Coding) UnmarshalKeyedList(data []byte, list interface{}, field string) error

UnmarshalKeyedList will unmarshal a list and match objects by comparing the key in the specified field instead of their position in the list.

When using with custom types the type should implement the following methods:

func (l *Links) UnmarshalBSONValue(typ bsontype.Type, bytes []byte) error {
	return stick.BSON.UnmarshalKeyedList(stick.InternalBSONValue(typ, bytes), l, "Ref")
}

func (l *Links) UnmarshalJSON(bytes []byte) error {
	return stick.JSON.UnmarshalKeyedList(bytes, l, "Ref")
}

type Field

type Field struct {
	Index int
	Type  reflect.Type
}

Field is dynamically accessible field.

type Map

type Map map[string]interface{}

Map represents a simple map. It provides methods to marshal and unmarshal concrete types to and from the map using JSON or BSON coding.

func MustMap

func MustMap(from interface{}, coding Coding) Map

MustMap will marshal the specified object to a map and panic on any error.

func (Map) Flat added in v0.29.4

func (m Map) Flat(separator string) Map

Flat will return a flattened map by separator.

func (*Map) Marshal

func (m *Map) Marshal(from interface{}, coding Coding) error

Marshal will marshal the specified value to the map. If the map already contains data, only fields present in the value are overwritten.

func (*Map) MustMarshal

func (m *Map) MustMarshal(from interface{}, coding Coding)

MustMarshal will marshal and panic on error.

func (*Map) MustUnmarshal

func (m *Map) MustUnmarshal(to interface{}, coding Coding)

MustUnmarshal will unmarshal and panic on error.

func (*Map) Unmarshal

func (m *Map) Unmarshal(to interface{}, coding Coding) error

Unmarshal will unmarshal the map to the specified value. If the value already contains data, only fields present in the map are overwritten.

type NoValidation

type NoValidation struct{}

NoValidation can be embedded in a struct to provide a no-op validation method.

func (*NoValidation) Validate

func (*NoValidation) Validate() error

Validate will perform no validation.

type Validatable added in v0.29.0

type Validatable interface {
	Validate() error
}

Validatable represents a type that can be validated.

Jump to

Keyboard shortcuts

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