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

ref(types): Loosen tag types, create new Primitive type #3108

Merged
merged 6 commits into from
Dec 16, 2020

Conversation

lobsterkatie
Copy link
Member

@lobsterkatie lobsterkatie commented Dec 8, 2020

The only thing we do with tags is serialize/JSONify them, and any primitive* can handle that. And since we do that, relay isn't an issue, because all it ever sees from us is strings anyway.

This loosens the type requirements for tags to include all members of a new union type called Primitive. It also adds a note to the setTag docstring pointing out that a tag can be unset by passing undefined. Finally, it refactors isPrimitive() into a type guard, and uses that type guard where possible.

Doesn't fully answer the request in #2218, but at least it will stop people's linters from freaking out when they pass undefined, as we ourselves suggest.

*Actually, symbols apparently don't let themselves be automatically cast to their string representation (EDIT: apparently BigInts don't either), so those are handled separately, in the normalizeValue function.

@github-actions
Copy link
Contributor

github-actions bot commented Dec 8, 2020

size-limit report

Path Size
@sentry/browser - CDN Bundle (gzipped) 19.97 KB (+0.17% 🔺)
@sentry/browser - Webpack 20.8 KB (+0.19% 🔺)
@sentry/react - Webpack 20.8 KB (+0.19% 🔺)
@sentry/browser + @sentry/tracing - CDN Bundle (gzipped) 27.12 KB (+0.13% 🔺)

@kamilogorek
Copy link
Contributor

kamilogorek commented Dec 8, 2020

How about we create Primitive type instead and use it here? (we'll then be able to use it in isPrimitive util with is Primitive user type guard once we upgrade our TypeScript version)

https://www.typescriptlang.org/docs/handbook/basic-types.html#object

type Primititve = number | string | boolean | bigint | symbol | null | undefined;
type NonPrimitive = object;

@lobsterkatie
Copy link
Member Author

lobsterkatie commented Dec 8, 2020

How about we create Primitive type instead and use it here? (we'll then be able to use it in isPrimitive util with is Primitive user type guard once we upgrade our TypeScript version)

typescriptlang.org/docs/handbook/basic-types.html#object

type Primititve = number | string | boolean | bigint | symbol | null | undefined;
type NonPrimitive = object;

Oooh, I like it. Good idea.

UPDATE: Done.

MIGRATION.md Outdated
`Scope`.

```typescript
setTags(tags: { [key: string]: string }): this;
setTags(tags: { [key: string]: string | number | boolean | bigint| symbol| null | undefined }): this;
Copy link
Member Author

Choose a reason for hiding this comment

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

I listed them out here in the migration doc for ease of reading, but used the Primitive type everywhere in the code.

@lobsterkatie lobsterkatie changed the title ref(types): Loosen tag types ref(types): Loosen tag types, create new Primitive type Dec 8, 2020
@lobsterkatie lobsterkatie force-pushed the kmclb-loosen-tag-types branch 2 times, most recently from b833312 to 3d4c98f Compare December 14, 2020 21:07
packages/browser/src/integrations/globalhandlers.ts Outdated Show resolved Hide resolved
@@ -119,7 +119,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
let eventId: string | undefined = hint && hint.event_id;

const promisedEvent = isPrimitive(message)
? this._getBackend().eventFromMessage(`${message}`, level, hint)
? this._getBackend().eventFromMessage(String(message), level, hint)
Copy link
Member

Choose a reason for hiding this comment

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

I wonder why the change here?

Copy link
Member Author

@lobsterkatie lobsterkatie Dec 15, 2020

Choose a reason for hiding this comment

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

Because symbols and bigints can't be automatically coerced to string form (see links in PR description) - you have to do it manually with String(x).

packages/types/src/misc.ts Outdated Show resolved Hide resolved
@lobsterkatie lobsterkatie force-pushed the kmclb-loosen-tag-types branch from 6d06745 to d1355bd Compare December 15, 2020 18:01
@HazAT HazAT merged commit 1fb0b4c into master Dec 16, 2020
@HazAT HazAT deleted the kmclb-loosen-tag-types branch December 16, 2020 10:45
chrisbobbe added a commit to chrisbobbe/zulip-mobile that referenced this pull request Feb 22, 2021
One thing we'd like to have is the merged PR
getsentry/sentry-javascript#3108, a type adjustment that lets us use
`undefined` as a value in `setTag` and `setTags`. The author of that
PR says that passing `undefined` for a tag's value is an unofficial
way to unset a tag from the scope. If that works, we'd like to do it
until getsentry/sentry-javascript#2218 is solved more officially.

I see just one declared breaking change at 2.0.0 [1]:

"""
- build(android): Changes android package name from
  `io.sentry.RNSentryPackage` to `io.sentry.react.RNSentryPackage`
  (Breaking). zulip#1131
"""

But autolinking handles the name change just fine, so there's
nothing special we have to do about that.

We don't have a complete libdef yet, but we seem to have good
coverage for our usage in what we've translated from the TypeScript
manually so far. So, double-check all of that against the new
version and fix a few things that were slightly wrong or out-of-date
(including by taking the relevant changes from
getsentry/sentry-javascript#3108).

[1] https://github.com/getsentry/sentry-react-native/blob/master/CHANGELOG.md#200
chrisbobbe added a commit to chrisbobbe/zulip-mobile that referenced this pull request Feb 22, 2021
…sent.

Instead of omitting them.

We're about to start keeping these tags on the global scope, which
means we have to listen to and propagate all changes in the server
version, including changes where the server version disappears. It's
been reported [1] that passing `undefined` as a tag's value in
`setTags` will clear the tag from the current scope, and I've
verified this experimentally.

[1] getsentry/sentry-javascript#3108 (comment)
chrisbobbe added a commit to chrisbobbe/zulip-mobile that referenced this pull request Feb 22, 2021
…sent.

Instead of omitting them.

We're about to start keeping these tags on the global scope, which
means we have to listen to and propagate all changes in the server
version, including changes where the server version disappears. It's
been reported [1] that passing `undefined` as a tag's value in
`setTags` will clear the tag from the current scope, and I've
verified this experimentally.

[1] getsentry/sentry-javascript#3108 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants