From 62d2d1598a6ee73088bc796f61583a2a03cb3d21 Mon Sep 17 00:00:00 2001 From: Roy Crihfield Date: Wed, 18 Oct 2023 19:03:21 -0500 Subject: [PATCH] gql -> rpc dag-json encoding --- gql/resolver.go | 2 +- gql/util.go | 73 +++++++++++----------------- x/registry/keeper/grpc_query_test.go | 6 +-- x/registry/keeper/keeper.go | 11 +++++ 4 files changed, 42 insertions(+), 50 deletions(-) diff --git a/gql/resolver.go b/gql/resolver.go index 0c41bbb0..a3b49da0 100644 --- a/gql/resolver.go +++ b/gql/resolver.go @@ -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), }, ) diff --git a/gql/util.go b/gql/util.go index aca90b8f..cc53a1d5 100644 --- a/gql/util.go +++ b/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, diff --git a/x/registry/keeper/grpc_query_test.go b/x/registry/keeper/grpc_query_test.go index c5ef2300..c28c6a72 100644 --- a/x/registry/keeper/grpc_query_test.go +++ b/x/registry/keeper/grpc_query_test.go @@ -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"}, }, }, }, diff --git a/x/registry/keeper/keeper.go b/x/registry/keeper/keeper.go index b4cc5754..a15c48be 100644 --- a/x/registry/keeper/keeper.go +++ b/x/registry/keeper/keeper.go @@ -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)