Skip to content

Commit

Permalink
feat(cosmos): Always include alleged name and slot id in vstorage Cap…
Browse files Browse the repository at this point in the history
…Data remotable representations
  • Loading branch information
gibson042 authored and mhofman committed Aug 7, 2023
1 parent 16be21e commit 47defd4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
25 changes: 13 additions & 12 deletions golang/cosmos/x/vstorage/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,24 +142,24 @@ func capdataBigintToDigits(bigint *capdata.CapdataBigint) interface{} {
}

// capdataRemotableToString represents a Remotable as a bracketed string
// containing its alleged name (e.g., "[Foo {}]").
// containing its alleged name and id from `slots`
// (e.g., "[Alleged: IST brand <board007>]").
func capdataRemotableToString(r *capdata.CapdataRemotable) interface{} {
iface := "Remotable"
if r.Iface != nil || *r.Iface != "" {
iface = *r.Iface
}
return fmt.Sprintf("[%s {}]", iface)
return fmt.Sprintf("[%s <%s>]", iface, r.Id)
}

// capdataRemotableToObject represents a Remotable as an object containing
// its id from `slots` and alleged name minus any "Alleged:" prefix or "brand"
// suffix (e.g., `{ "id": "board007", "allegedName": "IST" }`).
// its id from `slots` and its alleged name minus any "Alleged:" prefix
// (e.g., `{ "id": "board007", "allegedName": "IST brand" }`).
func capdataRemotableToObject(r *capdata.CapdataRemotable) interface{} {
iface := "Remotable"
if r.Iface != nil || *r.Iface != "" {
iface = *r.Iface
iface, _ = strings.CutPrefix(iface, "Alleged: ")
iface, _ = strings.CutSuffix(iface, " brand")
}
return map[string]interface{}{"id": r.Id, "allegedName": iface}
}
Expand All @@ -177,6 +177,9 @@ func (k Querier) CapData(c context.Context, req *types.QueryCapDataRequest) (*ty
Bigint: capdataBigintToDigits,
}

// A response Value is "<prefix><separator-joined items><suffix>".
prefix, separator, suffix := "", "\n", ""

// Read options.
mediaType, ok := capDataResponseMediaTypes[req.MediaType]
if !ok {
Expand All @@ -186,14 +189,12 @@ func (k Querier) CapData(c context.Context, req *types.QueryCapDataRequest) (*ty
if !ok {
return nil, status.Error(codes.InvalidArgument, "invalid item_format")
}
remotableFormat, ok := capDataRemotableValueFormats[req.RemotableValueFormat]
if !ok {
switch remotableFormat, ok := capDataRemotableValueFormats[req.RemotableValueFormat]; {
case !ok:
return nil, status.Error(codes.InvalidArgument, "invalid remotable_value_format")
}
switch remotableFormat {
case FormatRemotableAsObject:
case remotableFormat == FormatRemotableAsObject:
valueTransformations.Remotable = capdataRemotableToObject
case FormatRemotableAsString:
case remotableFormat == FormatRemotableAsString:
valueTransformations.Remotable = capdataRemotableToString
}

Expand Down Expand Up @@ -238,7 +239,7 @@ func (k Querier) CapData(c context.Context, req *types.QueryCapDataRequest) (*ty

return &types.QueryCapDataResponse{
BlockHeight: cell.BlockHeight,
Value: strings.Join(responseItems, "\n"),
Value: prefix + strings.Join(responseItems, separator) + suffix,
}, nil
}

Expand Down
20 changes: 10 additions & 10 deletions golang/cosmos/x/vstorage/keeper/keeper_grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ func TestCapData(t *testing.T) {
// and deriving expectations from marshalling to avoid spurious mismatches
// from Go's unpredictable field ordering (e.g., `{"a":0,"b":1}` vs. `{"b":1,"a":0}`).
slots := []any{"a"}
deepSmallcapsBody := `{"arr":[{"bigint":"+42","remotable":"$0.Foo","ref2":"$0"}]}`
deepSmallcapsBody := `{"arr":[{"bigint":"+42","remotable":"$0.Alleged: Foo brand","ref2":"$0"}]}`
deepLegacyBody := deepSmallcapsBody
legacyFromSmallcaps := [][2]string{
[2]string{`"+42"`, `{"@qclass":"bigint","digits":"42"}`},
[2]string{`"$0.Foo"`, `{"@qclass":"slot","index":0,"iface":"Foo"}`},
[2]string{`"$0.Alleged: Foo brand"`, `{"@qclass":"slot","index":0,"iface":"Alleged: Foo brand"}`},
[2]string{`"$0"`, `{"@qclass":"slot","index":0}`},
}
for _, pair := range legacyFromSmallcaps {
Expand All @@ -183,8 +183,8 @@ func TestCapData(t *testing.T) {
"arr": []any{
map[string]any{
"bigint": "42",
"remotable": "[Foo {}]",
"ref2": "[Foo {}]",
"remotable": "[Alleged: Foo brand <a>]",
"ref2": "[Alleged: Foo brand <a>]",
},
},
}),
Expand All @@ -197,8 +197,8 @@ func TestCapData(t *testing.T) {
BlockHeight: "1",
Value: mustMarshalTwoLines(map[string]any{
"arr-0-bigint": "42",
"arr-0-remotable": "[Foo {}]",
"arr-0-ref2": "[Foo {}]",
"arr-0-remotable": "[Alleged: Foo brand <a>]",
"arr-0-ref2": "[Alleged: Foo brand <a>]",
}),
},
})
Expand All @@ -211,8 +211,8 @@ func TestCapData(t *testing.T) {
"arr": []any{
map[string]any{
"bigint": "42",
"remotable": map[string]any{"id": "a", "allegedName": "Foo"},
"ref2": map[string]any{"id": "a", "allegedName": "Foo"},
"remotable": map[string]any{"id": "a", "allegedName": "Foo brand"},
"ref2": map[string]any{"id": "a", "allegedName": "Foo brand"},
},
},
}),
Expand All @@ -226,9 +226,9 @@ func TestCapData(t *testing.T) {
Value: mustMarshalTwoLines(map[string]any{
"arr-0-bigint": "42",
"arr-0-remotable-id": "a",
"arr-0-remotable-allegedName": "Foo",
"arr-0-remotable-allegedName": "Foo brand",
"arr-0-ref2-id": "a",
"arr-0-ref2-allegedName": "Foo",
"arr-0-ref2-allegedName": "Foo brand",
}),
},
})
Expand Down

0 comments on commit 47defd4

Please sign in to comment.