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

In/NotIn and const rules show enum values for enum type #699

Merged
merged 20 commits into from
Jan 10, 2023
Merged

In/NotIn and const rules show enum values for enum type #699

merged 20 commits into from
Jan 10, 2023

Conversation

elb3k
Copy link
Contributor

@elb3k elb3k commented Oct 30, 2022

Problem

The error message for enum in/not_in rules show numbers instead of enum values
Sample case

Input:

...
enum Enum {
    UNKNOWN_ENUM = 0;
    OPTION_1 = 1;
    OPTION_2 = 2;
}

message Book {
    Enum enum1 = 1 [(validate.rules).enum = {in: [1, 2]}];
    Enum enum2 = 2 [(validate.rules).enum = {not_in: [1]}];
    Enum enum3 = 3 [(validate.rules).enum = {const: 1}];
}

Current output:

func (m *Book) validate(all bool) error {
	...
	if _, ok := _Book_Enum1_InLookup[m.GetEnum1()]; !ok {
		err := BookValidationError{
			field:  "Enum1",
			reason: "value must be in list [1 2]",
		}
		...
	}

	if _, ok := _Book_Enum2_NotInLookup[m.GetEnum2()]; ok {
		err := BookValidationError{
			field:  "Enum2",
			reason: "value must not be in list [1]",
		}
		...
	}
        if m.GetEnum3() != 1 {
		err := BookValidationError{
			field:  "Enum3",
			reason: "value must equal 1",
		}
		...
	}
}

Desired output:

func (m *Book) validate(all bool) error {
	...
	if _, ok := _Book_Enum1_InLookup[m.GetEnum1()]; !ok {
		err := BookValidationError{
			field:  "Enum1",
			reason: "value must be in list [OPTION_1 OPTION_2]",
		}
		...
	}

	if _, ok := _Book_Enum2_NotInLookup[m.GetEnum2()]; ok {
		err := BookValidationError{
			field:  "Enum2",
			reason: "value must not be in list [OPTION_1]",
		}
                ...
	}
        if m.GetEnum3() != 1 {
		err := BookValidationError{
			field:  "Enum3",
			reason: "value must equal OPTION_1",
		}
		....
	}
}

Changes

  • Added isEnum function to check if the field type is enum
  • Added enumList function to return options string (e.g. [OPTION_1 OPTION_2] ) for given field and in/notIn list
  • Added enumVal function to return option (for const)
  • Change cpp, goshared templates

TODO

@CLAassistant
Copy link

CLAassistant commented Oct 30, 2022

CLA assistant check
All committers have signed the CLA.

@elb3k elb3k marked this pull request as ready for review October 31, 2022 06:29
@elliotmjackson elliotmjackson marked this pull request as draft October 31, 2022 18:21
@elliotmjackson
Copy link
Contributor

Hi @elb3k! welcome to PGV and thank you for raising this. I know this is still a draft... just want to connect with you on a few things:

  1. Could you help out your reviewers with a detailed description of what you're trying to do and what problem you're trying to solve?
  2. Running CI locally is your best way to make sure everything is passing... view the development section of our readme for more detail on how to do this - your feedback on this section is welcomed and encouraged.
  3. Lastly, adding tests to cover your changes and updating the compatibility set is always a good way to improve your review outcome.

Good luck - reach out if you need anything.

@elb3k
Copy link
Contributor Author

elb3k commented Nov 1, 2022

Hi @elb3k! welcome to PGV and thank you for raising this. I know this is still a draft... just want to connect with you on a few things:

  1. Could you help out your reviewers with a detailed description of what you're trying to do and what problem you're trying to solve?
  2. Running CI locally is your best way to make sure everything is passing... view the development section of our readme for more detail on how to do this - your feedback on this section is welcomed and encouraged.
  3. Lastly, adding tests to cover your changes and updating the compatibility set is always a good way to improve your review outcome.

Good luck - reach out if you need anything.

Thanks, @elliotmjackson will address all comments!

@elb3k elb3k changed the title In/NotIn message show enum values for enum type In/NotIn and const error message show enum values for enum type Nov 2, 2022
@elb3k elb3k changed the title In/NotIn and const error message show enum values for enum type In/NotIn and const rules show enum values for enum type Nov 2, 2022
@elb3k elb3k marked this pull request as ready for review November 3, 2022 09:29
@elb3k
Copy link
Contributor Author

elb3k commented Nov 4, 2022

Hey @elliotmjackson.
I added support for golang and cpp (my primary usecases)
and java seems to have this already (in/not_in uses Arrays.toString() and const uses Enum_Val.toString())

But can't wrap my head around adding python support.
Or using python example. Can you give some pointers?

@elliotmjackson
Copy link
Contributor

relates to #638 (comment)

rule_comparison.md Outdated Show resolved Hide resolved
@elliotmjackson
Copy link
Contributor

I sympathise with you on the test harness @elb3k, I dont totally understand it myself... from a high-level, tests are defined in the protos found in tests/harness/cases, the resulting code is generated with make testcases and the harness can be run with make ci. Probably start with tests/harness/cases/enums.proto and define a message which utilises your changes, then head on over to tests/harness/executor & enumCases to add your case assertions.

@elb3k
Copy link
Contributor Author

elb3k commented Dec 2, 2022

Contributor

Awesome! If python supports it by default, will add test cases and test harnesses

@elb3k
Copy link
Contributor Author

elb3k commented Dec 27, 2022

@elliotmjackson added the support for python too. Current harness test cases cover all of the cases with error messages.
But the test cases do not match the exact messages. Making a change to exactly match the error message would be significant uplift from me.

@elliotmjackson elliotmjackson enabled auto-merge (squash) January 10, 2023 16:45
@elliotmjackson elliotmjackson merged commit a658586 into bufbuild:main Jan 10, 2023
@elb3k
Copy link
Contributor Author

elb3k commented Jan 12, 2023

Appreciate it @elliotmjackson!

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.

None yet

3 participants