ref

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package ref for struct instance reflector.

Index

Constants

View Source
const (
	TagDescription = "desc"
	TagExec        = "exec"
	TagIgnore      = "ignore"
	TagLabel       = "label"
	TagMock        = "mock"
	TagName        = "json"
	TagValidate    = "validate"
)

Variables

View Source
var (
	RegexMatchAllowEmptyField = regexp.MustCompile(`omitempty\s*,\s*`)
	RegexMatchExportedField   = regexp.MustCompile(`^[A-Z]`)
	RegexMatchRequiredField   = regexp.MustCompile(`required`)
	RegexRemoveOmitEmpty      = regexp.MustCompile(`\s*,\s*omitempty`)

	RegexRemoveRequires = []*regexp.Regexp{
		regexp.MustCompile(`\s*,\s*required`),
		regexp.MustCompile(`required\s*,\s*`),
	}
)

Functions

This section is empty.

Types

type Builder

type Builder struct {
	Fields []*Field
	Pkg    string
	Struct string
	// contains filtered or unexported fields
}

Builder is a component that used to parse the instance to field lists.

func New

func New(p any) (builder *Builder, err error)

New creates a new Builder instance.

func (*Builder) Keys

func (o *Builder) Keys() (list []*Field)

Keys returns the sorted field names.

func (*Builder) Seek

func (o *Builder) Seek() (err error)

Seek fields from the reflected value.

type Field

type Field struct {
	Name, Condition    string
	Label, Description string
	Mock               any

	AllowEmpty                             bool
	Ignored, IsArray, IsRequired, IsStruct bool
	TrueType                               string

	Child      *Builder
	OriginName string
	OriginType reflect.Type
	// contains filtered or unexported fields
}

Field is a component that represents a field of a struct instance.

func NewField

func NewField(sf reflect.StructField, depth int) (field *Field, err error)

NewField creates a new field component.

type RegisterManager

type RegisterManager interface {
	// Add
	// adds a new instance to the register manager.
	//
	//   ref.Registry.Add("app/logics/user/login.Request", login.Request{})
	Add(name string, instance any) RegisterManager

	// Get
	// gets a struct instance by name.
	//
	//   obj, has := ref.Registry.Get("app/logics/user/login.Request")
	Get(name string) (instance any, has bool)
}

RegisterManager is a register manager that can register and get struct instances.

var Registry RegisterManager

Registry is a singleton instance of register manager.

Jump to

Keyboard shortcuts

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