|
| 1 | +/// a simple filter function to be used inside [SimpleEnvironmentFilter] |
| 2 | +typedef EnvironmentFilterFunc = bool Function(Set<String>); |
| 3 | +const kEnvironmentsName = '__environments__'; |
| 4 | + |
| 5 | +/// filter for whether to register for the given set of environments |
| 6 | +/// clients can extend this class to maker |
| 7 | +/// their own environmentFilters |
| 8 | +abstract class EnvironmentFilter { |
| 9 | + /// holds passed environment keys |
| 10 | + /// to be used inside the filter or |
| 11 | + /// retrieved later by users |
| 12 | + final Set<String?> environments; |
| 13 | + |
| 14 | + /// default constructor |
| 15 | + const EnvironmentFilter(this.environments); |
| 16 | + |
| 17 | + /// This function is called before every |
| 18 | + /// registration call, if it returns true, the dependency |
| 19 | + /// will be registered otherwise, it will be ignored |
| 20 | + bool canRegister(Set<String> depEnvironments); |
| 21 | +} |
| 22 | + |
| 23 | +/// A simple filter that can be used directly for simple use cases |
| 24 | +/// without having to extend the base [EnvironmentFilter] |
| 25 | +class SimpleEnvironmentFilter extends EnvironmentFilter { |
| 26 | + final EnvironmentFilterFunc filter; |
| 27 | + |
| 28 | + const SimpleEnvironmentFilter( |
| 29 | + {required this.filter, Set<String> environments = const {}}) |
| 30 | + : super(environments); |
| 31 | + |
| 32 | + @override |
| 33 | + bool canRegister(Set<String> depEnvironments) => filter(depEnvironments); |
| 34 | +} |
| 35 | + |
| 36 | +/// This filter validates dependencies with no environment |
| 37 | +/// keys or contain the provided [environment] |
| 38 | +class NoEnvOrContains extends EnvironmentFilter { |
| 39 | + NoEnvOrContains(String? environment) : super({environment}); |
| 40 | + |
| 41 | + @override |
| 42 | + bool canRegister(Set<String> depEnvironments) { |
| 43 | + return (depEnvironments.isEmpty) || |
| 44 | + depEnvironments.contains(environments.first); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/// This filter validates dependencies with no environment |
| 49 | +/// keys, or the ones containing all the provided [environments] |
| 50 | +class NoEnvOrContainsAll extends EnvironmentFilter { |
| 51 | + const NoEnvOrContainsAll(Set<String> environments) : super(environments); |
| 52 | + |
| 53 | + @override |
| 54 | + bool canRegister(Set<String> depEnvironments) { |
| 55 | + return (depEnvironments.isEmpty) || |
| 56 | + depEnvironments.containsAll(environments); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/// This filter validates dependencies with no environment |
| 61 | +/// keys, or the ones containing one of the provided [environments] |
| 62 | +class NoEnvOrContainsAny extends EnvironmentFilter { |
| 63 | + const NoEnvOrContainsAny(Set<String> environments) : super(environments); |
| 64 | + |
| 65 | + @override |
| 66 | + bool canRegister(Set<String> depEnvironments) { |
| 67 | + return (depEnvironments.isEmpty) || |
| 68 | + depEnvironments.intersection(environments).isNotEmpty; |
| 69 | + } |
| 70 | +} |
0 commit comments