Documentation
¶
Overview ¶
Package form is a simplified implementation of JSON-Forms, which renders HTML forms from a JSON configuration.
Index ¶
- Constants
- func Editor(schema schema.Schema, element Element, value any, ...) (string, error)
- func LookupCodeSchema() schema.Element
- func SortLookupCodeByGroupThenLabel(a LookupCode, b LookupCode) int
- func SortLookupCodeByLabel(a LookupCode, b LookupCode) int
- func Use(name string, widget Widget)
- func Viewer(schema schema.Schema, element Element, value any, ...) (string, error)
- type Element
- func (element *Element) AllElements() []*Element
- func (element *Element) Edit(form *Form, lookupProvider LookupProvider, value any, b *html.Builder) error
- func (element *Element) Encoding() string
- func (element *Element) GetSchema(s *schema.Schema) schema.Element
- func (element *Element) GetSliceOfString(value any, s *schema.Schema) []string
- func (element *Element) GetString(value any, s *schema.Schema) string
- func (element Element) IsEmpty() bool
- func (element *Element) UnmarshalMap(data map[string]any) error
- func (element *Element) View(form *Form, lookupProvider LookupProvider, value any, b *html.Builder) error
- func (element *Element) Widget() (Widget, error)
- type Form
- func (form *Form) BuildEditor(value any, lookupProvider LookupProvider, builder *html.Builder) error
- func (form *Form) BuildViewer(value any, lookupProvider LookupProvider, builder *html.Builder) error
- func (form *Form) Editor(value any, lookupProvider LookupProvider) (string, error)
- func (form *Form) Encoding() string
- func (form *Form) OptionInt(name string) int
- func (form *Form) OptionString(name string) string
- func (form *Form) SetURLValues(object any, values url.Values, lookupProvider LookupProvider) error
- func (form *Form) Viewer(value any, lookupProvider LookupProvider) (string, error)
- type LookupCode
- type LookupCodeMaker
- type LookupGroup
- type LookupProvider
- type ReadOnlyLookupGroup
- type URLValueSetter
- type UnmarshalMaper
- type Widget
- type WritableLookupGroup
Constants ¶
const NewItemIdentifier = "::NEWVALUE::"
NewItemIdentifier is a prefix appended to select options when the User wants to add a new item into a WritableLookupGroup
Variables ¶
This section is empty.
Functions ¶
func Editor ¶ added in v0.10.0
func Editor(schema schema.Schema, element Element, value any, lookupProvider LookupProvider, options ...string) (string, error)
Editor creates an in-place form and executes its "Editor" method
func LookupCodeSchema ¶ added in v0.22.9
LookupCodeSchema defines the validation schema for LookupCodes
func SortLookupCodeByGroupThenLabel ¶ added in v0.15.0
func SortLookupCodeByGroupThenLabel(a LookupCode, b LookupCode) int
SortLookupCodeByGroupThenLabel is a sort function that works with the sort.Slice function.
func SortLookupCodeByLabel ¶ added in v0.15.0
func SortLookupCodeByLabel(a LookupCode, b LookupCode) int
SortLookupCodeByLabel is a sort function that works with the sort.Slice function.
Types ¶
type Element ¶ added in v0.9.1
type Element struct {
Type string `json:"type"` // The kind of form element
ID string `json:"id"` // The ID of the element (needed by some widgets)
Path string `json:"path"` // Path to the data value displayed in for this form element
Label string `json:"label,omitempty"` // Short label to be displayed on the form element
Description string `json:"description,omitempty"` // Longer description text to be displayed on the form element
Options mapof.Any `json:"options,omitempty"` // Additional custom properties defined by individual widgets
Children []Element `json:"children,omitempty"` // Array of sub-form elements that may be displayed depending on the kind.
ReadOnly bool `json:"readOnly,omitempty"` // If true, then this element is read-only
}
Element defines a single form element, or a nested form layout. It can be serialized to and from a database.
func MustParse ¶ added in v0.4.0
MustParse guarantees that a value has been parsed into a Form, or else it panics the application.
func NewElement ¶ added in v0.9.1
func NewElement() Element
NewElement returns a fully populated Element object.
func Parse ¶ added in v0.3.9
Parse attempts to convert a value into a Form. Currently supports map[string]any, []byte, string, and UnmarshalMaper interface.
func (*Element) AllElements ¶ added in v0.9.1
AllElements returns pointers to all of the valid paths in this form
func (*Element) Edit ¶ added in v0.10.0
func (element *Element) Edit(form *Form, lookupProvider LookupProvider, value any, b *html.Builder) error
Edit returns an HTML form that can edit this element.
func (*Element) Encoding ¶ added in v0.20.0
Encoding returns the form encoding required by this widget.
func (*Element) GetSchema ¶ added in v0.20.0
GetSchema finds and returns the schema.Element associated with this Element path
func (*Element) GetSliceOfString ¶ added in v0.9.1
GetSliceOfString rturns a slice of strings for a provided path.
func (*Element) GetString ¶ added in v0.9.1
GetString returns the value of the element at the provided path. If the schema is present, then it is used to resolve the value. If the schema is not present, then the value is returned using path lookup instead.
func (*Element) UnmarshalMap ¶ added in v0.9.1
UnmarshalMap parses data from a generic structure (mapof.Any) into a Form record.
type Form ¶
Form defines all of the data for this JSON form. This can be marshalled/unmarshalled as JSON, or entered directly into Go source code.
func (*Form) BuildEditor ¶ added in v0.10.0
func (form *Form) BuildEditor(value any, lookupProvider LookupProvider, builder *html.Builder) error
BuildEditor generates an editable view of this form
func (*Form) BuildViewer ¶ added in v0.10.0
func (form *Form) BuildViewer(value any, lookupProvider LookupProvider, builder *html.Builder) error
BuildViewer generates a read-only view of this form
func (*Form) Editor ¶ added in v0.10.0
func (form *Form) Editor(value any, lookupProvider LookupProvider) (string, error)
Editor returns a fully populated HTML form for this Form
func (*Form) Encoding ¶ added in v0.20.0
Encoding returns the "enctype" attribute for the form. Default is ""
func (*Form) OptionString ¶ added in v0.24.0
OptionString returns the string value of a Form option.
func (*Form) SetURLValues ¶ added in v0.22.12
SetURLValues applies all of the data from the value map into the target object
type LookupCode ¶ added in v0.9.1
type LookupCode struct {
Value string `json:"value,omitempty" form:"value" bson:"value,omitempty"` // Internal value of the LookupCode
Label string `json:"label,omitempty" form:"label" bson:"label,omitempty"` // Human-friendly label/name of the LookupCode
Description string `json:"description,omitempty" form:"description" bson:"description,omitempty"` // Optional long description of the LookupCode
Icon string `json:"icon,omitempty" form:"icon" bson:"icon,omitempty"` // Optional icon to use when displaying the LookupCode
Group string `json:"group,omitempty" form:"group" bson:"group,omitempty"` // Optiional grouping to use when displaying the LookupCode
Href string `json:"href,omitempty" form:"href" bson:"href,omitempty"` // Optional URL to use when using this LookupCode
}
LookupCode represents a single value/label pair to be used in place of Enums for optional lists.
func AsLookupCode ¶ added in v0.16.0
func AsLookupCode[T LookupCodeMaker](maker T) LookupCode
AsLookupCode is a helper function that converts any object that implements the LookupCodeMaker interface into a form.LookupCode
func GetLookupCodes ¶ added in v0.9.1
func GetLookupCodes(element *Element, schemaElement schema.Element, lookupProvider LookupProvider) ([]LookupCode, bool)
GetLookupCodes returns a list of LookupCodes derived from: 1) an "enum" (string or slice-of-lookupCode) in the form element, 2) a "datasource" value that is looked up in the lookupProvider 3) a value enumerated in the schema
The boolean value is TRUE if this comes from a WritableLookupGroup
func NewLookupCode ¶ added in v0.9.1
func NewLookupCode(value string) LookupCode
NewLookupCode creates a new LookupCode from a string
func ParseLookupCode ¶ added in v0.15.2
func ParseLookupCode(value any) LookupCode
ParseLookupCode converts a number of values into a LookupCode, including: LookupCode, , mapof.Any, mapof.String, map[string]any, map[string]string, and string. For strings, the string value is used for both the .Value and .Label If no compatible type is found, then an empty LookupCode is returned.
func (*LookupCode) GetPointer ¶ added in v0.15.5
func (lookupCode *LookupCode) GetPointer(name string) (any, bool)
GetPointer returns pointers to the named property of this LookupCode
func (LookupCode) GetStringOK ¶ added in v0.25.2
func (lookupCode LookupCode) GetStringOK(name string) (string, bool)
GetStringOK returns the string value of each property of this LookupCode
func (LookupCode) ID ¶ added in v0.12.0
func (lookupCode LookupCode) ID() string
ID returns the unique ID of the LookupCode, allowing them to be used as a set.Value
type LookupCodeMaker ¶ added in v0.16.0
type LookupCodeMaker interface {
// LookupCode returns the data from current object in the form of a form.LookupCode
LookupCode() LookupCode
}
LookupCodeMaker is an interface that wraps the LookupCode method
type LookupGroup ¶ added in v0.13.1
type LookupGroup interface {
Get() []LookupCode
}
LookupGroup is an read-only interface that returns a list of LookupCodes
type LookupProvider ¶ added in v0.9.1
type LookupProvider interface {
Group(name string) LookupGroup
}
LookupProvider is an external object that can inject LookupCodes based on their URL.
type ReadOnlyLookupGroup ¶ added in v0.13.1
type ReadOnlyLookupGroup []LookupCode
ReadOnlyLookupGroup is a simple implementation of the LookupGroup interface that returns a static list of LookupCodes.
func NewReadOnlyLookupGroup ¶ added in v0.13.1
func NewReadOnlyLookupGroup(codes ...LookupCode) ReadOnlyLookupGroup
NewReadOnlyLookupGroup returns a fully initialized ReadOnlyLookupGroup
func (ReadOnlyLookupGroup) Get ¶ added in v0.13.1
func (group ReadOnlyLookupGroup) Get() []LookupCode
Get returns the list of LookupCodes
func (ReadOnlyLookupGroup) Value ¶ added in v0.22.8
func (group ReadOnlyLookupGroup) Value(value string) LookupCode
Value returns the LookupCode that matches the provided value, and an empty LookupCode if no match is found.
type URLValueSetter ¶ added in v0.25.2
type URLValueSetter interface {
// SetURLValue applies applies all values from a url.Values slice to the provided object.
SetURLValue(form *Form, element *Element, object any, values url.Values) error
}
URLValueSetter interface wraps the SetURLValue method, which applies the values from a url.Values slice into an arbitrary object.
type UnmarshalMaper ¶ added in v0.6.4
type UnmarshalMaper interface {
// UnmarshalMap returns a value in the format map[string]interface
UnmarshalMap() map[string]any
}
UnmarshalMaper wraps the UnmarshalMap interface
type Widget ¶ added in v0.10.0
type Widget interface {
View(form *Form, element *Element, lookupProvider LookupProvider, value any, builder *html.Builder) error
Edit(form *Form, element *Element, lookupProvider LookupProvider, value any, builder *html.Builder) error
ShowLabels() bool
Encoding(element *Element) string
}
Widget defines a data type that can be included in a form
type WritableLookupGroup ¶ added in v0.13.1
type WritableLookupGroup interface {
LookupGroup
Add(name string) (string, error)
}
WritableLookupGroup is a read-write interface that returns a list of LookupCodes, and can add new codes to the list.