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

RSTUDIO-539: Better visualization for mixed and dictionaries #1663

Merged
merged 21 commits into from
Jun 12, 2024

Conversation

gagik
Copy link
Collaborator

@gagik gagik commented Jun 11, 2024

Closes #1662.

After:

Screenshot 2024-06-11 at 17 01 31 Screenshot 2024-06-11 at 17 01 51

Before:

Screenshot 2024-06-11 at 16 59 31 Screenshot 2024-06-11 at 16 59 38

Data Used

class Car extends Realm.Object {
  static schema = {
    name: "Car",
    properties: {
      car: "Car?",
      mixedField: "mixedField",
      hashMapField: "mixed{}",
    },
  };
}
...
realm.write(() => {
  realm.deleteAll();

  new Car(realm, { mixedField: [[123]], car: null });
  const car = new Car(realm, {
    mixedField: [],
    car: null,
  });
  new Car(realm, {
    mixedField: [[123]],
    car: null,
    hashMapField: { test: "nice", a: { b: "whatever" }, c: [1, 2, 3, {}] },
  });
  new Car(realm, { mixedField: [[123]], car: null });

  new Car(realm, { mixedField: [[123], car], car });
});

// If it is possible to serialize the object to a simpler structure with toJSON, do it.
const simplifiedObject =
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(object as any).toJSON != null ? (object as any).toJSON() : object;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this is a bit hacky but helps clean things up... mainly symbols appearing in the output.

Copy link
Member

Choose a reason for hiding this comment

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

I'd be interested to learn what symbols are appearing, we might want to fix that by ensuring these aren't defined as enumerable by the SDK.

Copy link
Member

Choose a reason for hiding this comment

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

If you're interested in a less hack'y way for doing this:

const simplifiedObject = object && typeof object === "object" && "toJSON" in object && typeof object.toJSON === "function";

https://www.typescriptlang.org/play/?#code/CYUwxgNghgTiAEYD2A7AzgF3kgRgK3AwC54BXFAaxSQHcUBuAKEeXSzQEsBbABwg4BmHEMADy+QvAC82CWCwAyBfAwBPHiCQDZBedKkyARLl0ZD8JfEMYkAKQDKogHLmOKHZMtqNWj-IB0Ng7O+kYC5PIcqIZMQA

@gagik gagik requested review from kneth and kraenhansen June 11, 2024 15:36
@@ -1,6 +1,5 @@
import { display as displayDataCell } from '../ui/RealmBrowser/Content/Table/types/DataCell';
import { stringify } from 'flatted';

import { InspectOptions, inspect } from 'util';
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
import { InspectOptions, inspect } from 'util';
import { InspectOptions, inspect } from 'node:util';

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Is there an advantage to this import over the other, is it to make it explicit this is from part of node? We actually also import util directly in an earlier file (which I chose to leave untouched to avoid unnecessary side effects) so I could also change it there

import util from 'util';

Copy link
Member

Choose a reason for hiding this comment

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

Is there an advantage to this import over the other, is it to make it explicit this is from part of node?

Yes. The most obvious is to avoid loading a node_modules/util which might have been installed by some mis-behaving dependency.

We actually also import util directly in an earlier file

It's most likely because it predates the introduction of the node: prefix for the built-in modules.

Copy link
Contributor

@kneth kneth left a comment

Choose a reason for hiding this comment

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

It is more human readable.

@gagik gagik merged commit e4ca01b into channel/major-15 Jun 12, 2024
3 checks passed
@gagik gagik deleted the gagik/visualize-json-better branch June 12, 2024 11:21
Copy link
Member

@kraenhansen kraenhansen left a comment

Choose a reason for hiding this comment

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

👍

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Better visualization for mixed and dictionaries
3 participants