chain

package module
v0.0.0-...-8352b7d Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2020 License: Apache-2.0 Imports: 3 Imported by: 0

README

chain

Docs

A generic implementation of the chaining logic in the errors package in the standard library. Who even knows what this is useful for tho?

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As(v interface{}, target interface{}) bool

As finds the first value in v's chain that matches target, and if so, sets target to that value and returns true. Otherwise, it returns false.

The chain consists of v itself followed by the sequence of values obtained by repeatedly calling Unwrap.

A value matches target if its concrete value is assignable to the value pointed to by target, or if the value has a method As(interface{}) bool such that As(target) returns true. In the latter case, the As method is responsible for setting target.

A value type might provide an As method so it can be treated as if it were a different value type.

As panics if target is not a non-nil pointer.

func Build

func Build(vals ...interface{}) interface{}

Build chains together vals, returning the last element.

If vals is empty, this will panic.

If vals contains a single element, it is returned and no action is taken.

Otherwise, a chain is built using simple rules, processed from first to last element. If an element implements Wrap(interface{}) bool, the Wrap method is called and passed the previous element. If Wrap returns true, then chaining continues. If the element does not implement Wrap, or Wrap returns false, the value is wrapped with an unspecified type and then chaining continues.

func Is

func Is(v interface{}, target interface{}) bool

Is reports whether any value in v's chain matches target.

The chain consists of v itself followed by the sequence of values obtained by repeatedly calling Unwrap.

A value is considered a match if it is equal to target or if it implements an Is(interface{}) bool such that Is(target) returns true.

A value type might provide an Is method so it can be treated as equivalent to an existing value. For example, if MyValue defines:

		func (m MyValue) Is(target interface{}) bool {
			return target == "foo"
   }

then Is(MyValue{}, "foo") returns true.

func Unwrap

func Unwrap(v interface{}) (interface{}, bool)

Unwrap returns the result of calling the Unwrap method on v, if v's type implements iface.Unwrap. Otherwise, Unwrap returns nil and false.

Types

type Holder

type Holder struct {
	Value interface{}
	Ok    bool
}

Holder holds an arbitrary Value and a positive assertion that it was intentionaly filled.

This is useful for differentiating between zero values and nils that were intentionally filled, or just automatically filled during instantiation.

The reason it is included in this package is that it is a useful building block for implementing wrapper types for chaining.

func Hold

func Hold(v interface{}) Holder

Hold builds a new Holder and sets it to v

func (*Holder) Get

func (h *Holder) Get() (interface{}, bool)

Get returns the Holder's Value and the "filled" assertion.

func (*Holder) Set

func (h *Holder) Set(v interface{})

Set sets the Holder's Value to v.

type Link struct {
	// contains filtered or unexported fields
}

Link is a generic chainable wrapper for any value.

It holds a value and wraps another value.

func (*Link) As

func (l *Link) As(target interface{}) bool

As returns chain.As(v, target) where v is the held value

func (*Link) Is

func (l *Link) Is(target interface{}) bool

Is returns true if the target equals the held value

func (*Link) Set

func (l *Link) Set(v interface{}) *Link

Set sets the Link's held value to v

func (*Link) Unwrap

func (l *Link) Unwrap() (interface{}, bool)

Unwrap unwraps the wrapped value

func (*Link) Wrap

func (l *Link) Wrap(v interface{}) bool

Wrap sets the Link's wrapped value

Directories

Path Synopsis
internal
x

Jump to

Keyboard shortcuts

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