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

Finish off the query_mapper function? #130

Open
sckott opened this issue Oct 8, 2024 · 1 comment
Open

Finish off the query_mapper function? #130

sckott opened this issue Oct 8, 2024 · 1 comment

Comments

@sckott
Copy link
Collaborator

sckott commented Oct 8, 2024

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
Copy link
Collaborator Author

sckott commented Oct 9, 2024

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)
      }
    },
  )
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant