Skip to content

Commit

Permalink
fix #189 serialization: change deserialize_path to deserialize and re…
Browse files Browse the repository at this point in the history
…move deserialize_string
  • Loading branch information
sckott committed Nov 17, 2020
1 parent 3f040fc commit 521a124
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 173 deletions.
2 changes: 1 addition & 1 deletion R/cassette_class.R
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ Cassette <- R6::R6Class(
#' @description get http interactions from the cassette via the serializer
#' @return list
deserialized_hash = function() {
tmp <- self$serializer$deserialize_path()
tmp <- self$serializer$deserialize()
if (inherits(tmp, "list")) {
return(tmp)
} else {
Expand Down
2 changes: 1 addition & 1 deletion R/configuration.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
#' the replacement and then when reading from the cassette we do the reverse
#' replacement to get back to the real data. Before record replacement happens
#' in internal function `write_interactions()`, while before playback
#' replacement happens in internal function `YAML$deserialize_path()`
#' replacement happens in internal function `YAML$deserialize()`
#'
#' ## Errors
#'
Expand Down
14 changes: 3 additions & 11 deletions R/serializer.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,28 @@ Serializer <- R6::R6Class("Serializer",
file_extension = NULL,
#' @field path (character) full path to the yaml file
path = NULL,
#' @field string (character) path string
string = NULL,

#' @description Create a new YAML object
#' @param file_extension (character) A file extension
#' @param path (character) path to the cassette, excluding the cassette
#' directory and the file extension. only use if not passing a string
#' @param string (character) path string. only use if not passing a path
#' directory and the file extension
#' @return A new `YAML` object
initialize = function(file_extension = NULL, path = NULL, string = NULL) {
initialize = function(file_extension = NULL, path = NULL) {
self$file_extension <- file_extension
if (is.null(path)) {
self$path <- paste0(cassette_path(), "/", basename(tempfile()), self$file_extension)
} else {
self$path <- paste0(cassette_path(), "/", path, self$file_extension)
}
self$string <- string
},
#' @description Serializes a hash - REPLACED BY YAML/JSON METHODS
#' @param x (list) the object to serialize
#' @param path (character) the file path
#' @param bytes (logical) whether to preserve exact body bytes or not
#' @return (character) the YAML or JSON string to write to disk
serialize = function(x, path, bytes) {},
#' @description Deserializes a string - REPLACED BY YAML/JSON METHODS
#' @param string (character) the YAML or JSON string
#' @return (list) the deserialized object, an R list
deserialize_string = function(string = NULL) {},
#' @description Serializes a file - REPLACED BY YAML/JSON METHODS
deserialize_path = function() {}
deserialize = function() {}
),
private = list(
strip_newlines = function(x) {
Expand Down
23 changes: 6 additions & 17 deletions R/serializers-json.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,16 @@
#' fun <- ww$serialize()
#' fun(list(http_interactions = list(response = list(body = "bar"))),
#' path = ww$path, bytes = FALSE)
#' ww$deserialize_path()
#' ww$deserialize_string(string = '["hey", "hi", "hello"]')
#' ww$deserialize_string(string = '{"foo": "bar"}')
#' ww$deserialize()
#' }
JSON <- R6::R6Class("JSON",
inherit = Serializer,
public = list(
#' @description Create a new `JSON` object
#' @param path (character) full path to the yaml file
#' @param string (character) path string
#' @return A new `JSON` object
initialize = function(path = NULL, string = NULL) {
super$initialize(".json", path, string)
initialize = function(path = NULL) {
super$initialize(".json", path)
},

#' @description Serializes the given hash using internal fxn write_json
Expand All @@ -34,18 +31,10 @@ JSON <- R6::R6Class("JSON",
}
},

#' @description Deserializes the given string using jsonlite::fromJSON
#' @param string (character) the YAML string
#' @description Deserializes the content at the file path using
#' jsonlite::fromJSON
#' @return (list) the deserialized object, an R list
deserialize_string = function(string = NULL) {
if (is.null(self$string)) str <- string else self$string
if (is.null(str)) stop("Must pass a string", call. = FALSE)
jsonlite::fromJSON(str)
},

#' @description Deserializes the given string using jsonlite::fromJSON
#' @return (list) the deserialized object, an R list
deserialize_path = function() {
deserialize = function() {
str <- sensitive_put_back(readLines(self$path))
tmp <- jsonlite::fromJSON(str, FALSE)
private$process_body(tmp)
Expand Down
25 changes: 7 additions & 18 deletions R/serializers-yaml.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,17 @@
#' fun <- yy$serialize()
#' fun(list(http_interactions = list(response = list(body = "bar"))),
#' path = yy$path, bytes = FALSE)
#' yy$deserialize_path()
#' yy$deserialize_string(string = "- hey\n- hi\n- hello")
#' yy$deserialize_string(string = "- foo\n- bar\n- 3.14")
#' yy$deserialize()
#' }
YAML <- R6::R6Class("YAML",
inherit = Serializer,
public = list(
#' @description Create a new YAML object
#' @param path (character) path to the cassette, excluding the cassette
#' directory and the file extension. only use if not passing a string
#' @param string (character) path string. only use if not passing a path
#' directory and the file extension
#' @return A new `YAML` object
initialize = function(path = NULL, string = NULL) {
super$initialize(".yml", path, string)
initialize = function(path = NULL) {
super$initialize(".yml", path)
},

#' @description Serializes the given hash using internal fxn write_yaml
Expand All @@ -36,18 +33,10 @@ YAML <- R6::R6Class("YAML",
}
},

#' @description Deserializes the given string using yaml::yaml.load
#' @param string (character) the YAML string
#' @description Deserializes the content at the path using
#' yaml::yaml.load_file
#' @return (list) the deserialized object, an R list
deserialize_string = function(string = NULL) {
str <- if (is.null(self$string)) string else self$string
if (is.null(str)) stop("Must pass a string", call. = FALSE)
yaml::yaml.load(str)
},

#' @description Deserializes the given string using yaml::yaml.load_file
#' @return (list) the deserialized object, an R list
deserialize_path = function() {
deserialize = function() {
str <- sensitive_put_back(readLines(self$path))
tmp <- yaml::yaml.load(str)
private$process_body(tmp)
Expand Down
42 changes: 9 additions & 33 deletions man/JSON.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 7 additions & 32 deletions man/Serializer.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 10 additions & 34 deletions man/YAML.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/vcr_configure.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 521a124

Please sign in to comment.