protoc-gen-validate icon indicating copy to clipboard operation
protoc-gen-validate copied to clipboard

Golang: `optional` or `oneof` keyword with `.string = {in: []}` generates non-functioning map in code

Open XyLearningProgramming opened this issue 2 years ago • 1 comments

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.

XyLearningProgramming avatar Jun 23 '22 09:06 XyLearningProgramming

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.

yiwankb avatar Jul 26 '22 06:07 yiwankb