protoc-gen-validate
protoc-gen-validate copied to clipboard
Golang: `optional` or `oneof` keyword with `.string = {in: []}` generates non-functioning map in code
In PGV 0.6.7,
I noticed messages that have optional
or oneof
keyword with .string = {in: []}
generates non-functioning map in code. For instance,
optional string example = 1 [
(validate.rules).string = {
in : [ "foo" ]
}
];
will result in
var _Example_Example_InLookup = map[*string]struct{}{
"foo": {},
}
The *string
and string
don't match. To fix it, I have to use gofmt -r 'map[*string]struct{} -> map[string]struct{}' -w ***.pb.validate.go
.
I also had the same problem.
message QueryTaskRequest{ repeated int32 actives = 1 [(validate.rules).repeated = {ignore_empty: true, items: {int32: {in: [0, 1]}}}]; }
will result in
`if len(m.GetActives()) > 0 {
for idx, item := range m.GetActives() {
_, _ = idx, item
if _, ok := _QueryTaskRequest_Actives_InLookup[item]; !ok {
err := QueryTaskRequestValidationError{
field: fmt.Sprintf("Actives[%v]", idx),
reason: "value must be in list [0 1]",
}
if !all {
return err
}
errors = append(errors, err)
}
}
}`
The '_QueryTaskRequest_Actives_InLookup' can't compile.