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

Add aliases to JsonValue to enum value to be decoded from different JSON values #1459

Open
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

mateusfccp
Copy link

@mateusfccp mateusfccp commented Nov 26, 2024

Solves #1380.

Adds a parameter to JsonValue called aliases, which accepts a list of values that can be alternatively used to decode a JSON.

Example:

enum StatusCode {
  @JsonValue(200, aliases: [201, 202])
  success,
  @JsonValue(301)
  movedPermanently,
  @JsonValue(302)
  found,
  @JsonValue(500)
  internalServerError,
}

With the code above, all of 200, 201 and 202 would be decoded as StatusCode.success.

When calling toJson, however, the value parameter is used. In this case, 200.

Notes

  • The $enumDecode and $enumDecodeNullable were not changed because they are public and there are packages that depend on them. Instead, $enumDecodeWithDecodeMap and $enumDecodeNullableWithDecodeMap were introduced. We may want to deprecate the former at some point and remove them in a major version.

@kevmoo
Copy link
Collaborator

kevmoo commented Dec 2, 2024

Ideally we'd have a couple of tests to validate the failure cases, but I get that those can be tricky to add.

@mateusfccp
Copy link
Author

@kevmoo What would be the failure cases for this PR? Can you give an example?

@kevmoo
Copy link
Collaborator

kevmoo commented Dec 2, 2024

@kevmoo What would be the failure cases for this PR? Can you give an example?

Unsupported types in the set is the big one.
null isn't supported, right?
Those types of things.

@mateusfccp
Copy link
Author

mateusfccp commented Dec 2, 2024

@kevmoo What would be the failure cases for this PR? Can you give an example?

Unsupported types in the set is the big one. null isn't supported, right? Those types of things.

Ah, yes, the idea is to mimic value, so until we have proper union types in Dart, we use Object? but should only accept String and int.

I am going to work on this test.

@kevmoo
Copy link
Collaborator

kevmoo commented Dec 17, 2024

You'll need to rebase this fix.

@mateusfccp
Copy link
Author

You'll need to rebase this fix.

I rebased to master and included the requested test.

In my local machine, a test was failing, but I didn't change anything related to it (the *ToJson generator).

image

@mateusfccp
Copy link
Author

Mmm, I had made a regular merge, but GitHub is saying to me that "this branch must not contain merge commits", so I rewrote the commits using a rebase instead, but it is still giving me this message.

@mateusfccp
Copy link
Author

mateusfccp commented Dec 18, 2024

Well, the test that did not pass locally passed here, so we are good with it.

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.

2 participants