types.gno
2.36 Kb · 104 lines
1package test2
2
3import (
4 "gno.land/p/nt/avl/v0"
5 "gno.land/p/nt/ownable/v0"
6 "gno.land/p/nt/seqid/v0"
7)
8
9// FieldKind is the input type a field renders as, and the validation it gets.
10type FieldKind string
11
12const (
13 KindText FieldKind = "text"
14 KindTextarea FieldKind = "textarea"
15 KindNumber FieldKind = "number"
16 KindSelect FieldKind = "select"
17)
18
19// Limits. Answers are stored on chain and the respondent pays the storage
20// deposit for them, so the caps exist to keep that cost predictable rather
21// than to save the realm anything.
22const (
23 MaxFields = 8
24 MaxTitleLen = 120
25 MaxDescriptionLen = 2048
26 MaxLabelLen = 120
27 MaxOptionLen = 120
28 MaxTextLen = 256
29 MaxTextareaLen = 2048
30 MaxNumberLen = 64
31)
32
33// Field is one question on a form.
34type Field struct {
35 Label string
36 Kind FieldKind
37 Required bool
38 Options []string // select only
39}
40
41// MaxLen is the longest answer the field accepts.
42func (f Field) MaxLen() int {
43 switch f.Kind {
44 case KindTextarea:
45 return MaxTextareaLen
46 case KindNumber:
47 return MaxNumberLen
48 case KindSelect:
49 return MaxOptionLen
50 default:
51 return MaxTextLen
52 }
53}
54
55// Slug rules. A slug is the form's ID and its URL segment, chosen at creation:
56// lowercase letters and digits, single hyphens between words, 3–48 characters.
57const (
58 MinSlugLen = 3
59 MaxSlugLen = 48
60)
61
62// Form is a published questionnaire and its responses.
63type Form struct {
64 ID string // the slug
65 Title string
66 Description string
67 Fields []Field
68 OnePerAddr bool
69 Deadline int64 // chain height; 0 means none
70 Closed bool
71 CreatedAt int64
72
73 owner *ownable.Ownable
74 responses *avl.Tree // response id -> *Response
75 byAddr *avl.Tree // author address -> response id
76}
77
78// Owner returns the form owner's address.
79func (f *Form) Owner() address {
80 return f.owner.Owner()
81}
82
83// ResponseCount returns how many responses the form currently holds.
84func (f *Form) ResponseCount() int {
85 return f.responses.Size()
86}
87
88// IsOpen reports whether the form accepts submissions at the given height.
89func (f *Form) IsOpen(height int64) bool {
90 if f.Closed {
91 return false
92 }
93
94 return f.Deadline == 0 || height < f.Deadline
95}
96
97// Response is one submission to a form. Answers align with Form.Fields.
98type Response struct {
99 ID seqid.ID
100 Form string // the form slug
101 Author address
102 Height int64
103 Answers []string
104}