structs

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2023 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package structs helps with reflection and is based on the archived github.com/fatih/structs package. Reflection is used a tremendous amount in the confire package, so it makes sense to have helpers for reflection to simplify things.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsStruct

func IsStruct(spec interface{}) bool

IsStruct returns true if the given variable is a struct or a pointer to a struct.

Types

type Field

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

Field represents a single struct field and encapsulates high-level functionality for managing and working with the field using the reflect package.

func (*Field) CanAddr

func (f *Field) CanAddr() bool

CanAddr reports whether the value's pointer can be obtained with Pointer(). Such values are called addressable. A value is addressable if it is an element of a slice, an element of an addressable array, a field of an addressable struct, or the result of dereferencing a pointer. If CanAddr returns false, calling Addr will panic.

func (*Field) CanSet

func (f *Field) CanSet() bool

CanSet returns true if the given field is settable.

func (*Field) Elem

func (f *Field) Elem() *Field

Elem returns a Field with the value that the interface f contains or that the pointer f points to. It panics if f's Kind is not Interface or Pointer. It returns the zero Value if f is nil.

func (*Field) Fields

func (f *Field) Fields() []*Field

Fields returns a slice of Fields, usually used to get the fields from a nested struct.

func (*Field) Init

func (f *Field) Init() error

Initialize a nil pointer to point to a zero-valued version of the struct.

func (*Field) InterfaceFrom

func (f *Field) InterfaceFrom(fn func(interface{}, *bool))

InterfaceFrom is a complex type assertion that allows you to pass in a function that performs a type assertion to the underlying field value. If the underlying value can interface and implements the specified assertion, then the value and assertion bool are set on the arguments, allowing you to "extract" an interface type from the value.

That was a bit complicated, so here is an example. Say you wanted to use the field as a encoding.TextUnmarshaler (e.g. you want to call the UnmarshalText() method of the value in the field). You would pass in a function as follows:

field.InterfaceFrom(func(v interface{}, ok *bool) { t, *ok := v.(encoding.TextUnmarshaler )})

The variable t is now a TextUnmarshaler, and you can call t.UnmarshalText() so long as the ok is true or t is not nil.

func (*Field) IsEmbedded

func (f *Field) IsEmbedded() bool

IsEmbedded returns true if the given field is an anonymous field.

func (*Field) IsExported

func (f *Field) IsExported() bool

IsExported returns true if the given field is exported.

func (*Field) IsNil

func (f *Field) IsNil() bool

IsNil reports if the field value is nil. The value must be a chan, func, interface, map, pointer, or slice value, otherwise this method panics.

func (*Field) IsZero

func (f *Field) IsZero() bool

IsZero returns true if the given field has a zero-value. Panics if the field is not exported

func (*Field) Kind

func (f *Field) Kind() reflect.Kind

Kind returns the fields kind, such as "string", "map", "bool", etc ..

func (*Field) Name

func (f *Field) Name() string

Name returns the name of the given field

func (*Field) Pointer

func (f *Field) Pointer() interface{}

Pointer returns a pointer interface representing the address of f. It panics if CanAddr() returns false. This is typically used to used to obtain a pointer to a struct field or slice element in order to call a method that requires a pointer receiver.

func (*Field) Reflect

func (f *Field) Reflect() reflect.Value

Reflect returns the underlying reflect value for the field

func (*Field) Set

func (f *Field) Set(val interface{}) error

Set sets the field to given value v. It returns an error if the field is not settable (not addressable or not exported) or if the given value's type doesn't match the fields type.

func (*Field) Tag

func (f *Field) Tag(key string) string

Tag returns the value associated with the key in the tag string. If there is no such key in the tag, an empty string is returned.

func (*Field) Type

func (f *Field) Type() reflect.Type

Type returns the field value's type.

func (*Field) TypeKind

func (f *Field) TypeKind() reflect.Kind

Kind returns the type of the field's kind, e.g. Array, Chan, Map, Pointer, or Slice.

func (*Field) Value

func (f *Field) Value() interface{}

Value returns the underlying value of the field, panics if the field is not exported.

func (*Field) Zero

func (f *Field) Zero() error

Zero sets the field to its zero value.

type Struct

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

Struct encapsulates a struct type to provide reflection around the struct.

func New

func New(spec interface{}) (*Struct, error)

New returns a wrapped struct ready for reflection. It returns an error if the spec is not a struct or a pointer to a struct.

func (*Struct) Field

func (s *Struct) Field(name string) (*Field, error)

Field returns a field by the specified name, returning an error if not found.

func (*Struct) Fields

func (s *Struct) Fields() []*Field

Fields returns a slice of all the fields on the struct.

func (*Struct) HasZero

func (s *Struct) HasZero() bool

HasZero returns true if any field in a struct is zero-valued for their type. Primitive types such as string, bool, int have zero-values "", false, 0, etc. Collection types are generally zero-valued if they are empty or nil. Nested structs are zero-valued if they are nil (for pointers) or if they are also zero-valued, e.g. all the fields on the struct are the zero-value for their type. If the field implements the IsZero() method (e.g. such as time.Time) then it is used.

func (*Struct) IsPointer

func (s *Struct) IsPointer() bool

Returns true if the wrapped struct is a pointer.

func (*Struct) IsZero

func (s *Struct) IsZero() bool

IsZero returns true if all fields on the struct are a zero-value for their type. Primitive types such as string, bool, int have zero-values "", false, 0, etc. Collection types are generally zero-valued if they are empty or nil. Nested structs are zero-valued if they are nil (for pointers) or if they are also zero-valued, e.g. all the fields on the struct are the zero-value for their type. If the field implements the IsZero() method (e.g. such as time.Time) then it is used.

func (*Struct) Name

func (s *Struct) Name() string

Name returns the struct type name within its package.

func (*Struct) Names

func (s *Struct) Names() []string

Names returns a slice with all of the field names on the struct.

func (*Struct) NumField

func (s *Struct) NumField() int

type Zero

type Zero interface {
	IsZero() bool
}

Jump to

Keyboard shortcuts

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