/
topic.go
63 lines (52 loc) · 1.33 KB
/
topic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package types
import (
"cosmossdk.io/errors"
"regexp"
)
const (
maxTopicLength = 70
maxDescriptionLength = 5000
)
func (t Topic) Validate() error {
return validateDescription(t.Description)
}
func (t Topic) NextRecordOffset() uint64 {
return t.TotalRecords
}
func (t Topic) IncreaseTotalRecords() Topic {
return Topic{
TotalRecords: t.TotalRecords + 1,
TotalWriters: t.TotalWriters,
Description: t.Description,
}
}
func (t Topic) IncreaseTotalWriters() Topic {
return Topic{
TotalRecords: t.TotalRecords,
TotalWriters: t.TotalWriters + 1,
Description: t.Description,
}
}
func (t Topic) DecreaseTotalWriters() Topic {
return Topic{
TotalRecords: t.TotalRecords,
TotalWriters: t.TotalWriters - 1,
Description: t.Description,
}
}
func validateTopicName(topicName string) error {
if len(topicName) > maxTopicLength {
return errors.Wrapf(ErrMessageTooLarge, "topicName (%d > %d)", len(topicName), maxTopicLength)
}
// cannot be an empty string
if !regexp.MustCompile("^[A-Za-z0-9._-]+$").MatchString(topicName) {
return errors.Wrapf(ErrInvalidTopic, "topic %s", topicName)
}
return nil
}
func validateDescription(description string) error {
if len(description) > maxDescriptionLength {
return errors.Wrapf(ErrMessageTooLarge, "description (%d > %d)", len(description), maxDescriptionLength)
}
return nil
}