structor

package module
v0.0.0-...-613f325 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2016 License: MIT Imports: 3 Imported by: 0

README

structor Build Status Coverage Status GoDoc

This Go package provides high-level reflection utilities for reading and writing struct fields.

Install

go get github.com/stephanos/structor

Documentation

godoc.org

License

MIT (see LICENSE).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Codec

type Codec struct {
	// Type returns the struct's type.
	Type reflect.Type

	// Fields returns the struct's field codecs.
	Fields []*FieldCodec

	// FieldNames returns the struct's exportable field names.
	FieldNames []string

	// Attrs can contain custom attributes of the codec.
	Attrs map[string]interface{}

	// Complete is whether the codec is completely processed.
	// An incomplete codec may be encountered when walking a recursive struct.
	Complete bool
}

Codec represents a struct and its fields.

type FieldCodec

type FieldCodec struct {

	// Parent is a reference to the struct the field is in.
	Parent *Codec

	// Index is the index of the field in the struct.
	Index int

	// Anonymous is whether the field is embedded.
	Anonymous bool

	// Name is the name of the field in the struct.
	Name string

	// Tag is the codec for the field's tags.
	Tag *TagCodec

	// Attrs can contain custom attributes of the field's codec.
	Attrs map[string]interface{}

	// Type is the field's type.
	Type reflect.Type

	// KeyType is the type of the field's key, if any.
	KeyType *reflect.Type

	// ElemType is the type of the field's collection's value type, if any.
	ElemType *reflect.Type
}

FieldCodec represents a struct field.

type FieldReader

type FieldReader struct {
	*FieldCodec
	// contains filtered or unexported fields
}

FieldReader represents a readable struct field.

func (*FieldReader) Value

func (fld *FieldReader) Value() interface{}

Value returns the field's value.

type FieldWriter

type FieldWriter struct {
	*FieldReader
	// contains filtered or unexported fields
}

FieldWriter represents a writable field.

func (*FieldWriter) SetValue

func (fld *FieldWriter) SetValue(value interface{}) error

SetValue applies the passed-in value to the field. It returns an error if the field is not writable or the value's type does not match the field's.

type Reader

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

Reader represents a readable struct.

func (*Reader) Fields

func (r *Reader) Fields() []*FieldReader

Fields returns the readable fields of the struct.

type Set

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

Set is a collection of codecs, for a specific tag name.

func NewSet

func NewSet(tagName string) *Set

NewSet returns a new codec set for the passed-in tag name.

func (*Set) Add

func (s *Set) Add(src interface{}) error

Add creates a new codec from the passed-in value and adds it to the set. It expects a struct, struct pointer or reflect.Type of a struct.

func (*Set) AddMust

func (s *Set) AddMust(src interface{})

AddMust creates a new codec from the passed-in value and adds it to the set. It panics if an error occurs.

func (*Set) Get

func (s *Set) Get(src interface{}) (*Codec, error)

Get returns the according codec for the passed-in value. It returns an error if the value is invalid or no codec was found.

func (*Set) NewReader

func (s *Set) NewReader(src interface{}) (*Reader, error)

NewReader returns a new reader for the passed-in value, expecting a struct or struct pointer. It returns an error if the value is invalid or no matching codec was found.

func (*Set) NewWriter

func (s *Set) NewWriter(dst interface{}) (*Writer, error)

NewWriter returns a new writer for the passed-in value, expecting a struct pointer. It returns an error if the value is invalid or no matching codec was found.

func (*Set) SetValidateFunc

func (s *Set) SetValidateFunc(fn func(*Set, *Codec) error)

SetValidateFunc defines a function to validate a codec before it is added. When it returns an error the codec is not added to the set.

type TagCodec

type TagCodec struct {
	Values map[int]string
}

TagCodec represents a struct field's tag.

func (*TagCodec) IndexOf

func (tc *TagCodec) IndexOf(want string) int

IndexOf returns the index of the passed-in value.

func (*TagCodec) Modifiers

func (tc *TagCodec) Modifiers() []string

type Writer

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

Writer represents a writable struct.

func (*Writer) Fields

func (w *Writer) Fields() []*FieldWriter

Fields returns the writable fields of the struct.

Jump to

Keyboard shortcuts

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