Skip to content

Commit

Permalink
support in rule for repeated in32 and in64
Browse files Browse the repository at this point in the history
  • Loading branch information
mortezaPRK authored Feb 2, 2025
1 parent 94f2b56 commit 9d58897
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
28 changes: 28 additions & 0 deletions templates/cc/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ const msgTpl = `
{{- end }}
};
{{ end }}{{ end }}
{{ if has .Rules.Items.GetInt64 "In" }} {{ if .Rules.Items.GetInt64.In }}
const std::set<int64_t> {{ lookup .Field "InLookup" }} = {
{{- range .Rules.Items.GetInt64.In }}
{{ inKey $f . }},
{{- end }}
};
{{ end }}{{ end }}
{{ if has .Rules.Items.GetInt32 "In" }} {{ if .Rules.Items.GetInt32.In }}
const std::set<int32_t> {{ lookup .Field "InLookup" }} = {
{{- range .Rules.Items.GetInt32.In }}
{{ inKey $f . }},
{{- end }}
};
{{ end }}{{ end }}
{{ if has .Rules.Items.GetEnum "In" }} {{ if .Rules.Items.GetEnum.In }}
const std::set<{{ inType .Field .Rules.Items.GetEnum.In }}> {{ lookup .Field "InLookup" }} = {
{{- range .Rules.Items.GetEnum.In }}
Expand All @@ -63,6 +77,20 @@ const msgTpl = `
{{- end }}
};
{{ end }}{{ end }}
{{ if has .Rules.Items.GetInt64 "NotIn" }} {{ if .Rules.Items.GetInt64.NotIn }}
const std::set<int64_t> {{ lookup .Field "NotInLookup" }} = {
{{- range .Rules.Items.GetInt64.NotIn }}
{{ inKey $f . }},
{{- end }}
};
{{ end }}{{ end }}
{{ if has .Rules.Items.GetInt32 "NotIn" }} {{ if .Rules.Items.GetInt32.NotIn }}
const std::set<int32_t> {{ lookup .Field "NotInLookup" }} = {
{{- range .Rules.Items.GetInt32.NotIn }}
{{ inKey $f . }},
{{- end }}
};
{{ end }}{{ end }}
{{ if has .Rules.Items.GetEnum "NotIn" }} {{ if .Rules.Items.GetEnum.NotIn }}
const std::set<{{ inType .Field .Rules.Items.GetEnum.NotIn }}> {{ lookup .Field "NotInLookup" }} = {
{{- range .Rules.Items.GetEnum.NotIn }}
Expand Down
4 changes: 4 additions & 0 deletions tests/harness/cases/repeated.proto
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ message RepeatedItemPattern { repeated string val = 1 [(validate.rules).repeate
message RepeatedEmbedSkip { repeated Embed val = 1 [(validate.rules).repeated.items.message.skip = true]; }
message RepeatedItemIn { repeated string val = 1 [(validate.rules).repeated.items.string = {in: ["foo", "bar"]}]; }
message RepeatedItemNotIn { repeated string val = 1 [(validate.rules).repeated.items.string = {not_in: ["foo", "bar"]}]; }
message RepeatedItemInt64In { repeated int64 val = 1 [(validate.rules).repeated.items.int64 = {in: [1, 2]}]; }
message RepeatedItemInt64NotIn { repeated int64 val = 1 [(validate.rules).repeated.items.int64 = {not_in: [1, 2]}]; }
message RepeatedItemInt32In { repeated int32 val = 1 [(validate.rules).repeated.items.int32 = {in: [1, 2]}]; }
message RepeatedItemInt32NotIn { repeated int32 val = 1 [(validate.rules).repeated.items.int32 = {not_in: [1, 2]}]; }
message RepeatedEnumIn { repeated AnEnum val = 1 [(validate.rules).repeated.items.enum = {in: [0]}]; }
message RepeatedEnumNotIn { repeated AnEnum val = 1 [(validate.rules).repeated.items.enum = {not_in: [0]}]; }
message RepeatedEmbeddedEnumIn { repeated AnotherInEnum val = 1 [(validate.rules).repeated.items.enum = {in: [0]}]; enum AnotherInEnum {A = 0; B = 1; }}
Expand Down
9 changes: 8 additions & 1 deletion tests/harness/executor/cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,14 @@ var repeatedCases = []TestCase{
{"repeated - items - valid (in)", &cases.RepeatedItemIn{Val: []string{"foo"}}, 0},
{"repeated - items - invalid (not_in)", &cases.RepeatedItemNotIn{Val: []string{"foo"}}, 1},
{"repeated - items - valid (not_in)", &cases.RepeatedItemNotIn{Val: []string{"baz"}}, 0},

{"repeated - items - invalid (int64 in)", &cases.RepeatedItemInt64In{Val: []int64{3}}, 1},
{"repeated - items - valid (int64 in)", &cases.RepeatedItemInt64In{Val: []int64{1}}, 0},
{"repeated - items - invalid (int64 not_in)", &cases.RepeatedItemInt64NotIn{Val: []int64{1}}, 1},
{"repeated - items - valid (int64 not_in)", &cases.RepeatedItemInt64NotIn{Val: []int64{3}}, 0},
{"repeated - items - invalid (int32 in)", &cases.RepeatedItemInt32In{Val: []int32{3}}, 1},
{"repeated - items - valid (int32 in)", &cases.RepeatedItemInt32In{Val: []int32{1}}, 0},
{"repeated - items - invalid (int32 not_in)", &cases.RepeatedItemInt32NotIn{Val: []int32{1}}, 1},
{"repeated - items - valid (int32 not_in)", &cases.RepeatedItemInt32NotIn{Val: []int32{3}}, 0},
{"repeated - items - invalid (enum in)", &cases.RepeatedEnumIn{Val: []cases.AnEnum{1}}, 1},
{"repeated - items - valid (enum in)", &cases.RepeatedEnumIn{Val: []cases.AnEnum{0}}, 0},
{"repeated - items - invalid (enum not_in)", &cases.RepeatedEnumNotIn{Val: []cases.AnEnum{0}}, 1},
Expand Down

0 comments on commit 9d58897

Please sign in to comment.