-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
75 lines (63 loc) · 1.79 KB
/
util.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
package msgs
import (
"sort"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
)
type TestBlockWithMsgs struct {
Block uint32
Msgs []TestSdkMsg
}
type TestSdkMsg struct {
Msg sdk.Msg
ExpectedIsOk bool
ExpectedRespCode uint32
}
// SampleMsg is a struct containing a sample msg and its name.
type SampleMsg struct {
Name string
Msg sdk.Msg
}
// GetMsgNameWithModuleVersion returns the name of the msg type along with module and version, given its type url.
func GetMsgNameWithModuleVersion(typeUrl string) string {
tokens := strings.Split(typeUrl, ".")
if !IsValidMsgFormat(tokens) {
panic("invalid type url: " + typeUrl)
}
lastThreeTokens := tokens[len(tokens)-3:]
result := strings.Join(lastThreeTokens, ".")
return result
}
// IsValidMsgFormat returns true if the given tokens are of the form: "<module>.<version>.Msg<MsgName>"
func IsValidMsgFormat(tokens []string) bool {
tokenLen := len(tokens)
if tokenLen < 3 {
return false
}
if tokens[tokenLen-2] == "" || tokens[tokenLen-3] == "" {
return false
}
return strings.HasPrefix(tokens[tokenLen-1], "Msg")
}
// GetSampleMsgs returns a list of sample msgs for each non-nil map value in the input.
func GetNonNilSampleMsgs(typeUrlToSampleMsgMap map[string]sdk.Msg) []SampleMsg {
sampleMsgs := make([]SampleMsg, 0)
for name, sample := range typeUrlToSampleMsgMap {
if sample != nil {
shortName := GetMsgNameWithModuleVersion(name)
sampleMsgs = append(sampleMsgs, SampleMsg{shortName, sample})
}
}
sort.Slice(sampleMsgs, func(i, j int) bool {
return sampleMsgs[i].Name < sampleMsgs[j].Name
})
return sampleMsgs
}
// CopyMap returns a copy of the given map.
func CopyMap(m map[string]sdk.Msg) map[string]sdk.Msg {
result := make(map[string]sdk.Msg)
for k, v := range m {
result[k] = v
}
return result
}