schema

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2023 License: MPL-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidType   = errors.New("field type invalid")
	ErrRequiredField = errors.New("required field not set")
	ErrWrongPattern  = errors.New("doesn't match pattern")
)

Functions

func OptionValues

func OptionValues(options ...Option) utils.Set[string]

func WithCredentials added in v0.2.0

func WithCredentials(ctx context.Context, creds *Credentials) context.Context

Types

type AMQP added in v0.3.0

type AMQP struct {
	Exchange    string                  // Exchange name, can be empty
	Key         Template[NotifyContext] // Routing key, usually required
	Retry       int                     // maximum number of retries (negative means no retries)
	Timeout     time.Duration           // publish timeout
	Interval    time.Duration           // interval between attempts (publish message)
	Headers     map[string]string       // arbitrary headers (only string supported)
	Type        string                  // optional content type property; if not set and message is nil, type is set to application/json
	Correlation Template[NotifyContext] // optional correlation ID template (commonly result ID)
	ID          Template[NotifyContext] // optional correlation ID template (commonly result ID), useful for client-side deduplication
	Message     Template[NotifyContext] // payload content, if not set - JSON representation of storage result
}

type Credentials added in v0.2.0

type Credentials struct {
	User   string
	Groups []string
	Email  string
}

func CredentialsFromContext added in v0.2.0

func CredentialsFromContext(ctx context.Context) *Credentials

type Field

type Field struct {
	Name        string                   // column name in database.
	Label       string                   // short name of field which will be shown in UI, if not set - [Field.Name] is used.
	Description string                   // (markdown) optional description for the field, also shown in UI as help text.
	Required    bool                     // make field as required: empty values will not be accepted as well as at least one option should be selected.
	Disabled    bool                     // user input will be ignored, by field will be visible in UI. Doesn't apply for options.
	Hidden      bool                     // user input will be ignored, field not visible in UI
	Default     Template[RequestContext] // golang template expression for the default value.  Doesn't apply for options with [Field.Multiple].
	Type        Type                     // (default [TypeString]) field type used for user input validation.
	Pattern     string                   // optional regexp to validate content, applicable only for string type
	Options     []Option                 // allowed values. If [Field.Multiple] set, it acts as "any of", otherwise "one of".
	Multiple    bool                     // allow picking multiple options. Column type in database MUST be ARRAY of corresponding type.
	Multiline   bool                     // multiline input (for [TypeString] only)
	Icon        string                   // optional MDI icon
}

func (*Field) Parse

func (f *Field) Parse(value string, locale *time.Location, viewCtx *RequestContext) (any, error)

type FieldError added in v0.4.0

type FieldError struct {
	Name  string
	Error error
}

func ParseForm added in v0.4.0

func ParseForm(definition *Form, tzLocation *time.Location, viewCtx *RequestContext) (map[string]any, []FieldError)

ParseForm converts user request to parsed field.

type Form

type Form struct {
	Name        string                   // unique form name, if not set - file name without extension will be used.
	Table       string                   // database table name
	Title       string                   // optional title for the form
	Description Template[RequestContext] // (markdown) optional description of the form
	Fields      []Field                  // form fields
	Webhooks    []Webhook                // Webhook (HTTP) notification
	AMQP        []AMQP                   // AMQP notification
	Success     Template[ResultContext]  // markdown message for success (also go template with available .Result)
	Failed      Template[ResultContext]  // markdown message for failed (also go template with .Error)
	Policy      *Policy                  // optional access policy
	Codes       utils.Set[string]        // optional access tokens
}

func Default

func Default() Form

func FormsFromFS

func FormsFromFS(src fs.FS) ([]Form, error)

func FormsFromFile

func FormsFromFile(fs fs.FS, file string) ([]Form, error)

func FormsFromStream

func FormsFromStream(reader io.Reader) ([]Form, error)

func (*Form) HasCodeAccess added in v0.4.0

func (f *Form) HasCodeAccess() bool

func (*Form) IsAllowed added in v0.2.0

func (f *Form) IsAllowed(creds *Credentials) bool

IsAllowed checks permission for the provided credentials. Always allowed for nil policy or for nil creds, and always prohibited if policy returns non-boolean value.

type NotifyContext added in v0.4.0

type NotifyContext struct {
	Form   *Form
	Result map[string]any
}

NotifyContext is used for rendering notification message.

type Option

type Option struct {
	Label string // label for UI
	Value string // if not set - Label is used, allowed value should match textual representation of form value
}

type Policy added in v0.2.0

type Policy struct{ cel.Program }

func (*Policy) UnmarshalText added in v0.2.0

func (p *Policy) UnmarshalText(text []byte) error

type RequestContext added in v0.4.0

type RequestContext struct {
	Headers     http.Header
	Query       url.Values
	Form        url.Values
	Code        string       // access code
	Credentials *Credentials // optional user credentials
}

RequestContext is used for rendering default values.

func (*RequestContext) Email added in v0.4.0

func (rc *RequestContext) Email() string

func (*RequestContext) Groups added in v0.4.0

func (rc *RequestContext) Groups() []string

func (*RequestContext) User added in v0.4.0

func (rc *RequestContext) User() string

type ResultContext added in v0.4.0

type ResultContext struct {
	Form   *Form
	Result map[string]any
	Error  error
}

ResultContext is used for rendering result message (success or fail).

type Template

type Template[T any] struct {
	Valid bool
	Value *template.Template
}

func MustTemplate added in v0.4.0

func MustTemplate[T any](text string) Template[T]

func NewTemplate added in v0.4.0

func NewTemplate[T any](text string) (Template[T], error)

func (*Template[T]) Bytes added in v0.4.0

func (t *Template[T]) Bytes(data *T) ([]byte, error)

func (*Template[T]) String added in v0.4.0

func (t *Template[T]) String(data *T) (string, error)

func (*Template[T]) UnmarshalText

func (t *Template[T]) UnmarshalText(text []byte) error

type Type

type Type string
const (
	TypeString   Type = "string" // default, also for enums
	TypeInteger  Type = "integer"
	TypeFloat    Type = "float"
	TypeBoolean  Type = "boolean"
	TypeDate     Type = "date"
	TypeDateTime Type = "date-time"
)

func (Type) Is

func (t Type) Is(value string) bool

func (Type) Parse

func (t Type) Parse(value string, locale *time.Location) (any, error)

func (*Type) UnmarshalText

func (t *Type) UnmarshalText(text []byte) error

type Webhook

type Webhook struct {
	URL      string                  // URL for POST webhook, where payload is JSON with fields from database column.
	Method   string                  // HTTP method to perform, default is POST
	Retry    int                     // maximum number of retries (negative means no retries)
	Timeout  time.Duration           // request timeout
	Interval time.Duration           // interval between attempts (for non 2xx code)
	Headers  map[string]string       // arbitrary headers (ex: Authorization)
	Message  Template[NotifyContext] // payload content, if not set - JSON representation of storage result
}

Jump to

Keyboard shortcuts

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