Skip to content

Required oneof#523

Merged
dmdashenkov merged 15 commits intomasterfrom
required-oneof
Apr 6, 2020
Merged

Required oneof#523
dmdashenkov merged 15 commits intomasterfrom
required-oneof

Conversation

@dmdashenkov
Copy link
Contributor

@dmdashenkov dmdashenkov commented Apr 3, 2020

In this PR we add a new validation option: (is_required) for oneof group fields.

Previously, to make a oneof "required" (i.e. to enforce that one field is always set), the users had to write a (required_field) expression:

message Meal {
    option (required_field) = "fish | meat | veggies";

    oneof choice {
        Fish fish = 1;
        Meat meat = 2;
        Veggitables veggies = 3;
    }
}

This approach is error-prone, as the field names may be misspelled, the fields may be added or deleted, etc.

Now, the users can mark the oneof group with a (is_required) option:

message Meal {
    oneof choice {
        option (is_required) = true;

        Fish fish = 1;
        Meat meat = 2;
        Veggitables veggies = 3;
    }
}

The second snippet is equivalent to the first one but has less room for accidental errors.

Fixes #521.

@dmdashenkov dmdashenkov self-assigned this Apr 3, 2020
@codecov
Copy link

codecov bot commented Apr 3, 2020

Codecov Report

Merging #523 into master will increase coverage by 0.25%.
The diff coverage is 100.00%.

@@             Coverage Diff              @@
##             master     #523      +/-   ##
============================================
+ Coverage     73.90%   74.16%   +0.25%     
- Complexity     2912     2935      +23     
============================================
  Files           500      502       +2     
  Lines         11747    11787      +40     
  Branches        658      661       +3     
============================================
+ Hits           8682     8742      +60     
+ Misses         2841     2823      -18     
+ Partials        224      222       -2     

@dmdashenkov dmdashenkov marked this pull request as ready for review April 3, 2020 16:39
@dmdashenkov
Copy link
Contributor Author

@armiol, @alexander-yevsyukov, PTAL.

*/

package io.spine.code.gen.java;
package io.spine.type;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we change the package?

Copy link
Contributor

Choose a reason for hiding this comment

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

Currently the type package contains only few classes that deal with known types.

Copy link
Contributor

@alexander-yevsyukov alexander-yevsyukov left a comment

Choose a reason for hiding this comment

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

There's only one issue related to the changed package. Please see my question.

*/

package io.spine.code.gen.java;
package io.spine.type;
Copy link
Contributor

Choose a reason for hiding this comment

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

The same question regarding the package as for the class under the test. Why do change the package?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We move OneofDeclaration from another module. I've changed the package to a more appropriate io.spine.code.proto, which is also the package where the FieldDeclaration resides.

@dmdashenkov
Copy link
Contributor Author

@alexander-yevsyukov, PTAL again.

Copy link
Contributor

@alexander-yevsyukov alexander-yevsyukov left a comment

Choose a reason for hiding this comment

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

LGTM

@dmdashenkov dmdashenkov merged commit 963d21c into master Apr 6, 2020
@dmdashenkov dmdashenkov deleted the required-oneof branch April 6, 2020 10:24
@dmitrykuzmin dmitrykuzmin mentioned this pull request Sep 8, 2020
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.

Allow requiring oneof values

2 participants