Skip to content

Commit

Permalink
Fix generating lookup for repeated.items.any.in
Browse files Browse the repository at this point in the history
Signed-off-by: StitchCula <stitchgl@outlook.com>
  • Loading branch information
stitchcula committed Jan 10, 2022
1 parent 6a061a0 commit 5f7bdb8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
14 changes: 14 additions & 0 deletions templates/cc/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ const msgTpl = `
{{- end }}
};
{{ end }}{{ end }}
{{ if has .Rules.Items.GetAny "In" }} {{ if .Rules.Items.GetAny.In }}
const std::set<string> {{ lookup .Field "InLookup" }} = {
{{- range .Rules.Items.GetAny.In }}
{{ inKey $f . }},
{{- end }}
};
{{ end }}{{ end }}
{{ end }}{{ end }}
{{ if has .Rules "Items"}}{{ if .Rules.Items }}
Expand All @@ -63,6 +70,13 @@ const msgTpl = `
{{- end }}
};
{{ end }}{{ end }}
{{ if has .Rules.Items.GetAny "NotIn" }} {{ if .Rules.Items.GetAny.NotIn }}
const std::set<string> {{ lookup .Field "NotInLookup" }} = {
{{- range .Rules.Items.GetAny.NotIn }}
{{ inKey $f . }},
{{- end }}
};
{{ end }}{{ end }}
{{ end }}{{ end }}
{{ if has .Rules "Pattern"}}{{ if .Rules.Pattern }}
Expand Down
14 changes: 14 additions & 0 deletions templates/goshared/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ var _ interface{
{{- 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 }}
{{ inKey $f . }}: {},
{{- end }}
}
{{ end }}{{ end }}
{{ end }}{{ end }}
{{ if has .Rules "Items"}}{{ if .Rules.Items }}
Expand All @@ -191,6 +198,13 @@ var _ interface{
{{- end }}
}
{{ end }}{{ end }}
{{ if has .Rules.Items.GetAny "NotIn" }} {{ if .Rules.Items.GetAny.NotIn }}
var {{ lookup .Field "NotInLookup" }} = map[string]struct{}{
{{- range .Rules.Items.GetAny.NotIn }}
{{ inKey $f . }}: {},
{{- end }}
}
{{ end }}{{ end }}
{{ end }}{{ end }}
{{ if has .Rules "Keys"}}{{ if .Rules.Keys }}
Expand Down
3 changes: 3 additions & 0 deletions tests/harness/cases/repeated.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "tests/harness/cases/other_package/embed.proto";

import "validate/validate.proto";
import "google/protobuf/duration.proto";
import "google/protobuf/any.proto";

message Embed { int64 val = 1 [(validate.rules).int64.gt = 0]; }
enum AnEnum {
Expand All @@ -30,6 +31,8 @@ message RepeatedEnumIn { repeated AnEnum val = 1 [(validate.rules).repeated.item
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; }}
message RepeatedEmbeddedEnumNotIn { repeated AnotherNotInEnum val = 1 [(validate.rules).repeated.items.enum = {not_in: [0]}]; enum AnotherNotInEnum {A = 0; B = 1; }}
message RepeatedAnyIn { repeated google.protobuf.Any val = 1 [(validate.rules).repeated.items.any = {in: ["type.googleapis.com/google.protobuf.Duration"]}]; }
message RepeatedAnyNotIn { repeated google.protobuf.Any val = 1 [(validate.rules).repeated.items.any = {not_in: ["type.googleapis.com/google.protobuf.Timestamp"]}]; }
message RepeatedMinAndItemLen { repeated string val = 1 [(validate.rules).repeated = { items { string { len: 3 } }, min_items: 1 }]; }
message RepeatedMinAndMaxItemLen { repeated string val = 1 [(validate.rules).repeated.min_items = 1, (validate.rules).repeated.max_items = 3]; }
message RepeatedDuration { repeated google.protobuf.Duration val = 1 [(validate.rules).repeated = { items { duration { gte { nanos: 1000000}}}}]; }
Expand Down
5 changes: 5 additions & 0 deletions tests/harness/executor/cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,11 @@ var repeatedCases = []TestCase{
{"repeated - items - invalid (embedded enum not_in)", &cases.RepeatedEmbeddedEnumNotIn{Val: []cases.RepeatedEmbeddedEnumNotIn_AnotherNotInEnum{0}}, 1},
{"repeated - items - valid (embedded enum not_in)", &cases.RepeatedEmbeddedEnumNotIn{Val: []cases.RepeatedEmbeddedEnumNotIn_AnotherNotInEnum{1}}, 0},

{"repeated - items - invalid (any in)", &cases.RepeatedAnyIn{Val: []*anypb.Any{{TypeUrl: "type.googleapis.com/google.protobuf.Timestamp"}}}, 1},
{"repeated - items - valid (any in)", &cases.RepeatedAnyIn{Val: []*anypb.Any{{TypeUrl: "type.googleapis.com/google.protobuf.Duration"}}}, 0},
{"repeated - items - invalid (any not_in)", &cases.RepeatedAnyNotIn{Val: []*anypb.Any{{TypeUrl: "type.googleapis.com/google.protobuf.Timestamp"}}}, 1},
{"repeated - items - valid (any not_in)", &cases.RepeatedAnyNotIn{Val: []*anypb.Any{{TypeUrl: "type.googleapis.com/google.protobuf.Duration"}}}, 0},

{"repeated - embed skip - valid", &cases.RepeatedEmbedSkip{Val: []*cases.Embed{{Val: 1}}}, 0},
{"repeated - embed skip - valid (invalid element)", &cases.RepeatedEmbedSkip{Val: []*cases.Embed{{Val: -1}}}, 0},
{"repeated - min and items len - valid", &cases.RepeatedMinAndItemLen{Val: []string{"aaa", "bbb"}}, 0},
Expand Down

0 comments on commit 5f7bdb8

Please sign in to comment.