gql -> rpc dag-json encoding

This commit is contained in:
Roy Crihfield 2023-10-18 19:03:21 -05:00 committed by Nabarun
parent 378c6d7cde
commit 62d2d1598a
4 changed files with 42 additions and 50 deletions

View File

@ -121,7 +121,7 @@ func (q queryResolver) QueryRecords(ctx context.Context, attributes []*KeyValueI
res, err := nsQueryClient.ListRecords(
context.Background(),
&registrytypes.QueryListRecordsRequest{
Attributes: parseRequestAttributes(attributes),
Attributes: toRpcAttributes(attributes),
All: (all != nil && *all),
},
)

View File

@ -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
case value.Int != nil:
rpcval.Value = &registrytypes.QueryListRecordsRequest_ValueInput_Int{int64(*value.Int)}
case value.Float != nil:
rpcval.Value = &registrytypes.QueryListRecordsRequest_ValueInput_Float{*value.Float}
case value.String != nil:
rpcval.Value = &registrytypes.QueryListRecordsRequest_ValueInput_String_{*value.String}
case value.Boolean != nil:
rpcval.Value = &registrytypes.QueryListRecordsRequest_ValueInput_Boolean{*value.Boolean}
case value.Link != nil:
rpcval.Value = &registrytypes.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))
}
var val registrytypes.QueryListRecordsRequest_ValueInput
if value.String != nil {
val.String_ = *value.String
val.Type = "string"
rpcval.Value = &registrytypes.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)
}
rpcval.Value = &registrytypes.QueryListRecordsRequest_ValueInput_Map{&contents}
}
return &rpcval
}
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 := &registrytypes.QueryListRecordsRequest_ReferenceInput{
Id: value.Link.String(),
}
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)
}
val.Values = values
val.Type = "array"
}
return &val
}
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 := &registrytypes.QueryListRecordsRequest_KeyValueInput{
Key: value.Key,
Value: parsedValue,

View File

@ -69,8 +69,7 @@ func (suite *KeeperTestSuite) TestGrpcGetRecordLists() {
{
Key: "type",
Value: &registrytypes.QueryListRecordsRequest_ValueInput{
Type: "string",
String_: "WebsiteRegistrationRecord",
Value: &registrytypes.QueryListRecordsRequest_ValueInput_String_{"WebsiteRegistrationRecord"},
},
},
},
@ -141,8 +140,7 @@ func (suite *KeeperTestSuite) TestGrpcGetRecordLists() {
{
Key: "x500state_name",
Value: &registrytypes.QueryListRecordsRequest_ValueInput{
Type: "string",
String_: "california",
Value: &registrytypes.QueryListRecordsRequest_ValueInput_String_{"california"},
},
},
},

View File

@ -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)