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

traits: ability to define what optional means #146

Closed
michael-brade opened this issue Sep 17, 2023 · 7 comments
Closed

traits: ability to define what optional means #146

michael-brade opened this issue Sep 17, 2023 · 7 comments
Assignees
Labels

Comments

@michael-brade
Copy link

Hi, here is another idea: right now there the possibility to define what "nothing" means by implementing is_nothing in a trait. However, this is applied to both TAO_JSON_BIND_REQUIRED and TAO_JSON_BIND_OPTIONAL. Would it be possible to limit it to TAO_JSON_BIND_OPTIONAL? Or is it possible to find another way to define what it means for a value to be optional? Sometimes I want to serialize a false value and sometimes I don't (need to). But I can only implement one is_nothing for bool.

@ColinH ColinH self-assigned this Sep 17, 2023
@ColinH
Copy link
Member

ColinH commented Sep 17, 2023

Am I right to assume that working with std::optional< bool > is not a viable work-around?

@michael-brade
Copy link
Author

yes because that is an API change and it wouldn't be true. The object always has the bool (or int or string for that matter), it's just about how to serialize this object to json.

@ColinH
Copy link
Member

ColinH commented Sep 18, 2023

After checking the source I believe that is_nothing should never have been applied to non-optional members in the first place.

@ColinH ColinH added bug and removed enhancement labels Sep 18, 2023
@ColinH ColinH closed this as completed in 32f4d55 Sep 18, 2023
@ColinH
Copy link
Member

ColinH commented Sep 18, 2023

Now I'm wondering whether we still need for_nothing_value.

@michael-brade
Copy link
Author

Great, I agree with that so far.

Yes, I like for_nothing_value because then I can at least somewhat individually decide if I want the 0/false/empty values in a json representation or not.

@ColinH
Copy link
Member

ColinH commented Sep 19, 2023

After the most recent change binding::object encodes a member when the object uses for_nothing_value::encode and/or the member uses member_kind::required and/or the member's is_nothing_value is false.

Consequently for_nothing_value does not influence the behaviour of required members. That sounds reasonable and I'm prone to believe that it can stay like this. @michael-brade Do you agree, does this work for your use cases?

template< typename A, template< typename... > class Traits, typename C >
[[nodiscard]] static bool require_encode( const C& x )
{
   return ( N == for_nothing_value::encode ) || ( A::kind == member_kind::required ) || ( !A::template is_nothing< Traits >( x ) );
}

@michael-brade
Copy link
Author

Yes, I agree and the logic is very good. Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants