Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(textual): Remove msg header screen #15208

Merged
merged 17 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions x/tx/textual/any.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func NewAnyValueRenderer(t *SignModeHandler) ValueRenderer {
// Format implements the ValueRenderer interface.
func (ar anyValueRenderer) Format(ctx context.Context, v protoreflect.Value) ([]Screen, error) {
msg := v.Message().Interface()
omitHeader := 0

anymsg, ok := msg.(*anypb.Any)
if !ok {
return nil, fmt.Errorf("expected Any, got %T", msg)
Expand All @@ -45,10 +47,16 @@ func (ar anyValueRenderer) Format(ctx context.Context, v protoreflect.Value) ([]
return nil, err
}

screens := make([]Screen, 1+len(subscreens))
// The Any value renderer suppresses emission of the object header
_, isMsgRender := vr.(*messageValueRenderer)
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
if isMsgRender && subscreens[0].Content == fmt.Sprintf("%s object", internalMsg.ProtoReflect().Descriptor().Name()) {
omitHeader = 1
}

screens := make([]Screen, (1-omitHeader)+len(subscreens))
screens[0].Content = anymsg.GetTypeUrl()
for i, subscreen := range subscreens {
subscreen.Indent++
for i, subscreen := range subscreens[omitHeader:] {
subscreen.Indent += 1 - omitHeader
screens[i+1] = subscreen
}

Expand Down Expand Up @@ -82,6 +90,16 @@ func (ar anyValueRenderer) Parse(ctx context.Context, screens []Screen) (protore
subscreens[i-1].Indent--
}

// Prepend with a "%s object" if the message goes through the default
// messageValueRenderer.
if _, found := ar.tr.messages[msgType.Descriptor().FullName()]; !found {
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
for i := range subscreens {
subscreens[i].Indent++
}

subscreens = append([]Screen{{Content: fmt.Sprintf("%s object", msgType.Descriptor().Name())}}, subscreens...)
}

internalMsg, err := vr.Parse(ctx, subscreens)
if err != nil {
return nilValue, err
Expand Down
18 changes: 6 additions & 12 deletions x/tx/textual/internal/testdata/any.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"@type": "/Foo"
},
"screens": [
{"content": "/Foo"},
{"content": "Foo object", "indent": 1}
{"content": "/Foo"}
]
},
{
Expand All @@ -15,8 +14,7 @@
},
"screens": [
{"content": "/Foo"},
{"content": "Foo object", "indent": 1},
{"title": "Full name", "content": "testing", "indent": 2}
{"title": "Full name", "content": "testing", "indent": 1}
]
},
{
Expand All @@ -38,8 +36,7 @@
},
"screens": [
{"content": "/google.protobuf.Any"},
{"content": "/Foo", "indent": 1},
{"content": "Foo object", "indent": 2}
{"content": "/Foo", "indent": 1}
]
},
{
Expand All @@ -53,8 +50,7 @@
"screens": [
{"content": "/google.protobuf.Any"},
{"content": "/Foo", "indent": 1},
{"content": "Foo object", "indent": 2},
{"title": "Full name", "content": "testing", "indent": 3}
{"title": "Full name", "content": "testing", "indent": 2}
]
},
{
Expand All @@ -67,10 +63,8 @@
},
"screens": [
{"content": "/A"},
{"content": "A object", "indent": 1},
{"title": "ANY", "content": "/Foo", "indent": 2},
{"content": "Foo object", "indent": 3},
{"title": "Full name", "content": "testing", "indent": 4}
{"title": "ANY", "content": "/Foo", "indent": 1},
{"title": "Full name", "content": "testing", "indent": 2}
]
}
]
Loading