-
Notifications
You must be signed in to change notification settings - Fork 44
/
params.go
122 lines (98 loc) · 3.02 KB
/
params.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package types
import (
"fmt"
"strings"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
)
const (
// DefaultParamsSpace represents the default paramspace for the Params keeper
DefaultParamsSpace = ModuleName
)
var (
// DefaultReasons represents the default set of reasons that can be adopted by subspaces
DefaultReasons []StandardReason
)
var (
// ReasonsKey represents the params key used to store available default reasons
ReasonsKey = []byte("StandardReasons")
)
// --------------------------------------------------------------------------------------------------------------------
// ParamKeyTable returns the key declaration for the parameters
func ParamKeyTable() paramstypes.KeyTable {
return paramstypes.NewKeyTable().
RegisterParamSet(&Params{})
}
// NewParams returns a new Params instance
func NewParams(reasons []StandardReason) Params {
return Params{
StandardReasons: reasons,
}
}
// DefaultParams returns the default params
func DefaultParams() Params {
return Params{
StandardReasons: DefaultReasons,
}
}
// ParamSetPairs implements the ParamSet interface and returns the key/value pairs
// of reports module's parameters.
func (params *Params) ParamSetPairs() paramstypes.ParamSetPairs {
return paramstypes.ParamSetPairs{
paramstypes.NewParamSetPair(ReasonsKey, ¶ms.StandardReasons, ValidateStandardReasonsParam),
}
}
// Validate perform basic checks on all parameters to ensure they are correct
func (params Params) Validate() error {
return ValidateStandardReasonsParam(params.StandardReasons)
}
// ValidateStandardReasonsParam validates the reasons params value
func ValidateStandardReasonsParam(i interface{}) error {
reasons, ok := i.([]StandardReason)
if !ok {
return fmt.Errorf("invalid parameters type: %s", i)
}
err := NewStandardReasons(reasons...).Validate()
if err != nil {
return err
}
return nil
}
// --------------------------------------------------------------------------------------------------------------------
type StandardReasons []StandardReason
// NewStandardReasons returns a new instance of StandardReasons contains the given reasons
func NewStandardReasons(reasons ...StandardReason) StandardReasons {
return reasons
}
// Validate implements fmt.Validator
func (s StandardReasons) Validate() error {
ids := map[uint32]bool{}
for _, reason := range s {
if _, duplicated := ids[reason.ID]; duplicated {
return fmt.Errorf("duplicated reason with id %d", reason.ID)
}
ids[reason.ID] = true
err := reason.Validate()
if err != nil {
return err
}
}
return nil
}
// NewStandardReason returns a new StandardReason instance
func NewStandardReason(id uint32, title string, description string) StandardReason {
return StandardReason{
ID: id,
Title: title,
Description: description,
}
}
// Validate implements fmt.Validator
func (r StandardReason) Validate() error {
if r.ID == 0 {
return fmt.Errorf("invalid id: %d", r.ID)
}
if strings.TrimSpace(r.Title) == "" {
return fmt.Errorf("invalid title: %s", r.Title)
}
return nil
}