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

#feat: Add value or absent option #449

Merged
merged 5 commits into from
Feb 23, 2024

Conversation

LiLatee
Copy link
Contributor

@LiLatee LiLatee commented Feb 20, 2024

Hey! 👋
I've been lacking the option to send a value if it exists and not send any value (neither null) if not present.

EDIT: Converted to DRAFT, because it's not working yet.

@LiLatee LiLatee marked this pull request as draft February 20, 2024 16:24
@knaeckeKami
Copy link
Collaborator

knaeckeKami commented Feb 20, 2024

Why do you need a separate class for that?
Adding a new subtype to a sealed class is a breaking change.

And there are no new states - a value is either present or absent. we don't need a separate type for this, this could be a static method or factory, couldn't it?

factory Value.ofNullabe(T? t) {
   if(t == null} { 
      return Value.absent();
   }
   return Value.present(t);
}

@LiLatee
Copy link
Contributor Author

LiLatee commented Feb 20, 2024

Adding a new subtype to a sealed class is a breaking change.

Yes, I've noticed that 😅
So you are right, it's better to just add a factory. Seems to work fine in my case 👌 Idk why I complicated it so much 😞
Thanks!

@LiLatee LiLatee marked this pull request as ready for review February 20, 2024 17:15
group(
'requireValue',
() {
test('on $AbsentValue throws', () {
Copy link
Collaborator

Choose a reason for hiding this comment

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

please remove the $ from the test strings.

I like static test names better if possible, dynamic ones make filtering by test name harder, and the test names generally become weird:

Screenshot 2024-02-20 at 17 51 10

Copy link
Collaborator

@knaeckeKami knaeckeKami Feb 20, 2024

Choose a reason for hiding this comment

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

Screenshot 2024-02-20 at 17 52 59

Also breaks IntellIj "rerun test" feature

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For sure ${Value.ofNullable} should not be there. I forgot to check how it looks like in logs.
But personally I like to use e.g. $AbsentValue, because it's very helpful when refactoring the code. You don't have to remember about every place in the code where you have just a string with the class name.
But I changed all the names to static ones 👌

/// If the value is null. It will not be serialized.
factory Value.ofNullable(T? value) {
if (value == null) {
return AbsentValue();
Copy link
Collaborator

Choose a reason for hiding this comment

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

can this be const?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It should be... My mistake. I am used to having a lint prefer_const_constructors 😅 Thanks 🙏

Copy link
Collaborator

Choose a reason for hiding this comment

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

yeah I know, gql_pedantic should be modernized at some point ;)

@knaeckeKami
Copy link
Collaborator

final nit:

Thinking about it, I think .ofNullable is not a great name for the behavior "present if not null, absent otherwise".

What about Value.absentWhenNull() ? of can you think of something more descriptive?

@LiLatee
Copy link
Contributor Author

LiLatee commented Feb 20, 2024

Regarding the name I was thinking about Value.valueOrAbsent, but I have to admit that your absentWhenNull looks better 👌 Changed

@knaeckeKami
Copy link
Collaborator

thanks! lgtm

@knaeckeKami knaeckeKami merged commit 44c3686 into gql-dart:master Feb 23, 2024
20 checks passed
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