webmockr icon indicating copy to clipboard operation
webmockr copied to clipboard

Finish off the query_mapper function?

Open sckott opened this issue 1 year ago • 1 comments

webmock has quite a bit of logic in their QueryMapper class https://github.com/bblimke/webmock/blob/9ff63ac7c845a75278e43236e0128e197f597462/lib/webmock/util/query_mapper.rb#L4

Right now ours is basically a dummy function that returns itself unless NULL.

Not sure I have the time or if it makes sense to port that line for line. Perhaps it makes more sense to build on something else if it exists or not, not sure if its worth it yet

I am pretty sure though that we likely do not have good behavior for more complicated request bodies

sckott avatar Oct 08 '24 22:10 sckott

started working on the port - still not sure if worth doing, but here's the work

QueryMapper <- R6::R6Class(
  "QueryMapper",
  public = list(
    #' @field pattern a list
    query = NULL,

    #' @description query to values
    #' @param query (character) a string
    #' @return xx
    initialize = function(query) {
      self$query <- query
    },

    #' @description Match a request body pattern against a pattern
    #' @param options (character) options
    #' @return list
    #' @examples
    #' query = "?one=1&two=2&three=3"
    query_to_values = function(query, options = list()) {
      if (rlang::is_empty(query)) return(NULL)
      nots <- c("flat", "dot", "subscript", "flat_array")
      options$notation <- options[["notation"]] %||% "subscript"
      if (!options$notation %in% nots) {
        rlang::abort(
          glue("Notation must be one of: {paste(nots, collapse=', ')}")
        )
      }
      empty_accumulator <- ifelse("flat_array" == options$notation, "[]", "{}")
      query_array <- self$collect_query_parts(query)
      query_hash <- self$collect_query_hash(query_array, empty_accumulator, options)
      self$normalize_query_hash(query_hash, empty_accumulator, options)
    },

    #' @description normalize query list
    #' @param query (character) a string
    #' @return list
    #' @examples
    #' query = "?one=1&two=2&three=3"
    normalize_query_hash = function(query_hash, empty_accumulator, options) {
      query_hash.inject(empty_accumulator.dup) do |accumulator, (key, value)|
        if options[:notation] == :flat_array
          accumulator << [key, value]
        else
          accumulator[key] = value.kind_of?(Hash) ? dehash(value) : value
        end
        accumulator
      end
    },

    #' @description collect parts
    #' @param query (character) a string
    #' @return list
    #' @examples
    #' query = "?one=1&two=2&three=3"
    collect_query_parts = function(query) {
      tmp <- cc(lapply(strsplit(query, "&"), \(w) {
        if (!rlang::is_empty(w)) {
          strsplit(w, "=")
        }
      }))
      unlist(tmp, recursive = FALSE)
    },

    #' @description collect query as a list
    #' @param query_list (list) a list of vectors
    #' @param empty_accumulator (character) accumulator when empty string
    #' @param options (list) a list of options
    #' @return list
    #' @examples
    #' query = "?one=1&two=2&three=3"
    #' empty_accumulator = '{}'
    collect_query_hash = function(query_list, empty_accumulator, options) {
      query_list <- cc(query_list)
      for (item in query_list) {
        value <- if (rlang::is_empty(item[2])) {
          NULL
        } else {
          curl::curl_unescape(sub("+", " ", item[2]))
        }
        key <- curl::curl_unescape(item[1])
        # key <- iconv(key, Encoding(key), "ASCII")
        fill_accumulator_for_subscript(accumulator, key, value)
      }
    },
  )
)

sckott avatar Oct 09 '24 17:10 sckott