Skip to content

Commit c87857e

Browse files
ucprrodaine
andauthored
Fix invalid generation when using in-rule and not_in rule in map.keys.string (#847)
## Summary I fixed invalid generation for Go and cc when using in-rule and not_in rule in map.keys.string ## Issue Closes #785 --------- Co-authored-by: Chris Roche <[email protected]>
1 parent a3c8c99 commit c87857e

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

templates/cc/msg.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,21 @@ const msgTpl = `
9191
{{ end }}{{ end }}
9292
{{ end }}{{ end }}
9393
94-
{{ if has .Rules "Keys"}}{{ if .Rules.Keys }}
94+
{{ if has .Rules "Keys"}}{{ if .Rules.Keys }}
95+
{{ if has .Rules.Keys.GetString_ "In" }} {{ if .Rules.Keys.GetString_.In }}
96+
const std::set<string> {{ lookup .Field "InLookup" }} = {
97+
{{- range .Rules.Keys.GetString_.In }}
98+
{{ inKey $f . }},
99+
{{- end }}
100+
};
101+
{{ end }}{{ end }}
102+
{{ if has .Rules.Keys.GetString_ "NotIn" }} {{ if .Rules.Keys.GetString_.NotIn }}
103+
const std::set<string> {{ lookup .Field "NotInLookup" }} = {
104+
{{- range .Rules.Keys.GetString_.NotIn }}
105+
{{ inKey $f . }},
106+
{{- end }}
107+
};
108+
{{ end }}{{ end }}
95109
{{ if has .Rules.Keys.GetString_ "Pattern" }} {{ if .Rules.Keys.GetString_.Pattern }}
96110
const re2::RE2 {{ lookup .Field "Pattern" }}(re2::StringPiece({{ lit .Rules.Keys.GetString_.GetPattern }},
97111
sizeof({{ lit .Rules.Keys.GetString_.GetPattern }}) - 1));

templates/goshared/msg.go

+14
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,20 @@ var _ interface{
234234
{{ end }}{{ end }}
235235
236236
{{ if has .Rules "Keys"}}{{ if .Rules.Keys }}
237+
{{ if has .Rules.Keys.GetString_ "In" }} {{ if .Rules.Keys.GetString_.In }}
238+
var {{ lookup .Field "InLookup" }} = map[{{ inType .Field .Rules.Keys.GetString_.In }}]struct{}{
239+
{{- range .Rules.Keys.GetString_.In }}
240+
{{ inKey $f . }}: {},
241+
{{- end }}
242+
}
243+
{{ end }}{{ end }}
244+
{{ if has .Rules.Keys.GetString_ "NotIn" }} {{ if .Rules.Keys.GetString_.NotIn }}
245+
var {{ lookup .Field "NotInLookup" }} = map[{{ inType .Field .Rules.Keys.GetString_.NotIn }}]struct{}{
246+
{{- range .Rules.Keys.GetString_.NotIn }}
247+
{{ inKey $f . }}: {},
248+
{{- end }}
249+
}
250+
{{ end }}{{ end }}
237251
{{ if has .Rules.Keys.GetString_ "Pattern" }} {{ if .Rules.Keys.GetString_.Pattern }}
238252
var {{ lookup .Field "Pattern" }} = regexp.MustCompile({{ lit .Rules.Keys.GetString_.GetPattern }})
239253
{{ end }}{{ end }}

tests/harness/cases/maps.proto

+3
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,6 @@ message MultipleMaps {
3636
map <int32, bool> second = 2 [(validate.rules).map.keys.int32.lt = 0];
3737
map <int32, bool> third = 3 [(validate.rules).map.keys.int32.gt = 0];
3838
}
39+
40+
message MapKeysIn {map<string, string> val = 1 [(validate.rules).map.keys.string = {in: ["foo", "bar"]}]; }
41+
message MapKeysNotIn {map<string, string> val = 1 [(validate.rules).map.keys.string = {not_in: ["foo", "bar"]}]; }

tests/harness/executor/cases.go

+4
Original file line numberDiff line numberDiff line change
@@ -1184,8 +1184,12 @@ var mapCases = []TestCase{
11841184
{"map - keys - valid", &cases.MapKeys{Val: map[int64]string{-1: "a", -2: "b"}}, 0},
11851185
{"map - keys - valid (empty)", &cases.MapKeys{Val: map[int64]string{}}, 0},
11861186
{"map - keys - valid (pattern)", &cases.MapKeysPattern{Val: map[string]string{"A": "a"}}, 0},
1187+
{"map - keys - valid (in)", &cases.MapKeysIn{Val: map[string]string{"foo": "value"}}, 0},
1188+
{"map - keys - valid (not_in)", &cases.MapKeysNotIn{Val: map[string]string{"baz": "value"}}, 0},
11871189
{"map - keys - invalid", &cases.MapKeys{Val: map[int64]string{1: "a"}}, 1},
11881190
{"map - keys - invalid (pattern)", &cases.MapKeysPattern{Val: map[string]string{"A": "a", "!@#$%^&*()": "b"}}, 1},
1191+
{"map - keys - invalid (in)", &cases.MapKeysIn{Val: map[string]string{"baz": "value"}}, 1},
1192+
{"map - keys - invalid (not_in)", &cases.MapKeysNotIn{Val: map[string]string{"foo": "value"}}, 1},
11891193

11901194
{"map - values - valid", &cases.MapValues{Val: map[string]string{"a": "Alpha", "b": "Beta"}}, 0},
11911195
{"map - values - valid (empty)", &cases.MapValues{Val: map[string]string{}}, 0},

0 commit comments

Comments
 (0)