-
Notifications
You must be signed in to change notification settings - Fork 45
/
decoder.go
51 lines (43 loc) · 1.83 KB
/
decoder.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
package simulation
import (
"bytes"
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types/kv"
"github.com/desmos-labs/desmos/v2/x/staging/posts/types"
)
// NewDecodeStore returns a new decoder that unmarshals the KVPair's Value
// to the corresponding posts type
func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string {
return func(kvA, kvB kv.Pair) string {
switch {
case bytes.HasPrefix(kvA.Key, types.PostStorePrefix):
var postA, postB types.Post
cdc.MustUnmarshal(kvA.Value, &postA)
cdc.MustUnmarshal(kvB.Value, &postB)
return fmt.Sprintf("PostA: %s\nPostB: %s\n", postA.String(), postB.String())
case bytes.HasPrefix(kvA.Key, types.PostReactionsStorePrefix):
var reactionA, reactionB types.PostReaction
cdc.MustUnmarshal(kvA.Value, &reactionA)
cdc.MustUnmarshal(kvB.Value, &reactionB)
return fmt.Sprintf("PostReactionA: %s\nPostReactionB: %s\n", reactionA, reactionB)
case bytes.HasPrefix(kvA.Key, types.CommentsStorePrefix):
var commentA, commentB string
commentA = string(kvA.Value)
commentB = string(kvA.Value)
return fmt.Sprintf("CommentA: %s\nCommentB: %s\n", commentA, commentB)
case bytes.HasPrefix(kvA.Key, types.RegisteredReactionsStorePrefix):
var reactionA, reactionB types.RegisteredReaction
cdc.MustUnmarshal(kvA.Value, &reactionA)
cdc.MustUnmarshal(kvB.Value, &reactionB)
return fmt.Sprintf("RegisteredReactionA: %s\nRegisteredReactionB: %s\n", reactionA, reactionB)
case bytes.HasPrefix(kvA.Key, types.ReportsStorePrefix):
var reportsA, reportsB types.Reports
cdc.MustUnmarshal(kvA.Value, &reportsA)
cdc.MustUnmarshal(kvB.Value, &reportsB)
return fmt.Sprintf("ReportsA: %s\nReportsB: %s\n", reportsA.Reports, reportsB.Reports)
default:
panic(fmt.Sprintf("unexpected %s key %X (%s)", types.ModuleName, kvA.Key, kvA.Key))
}
}
}