form

package module
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: Apache-2.0 Imports: 9 Imported by: 11

README

Form 🤔

GoDoc Version Build Status Go Report Card Codecov

JSON to HTML (Forms for Go)

This expandable Go module generates HTML forms using JSON configurations. It is inspired by JSON-Forms but is not compatable with that standard.

DO NOT USE

This project is a work-in-progress, and should NOT be used by ANYONE, for ANY PURPOSE, under ANY CIRCUMSTANCES. It is WILL BE CHANGED UNDERNEATH YOU WITHOUT NOTICE OR HESITATION, and is expressly GUARANTEED to blow up your computer, send your cat into an infinite loop, and combine your hot and cold laundry into a single cycle.

Pull Requests Welcome

This library is a work in progress, and will benefit from your experience reports, use cases, and contributions. If you have an idea for making this library better, send in a pull request. We're all in this together! 🤔

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Editor added in v0.10.0

func Editor(schema schema.Schema, element Element, value any, lookupProvider LookupProvider) (string, error)

Viewer creates an in-place form and executes its "Editorr" method

func SortLookupCodeByGroupThenLabel added in v0.15.0

func SortLookupCodeByGroupThenLabel(a LookupCode, b LookupCode) bool

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) bool

SortLookupCodeByLabel is a sort function that works with the sort.Slice function.

func Use added in v0.20.0

func Use(name string, widget Widget)

Use adds a new widget into the widget registry.

func Viewer added in v0.10.0

func Viewer(schema schema.Schema, element Element, value any, lookupProvider LookupProvider) (string, error)

Viewer creates an in-place form and executes its "Viewer" method

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

func MustParse(data any) Element

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

func Parse added in v0.3.9

func Parse(data any) (Element, error)

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

func (element *Element) AllElements() []*Element

AllPaths returns pointers to all of the valid paths in this form

func (*Element) Edit added in v0.10.0

func (element *Element) Edit(schema *schema.Schema, lookupProvider LookupProvider, value any, b *html.Builder) error

func (*Element) Encoding added in v0.20.0

func (element *Element) Encoding() string

func (*Element) GetSchema added in v0.20.0

func (element *Element) GetSchema(s *schema.Schema) schema.Element

GetSchema finds and returns the schema.Element associated with this Element path

func (*Element) GetSliceOfString added in v0.9.1

func (element *Element) GetSliceOfString(value any, s *schema.Schema) []string

GetSliceOfString rturns a slice of strings for a provided path.

func (*Element) GetString added in v0.9.1

func (element *Element) GetString(value any, s *schema.Schema) string

GetValue 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) IsEmpty added in v0.17.12

func (element Element) IsEmpty() bool

IsEmpty returns TRUE if the element is not defined

func (*Element) UnmarshalMap added in v0.9.1

func (element *Element) UnmarshalMap(data map[string]any) error

UnmarshalMap parses data from a generic structure (mapof.Any) into a Form record.

func (*Element) View added in v0.10.0

func (element *Element) View(schema *schema.Schema, lookupProvider LookupProvider, value any, b *html.Builder) error

func (*Element) Widget added in v0.10.0

func (element *Element) Widget() (Widget, error)

type Form

type Form struct {
	Schema  schema.Schema
	Element Element
}

func New

func New(schema schema.Schema, element Element) Form

New returns a fully initialized Form object (with all required values)

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)

DrawString() generates this form as a string

func (*Form) Encoding added in v0.20.0

func (form *Form) Encoding() string

Encoding returns the "enctype" attribute for the form. Default is ""

func (*Form) SetAll added in v0.13.1

func (form *Form) SetAll(object any, value mapof.Any, lookupProvider LookupProvider) error

Do applies all of the data from the value map into the target object

func (*Form) Viewer added in v0.10.0

func (form *Form) Viewer(value any, lookupProvider LookupProvider) (string, error)

DrawString() generates this form as a string

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
}

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

func (*LookupCode) GetPointer added in v0.15.5

func (lookupCode *LookupCode) GetPointer(name string) (any, bool)

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

type Rule added in v0.6.0

type Rule struct {
	Path  string `json:"path"`
	Op    string `json:"op"`
	Value string `json:"value"`
}

func (Rule) HyperscriptRules added in v0.6.0

func (rule Rule) HyperscriptRules() string

func (Rule) IsEmpty added in v0.6.0

func (rule Rule) IsEmpty() bool

func (Rule) Operator added in v0.6.0

func (rule Rule) Operator() string

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(element *Element, schema *schema.Schema, lookupProvider LookupProvider, value any, builder *html.Builder) error
	Edit(element *Element, schema *schema.Schema, 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)
}

WriteableLookupGroup is a read-write interface that returns a list of LookupCodes, and can add new codes to the list.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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