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

js/console: Marshal by default native JS objects #2375

Merged
merged 1 commit into from
Apr 5, 2022

Conversation

codebien
Copy link
Collaborator

@codebien codebien commented Feb 4, 2022

Closes #1146

⚠️ BREAKING CHANGE: After this PR will be merged, all js/console methods like console.log and console.info, in the case a JavaScript Object (or an Array) is passed as an argument then it is converted into a string encoded as a JSON object.

Objects

export default function () {
  var user = [{}]

  console.log("the user", user) 
  // before: the user [object Object]
  // after: the user {"name":"Bob"}
}

Arrays

export default function () {
  var user = {name: "Bob"}

  console.log([1, 2, "mytest", {user: "Bob"}]) 
  // before: 1,2,mytest,[object Object]
  // after: [1,2,"mytest",{"user":"Bob"}]
}

The previous way can be restored by directly parsing the object to a string using the String function.

export default function () {
  var user = {name: "Bob"}
  console.log(String(user)) 
}

@codebien codebien self-assigned this Feb 4, 2022
@codebien codebien added this to the v0.37.0 milestone Feb 4, 2022
js/console.go Outdated Show resolved Hide resolved
@mstoykov
Copy link
Collaborator

mstoykov commented Feb 4, 2022

@codebien does this still happen with your code #644 (comment)

@@ -134,7 +134,8 @@ func TestConsole(t *testing.T) {
`"string"`: {Message: "string", Data: logrus.Fields{"source": "console"}},
`"string","a","b"`: {Message: "string a b", Data: logrus.Fields{"source": "console"}},
`"string",1,2`: {Message: "string 1 2", Data: logrus.Fields{"source": "console"}},
`{}`: {Message: "[object Object]", Data: logrus.Fields{"source": "console"}},
`{}`: {Message: "{}", Data: logrus.Fields{"source": "console"}},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried the branch locally, and I think I've spotted some missing supported cases? Namely, it seems that as of this PR's state, JSON arrays are not properly supported, nor are arrays with mixed type values. Moreover, console.log involving formatting with the backtick notation doesn't work as I anticipated either: console.log(\{"abc": 123}`), still prints [object Object]`.
It might not be in scope for this PR, but I wanted to double-check with you to make sure these points don't go unnoticed 👀 🙇🏻

Here's a little test I wrote locally to experiment with it

export default function() {
  let emptyObjectBody = JSON.parse(`{}`);
  console.log(`Want: ${JSON.stringify(emptyObjectBody)}`);
  // console.log(`Got: ${emptyObjectBody}`);
  console.log(emptyObjectBody);

  let objectBody = JSON.parse(`{ "abc": 123, "easy as": "do re mi" }`);
  console.log(`Want: ${JSON.stringify(objectBody)}`);
  // console.log(`Got: ${objectBody}`);
  console.log(objectBody);

  let emptyArrayBody = JSON.parse(`[]`);
  console.log(`Want: ${JSON.stringify(emptyArrayBody)}`);
  // console.log(`Got: ${emptyArrayBody}`);
  console.log(emptyArrayBody);

  let arrayBody = JSON.parse(`[1, 2, 3]`);
  console.log(`Want: ${JSON.stringify(arrayBody)}`);
  // console.log(`Got: ${arrayBody}`);
  console.log(arrayBody);

  let mixedArrayBody = JSON.parse(`["abc", 123, {"foo": "bar"}, [1, 2, 3]]`);
  console.log(`Want: ${JSON.stringify(mixedArrayBody)}`);
  // console.log(`Got: ${mixedArrayBody}`);
  console.log(mixedArrayBody);
}

outputing this:

INFO[0000] Want: {}                                      source=console
INFO[0000] {}                                            source=console
INFO[0000] Want: {"abc":123,"easy as":"do re mi"}        source=console
INFO[0000] {"abc":123,"easy as":"do re mi"}              source=console
INFO[0000] Want: []                                      source=console
INFO[0000]                                               source=console
INFO[0000] Want: [1,2,3]                                 source=console
INFO[0000] 1,2,3                                         source=console
INFO[0000] Want: ["abc",123,{"foo":"bar"},[1,2,3]]       source=console
INFO[0000] abc,123,[object Object],1,2,3                 source=console

As a result, I would probably add tests cases covering these scenarios 🙇🏻 What do you think?

@na-- na-- modified the milestones: v0.37.0, v0.38.0 Feb 28, 2022
@codebien codebien force-pushed the console-default-json branch 2 times, most recently from 3c02d50 to a8f8a1a Compare March 4, 2022 11:37
@codebien
Copy link
Collaborator Author

codebien commented Mar 4, 2022

I applied some changes, @oleiade you're cases should be supported now.

I dropped the msg argument, it didn't seem necessary, let me know if you see a case to maintain it.

msg goja.Value, args ...goja.Value)

@codebien does this still happen with your code #644 (comment)

@mstoykov Do you mean the case of a circular object printed as [object Object]? JSON.stringify doesn't support that case. In the case of browsers and runtimes the console seems to have a dedicated spec for formatting the outputs.

olegbespalov
olegbespalov previously approved these changes Mar 7, 2022
Copy link
Collaborator

@olegbespalov olegbespalov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 LGTM left only one non-blocking comment

js/console.go Show resolved Hide resolved
oleiade
oleiade previously approved these changes Mar 7, 2022
Copy link
Member

@oleiade oleiade left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 👍🏻 🌮

mstoykov
mstoykov previously approved these changes Mar 23, 2022
Copy link
Collaborator

@mstoykov mstoykov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Can you please explain how this is breaking change in the PR description. As well as point out how to get the previously reported values when possible.

js/console.go Outdated Show resolved Hide resolved
@codebien
Copy link
Collaborator Author

@mstoykov @oleiade @olegbespalov I updated the description with changes and added a check in case of an empty argument list (e.g console.log()).

oleiade
oleiade previously approved these changes Mar 29, 2022
Copy link
Member

@oleiade oleiade left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

@olegbespalov olegbespalov added the breaking change for PRs that need to be mentioned in the breaking changes section of the release notes label Mar 29, 2022
olegbespalov
olegbespalov previously approved these changes Mar 29, 2022
Copy link
Collaborator

@olegbespalov olegbespalov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment on lines 162 to 165
if tt.expected == "" {
assert.Nil(t, entry, "no logs expected")
return
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It likely should just log an empty string (?) this is what firefox does and IMO will be less confusing 🤔

@codebien codebien dismissed stale reviews from olegbespalov and oleiade via 61496d9 April 4, 2022 16:53
@codebien codebien force-pushed the console-default-json branch 2 times, most recently from 61496d9 to bb944f9 Compare April 4, 2022 16:55
@codebien
Copy link
Collaborator Author

codebien commented Apr 4, 2022

The output after the latest change

---script.js
export default function () {
	console.log()
	console.error()
	console.log("")
	console.log("the last one")
}

---output
INFO[0000]                                               source=console
ERRO[0000]                                               source=console
INFO[0000]                                               source=console
INFO[0000] the last one                                  source=console

Copy link
Collaborator

@olegbespalov olegbespalov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💪

@codebien codebien merged commit 2a6b3a5 into master Apr 5, 2022
@codebien codebien deleted the console-default-json branch April 5, 2022 08:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change for PRs that need to be mentioned in the breaking changes section of the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

console.log could show JSON content by default
5 participants