CedarPattern.from constructor
Implementation
factory CedarPattern.from(
List<Object?> components, {
String? raw,
List<Object?>? jsonForm,
}) {
String? literalAccumulator;
final comps = <CedarPatternComponent>[];
void flushLiteral() {
if (literalAccumulator case final literal? when literal.isNotEmpty) {
if (comps.isNotEmpty && comps.last is Literal) {
final previous = comps.removeLast() as Literal;
literalAccumulator = '${previous.literal}$literal';
}
comps.add(Literal(literalAccumulator!));
}
literalAccumulator = null;
}
void pushLiteral(String literal) {
literalAccumulator = '${literalAccumulator ?? ''}$literal';
}
void pushWildcard() {
flushLiteral();
if (comps.isEmpty || comps.last.literal.isNotEmpty) {
comps.add(const Wildcard());
}
}
for (final comp in components) {
switch (comp) {
case Literal(literal: final String value):
pushLiteral(value);
case Wildcard():
pushWildcard();
case StringValue(:final value):
pushLiteral(value);
case final String value:
if (value == 'Wildcard') {
pushWildcard();
} else {
pushLiteral(value);
}
case Map<Object?, Object?> map:
if (map.length != 1) {
throw ArgumentError.value(
map,
'components',
'pattern component map must contain exactly one entry',
);
}
final entry = map.entries.first;
switch (entry.key) {
case 'Literal':
final literal = entry.value;
if (literal is! String) {
throw ArgumentError.value(
entry.value,
'components',
'Literal component must be a string',
);
}
pushLiteral(literal);
case 'Wildcard':
pushWildcard();
default:
throw ArgumentError.value(
entry.key,
'components',
'Unknown pattern component type',
);
}
default:
throw ArgumentError.value(
comp,
'components',
'must describe a literal or wildcard',
);
}
}
flushLiteral();
return CedarPattern(
comps,
raw: raw,
jsonForm: jsonForm == null
? null
: List<Object?>.unmodifiable(
jsonForm.map(_clonePatternJsonComponent),
),
);
}