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

Added overloaded set method for dictionary #5377

Merged
merged 10 commits into from
Feb 13, 2023
Merged

Conversation

papafe
Copy link
Contributor

@papafe papafe commented Feb 6, 2023

This closes #4286

☑️ ToDos

  • 📝 Changelog entry
  • 🚦 Tests

@cla-bot cla-bot bot added the cla: yes label Feb 6, 2023
@papafe
Copy link
Contributor Author

papafe commented Feb 6, 2023

For the reviewers:

  • I've left some TODOs in the code with some questions about things I'm not sure about
  • I tried to avoid duplicating code, but I am not sure mine was the best approach to the overloaded function
  • Not sure if the docs make sense. Especially with the element input, I am not sure how to explain that it can potentially add multiple key-value pairs
  • I will add an entry to the changelog once there is an agreement that at least the method definition makes sense 😁

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.

Minor comments. Great job!

integration-tests/tests/src/tests/dictionary.ts Outdated Show resolved Hide resolved
this.realm.write(() => item.dict.remove("key1").remove("key2"));

expect(Object.keys(item.dict)).deep.equals([]);
//expect(item.dict).deep.equals({}); //TODO Not sure why this does not work
Copy link
Member

Choose a reason for hiding this comment

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

I guess it's because dict has instance methods besides the key/value pairs it might have.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see! Thanks

packages/realm/src/Dictionary.ts Outdated Show resolved Hide resolved
packages/realm/src/Dictionary.ts Outdated Show resolved Hide resolved
packages/realm/src/Dictionary.ts Outdated Show resolved Hide resolved
Comment on lines 265 to 269
const inputObj = typeof elementOrKey == "string" ? { [elementOrKey]: value } : elementOrKey;
for (const [key, val] of Object.entries(inputObj)) {
internal.insertAny(key, toBinding(val, undefined));
}
return this;
Copy link
Member

Choose a reason for hiding this comment

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

Great catch .. I also found this on my other PR - let's see who gets to merge first 😄 (although it shouldn't conflict too much).

@@ -44,7 +44,7 @@ export class ObjectListeners<T> {
});
} catch (err) {
// Scheduling a throw on the event loop,
// since throwing synchroniously here would result in an abort in the calling C++
// since throwing synchronously here would result in an abort in the calling C++
Copy link
Member

Choose a reason for hiding this comment

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

Thanks. Strange I didn't catch these in 789a688 🤔

* @param value The value of the element to add
* @throws {@link AssertionError} If not inside a write transaction or if value violates type constraints
* @returns The dictionary
* @since 10.6.0 //TODO Should also this be 12.0?
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure about this, also considering this is an overloaded method

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe it should be 10.6.0 as we introduced Realm.Dictionary in that version

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.

Great job!

* @param value The value of the element to add
* @throws {@link AssertionError} If not inside a write transaction or if value violates type constraints
* @returns The dictionary
* @since 12.0 //TODO Is this correct?
Copy link
Contributor

Choose a reason for hiding this comment

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

It will be 12.0.0 (or maybe 12.0.0-rc.0)

* @param value The value of the element to add
* @throws {@link AssertionError} If not inside a write transaction or if value violates type constraints
* @returns The dictionary
* @since 10.6.0 //TODO Should also this be 12.0?
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe it should be 10.6.0 as we introduced Realm.Dictionary in that version

* bindgen:
  [bindgen] Migrated `tsm` → `tsx` (#5378)
  [bindgen] Refactor namespaces (#5376)
  Fixed space
  Small correction to docs

# Conflicts:
#	packages/realm/src/Dictionary.ts
@papafe papafe marked this pull request as ready for review February 7, 2023 13:35
Copy link
Contributor

@takameyer takameyer left a comment

Choose a reason for hiding this comment

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

Just some minor things. Otherwise LGTM!

@@ -1,7 +1,7 @@
## vNext (TBD)

### Enhancements
* None
* Add an overload to `Dictionary.set` method that takes two arguments, a `key` and a `value`. ([#4286](https://github.com/realm/realm-js/issues/4286))
Copy link
Contributor

Choose a reason for hiding this comment

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

Could be useful to provide an example of the new syntax in the changelog for anyone reading this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added an example of usage

internal.insertAny(key, toBinding(value, undefined));
const elements = typeof elementsOrKey == "string" ? { [elementsOrKey]: value } : elementsOrKey;
for (const [key, val] of Object.entries(elements)) {
internal.insertAny(key, toBinding(val, undefined));
Copy link
Contributor

Choose a reason for hiding this comment

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

Does undefined need to be explicitly set in toBinding? Any argument not defined will be undefined in its method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. This seems to have been done mostly in this commit: e9ce475#diff-90e9134a0cbc40c4c81b2a112040ea96569d8de35ce930f7e3d2f20521a712e7, maybe it was done to make the parameter explicit?

@papafe papafe merged commit d64f7e3 into bindgen Feb 13, 2023
@papafe papafe deleted the fp/add-set-overload branch February 13, 2023 10:45
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 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.

4 participants