types.gno
2.13 Kb · 97 lines
1package test
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// Form is a published questionnaire and its responses.
56type Form struct {
57 ID seqid.ID
58 Title string
59 Description string
60 Fields []Field
61 OnePerAddr bool
62 Deadline int64 // chain height; 0 means none
63 Closed bool
64 CreatedAt int64
65
66 owner *ownable.Ownable
67 responses *avl.Tree // response id -> *Response
68 byAddr *avl.Tree // author address -> response id
69}
70
71// Owner returns the form owner's address.
72func (f *Form) Owner() address {
73 return f.owner.Owner()
74}
75
76// ResponseCount returns how many responses the form currently holds.
77func (f *Form) ResponseCount() int {
78 return f.responses.Size()
79}
80
81// IsOpen reports whether the form accepts submissions at the given height.
82func (f *Form) IsOpen(height int64) bool {
83 if f.Closed {
84 return false
85 }
86
87 return f.Deadline == 0 || height < f.Deadline
88}
89
90// Response is one submission to a form. Answers align with Form.Fields.
91type Response struct {
92 ID seqid.ID
93 Form seqid.ID
94 Author address
95 Height int64
96 Answers []string
97}