attrs

package
v1.6.9 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2024 License: GPL-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HookFormFieldForType = "attrs.FormFieldForType"
	DefaultForType       = "attrs.DefaultForType"
)
View Source
const ATTR_TAG_NAME = "attrs"

Variables

This section is empty.

Functions

func FieldNames

func FieldNames(d any, exclude []string) []string

A shortcut for getting the names of all fields in a Definer.

The exclude parameter can be used to exclude certain fields from the result.

This function is useful when you need to get the names of all fields in a model, but you want to exclude certain fields (e.g. fields that are not editable).

func ForceSet

func ForceSet(d Definer, name string, value interface{}) error

ForceSet sets the value of a field on a Definer.

If the field is not found, the value is not of the correct type or another constraint is violated, this function will panic.

This function will allow setting the value of a field that is marked as not editable.

func Get

func Get[T any](d Definer, name string) T

Get retrieves the value of a field on a Definer.

If the field is not found, this function will panic.

Type assertions are used to ensure that the value is of the correct type, as well as providing less work for the caller.

func Method

func Method[T any](obj interface{}, name string) (n T, ok bool)

Method retrieves a method from an object.

The generic type parameter must be the type of the method.

func PrimaryKey added in v1.6.6

func PrimaryKey(d Definer) interface{}

PrimaryKey returns the primary key field of a Definer.

If the primary key field is not found, this function will panic.

func RegisterFormFieldType

func RegisterFormFieldType(valueOfType any, getField func(opts ...func(fields.Field)) fields.Field)

RegisterFormFieldType registers a field type for a given valueOfType.

getField is a function that returns a fields.Field for the given valueOfType.

The valueOfType can be a reflect.Type or any value, in which case the reflect.TypeOf(valueOfType) will be used.

This is a shortcut function for the `HookFormFieldForType` hook.

func Set

func Set(d Definer, name string, value interface{}) error

Set sets the value of a field on a Definer.

If the field is not found, the value is not of the correct type or another constraint is violated, this function will panic.

If the field is marked as non editable, this function will panic.

func SetMany

func SetMany(d Definer, values map[string]interface{}) error

SetMany sets multiple fields on a Definer.

The values parameter is a map where the keys are the names of the fields to set.

The values must be of the correct type for the fields.

func ToString

func ToString(v any) string

ToString converts a value to a string.

This should be the human-readable representation of the value.

Types

type DefaultGetter

type DefaultGetter func(f Field, new_field_t_indirected reflect.Type, field_v reflect.Value) (interface{}, bool)

type Definer

type Definer interface {
	// Retrieves the field definitions for the model.
	FieldDefs() Definitions
}

Definer is the interface that wraps the FieldDefs method.

FieldDefs retrieves the field definitions for the model.

func DefinerList added in v1.6.7

func DefinerList[T Definer](list []T) []Definer

DefinerList converts a slice of []T where the underlying type is of type Definer to []Definer.

type Definitions

type Definitions interface {
	// Set sets the value of the field with the given name (or panics if not found).
	Set(name string, value interface{}) error

	// Retrieves the value of the field with the given name (or panics if not found).
	Get(name string) interface{}

	// Retrieves the field with the given name.
	//
	// If the field is not found, the second return value will be false.
	Field(name string) (f Field, ok bool)

	// Set sets the value of the field with the given name (or panics if not found).
	//
	// This method will allow setting the value of a field that is marked as not editable.
	ForceSet(name string, value interface{}) error

	// Retrieves the primary field.
	Primary() Field

	// Instance returns the underlying model instance.
	Instance() Definer

	// Retrieves a slice of all fields.
	//
	// The order of the fields is the same as they were defined.
	Fields() []Field

	// Retrieves the number of fields.
	Len() int
}

Definitions is the interface that wraps the methods for a model's field definitions.

This is some sort of management- interface which allows for simpler and more uniform management of model fields.

func AutoDefinitions

func AutoDefinitions[T Definer](instance T, include ...any) Definitions

AutoDefinitions automatically generates definitions for a struct.

It does this by iterating over the fields of the struct and checking for the `attrs` tag. If the tag is present, it will parse the tag and generate the definition.

If the `include` parameter is provided, it will only generate definitions for the fields that are included.

type Field

type Field interface {
	Labeler
	Helper
	Stringer
	Namer

	// Retrieves the underlying model instance.
	Instance() Definer

	// Retrieves the related model instance for a foreign key field.
	ForeignKey() Definer

	// Retrieves the related model instance for a many-to-many field.
	ManyToMany() Definer

	// Retrieves the related model instance for a one-to-one field.
	OneToOne() Definer

	// Reports whether the field is the primary field.
	//
	// A model can technically have multiple primary fields, but this is not recommended.
	//
	// When for example, calling `Primary()` on the `Definitions` interface - only one will be returned.
	IsPrimary() bool

	// Reports whether the field is allowed to be null.
	//
	// If not, the field should panic when trying to set the value to nil / a reflect.Invalid value.
	AllowNull() bool

	// Reports whether the field is allowed to be blank.
	//
	// If not, the field should panic when trying to set the value to a blank value if the field is not of types:
	// bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, complex64, complex128.
	//
	// this means that for example, a string field should panic when trying to set the value to an empty string.
	AllowBlank() bool

	// Reports whether the field is allowed to be edited.
	//
	// If not, the field should panic when trying to set the value, unless the force parameter passed to the `SetValue` method is true.
	AllowEdit() bool

	// Retrieves the value of the field.
	GetValue() interface{}

	// Retrieves the default value of the field.
	//
	// Fields should also check the main model instance for methods like `GetDefault<FieldName>` to retrieve the default value.
	GetDefault() interface{}

	// Sets the value of the field.
	//
	// If the field is not allowed to be edited, this method should panic.
	// If the field is not allowed to be null, this method should panic when trying to set the value to nil / a reflect.Invalid value.
	// If the field is not allowed to be blank, this method should panic when trying to set the value to a blank value if the field is not of types:
	SetValue(v interface{}, force bool) error

	// Retrieves the form field for the field.
	//
	// This is used to generate forms for the field.
	FormField() fields.Field

	// Validates the field's value.
	Validate() error
}

type FieldConfig

type FieldConfig struct {
	Null          bool                                          // Whether the field allows null values
	Blank         bool                                          // Whether the field allows blank values
	ReadOnly      bool                                          // Whether the field is read-only
	Primary       bool                                          // Whether the field is a primary key
	Label         string                                        // The label for the field
	HelpText      string                                        // The help text for the field
	RelForeignKey Definer                                       // The related object for the field (foreign key)
	RelManyToMany Definer                                       // The related objects for the field (many to many, not implemented
	RelOneToOne   Definer                                       // The related object for the field (one to one, not implemented)
	Default       any                                           // The default value for the field (or a function that returns the default value)
	Validators    []func(interface{}) error                     // Validators for the field
	FormField     func(opts ...func(fields.Field)) fields.Field // The form field for the field
	WidgetAttrs   map[string]string                             // The attributes for the widget
	FormWidget    func(FieldConfig) widgets.Widget              // The form widget for the field
	Setter        func(Definer, interface{}) error              // A custom setter for the field
	Getter        func(Definer) (interface{}, bool)             // A custom getter for the field
}

FieldConfig is a configuration for a field.

This defines how a field should behave and how it should be displayed in a form.

type FieldDef

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

func NewField

func NewField[T any](instance *T, name string, conf *FieldConfig) *FieldDef

NewField creates a new field definition for the given instance.

This can then be used for managing the field in a more abstract way.

func (*FieldDef) AllowBlank

func (f *FieldDef) AllowBlank() bool

func (*FieldDef) AllowEdit

func (f *FieldDef) AllowEdit() bool

func (*FieldDef) AllowNull

func (f *FieldDef) AllowNull() bool

func (*FieldDef) ForeignKey added in v1.6.9

func (f *FieldDef) ForeignKey() Definer

func (*FieldDef) FormField

func (f *FieldDef) FormField() fields.Field

func (*FieldDef) GetDefault

func (f *FieldDef) GetDefault() interface{}

func (*FieldDef) GetValue

func (f *FieldDef) GetValue() interface{}

func (*FieldDef) HelpText

func (f *FieldDef) HelpText() string

func (*FieldDef) Instance

func (f *FieldDef) Instance() Definer

func (*FieldDef) IsPrimary

func (f *FieldDef) IsPrimary() bool

func (*FieldDef) Label

func (f *FieldDef) Label() string

func (*FieldDef) ManyToMany added in v1.6.9

func (f *FieldDef) ManyToMany() Definer

func (*FieldDef) Name

func (f *FieldDef) Name() string

func (*FieldDef) OneToOne added in v1.6.9

func (f *FieldDef) OneToOne() Definer

func (*FieldDef) Rel added in v1.6.7

func (f *FieldDef) Rel() Definer

func (*FieldDef) SetValue

func (f *FieldDef) SetValue(v interface{}, force bool) error

func (*FieldDef) ToString

func (f *FieldDef) ToString() string

func (*FieldDef) Validate

func (f *FieldDef) Validate() error

type FormFieldGetter

type FormFieldGetter func(f Field, new_field_t_indirected reflect.Type, field_v reflect.Value, opts ...func(fields.Field)) (fields.Field, bool)

type Helper

type Helper interface {
	// HelpText returns a description of the field.
	//
	// This is displayed to the user in for example, forms.
	HelpText() string
}

type Labeler

type Labeler interface {
	// Label returns the human-readable name of the field.
	//
	// This is the name that is displayed to the user in for example, forms and column headers.
	Label() string
}

type Namer

type Namer interface {
	// Retrieves the name of the field.
	//
	// This is the name that is used to identify the field in the definitions.
	//
	// It is also the name that is used mainly in forms.
	Name() string
}

type ObjectDefinitions

type ObjectDefinitions struct {
	Object       Definer
	PrimaryField string
	ObjectFields *orderedmap.OrderedMap[string, Field]
}

func Define

func Define(d Definer, fieldDefinitions ...Field) *ObjectDefinitions

Define creates a new object definitions.

This can then be returned by the FieldDefs method of a model to make it comply with the Definer interface.

func (*ObjectDefinitions) Field

func (d *ObjectDefinitions) Field(name string) (f Field, ok bool)

func (*ObjectDefinitions) Fields

func (d *ObjectDefinitions) Fields() []Field

func (*ObjectDefinitions) ForceSet

func (d *ObjectDefinitions) ForceSet(name string, value interface{}) error

func (*ObjectDefinitions) Get

func (d *ObjectDefinitions) Get(name string) interface{}

func (*ObjectDefinitions) Instance added in v1.6.9

func (d *ObjectDefinitions) Instance() Definer

func (*ObjectDefinitions) Len

func (d *ObjectDefinitions) Len() int

func (*ObjectDefinitions) Primary

func (d *ObjectDefinitions) Primary() Field

func (*ObjectDefinitions) Set

func (d *ObjectDefinitions) Set(name string, value interface{}) error

type Scanner

type Scanner interface {
	// ScanAttribute scans the value of the attribute.
	//
	// This is used to set the value of the field from a raw value.
	ScanAttribute(src any) error
}

type Stringer

type Stringer interface {
	// ToString returns a string representation of the value.
	//
	// This should be the human-readable version of the value, for example for a list display.
	ToString() string
}

Jump to

Keyboard shortcuts

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