normalizeContextVariable function
Parses one raw entry from templateConfig.contextVariables into a
declaration named by its token, e.g. user.age. Returns null when the
namespace or key is missing or unusable.
A missing or unknown type is inferred from a typed fallback, so a number or
bool token still prints as one; anything else is text. With no fallback, a
number falls back to 0 and a bool to false, so copy never shows a gap
where the author expected a value.
Implementation
VariableSchema? normalizeContextVariable(Map<String, dynamic> entry) {
final namespace = entry['namespace'];
if (namespace != userNamespace && namespace != eventNamespace) return null;
final key = entry['key'];
if (key is! String || !_contextKeyPattern.hasMatch(key)) return null;
final fallback = entry['fallbackValue'];
final type = switch (entry['type']) {
'number' => 'number',
'bool' => 'bool',
'string' => 'string',
_ => switch (fallback) {
num() => 'number',
bool() => 'bool',
_ => 'string',
},
};
final authored = fallback?.toString() ?? '';
return VariableSchema(
name: '$namespace.$key',
type: type,
fallbackValue: authored.isNotEmpty
? authored
: switch (type) {
'number' => '0',
'bool' => 'false',
_ => '',
},
);
}