-
Notifications
You must be signed in to change notification settings - Fork 37
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
add enums type inference in NativeSerializer and fix Enum valueof() #300
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nvergez, thanks a lot for the contribution 🎉
Code looks great 🌟
This changes the behavior of enum.valueOf()
. Indeed, the previous behavior wasn't correct (as you have said, it should have been an object, instead). I'll analyze this a bit more, then come back with a re-review.
In the meantime, do you think you can solve the Git conflicts?
@@ -149,7 +149,7 @@ export class EnumValue extends TypedValue { | |||
valueOf() { | |||
let result: any = { name: this.name, fields: [] }; | |||
|
|||
this.fields.forEach((field) => (result.fields[field.name] = field.value.valueOf())); | |||
this.fields.forEach((field, index) => (result.fields[index] = field.value.valueOf())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll have to analyze this a bit more, then come back.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned by you in the description, the existing behavior is not correct (at least, not practical) and we should change it to return fields
as an object, in the future (major release).
The change in this PR is not breaking 👍
@nvergez, do you think you can also add the following unit test in The first two assertions should normally hold with your changes, as well. Then, the third one would have to be adjusted, since your code uses indexes to populate Indeed, it seems that your code did not change the existing (incorrect) behavior of
In v13, we should definitely have |
Resolving the conflict will cause a test to fail in this PR because it will add the test from I'm unsure whether you would prefer to merge main into this branch, or just resolve the conflict and deal with a failing test. |
@nvergez, normally, the What test would fail? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! 🎉
@@ -149,7 +149,7 @@ export class EnumValue extends TypedValue { | |||
valueOf() { | |||
let result: any = { name: this.name, fields: [] }; | |||
|
|||
this.fields.forEach((field) => (result.fields[field.name] = field.value.valueOf())); | |||
this.fields.forEach((field, index) => (result.fields[index] = field.value.valueOf())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned by you in the description, the existing behavior is not correct (at least, not practical) and we should change it to return fields
as an object, in the future (major release).
The change in this PR is not breaking 👍
@nvergez, released here: https://github.com/multiversx/mx-sdk-js-core/releases/tag/v12.6.0 Thanks a lot for the contribution! |
I have implemented type inference for enum types in the NativeSerializer.
{ name: string; fields: any}
The key of each property in the
fields
object must correspond to the name in theFieldDefinition
.I have also updated the
valueof()
method ofEnumValue
. When we receive an ABI file from a smart contract built with complex enums, the names of the fields are the same as those in the Rust code. This means they can be either numbers or strings.This indicates that the following code cannot work correctly with a string as a name:
mx-sdk-js-core/src/smartcontracts/typesystem/enum.ts
Lines 150 to 152 in 30f4fbf
I believe the fields property should be an object rather than an array, but this change would definitely lead to a breaking change. However, I think my fix doesn't break anything by using the index instead of the name itself.