Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement new Compressed serializer #187

Open
sckott opened this issue Nov 14, 2020 · 3 comments
Open

Implement new Compressed serializer #187

sckott opened this issue Nov 14, 2020 · 3 comments
Milestone

Comments

@sckott
Copy link
Collaborator

sckott commented Nov 14, 2020

No description provided.

@sckott sckott added this to the v0.6 milestone Nov 14, 2020
@sckott
Copy link
Collaborator Author

sckott commented Nov 14, 2020

maybe use zip pkg, or maybe stick with utils::zip

@sckott
Copy link
Collaborator Author

sckott commented Nov 17, 2020

might bump to next milestone

@sckott sckott modified the milestones: v0.6, v0.7 Nov 17, 2020
@sckott
Copy link
Collaborator Author

sckott commented Nov 17, 2020

code so far:

#' @title The compressed serializer
#' @description class with methods for serializing via zip compression
#' @keywords internal
#' @examples \dontrun{
#' ww <- Compressed$new(path = "stuff3")
#' ww
#' ww$file_extension
#' fun <- ww$serialize()
#' fun(list(http_interactions = list(response = list(body = "bar"))),
#'   path = ww$path, bytes = FALSE)
#' ww$deserialize()
#' }
Compressed <- R6::R6Class("Compressed",
  inherit = Serializer,
  public = list(
    #' @description Create a new `Compressed` object
    #' @param path (character) full path to the yaml file
    #' @return A new `Compressed` object
    initialize = function(path = NULL) {
      super$initialize(".zz", path)
    },

    #' @description Serializes the given hash using internal fxn write_json
    #' @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 json string to write to disk
    serialize = function(x, path, bytes) {
      function(x, path, bytes) {
        z <- write_yaml2(x, path, bytes)
        zip::zip()
      }
    },

    #' @description Deserializes the given string using jsonlite::fromJSON
    #' @return (list) the deserialized object, an R list
    deserialize = function() {
      str <- sensitive_put_back(readLines(self$path))
      tmp <- jsonlite::fromJSON(str, FALSE)
      private$process_body(tmp)
    }
  )
)

# return list of yaml strings
write_yaml2 <- function(x, file, bytes) {
  write_header(file)
  lapply(x, write_interactions_yaml, bytes = bytes)
}
# return a string of yaml
write_interactions_yaml <- function(x, bytes) {
  z <- prep_interaction(x, file, bytes)
  ## hmm, not sure what to do here, need to remove separate write_header step
  ## and need to do probably list(http_interactions = z)
  ## but how do I do appending?
  tmp <- yaml::as.yaml(z)
  sensitive_remove(tmp)
}

@sckott sckott modified the milestones: v1.1, v2.0 Feb 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant