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

Fix array console output #1135

Merged
merged 3 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions common/remote_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,25 @@ func parseConsoleRemoteObjectPreview(logger *log.Logger, op *cdpruntime.ObjectPr
return string(bb)
}

func parseConsoleRemoteArrayPreview(logger *log.Logger, op *cdpruntime.ObjectPreview) string {
arr := make([]any, 0, len(op.Properties))
if op.Overflow {
logger.Warnf("parseConsoleRemoteArrayPreview", "array is too large and will be parsed partially")
}

for _, p := range op.Properties {
val := parseConsoleRemoteObjectValue(logger, p.Type, p.Subtype, p.Value, p.ValuePreview)
arr = append(arr, val)
}

bb, err := json.Marshal(arr)
if err != nil {
logger.Errorf("parseConsoleRemoteArrayPreview", "failed to marshal array to string: %v", err)
}

return string(bb)
}

//nolint:cyclop
func parseConsoleRemoteObjectValue(
logger *log.Logger,
Expand All @@ -268,6 +287,9 @@ func parseConsoleRemoteObjectValue(
}
case cdpruntime.TypeObject:
if op != nil {
if st == "array" {
return parseConsoleRemoteArrayPreview(logger, op)
}
return parseConsoleRemoteObjectPreview(logger, op)
}
if val == "Object" {
Expand Down
4 changes: 2 additions & 2 deletions tests/remote_obj_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestConsoleLogParse(t *testing.T) {
name: "bool", log: "true", want: "true",
},
{
name: "empty_array", log: "[]", want: "{}", // TODO: Improve this output
name: "empty_array", log: "[]", want: "[]",
},
{
name: "empty_object", log: "{}", want: "{}",
Expand All @@ -39,7 +39,7 @@ func TestConsoleLogParse(t *testing.T) {
name: "filled_object", log: `{"foo":{"bar1":"bar2"}}`, want: `{"foo":"Object"}`,
},
{
name: "filled_array", log: `["foo","bar"]`, want: `{"0":"foo","1":"bar"}`,
name: "filled_array", log: `["foo","bar"]`, want: `["foo","bar"]`,
},
{
name: "filled_array", log: `() => true`, want: `function()`,
Expand Down