Skip to content

Commit 8c492c0

Browse files
committed
switch order of new and old names in transmform_event_names
1 parent 9fe2764 commit 8c492c0

File tree

2 files changed

+3
-79
lines changed

2 files changed

+3
-79
lines changed

R/LayoutJson.R

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ LayoutJson <- R6::R6Class(
6363
#'
6464
#' @param transform_event_names
6565
#' * `NULL`: don't process names
66-
#' * a named `character` vector where the names are the original field names
67-
#' and the values the desired new field names,
66+
#' * a named `character` vector of the format `new_name = old_name`
6867
#' * or a `function` with a single mandatory argument that accepts a
6968
#' `character` vector of field names. Applied after `transform_event`.
7069
#'
@@ -241,8 +240,8 @@ apply_event_name_transformer = function(x, f){
241240
}
242241

243242
if (is.character(f)){
244-
rename_idx <- match(x, names(f), nomatch = 0L)
245-
x[rename_idx > 0L] <- f[rename_idx[rename_idx > 0L]]
243+
rename_idx <- match(x, f, nomatch = 0L)
244+
x[rename_idx > 0L] <- names(f)[rename_idx[rename_idx > 0L]]
246245
return(x)
247246
}
248247

tests/testthat/test_LayoutJson.R

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -90,81 +90,6 @@ test_that("formatting timestamps with LayoutJson works", {
9090
})
9191

9292

93-
test_that("LayoutJson.format_event() - with default settings - format event correctly", {
94-
95-
# Arrange
96-
event <- LogEvent$new(
97-
logger = Logger$new("dum/my"),
98-
level = 200L,
99-
timestamp = structure(1541175573.9308, class = c("POSIXct", "POSIXt")),
100-
caller = NA_character_,
101-
msg = "foo bar",
102-
rawMsg = "foobar-raw"
103-
)
104-
105-
lo <- LayoutJson$new(
106-
transform_event_names = c(
107-
"msg" = "content",
108-
"level" = "loglevel",
109-
"logger" = "log.logger",
110-
"caller" = "code.function",
111-
"rawMsg" = "log.record.template"
112-
),
113-
excluded_fields = NULL
114-
)
115-
116-
# Act
117-
res <- jsonlite::fromJSON(lo$format_event(event))
118-
119-
# Assert
120-
expect_setequal(
121-
names(res),
122-
c("content",
123-
"loglevel",
124-
"log.logger",
125-
"timestamp",
126-
"code.function",
127-
"log.record.template"))
128-
})
129-
130-
131-
test_that("LayoutJson.format_event() - with excluded fields - excludes field correctly", {
132-
133-
# Arrange
134-
event <- LogEvent$new(
135-
logger = Logger$new("dum/my"),
136-
level = 200L,
137-
timestamp = structure(1541175573.9308, class = c("POSIXct", "POSIXt")),
138-
caller = NA_character_,
139-
msg = "foo bar",
140-
rawMsg = "foobar-raw"
141-
)
142-
143-
lo <- LayoutJson$new(
144-
transform_event_names = c(
145-
"msg" = "content",
146-
"level" = "loglevel",
147-
"logger" = "log.logger",
148-
"caller" = "code.function",
149-
"rawMsg" = "log.record.template"
150-
),
151-
excluded_fields = c("log.record.template", "code.function")
152-
)
153-
154-
# Act
155-
res <- jsonlite::fromJSON(lo$format_event(event))
156-
157-
# Assert
158-
expect_setequal(
159-
names(res),
160-
c("content", "loglevel", "log.logger", "timestamp"))
161-
162-
expect_identical(res$content, "foo bar")
163-
expect_identical(res$loglevel, 200L)
164-
expect_identical(res$log.logger, "dum/my")
165-
})
166-
167-
16893
test_that("LayoutJson.format_event() - with field rename function - renames fields correctly", {
16994

17095
# Arrange

0 commit comments

Comments
 (0)