-
Notifications
You must be signed in to change notification settings - Fork 585
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In/NotIn and const rules show enum values for enum type (#699)
## Problem The error message for enum `in/not_in` rules show numbers instead of enum values Sample case **Input:** ```protobuf ... 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:** ```go 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:** ```go 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 - [x] Add test cases - [x] Update [compatibility set](https://github.com/bufbuild/protoc-gen-validate/blob/main/rule_comparison.md) Co-authored-by: Elliot Jackson <13633636+ElliotMJackson@users.noreply.github.com>
- Loading branch information
1 parent
9513a03
commit a658586
Showing
8 changed files
with
116 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
package cc | ||
|
||
const constTpl = `{{ $r := .Rules }} | ||
const constTpl = `{{ $f := .Field }}{{ $r := .Rules }} | ||
{{ if $r.Const }} | ||
if ({{ accessor . }} != {{ lit $r.GetConst }}) { | ||
{{- if isEnum $f }} | ||
{{ err . "value must equal " (enumVal $f $r.GetConst) }} | ||
{{- else }} | ||
{{ err . "value must equal " (lit $r.GetConst) }} | ||
{{- end }} | ||
} | ||
{{ end }} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package shared | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
pgs "github.com/lyft/protoc-gen-star" | ||
) | ||
|
||
func isEnum(f pgs.Field) bool { | ||
return f.Type().IsEnum() | ||
} | ||
|
||
func enumNamesMap(values []pgs.EnumValue) (m map[int32]string) { | ||
m = make(map[int32]string) | ||
for _, v := range values { | ||
if _, exists := m[v.Value()]; !exists { | ||
m[v.Value()] = v.Name().String() | ||
} | ||
} | ||
return m | ||
} | ||
|
||
// enumList - if type is ENUM, enum values are returned | ||
func enumList(f pgs.Field, list []int32) string { | ||
stringList := make([]string, 0, len(list)) | ||
if enum := f.Type().Enum(); enum != nil { | ||
names := enumNamesMap(enum.Values()) | ||
for _, n := range list { | ||
stringList = append(stringList, names[n]) | ||
} | ||
} else { | ||
for _, n := range list { | ||
stringList = append(stringList, fmt.Sprint(n)) | ||
} | ||
} | ||
return "[" + strings.Join(stringList, " ") + "]" | ||
} | ||
|
||
// enumVal - if type is ENUM, enum value is returned | ||
func enumVal(f pgs.Field, val int32) string { | ||
if enum := f.Type().Enum(); enum != nil { | ||
return enumNamesMap(enum.Values())[val] | ||
} | ||
return fmt.Sprint(val) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters