| 1 |
#' Abstract Class for Filterables |
| 2 |
#' |
| 3 |
#' @description Superclass for classes that have a `$filter()` method such as |
| 4 |
#' [Appenders] and [Loggers]. See [EventFilter] for details. |
| 5 |
#' |
| 6 |
#' @template abstract_class |
| 7 |
#' |
| 8 |
#' @export |
| 9 |
Filterable <- R6::R6Class( |
| 10 |
"Filterable", |
| 11 |
cloneable = FALSE, |
| 12 |
|
| 13 |
public = list( |
| 14 |
|
| 15 |
#' @description Determine whether the LogEvent `x` should be passed on to |
| 16 |
#' Appenders (`TRUE`) or not (`FALSE`). See also the active binding |
| 17 |
#' `filters`. |
| 18 |
#' |
| 19 |
#' @param event a [LogEvent] |
| 20 |
filter = function(event){ |
| 21 |
for (f in get(".filters", private)) { |
| 22 |
|
| 23 |
# we can assume f() is a valid Filter since it is aleady verrified by |
| 24 |
# $set_filters() |
| 25 |
if (is.function(f)){ |
| 26 |
r <- f(event) |
| 27 |
} else { |
| 28 |
r <- f[["filter"]](event) |
| 29 |
} |
| 30 |
|
| 31 |
if (isTRUE(r)){ |
| 32 |
# do nothing |
| 33 |
} else if (isFALSE(r)){ |
| 34 |
return(FALSE) |
| 35 |
} else { |
| 36 |
warning( |
| 37 |
"`$filter()` of ", class_fmt(self, c("R6", "Filterable")), |
| 38 |
" object did not return `TRUE` or `FALSE` but ", string_repr(r), |
| 39 |
". Please check its `$filters`", call. = FALSE |
| 40 |
) |
| 41 |
} |
| 42 |
} |
| 43 |
TRUE |
| 44 |
}, |
| 45 |
|
| 46 |
#' @description Attach a filter |
| 47 |
#' @param filter |
| 48 |
#' * a function with the single argument `event` that returns `TRUE` |
| 49 |
#' or `FALSE`; |
| 50 |
#' * an [EventFilter] [R6::R6] object; or |
| 51 |
#' * any \R object with a `$filter()` method. |
| 52 |
#' |
| 53 |
#' If a Filter returns a non-`FALSE` value, will be interpreted as `TRUE` |
| 54 |
#' (= no filtering takes place) and a warning will be thrown. |
| 55 |
#' |
| 56 |
#' @param name `character` scalar or `NULL`. An optional |
| 57 |
#' name which makes it easier to access (or remove) the filter |
| 58 |
add_filter = function(filter, name = NULL){ |
| 59 |
assert_filter(filter) |
| 60 |
assert(is.null(name) || is_scalar_character(name)) |
| 61 |
pos <- name %||% (length(private$.filters) + 1L) |
| 62 |
private[[".filters"]][[pos]] <- filter |
| 63 |
invisible(self) |
| 64 |
}, |
| 65 |
|
| 66 |
|
| 67 |
#' @description Remove a filter |
| 68 |
#' @param pos `character` or `integer` scalar. The name or index of the |
| 69 |
#' Filter to be removed. |
| 70 |
remove_filter = function(pos){ |
| 71 |
if (is.numeric(pos)) sort(pos, decreasing = TRUE) |
| 72 |
for (p in pos){ |
| 73 |
private[[".filters"]][[p]] <- NULL |
| 74 |
} |
| 75 |
invisible(self) |
| 76 |
}, |
| 77 |
|
| 78 |
|
| 79 |
#' @description Set or replace (all) Filters of parent object. See |
| 80 |
#' [EventFilter] for how Filters work. |
| 81 |
#' |
| 82 |
#' @param filters a `list` (named or unnamed) of [EventFilters][EventFilter] |
| 83 |
#' or predicate functions. See [is_filter()]. |
| 84 |
set_filters = function(filters){ |
| 85 |
filters <- standardize_filters_list(filters) |
| 86 |
private[[".filters"]] <- filters |
| 87 |
invisible(self) |
| 88 |
} |
| 89 |
), |
| 90 |
|
| 91 |
active = list( |
| 92 |
#' @field filters a `list` of all attached Filters. |
| 93 |
filters = function(){ |
| 94 |
get(".filters", private) |
| 95 |
} |
| 96 |
), |
| 97 |
|
| 98 |
private = list( |
| 99 |
.filters = list() |
| 100 |
) |
| 101 |
) |