Skip to content Search Apps Documentation Source Content File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

v1 source pure

Package forms is the Remix Lab forms block: a fixed field schema, typed and bounded answers, sequential submission ID...

Overview

Package forms is the Remix Lab forms block: a fixed field schema, typed and bounded answers, sequential submission IDs and a small review status (submitted, accepted, rejected).

All answers are public chain data. Recipes should only ask for information that is meant to be published.

Capacity counts live submissions (submitted or accepted). Rejected submissions stay readable for the record but free their slot, so a flood of junk can always be cleared by reviewing it.

The schema can change only while the form has no submissions; after that a host must create a new Form. A *Form belongs in an unexported host-realm variable, and the host authenticates submitters and reviewers.

Constants 2

Functions 1

func New

1func New(cfg Config, fields []Field) *Form
source

New creates a form. It panics on an invalid schema or configuration, since hosts call it from init with generated constants.

Types 4

type Config

struct
1type Config struct {
2	MaxSubmissions  int // live (submitted or accepted) submissions
3	MaxPerSubmitter int // live submissions per address; 0 means unlimited
4	MaxPayloadBytes int // sum of answer bytes
5}
source

Config bounds the form.

type Field

struct
 1type Field struct {
 2	Name      string // identifier, e.g. "title"
 3	Label     string
 4	Kind      FieldKind
 5	Required  bool
 6	MaxBytes  int      // text only
 7	Multiline bool     // text only
 8	Min, Max  int64    // integer only, inclusive
 9	Choices   []string // choice only
10}
source

Field describes one form field.

type FieldKind

ident
1type FieldKind int
source

FieldKind is the type of answer a field accepts.

Methods on FieldKind

func String

method on FieldKind
1func (k FieldKind) String() string
source

type Form

struct
1type Form struct {
2	cfg          Config
3	fields       []Field
4	subs         *avl.Tree    // zero-padded id -> *submission
5	byStatus     [3]*avl.Tree // per status: zero-padded id -> *submission
6	perSubmitter *avl.Tree    // address -> live submission count
7	nextID       uint64
8}
source

Form holds a schema and its submissions.

Methods on Form

func Config

method on Form
1func (f *Form) Config() Config
source

Config returns the form configuration.

func Count

method on Form
1func (f *Form) Count() int
source

Count implements core.SubmissionReader. It includes rejected entries.

func CountByStatus

method on Form
1func (f *Form) CountByStatus(status core.SubmissionStatus) int
source

CountByStatus returns the number of submissions in status.

func Fields

method on Form
1func (f *Form) Fields() []Field
source

Fields returns a copy of the schema.

func Normalize

method on Form
1func (f *Form) Normalize(answers []string) ([]string, error)
source

Normalize validates answers (one per field, in schema order) and returns their canonical form: integers are reformatted, and absent optional answers (including blank text) are "".

func Review

method on Form
1func (f *Form) Review(id uint64, status core.SubmissionStatus) error
source

Review moves a submitted entry to accepted or rejected. Decisions are final. Rejecting frees the entry's capacity slot and its submitter's per-address slot. The host must have authorized the reviewer.

func SetFields

method on Form
1func (f *Form) SetFields(fields []Field) error
source

SetFields replaces the schema. It fails once any submission exists.

func Submission

method on Form
1func (f *Form) Submission(id uint64) (core.SubmissionView, bool)
source

Submission implements core.SubmissionReader.

func Submissions

method on Form
1func (f *Form) Submissions(status int, offset, limit int) []core.SubmissionView
source

Submissions implements core.SubmissionReader, ordered by ID. status -1 returns all; any other value outside the defined statuses returns none. Each status has its own index, so paging never scans other entries.

func Submit

method on Form
1func (f *Form) Submit(submitter address, answers []string) (uint64, error)
source

Submit stores a submission from submitter, who must be the authenticated caller.

func SubmittedBy

method on Form
1func (f *Form) SubmittedBy(addr address) int
source

SubmittedBy returns how many live (submitted or accepted) submissions addr has.

Dependencies

Source Files 2