Skip to content

Commit

Permalink
Fix in rule for repeated int32 (#1240)
Browse files Browse the repository at this point in the history
Fix generating lookup for `repeated.items.int32.in`

This PR fixes the `in` field validation for repeated int32 fields.

The current generated code doesn't emit the lookup map e.g.
```go
// message DoSomethingRequest {
//   repeated int32 field_with_int32_type = 4 [ (validate.rules).repeated.items.int32 = { in: [25,50] } ];
// }

// Generated code doesn't have this section
var _DoSomethingRequest_FieldWithInt32Type_InLookup = map[int32]struct{}{
	25:  {},
	50:  {},
}
``` 

I also added a Test cases for both int32 and int64
  • Loading branch information
mortezaPRK authored Jan 29, 2025
1 parent f6943c1 commit 94f2b56
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions templates/goshared/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,20 @@ var _ interface{
{{- end }}
}
{{ end }}{{ end }}
{{ if has .Rules.Items.GetInt32 "In" }} {{ if .Rules.Items.GetInt32.In }}
var {{ lookup .Field "InLookup" }} = map[int32]struct{}{
{{- range .Rules.Items.GetInt32.In }}
{{ inKey $f . }}: {},
{{- end }}
}
{{ end }}{{ end }}
{{ if has .Rules.Items.GetInt32 "NotIn" }} {{ if .Rules.Items.GetInt32.NotIn }}
var {{ lookup .Field "NotInLookup" }} = map[int32]struct{}{
{{- range .Rules.Items.GetInt32.NotIn }}
{{ inKey $f . }}: {},
{{- end }}
}
{{ end }}{{ end }}
{{ if has .Rules.Items.GetAny "In" }} {{ if .Rules.Items.GetAny.In }}
var {{ lookup .Field "InLookup" }} = map[string]struct{}{
{{- range .Rules.Items.GetAny.In }}
Expand Down

0 comments on commit 94f2b56

Please sign in to comment.