Remove hard-coded record types #132
@ -121,7 +121,7 @@ func (q queryResolver) QueryRecords(ctx context.Context, attributes []*KeyValueI
|
||||
res, err := nsQueryClient.ListRecords(
|
||||
context.Background(),
|
||||
®istrytypes.QueryListRecordsRequest{
|
||||
Attributes: parseRequestAttributes(attributes),
|
||||
Attributes: toRpcAttributes(attributes),
|
||||
All: (all != nil && *all),
|
||||
},
|
||||
)
|
||||
|
73
gql/util.go
73
gql/util.go
@ -256,60 +256,43 @@ func GetGQLAuction(auction *auctiontypes.Auction, bids []*auctiontypes.Bid) (*Au
|
||||
return &gqlAuction, nil
|
||||
}
|
||||
|
||||
func parseRequestValue(value *ValueInput) *registrytypes.QueryListRecordsRequest_ValueInput {
|
||||
if value == nil {
|
||||
func toRpcValue(value *ValueInput) *registrytypes.QueryListRecordsRequest_ValueInput {
|
||||
var rpcval registrytypes.QueryListRecordsRequest_ValueInput
|
||||
|
||||
switch {
|
||||
case value == nil:
|
||||
return nil
|
||||
}
|
||||
var val registrytypes.QueryListRecordsRequest_ValueInput
|
||||
|
||||
if value.String != nil {
|
||||
val.String_ = *value.String
|
||||
val.Type = "string"
|
||||
}
|
||||
|
||||
if value.Int != nil {
|
||||
val.Int = int64(*value.Int)
|
||||
val.Type = "int"
|
||||
}
|
||||
|
||||
if value.Float != nil {
|
||||
val.Float = *value.Float
|
||||
val.Type = "float"
|
||||
}
|
||||
|
||||
if value.Boolean != nil {
|
||||
val.Boolean = *value.Boolean
|
||||
val.Type = "boolean"
|
||||
}
|
||||
|
||||
if value.Link != nil {
|
||||
reference := ®istrytypes.QueryListRecordsRequest_ReferenceInput{
|
||||
Id: value.Link.String(),
|
||||
case value.Int != nil:
|
||||
rpcval.Value = ®istrytypes.QueryListRecordsRequest_ValueInput_Int{int64(*value.Int)}
|
||||
case value.Float != nil:
|
||||
rpcval.Value = ®istrytypes.QueryListRecordsRequest_ValueInput_Float{*value.Float}
|
||||
case value.String != nil:
|
||||
rpcval.Value = ®istrytypes.QueryListRecordsRequest_ValueInput_String_{*value.String}
|
||||
case value.Boolean != nil:
|
||||
rpcval.Value = ®istrytypes.QueryListRecordsRequest_ValueInput_Boolean{*value.Boolean}
|
||||
case value.Link != nil:
|
||||
rpcval.Value = ®istrytypes.QueryListRecordsRequest_ValueInput_Link{value.Link.String()}
|
||||
case value.Array != nil:
|
||||
var contents registrytypes.QueryListRecordsRequest_ArrayInput
|
||||
for _, val := range value.Array {
|
||||
contents.Values = append(contents.Values, toRpcValue(val))
|
||||
}
|
||||
|
||||
val.Reference = reference
|
||||
val.Type = "reference"
|
||||
}
|
||||
|
||||
// handle arrays
|
||||
if value.Array != nil {
|
||||
values := []*registrytypes.QueryListRecordsRequest_ValueInput{}
|
||||
for _, v := range value.Array {
|
||||
val := parseRequestValue(v)
|
||||
values = append(values, val)
|
||||
rpcval.Value = ®istrytypes.QueryListRecordsRequest_ValueInput_Array{&contents}
|
||||
case value.Map != nil:
|
||||
var contents registrytypes.QueryListRecordsRequest_MapInput
|
||||
for _, kv := range value.Map {
|
||||
contents.Values[kv.Key] = toRpcValue(kv.Value)
|
||||
}
|
||||
val.Values = values
|
||||
val.Type = "array"
|
||||
rpcval.Value = ®istrytypes.QueryListRecordsRequest_ValueInput_Map{&contents}
|
||||
}
|
||||
|
||||
return &val
|
||||
return &rpcval
|
||||
}
|
||||
|
||||
func parseRequestAttributes(attrs []*KeyValueInput) []*registrytypes.QueryListRecordsRequest_KeyValueInput {
|
||||
func toRpcAttributes(attrs []*KeyValueInput) []*registrytypes.QueryListRecordsRequest_KeyValueInput {
|
||||
kvPairs := []*registrytypes.QueryListRecordsRequest_KeyValueInput{}
|
||||
|
||||
for _, value := range attrs {
|
||||
parsedValue := parseRequestValue(value.Value)
|
||||
parsedValue := toRpcValue(value.Value)
|
||||
kvPair := ®istrytypes.QueryListRecordsRequest_KeyValueInput{
|
||||
Key: value.Key,
|
||||
Value: parsedValue,
|
||||
|
@ -69,8 +69,7 @@ func (suite *KeeperTestSuite) TestGrpcGetRecordLists() {
|
||||
{
|
||||
Key: "type",
|
||||
Value: ®istrytypes.QueryListRecordsRequest_ValueInput{
|
||||
Type: "string",
|
||||
String_: "WebsiteRegistrationRecord",
|
||||
Value: ®istrytypes.QueryListRecordsRequest_ValueInput_String_{"WebsiteRegistrationRecord"},
|
||||
},
|
||||
},
|
||||
},
|
||||
@ -141,8 +140,7 @@ func (suite *KeeperTestSuite) TestGrpcGetRecordLists() {
|
||||
{
|
||||
Key: "x500state_name",
|
||||
Value: ®istrytypes.QueryListRecordsRequest_ValueInput{
|
||||
Type: "string",
|
||||
String_: "california",
|
||||
Value: ®istrytypes.QueryListRecordsRequest_ValueInput_String_{"california"},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
@ -21,6 +21,9 @@ import (
|
||||
bank "github.com/cosmos/cosmos-sdk/x/bank/keeper"
|
||||
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
|
||||
"github.com/tendermint/tendermint/libs/log"
|
||||
|
||||
"github.com/ipld/go-ipld-prime/codec/dagjson"
|
||||
basicnode "github.com/ipld/go-ipld-prime/node/basic"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -335,6 +338,14 @@ func (k Keeper) PutRecord(ctx sdk.Context, record types.Record) {
|
||||
}
|
||||
|
||||
func (k Keeper) processAttributes(ctx sdk.Context, attrs map[string]any, id string, prefix string) error {
|
||||
np := basicnode.Prototype.Any // Pick a stle for the in-memory data.
|
||||
nb := np.NewBuilder() // Create a builder.
|
||||
err := dagjson.Decode(nb, bytes.NewReader(content)) // Hand the builder to decoding -- decoding will fill it in!
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
n := nb.Build() // Call 'Build' to get the resulting Node. (It's immutable!)
|
||||
|
||||
for key, value := range attrs {
|
||||
if subRecord, ok := value.(map[string]any); ok {
|
||||
err := k.processAttributes(ctx, subRecord, id, key)
|
||||
|
Loading…
Reference in New Issue
Block a user