github.com/lino-network/lino@v0.6.11/utils/querier.go (about) 1 package utils 2 3 import ( 4 "fmt" 5 6 wire "github.com/cosmos/cosmos-sdk/codec" 7 sdk "github.com/cosmos/cosmos-sdk/types" 8 9 linotypes "github.com/lino-network/lino/types" 10 ) 11 12 // query store 13 type StoreQuerier = func(args ...string) (interface{}, sdk.Error) 14 15 // return query result 16 type QueryResolver = func(ctx sdk.Context, cdc *wire.Codec, path []string) ([]byte, sdk.Error) 17 18 func NewQueryResolver(numArgs int, resolver StoreQuerier) QueryResolver { 19 return func(ctx sdk.Context, cdc *wire.Codec, path []string) ([]byte, sdk.Error) { 20 if len(path) < 1 { 21 return nil, linotypes.ErrQueryFailed("invalid query") 22 } 23 substore := path[0] 24 path = path[1:] 25 if err := linotypes.CheckPathContentAndMinLength(path, numArgs); err != nil { 26 return nil, err 27 } 28 rst, err := resolver(path...) 29 if err != nil { 30 return nil, err 31 } 32 res, marshalErr := cdc.MarshalJSON(rst) 33 if marshalErr != nil { 34 return nil, linotypes.ErrQueryFailed(fmt.Sprintf("query %s failed", substore)) 35 } 36 return res, nil 37 } 38 }