Documentation
¶
Index ¶
- Constants
- func FieldNames(d any, exclude []string) []string
- func ForceSet(d Definer, name string, value interface{}) error
- func Get[T any](d Definer, name string) T
- func Method[T any](obj interface{}, name string) (n T, ok bool)
- func PrimaryKey(d Definer) interface{}
- func RegisterFormFieldType(valueOfType any, getField func(opts ...func(fields.Field)) fields.Field)
- func Set(d Definer, name string, value interface{}) error
- func SetMany(d Definer, values map[string]interface{}) error
- func ToString(v any) string
- type DefaultGetter
- type Definer
- type Definitions
- type Field
- type FieldConfig
- type FieldDef
- func (f *FieldDef) AllowBlank() bool
- func (f *FieldDef) AllowEdit() bool
- func (f *FieldDef) AllowNull() bool
- func (f *FieldDef) ForeignKey() Definer
- func (f *FieldDef) FormField() fields.Field
- func (f *FieldDef) GetDefault() interface{}
- func (f *FieldDef) GetValue() interface{}
- func (f *FieldDef) HelpText() string
- func (f *FieldDef) Instance() Definer
- func (f *FieldDef) IsPrimary() bool
- func (f *FieldDef) Label() string
- func (f *FieldDef) ManyToMany() Definer
- func (f *FieldDef) Name() string
- func (f *FieldDef) OneToOne() Definer
- func (f *FieldDef) Rel() Definer
- func (f *FieldDef) SetValue(v interface{}, force bool) error
- func (f *FieldDef) ToString() string
- func (f *FieldDef) Validate() error
- type FormFieldGetter
- type Helper
- type Labeler
- type Namer
- type ObjectDefinitions
- func (d *ObjectDefinitions) Field(name string) (f Field, ok bool)
- func (d *ObjectDefinitions) Fields() []Field
- func (d *ObjectDefinitions) ForceSet(name string, value interface{}) error
- func (d *ObjectDefinitions) Get(name string) interface{}
- func (d *ObjectDefinitions) Instance() Definer
- func (d *ObjectDefinitions) Len() int
- func (d *ObjectDefinitions) Primary() Field
- func (d *ObjectDefinitions) Set(name string, value interface{}) error
- type Scanner
- type Stringer
Constants ¶
const ( HookFormFieldForType = "attrs.FormFieldForType" DefaultForType = "attrs.DefaultForType" )
const ATTR_TAG_NAME = "attrs"
Variables ¶
This section is empty.
Functions ¶
func FieldNames ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.
Types ¶
type DefaultGetter ¶
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
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 (*FieldDef) ForeignKey ¶ added in v1.6.9
func (*FieldDef) GetDefault ¶
func (f *FieldDef) GetDefault() interface{}
func (*FieldDef) ManyToMany ¶ added in v1.6.9
type FormFieldGetter ¶
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) 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