{%- import "validation_macros.tmpl" as validation_macros %}
{%- set class_name = union.name ~ "_Data" %}
{%- set enum_name = union.name ~ "_Tag" -%}
// static
bool {{class_name}}::Validate(
const void* data,
mojo::internal::ValidationContext* validation_context,
bool inlined) {
if (!data) {
DCHECK(!inlined);
return true;
}
// If it is inlined, the alignment is already enforced by its enclosing
// object. We don't have to validate that.
DCHECK(!inlined || mojo::internal::IsAligned(data));
if (!inlined &&
!mojo::internal::ValidateNonInlinedUnionHeaderAndClaimMemory(
data, validation_context)) {
return false;
}
const {{class_name}}* object = static_cast<const {{class_name}}*>(data);
if (inlined && object->is_null())
return true;
switch (object->tag) {
{% for field in union.fields %}
case {{enum_name}}::k{{field.name|under_to_camel}}: {
{%- set field_expr = "object->data.f_" ~ field.name %}
{{validation_macros.validate_field(field, loop.index, field_expr, false)|indent(4)}}
return true;
}
{%- endfor %}
default: {
{% if union.extensible %}
return true;
{%- else %}
ReportValidationError(
validation_context,
mojo::internal::VALIDATION_ERROR_UNKNOWN_UNION_TAG,
"unknown tag in {{union.name}}");
return false;
{%- endif %}
}
}
}