stdesc

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2022 License: MIT Imports: 4 Imported by: 3

Documentation

Overview

Utils for creating structure descriptors, which allow reasonably fast access to structure fields, with support for things like aliasing and embedding.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsEmbedField added in v0.0.3

func IsEmbedField(pf PendingFiled) bool

Returns true, if field should be embedded, in order to get encoding/json like embedding. It's used by default FieldProcessor.

Types

type Computer added in v0.0.4

type Computer struct {
	// Fallbacks to processor, which embeds all anonymous fields and sets name to field name.
	// Note: path parameter may not be modified by this function.
	FieldProcessorFactory FieldProcessorFactory

	// Performs descriptor summarization.
	Summarizer Summarizer

	// Descriptor cache for each type.
	// Used if not nil.
	Cache *sync.Map
}

func (*Computer) ComputeDescriptor added in v0.0.4

func (c *Computer) ComputeDescriptor(ctx context.Context, ty reflect.Type) (desc Descriptor, err error)

Note: returned descriptor should be deep copied before modifying since it may be stored in cache.

type Descriptor

type Descriptor struct {
	Ty reflect.Type

	// also alows listing fields
	NameToField map[string]Field

	// Comptued summary of descriptor, performed by user code.
	ComputedSummary interface{}
}

type Field

type Field struct {
	Type reflect.Type // Field type, the most inner one, which can be accessed via get/set

	Name string
	Path []int // used to access fields via indexes, always at least one element in here
	Meta interface{}

	// Path to field, which contains struct, which contains this field
	ParentPath []int
}

func (*Field) MustGet

func (f *Field) MustGet(v reflect.Value) (res reflect.Value)

Returns value of given field or panics

func (*Field) MustSet

func (f *Field) MustSet(target, v reflect.Value)

Sets value of field. Note: panics if value is not addressable.

type FieldOptions

type FieldOptions struct {
	Skip bool // If true, field is skipped. Ignores all other settings.

	Embed bool // If true, and type is struct, then all fields from child structs are embedded as if they were in parent.
	Name  string
	Meta  interface{}

	Override bool // if true, and field with same name is set then overrides previous definition in case it was set.
}

type FieldProcessor

type FieldProcessor interface {
	ProcessField(pf PendingFiled) (options FieldOptions, err error)
}

FieldProcessor decides how field should be processed.

type FieldProcessorFactory added in v0.0.2

type FieldProcessorFactory interface {
	MakeFieldProcessor(ctx context.Context, ty reflect.Type) (fp FieldProcessor, err error)
}

type FieldProcessorFactoryFunc added in v0.0.2

type FieldProcessorFactoryFunc func(ctx context.Context, ty reflect.Type) (fp FieldProcessor, err error)

func (FieldProcessorFactoryFunc) MakeFieldProcessor added in v0.0.2

func (f FieldProcessorFactoryFunc) MakeFieldProcessor(ctx context.Context, ty reflect.Type) (fp FieldProcessor, err error)

type FieldProcessorFunc added in v0.0.2

type FieldProcessorFunc func(pf PendingFiled) (options FieldOptions, err error)

func (FieldProcessorFunc) MakeFieldProcessor added in v0.0.2

func (f FieldProcessorFunc) MakeFieldProcessor(ctx context.Context, ty reflect.Type) (fp FieldProcessor, err error)

func (FieldProcessorFunc) ProcessField added in v0.0.2

func (f FieldProcessorFunc) ProcessField(pf PendingFiled) (options FieldOptions, err error)

type PendingFiled added in v0.0.2

type PendingFiled struct {
	Field reflect.StructField
	Path  []int
}

type Summarizer added in v0.0.3

type Summarizer interface {
	SummaryzeDescriptor(ctx context.Context, desc Descriptor) (meta interface{}, err error)
}

type SummarizerFunc added in v0.0.3

type SummarizerFunc func(ctx context.Context, desc Descriptor) (meta interface{}, err error)

func (SummarizerFunc) SummaryzeDescriptor added in v0.0.3

func (f SummarizerFunc) SummaryzeDescriptor(ctx context.Context, desc Descriptor) (meta interface{}, err error)

Jump to

Keyboard shortcuts

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