types

package
v1.3.3 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package types provides type definitions and interfaces for form handling and validation.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRequired           = &formError{Rule: "Required", Message: "This field is required"}
	ErrInvalidEmail       = &formError{Rule: "InvalidEmail", Message: "This field must be a valid email address"}
	ErrInvalidURL         = &formError{Rule: "InvalidURL", Message: "This field must be a valid URL"}
	ErrInvalidIP          = &formError{Rule: "InvalidIP", Message: "This field must be a valid IP address"}
	ErrInvalidPort        = &formError{Rule: "InvalidPort", Message: "This field must be a valid Port number"}
	ErrInvalidMin         = &formError{Rule: "InvalidMin", Message: "This field must be a minimum of %d"}
	ErrInvalidMax         = &formError{Rule: "InvalidMax", Message: "This field must be a maximum of %d"}
	ErrInvalidMinLen      = &formError{Rule: "InvalidMinLen", Message: "This field must be a minimum length of %d"}
	ErrInvalidMaxLen      = &formError{Rule: "InvalidMaxLen", Message: "This field must be a maximum length of %d"}
	ErrInvalidRegexp      = &formError{Rule: "InvalidRegexp", Message: "This field must match the regular expression %s"}
	ErrInvalidPattern     = &formError{Rule: "InvalidPattern", Message: "This field must match the pattern %s"}
	ErrInvalidCustom      = &formError{Rule: "InvalidCustom", Message: "This field must match the custom rule"}
	ErrInvalidCustomCheck = &formError{Rule: "InvalidCustomCheck", Message: "This field must match the custom check"}
)

Functions

func IsEqual

func IsEqual(a, b string) bool

func SanitizeQuotesAndSpaces

func SanitizeQuotesAndSpaces(input string) string

Types

type Config

type Config struct {
	Title  string
	Fields FormFields
}

func (Config) GetFields

func (c Config) GetFields() FormFields

func (Config) GetTitle

func (c Config) GetTitle() string

type CustomField

type CustomField struct {
	Lbl  string
	DVal string
	Grp  string
}

func (CustomField) DefaultValue

func (f CustomField) DefaultValue() string

func (CustomField) Group

func (f CustomField) Group() string

func (CustomField) Label

func (f CustomField) Label() string

type CustomizableField

type CustomizableField interface {
	FormInput[FormInputObject[any]]
	Label() string
	DefaultValue() string
	Group() string
}

type DataExporter

type DataExporter interface {
	ExportToCSV(filename string) error
	ExportToYAML(filename string) error
	ExportToJSON(filename string) error
	ExportToXML(filename string) error
	ExportToExcel(filename string) error
	ExportToPDF(filename string) error
	ExportToMarkdown(filename string) error
}

type FieldAlignment

type FieldAlignment string
const (
	AlignmentDefault FieldAlignment = "default"
	AlignmentLeft    FieldAlignment = "left"
	AlignmentCenter  FieldAlignment = "center"
	AlignmentRight   FieldAlignment = "right"
)

func (FieldAlignment) Description

func (f FieldAlignment) Description() string

func (FieldAlignment) String

func (f FieldAlignment) String() string

type FieldDefinition

type FieldDefinition interface {
	Description() string
	String() string
}

type FieldPosition

type FieldPosition string
const (
	PositionDefault FieldPosition = "default"
	PositionTop     FieldPosition = "top"
	PositionBottom  FieldPosition = "bottom"
)

func (FieldPosition) Description

func (f FieldPosition) Description() string

func (FieldPosition) String

func (f FieldPosition) String() string

type FieldRule

type FieldRule interface {
	Validate(value string) error
}

type FieldSize

type FieldSize string
const (
	SizeDefault FieldSize = "default"
	SizeSmall   FieldSize = "small"
	SizeLarge   FieldSize = "large"
)

func (FieldSize) Description

func (f FieldSize) Description() string

func (FieldSize) String

func (f FieldSize) String() string

type FieldType

type FieldType string
const (
	FieldBool     FieldType = "bool"
	FieldInt      FieldType = "int"
	FieldText     FieldType = "text"
	FieldPass     FieldType = "password"
	FieldDate     FieldType = "date"
	FieldTime     FieldType = "time"
	FieldList     FieldType = "list"
	FieldFile     FieldType = "file"
	FieldTable    FieldType = "table"
	FieldFunction FieldType = "function"
)

func (FieldType) Description

func (f FieldType) Description() string

func (FieldType) String

func (f FieldType) String() string

type FormConfig

type FormConfig struct {
	Title string
	FormFields
}

func NewFormConfig

func NewFormConfig(title string, fields []FormInputObject[any]) FormConfig

func (*FormConfig) AddField

func (f *FormConfig) AddField(field FormInputObject[any])

func (*FormConfig) GetField

func (f *FormConfig) GetField(name string) FormInputObject[any]

func (*FormConfig) GetFieldValue

func (f *FormConfig) GetFieldValue(name string) any

func (*FormConfig) GetFields

func (f *FormConfig) GetFields() []FormInputObject[any]

func (*FormConfig) GetTitle

func (f *FormConfig) GetTitle() string

func (*FormConfig) RemoveField

func (f *FormConfig) RemoveField(name string) error

func (*FormConfig) SetField

func (f *FormConfig) SetField(name string, field FormInputObject[any]) error

func (*FormConfig) SetFieldValue

func (f *FormConfig) SetFieldValue(name string, value any) error

func (*FormConfig) SetTitle

func (f *FormConfig) SetTitle(title string)

type FormError

type FormError interface {
	Error() string
	ErrorOrNil() error
	FieldError() map[string]string
	FieldsError() map[string]string
}

type FormFields

type FormFields struct {
	Title  string
	Fields []FormInputObject[any]
}

func (FormFields) InputType

func (f FormFields) InputType() string

func (FormFields) Inputs

func (f FormFields) Inputs() []FormInputObject[any]

type FormGroup

type FormGroup interface {
	GetFields() FormFields

	GetFieldByID(id string) FieldDefinition
	GetFieldByIndex(index int) FieldDefinition
	GetFieldIndex(id string) int
	GetFieldID(index int) string

	FieldsCount() int

	Validate() error

	SetField(index int, field FieldDefinition)
	SetFieldByID(id string, field FieldDefinition)
	SetFields(fields FormFields)
}

FormGroup is an interface that manages goups of fields. It was created to provide a easy way to manage fields in a form, wrapping them in groups and providing methods to manipulate them.

type FormInput

type FormInput[T any] interface {
	FieldDefinition
	FormInputObject[T]

	Placeholder() string
	MinValue() int
	MaxValue() int
	Validation() func(string, func(interface{}) error) error
	IsRequired() bool
	Error() string
	SetPlaceholder(string)
	SetRequired(bool)
	SetMinValue(int)
	SetMaxValue(int)
	SetValidation(func(string, func(interface{}) error) error)
	SetValidationRules([]ValidationRule)
	ValidationRules() []ValidationRule
	Validate() error
	String() string
	FromString(string) error
	ToMap() map[string]interface{}
	FromMap(map[string]interface{}) error
}

func NewFormInput

func NewFormInput[T FormInputObject[any]](t T) FormInput[T]

func NewFormInputFromBytes

func NewFormInputFromBytes[T FormInputObject[any]](b []byte) FormInput[T]

func NewFormInputFromMap

func NewFormInputFromMap[T FormInputObject[any]](m map[string]interface{}) FormInput[T]

func NewFormInputFromString

func NewFormInputFromString[T FormInputObject[any]](str string) FormInput[T]

type FormInputObject

type FormInputObject[T any] interface {
	GetName() string
	GetType() reflect.Type
	GetValue() T
	SetValue(val T) error
}

FormInputObject is the most basic form input object. It will be used globally in all form input objects.

func NewFormInputList

func NewFormInputList[T any](t []T) []FormInputObject[T]

func NewFormInputListFromBytes

func NewFormInputListFromBytes[T any](b [][]byte) []FormInputObject[T]

func NewFormInputListFromInterface

func NewFormInputListFromInterface[T any](i []interface{}) []FormInputObject[T]

func NewFormInputListFromMap

func NewFormInputListFromMap[T any](m []map[string]interface{}) []FormInputObject[T]

func NewFormInputListFromString

func NewFormInputListFromString[T any](str []string) []FormInputObject[T]

func NewFormInputObject

func NewFormInputObject[T any](t T) FormInputObject[T]

func NewFormInputObjectFromBytes

func NewFormInputObjectFromBytes[T any](b []byte) FormInputObject[T]

func NewFormInputObjectFromMap

func NewFormInputObjectFromMap[T any](m map[string]interface{}) FormInputObject[T]

func NewFormInputObjectFromString

func NewFormInputObjectFromString[T any](str string) FormInputObject[T]

type FormInputValidationCustom

type FormInputValidationCustom func(value string) error

type FormInputValidationObject

type FormInputValidationObject[T any] func(value T) error

type FormInputValidationRule

type FormInputValidationRule[T any] struct {
	Min, Max  int
	ObjFunc   FormInputValidationObject[T]
	Challenge FormInputValidationCustom
	StrFunc   FormInputValidationString
}

type FormInputValidationString

type FormInputValidationString func(value string) error

type FormPart

type FormPart struct {
	*lipgloss.Style
	FormGroup
	Width, Height, MaxWidth, MaxHeight int
	Title                              string
}

FormPart is an interface that agroups fields and provides methods to manipulate them. It is used to group fields, manage screen space and fields distribution on the screen.

func (FormPart) GetLeftBound

func (s FormPart) GetLeftBound() int

func (FormPart) GetLowerBound

func (s FormPart) GetLowerBound() int

func (FormPart) GetRightBound

func (s FormPart) GetRightBound() int

func (FormPart) GetUpperBound

func (s FormPart) GetUpperBound() int

func (FormPart) GetWidth

func (s FormPart) GetWidth() int

type IMapper

type IMapper[T any] interface {
	// SerializeToFile serializes an object of type T to a file in the specified format.
	SerializeToFile(format string)
	// DeserializeFromFile deserializes an object of type T from a file in the specified format.
	DeserializeFromFile(format string) (T, error)
	// Serialize converts an object of type T to a byte array in the specified format.
	Serialize(format string) ([]byte, error)
	// Deserialize converts a byte array in the specified format to an object of type T.
	Deserialize(data []byte, format string) (T, error)
}

IMapper is a generic interface for serializing and deserializing objects of type T.

func NewMapper

func NewMapper[T any](object *T, filePath string) IMapper[T]

NewMapper creates a new instance of Mapper.

type Input

type Input[T any] struct {
	FieldDefinition
	FormInputObject[T]
	Name               string           `json:"name" yaml:"name" gorm:"column:name"`
	Desc               string           `json:"description" yaml:"description" gorm:"column:description"`
	Ph                 string           `json:"placeholder" yaml:"placeholder" gorm:"column:placeholder"`
	Tp                 reflect.Type     `json:"type" yaml:"type" gorm:"column:type"`
	Val                *T               `json:"value" yaml:"value" gorm:"column:value"`
	Req                bool             `json:"required" yaml:"required" gorm:"column:required"`
	Min                int              `json:"min" yaml:"min" gorm:"column:min"`
	Max                int              `json:"max" yaml:"max" gorm:"column:max"`
	Err                string           `json:"error" yaml:"error" gorm:"column:error"`
	ValidationRulesVal []ValidationRule `json:"validation_rules" yaml:"validation_rules" gorm:"column:validation_rules"`
}

func NewInput

func NewInput[T FormInputObject[any]](t T) *Input[T]

func (*Input[T]) Description

func (s *Input[T]) Description() string

func (*Input[T]) Error

func (s *Input[T]) Error() string

func (*Input[T]) FromMap

func (s *Input[T]) FromMap(m map[string]interface{}) error

func (*Input[T]) FromString

func (s *Input[T]) FromString(str string) error

func (*Input[T]) GetError

func (s *Input[T]) GetError() string

func (*Input[T]) GetName

func (s *Input[T]) GetName() string

func (*Input[T]) GetType

func (s *Input[T]) GetType() reflect.Type

func (*Input[T]) GetValue

func (s *Input[T]) GetValue() T

func (*Input[T]) IsRequired

func (s *Input[T]) IsRequired() bool

func (*Input[T]) MaxValue

func (s *Input[T]) MaxValue() int

func (*Input[T]) MinValue

func (s *Input[T]) MinValue() int

func (*Input[T]) Placeholder

func (s *Input[T]) Placeholder() string

func (*Input[T]) SetMaxValue

func (s *Input[T]) SetMaxValue(i int)

func (*Input[T]) SetMinValue

func (s *Input[T]) SetMinValue(min int)

func (*Input[T]) SetPlaceholder

func (s *Input[T]) SetPlaceholder(ph string)

func (*Input[T]) SetRequired

func (s *Input[T]) SetRequired(req bool)

func (*Input[T]) SetValidation

func (s *Input[T]) SetValidation(validation func(string, func(interface{}) error) error)

func (*Input[T]) SetValidationRules

func (s *Input[T]) SetValidationRules(rules []ValidationRule)

func (*Input[T]) SetValue

func (s *Input[T]) SetValue(val T) error

func (*Input[T]) String

func (s *Input[T]) String() string

func (*Input[T]) ToMap

func (s *Input[T]) ToMap() map[string]interface{}

func (*Input[T]) Validate

func (s *Input[T]) Validate() error

func (*Input[T]) Validation

func (s *Input[T]) Validation() func(string, func(interface{}) error) error

func (*Input[T]) ValidationRules

func (s *Input[T]) ValidationRules() []ValidationRule

type InputObject

type InputObject[T any] struct {
	Val  T      `json:"value" yaml:"value" gorm:"column:value"`
	Name string `json:"name" yaml:"name" gorm:"column:name"`
	// contains filtered or unexported fields
}

InputObject is a struct that implements the FormInputObject interface. It is used to store the value of the form input object in a serializable. It is used to store the value of the form input object in a serializable and to store metadata for easy and integrated serialization and type conversion.

func NewInputObject

func NewInputObject[T any](t T) *InputObject[T]

func (*InputObject[T]) GetName

func (s *InputObject[T]) GetName() string

func (*InputObject[T]) GetType

func (s *InputObject[T]) GetType() reflect.Type

func (*InputObject[T]) GetValue

func (s *InputObject[T]) GetValue() T

func (*InputObject[T]) SetValue

func (s *InputObject[T]) SetValue(val T) error

type Loader

type Loader interface {
	AddMessage(LoaderMessage)
	AddMessages([]LoaderMessage)
	ClearMessages()
	GetMessages() []LoaderMessage
	GetMessagesCount() int
	GetLastMessage() LoaderMessage
	GetFirstMessage() LoaderMessage
}

type LoaderMessage

type LoaderMessage struct {
	Message    string
	DelayAfter int
	Icon       string
	Color      string

	Progress        bool
	ProgressProcess interface{}
	ProgressTotal   interface{}
	ProgressCurrent interface{}
	ProgressMessage string
	ProgressIcon    string
}

type LoaderOrchestrator

type LoaderOrchestrator struct {
	LoaderMessages []LoaderMessage
}

func NewLoaderOrchestrator

func NewLoaderOrchestrator() *LoaderOrchestrator

func (*LoaderOrchestrator) AddMessage

func (l *LoaderOrchestrator) AddMessage(m LoaderMessage)

func (*LoaderOrchestrator) AddMessages

func (l *LoaderOrchestrator) AddMessages(m []LoaderMessage)

func (*LoaderOrchestrator) ClearMessages

func (l *LoaderOrchestrator) ClearMessages()

func (*LoaderOrchestrator) GetFirstMessage

func (l *LoaderOrchestrator) GetFirstMessage() LoaderMessage

func (*LoaderOrchestrator) GetLastMessage

func (l *LoaderOrchestrator) GetLastMessage() LoaderMessage

func (*LoaderOrchestrator) GetMessages

func (l *LoaderOrchestrator) GetMessages() []LoaderMessage

func (*LoaderOrchestrator) GetMessagesCount

func (l *LoaderOrchestrator) GetMessagesCount() int

type Mapper

type Mapper[T any] struct {
	// contains filtered or unexported fields
}

Mapper is a generic struct that implements the IMapper interface for serializing and deserializing objects.

func NewMapperPtr

func NewMapperPtr[T any](object *T, filePath string) *Mapper[*T]

NewMapperPtr creates a new instance of Mapper.

func NewMapperType

func NewMapperType[T any](object *T, filePath string) *Mapper[T]

NewMapperType creates a new instance of Mapper.

func NewMapperTypeWithObject

func NewMapperTypeWithObject[T any](object *T, filePath string) *Mapper[T]

NewMapperTypeWithObject creates a new instance of Mapper.

func (*Mapper[T]) Deserialize

func (m *Mapper[T]) Deserialize(data []byte, format string) (T, error)

Deserialize converts a byte array in the specified format to an object of type T.

func (*Mapper[T]) DeserializeFromFile

func (m *Mapper[T]) DeserializeFromFile(format string) (T, error)

DeserializeFromFile deserializes an object of type T from a file in the specified format.

func (*Mapper[T]) Serialize

func (m *Mapper[T]) Serialize(format string) ([]byte, error)

Serialize converts an object of type T to a byte array in the specified format.

func (*Mapper[T]) SerializeToFile

func (m *Mapper[T]) SerializeToFile(format string)

SerializeToFile serializes an object of type T to a file in the specified format.

type MultiTableHandler

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

func (*MultiTableHandler) GetHeaders

func (h *MultiTableHandler) GetHeaders() []string

func (*MultiTableHandler) GetRows

func (h *MultiTableHandler) GetRows() [][]string

func (*MultiTableHandler) NextTable

func (h *MultiTableHandler) NextTable()

func (*MultiTableHandler) PreviousTable

func (h *MultiTableHandler) PreviousTable()

type MultiTableManager

type MultiTableManager struct {
	Handlers []TableDataHandler
	Current  int
}

func (*MultiTableManager) GetCurrentHandler

func (m *MultiTableManager) GetCurrentHandler() TableDataHandler

func (*MultiTableManager) Next

func (m *MultiTableManager) Next()

func (*MultiTableManager) Previous

func (m *MultiTableManager) Previous()

type Shortcut

type Shortcut string
const (
	Q Shortcut = "q"
	F Shortcut = "f"

	UP    Shortcut = "up"
	DOWN  Shortcut = "down"
	LEFT  Shortcut = "left"
	RIGHT Shortcut = "right"

	DEL   Shortcut = "del"
	TAB   Shortcut = "tab"
	ESC   Shortcut = "esc"
	ENTER Shortcut = "enter"

	CTRLA Shortcut = "ctrl+a"
	CTRLE Shortcut = "ctrl+e"
	CTRLR Shortcut = "ctrl+r"
	CTRLC Shortcut = "ctrl+c"
	CTRLH Shortcut = "ctrl+h"

	SHIFTTAB Shortcut = "shift+tab"
)

func (Shortcut) Description

func (s Shortcut) Description() string

func (Shortcut) String

func (s Shortcut) String() string

type TableDataHandler

type TableDataHandler interface {
	GetHeaders() []string
	GetRows() [][]string
	GetArrayMap() map[string][]string
	GetHashMap() map[string]string
	GetObjectMap() []map[string]string
	GetByteMap() map[string][]byte
}

func NewTableHandler

func NewTableHandler(headers []string, rows [][]string) TableDataHandler

func NewTableHandlerFromMap

func NewTableHandlerFromMap(data map[string][]string) TableDataHandler

func NewTableHandlerFromRows

func NewTableHandlerFromRows(headers []string, rows [][]string) TableDataHandler

type TableHandler

type TableHandler struct {
	TableDataHandler
	Headers []string
	Rows    [][]string
}

func (*TableHandler) GetArrayMap

func (h *TableHandler) GetArrayMap() map[string][]string

func (*TableHandler) GetByteMap

func (h *TableHandler) GetByteMap() map[string][]byte

func (*TableHandler) GetHashMap

func (h *TableHandler) GetHashMap() map[string]string

func (*TableHandler) GetHeaders

func (h *TableHandler) GetHeaders() []string

func (*TableHandler) GetObjectMap

func (h *TableHandler) GetObjectMap() []map[string]string

func (*TableHandler) GetRows

func (h *TableHandler) GetRows() [][]string

type TableHandlerWithContext

type TableHandlerWithContext struct {
	TableHandler
	Context string
}

type ValidationRule

type ValidationRule string
const (
	Required ValidationRule = "required"
	Email    ValidationRule = "email"
	URL      ValidationRule = "url"
	IP       ValidationRule = "ip"
	Port     ValidationRule = "port"
	Min      ValidationRule = "min"
	Max      ValidationRule = "max"
	MinLen   ValidationRule = "min_len"
	MaxLen   ValidationRule = "max_len"
	Regexp   ValidationRule = "regexp"
	Pattern  ValidationRule = "pattern"
)

func (ValidationRule) Description

func (v ValidationRule) Description() string

func (ValidationRule) String

func (v ValidationRule) String() string

func (ValidationRule) Validate

func (v ValidationRule) Validate(value string, customCheck func(interface{}) error) error

Jump to

Keyboard shortcuts

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