CedarPattern.from constructor

CedarPattern.from(
  1. List<Object?> components, {
  2. String? raw,
})

Implementation

factory CedarPattern.from(List<Object?> components, {String? raw}) {
  final comps = <CedarPatternComponent>[];
  for (final comp in components) {
    switch (comp) {
      case Literal(literal: final String value) ||
            final String value ||
            StringValue(:final value):
        final component = switch (comps.lastOrNull) {
          null || Wildcard() => Literal(value),
          Literal() => Literal(comps.removeLast().literal + value),
        };
        comps.add(component);
      case Wildcard():
        if (comps.isEmpty || comps.last.literal.isNotEmpty) {
          comps.add(Wildcard());
        }
      default:
        throw ArgumentError.value(
          comp,
          'components',
          'must be a String or Wildcard',
        );
    }
  }
  return CedarPattern(comps, raw: raw);
}