-
Notifications
You must be signed in to change notification settings - Fork 206
/
proposal.go
56 lines (51 loc) · 1.78 KB
/
proposal.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
package types
import (
fmt "fmt"
"strings"
sdkerrors "cosmossdk.io/errors"
)
func checkSpecProposal(spec Spec) error {
if len(strings.TrimSpace(spec.Name)) == 0 {
return sdkerrors.Wrapf(ErrBlankSpecName, "spec name cannot be blank %v", spec)
}
if len(strings.TrimSpace(spec.Index)) == 0 {
return sdkerrors.Wrap(ErrBlankSpecName, "spec index cannot be blank")
}
if len(spec.ApiCollections) == 0 && len(spec.Imports) == 0 {
return sdkerrors.Wrap(ErrEmptyApis, "api list cannot be empty")
}
for _, apiCollection := range spec.ApiCollections {
checkUnique := map[string]bool{}
for idx, api := range apiCollection.Apis {
if len(strings.TrimSpace(api.Name)) == 0 {
prevApi := ""
if idx != 0 {
prevApi = apiCollection.Apis[idx-1].Name
} else if idx+1 < len(apiCollection.Apis) {
prevApi = apiCollection.Apis[idx+1].Name
}
return sdkerrors.Wrapf(ErrBlankApiName, "api name cannot be blank, %#v, in spec %s:%s, previous/next api: %s", apiCollection.CollectionData, spec.Index, spec.Name, prevApi)
}
if _, ok := checkUnique[api.Name]; ok {
return sdkerrors.Wrap(ErrDuplicateApiName, fmt.Sprintf("api name must be unique: %s", api.Name))
}
checkUnique[api.Name] = true
}
}
return nil
}
func stringSpec(spec Spec, b strings.Builder) strings.Builder {
b.WriteString(fmt.Sprintf(` Spec name:
Name: %s, Spec index: %s, Enabled: %t, ApiCollections: %d
`, spec.Name, spec.Index, spec.Enabled, len(spec.ApiCollections)))
for _, collection := range spec.ApiCollections {
b.WriteString(fmt.Sprintf(` ApiCollection: %v
`, collection.CollectionData))
for _, api := range collection.Apis {
b.WriteString(fmt.Sprintf(` Api:
Name: %s, Enabled: %t, ComputeUntis: %d
`, api.Name, api.Enabled, api.ComputeUnits))
}
}
return b
}