/
querier.go
77 lines (65 loc) · 2.38 KB
/
querier.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
// Copyright (C) 2020 Bluzelle
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License, version 3,
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package keeper
import (
"github.com/bluzelle/curium/x/crud/internal/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/tendermint/abci/types"
)
const (
QueryRead = "read"
QueryHas = "has"
QueryKeys = "keys"
)
func NewQuerier(keeper Keeper) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err sdk.Error) {
switch path[0] {
case QueryRead:
return queryRead(ctx, path[1:], req, keeper)
case QueryHas:
return queryHas(ctx, path[1:], req, keeper)
case QueryKeys:
return queryKeys(ctx, path[1:], req, keeper)
default:
return nil, sdk.ErrUnknownRequest("unknown crud query endpoint")
}
}
}
func queryRead(ctx sdk.Context, path []string, _ abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
value := keeper.GetBLZValue(ctx, path[0], path[1]).Value
if len(value) == 0 {
return []byte{}, sdk.ErrUnknownRequest("could not read key")
}
res, err := codec.MarshalJSONIndent(keeper.cdc, types.QueryResultRead{UUID: path[0], Key: path[1], Value: value})
if err != nil {
panic("could not marshal result to JSON")
}
return res, nil
}
func queryHas(ctx sdk.Context, path []string, _ abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
has := keeper.IsKeyPresent(ctx, path[0], path[1])
res, err := codec.MarshalJSONIndent(keeper.cdc, types.QueryResultHas{UUID: path[0], Key: path[1], Has: has})
if err != nil {
panic("could not marshal result to JSON")
}
return res, nil
}
func queryKeys(ctx sdk.Context, path []string, _ abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
res, err := codec.MarshalJSONIndent(keeper.cdc, keeper.GetKeys(ctx, path[0]))
if err != nil {
panic("could not marshal result to JSON")
}
return res, nil
}