1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//! Map field rule code generation.
use proc_macro2::{Ident, TokenStream};
use prost_reflect::{DescriptorPool, FieldDescriptor};
use quote::quote;
use prost_protovalidate_types::{Ignore, MapRules};
use crate::Error;
use crate::codegen;
use crate::naming::NamingContext;
use crate::rules;
#[allow(clippy::cast_possible_truncation, clippy::too_many_lines)]
pub(crate) fn generate(
rules: &MapRules,
field: &FieldDescriptor,
field_ident: &Ident,
proto_name: &str,
_pool: &DescriptorPool,
_naming: &NamingContext,
) -> Result<Vec<TokenStream>, Error> {
// Resolve the synthetic `value` field's kind once so we can propagate
// `enum.defined_only` when the map value is an enum type.
let value_kind = field
.kind()
.as_message()
.and_then(|entry| entry.get_field_by_name("value"))
.map(|v| v.kind());
let mut checks = Vec::new();
// Min pairs
if let Some(min) = rules.min_pairs {
let min_usize = min as usize;
let msg = format!("must have at least {min} entries");
checks.push(quote! {
if self.#field_ident.len() < #min_usize {
violations.push(::prost_protovalidate::Violation::new(
#proto_name, "map.min_pairs", #msg,
));
}
});
}
// Max pairs
if let Some(max) = rules.max_pairs {
let max_usize = max as usize;
let msg = format!("must have at most {max} entries");
checks.push(quote! {
if self.#field_ident.len() > #max_usize {
violations.push(::prost_protovalidate::Violation::new(
#proto_name, "map.max_pairs", #msg,
));
}
});
}
// Per-entry key constraints.
//
// Each key-rule violation is captured locally, then marked with
// `for_key = true` and prepended with `map.keys` on the rule path
// and the entry's key subscript on the field path — matching the
// runtime's [`MapEval`] key-violation post-processing exactly.
//
// Honors `map.keys.ignore`: `IGNORE_ALWAYS` skips the key checks
// entirely; `IGNORE_IF_ZERO_VALUE` wraps them in a default-value
// guard so zero-valued keys pass without violations (matching
// runtime's `build_value(..., nested = true)` semantics).
let key_kind = field
.kind()
.as_message()
.and_then(|entry| entry.get_field_by_name("key"))
.map(|k| k.kind());
if let Some(ref keys) = rules.keys {
let keys_ignore = codegen::ignore_mode_of(keys.ignore);
if keys_ignore != Ignore::Always {
if let Some(ref type_rules) = keys.r#type {
let key_access = quote!((*_k));
let key_checks =
rules::generate_scalar_type_checks(type_rules, &key_access, "", &[])?;
if !key_checks.is_empty() {
let key_subscript =
map_key_subscript_prepend("_v_inner", proto_name, &field.kind());
let body = if keys_ignore == Ignore::IfZeroValue {
if let Some(default_check) = key_kind
.as_ref()
.and_then(|k| codegen::generate_element_default_check(k, &key_access))
{
quote! {
if #default_check {
let violations = &mut _local_violations;
#(#key_checks)*
}
}
} else {
quote! {
let violations = &mut _local_violations;
#(#key_checks)*
}
}
} else {
quote! {
let violations = &mut _local_violations;
#(#key_checks)*
}
};
checks.push(quote! {
for (_k, _) in &self.#field_ident {
let mut _local_violations: ::std::vec::Vec<
::prost_protovalidate::Violation,
> = ::std::vec::Vec::new();
{
#body
}
for mut _v_inner in _local_violations {
_v_inner.mark_for_key();
_v_inner.prepend_rule_path("map.keys");
#key_subscript
violations.push(_v_inner);
}
}
});
}
}
}
}
// Per-entry value constraints — same pattern as keys, with
// `map.values` as the rule-path prefix.
if let Some(ref values) = rules.values {
let values_ignore = codegen::ignore_mode_of(values.ignore);
if values_ignore != Ignore::Always {
if let Some(ref type_rules) = values.r#type {
let val_access = quote!((*_v));
let defined_values = value_kind
.as_ref()
.map(rules::defined_enum_values)
.unwrap_or_default();
let val_checks = rules::generate_scalar_type_checks(
type_rules,
&val_access,
"",
&defined_values,
)?;
if !val_checks.is_empty() {
let key_subscript =
map_key_subscript_prepend("_v_inner", proto_name, &field.kind());
let body = if values_ignore == Ignore::IfZeroValue {
if let Some(default_check) = value_kind
.as_ref()
.and_then(|k| codegen::generate_element_default_check(k, &val_access))
{
quote! {
if #default_check {
let violations = &mut _local_violations;
#(#val_checks)*
}
}
} else {
quote! {
let violations = &mut _local_violations;
#(#val_checks)*
}
}
} else {
quote! {
let violations = &mut _local_violations;
#(#val_checks)*
}
};
checks.push(quote! {
for (_k, _v) in &self.#field_ident {
let mut _local_violations: ::std::vec::Vec<
::prost_protovalidate::Violation,
> = ::std::vec::Vec::new();
{
#body
}
for mut _v_inner in _local_violations {
_v_inner.prepend_rule_path("map.values");
#key_subscript
violations.push(_v_inner);
}
}
});
}
}
}
}
Ok(checks)
}
/// Emit a `prepend_*_key` call on `violation_ident` based on the map's
/// key kind, producing a subscripted field path like `field["key"]` /
/// `field[42]` / `field[true]` that matches the canonical runtime format.
fn map_key_subscript_prepend(
violation_ident: &str,
proto_name: &str,
map_field_kind: &prost_reflect::Kind,
) -> TokenStream {
use prost_reflect::Kind;
let viol = quote::format_ident!("{}", violation_ident);
// The kind we have is the map-entry message; drill in to the key field.
let key_kind = map_field_kind
.as_message()
.and_then(|entry| entry.get_field_by_name("key"))
.map(|k| k.kind());
match key_kind {
Some(Kind::String) => quote! {
#viol.prepend_string_key(#proto_name, _k.as_str());
},
Some(Kind::Bool) => quote! {
#viol.prepend_bool_key(#proto_name, *_k);
},
Some(Kind::Int32 | Kind::Sint32 | Kind::Sfixed32) => quote! {
#viol.prepend_int_key(#proto_name, ::core::convert::From::from(*_k));
},
Some(Kind::Int64 | Kind::Sint64 | Kind::Sfixed64) => quote! {
#viol.prepend_int_key(#proto_name, *_k);
},
Some(Kind::Uint32 | Kind::Fixed32) => quote! {
#viol.prepend_uint_key(#proto_name, ::core::convert::From::from(*_k));
},
Some(Kind::Uint64 | Kind::Fixed64) => quote! {
#viol.prepend_uint_key(#proto_name, *_k);
},
_ => quote! {
#viol.prepend_field_path(#proto_name);
},
}
}