Skip to content

Commit

Permalink
Make Describing Fields More Robust to Different Objects
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenMMortimer committed May 14, 2019
1 parent 9c32119 commit 9c10b48
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
52 changes: 49 additions & 3 deletions R/read-metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,57 @@ sf_describe_object_fields <- function(object_name){

stopifnot(length(object_name) == 1)

obj_dat <- sf_describe_objects(object_names = object_name, api_type = "SOAP")[[1]]
obj_fields_list <- obj_dat[names(obj_dat) == "fields"] %>% map(collapse_list_with_dupe_names)

# suppress deprecation warnings
suppressWarnings(obj_fields_dat <- rforcecom.getObjectDescription(objectName=object_name))
obj_fields_dat <- obj_fields_dat %>%
as_tibble() %>%
type_convert(col_types=cols())
obj_fields_dat <- obj_fields_list %>%
# explicitly combine duplicated names because many tidyverse functions break whenever that occurs
map(collapse_list_with_dupe_names) %>%
# convert the fields, some simple datatypes, some complex datatypes (lists) into one row each
map_df(~as_tibble(modify_if(., ~(length(.x) > 1 | is.list(.x)), list)))

return(obj_fields_dat)
}

#' Collapse Elements in List with Same Name
#'
#' This function looks for instances of elements in a list that have the same name
#' and then combine them all into a single comma separated character string (referenceTo)
#' or \code{tbl_df} (picklistValues).
#'
#' @importFrom readr type_convert cols
#' @importFrom dplyr as_tibble
#' @param x list; a list, typically returned from the API that we would parse through
#' @note The tibble only contains the fields that the user can view, as defined by
#' the user's field-level security settings.
#' @return A \code{list} containing one row per field for the requested object.
#' @examples \dontrun{
#' obj_dat <- sf_describe_objects(object_names = object_name, api_type = "SOAP")[[1]]
#' obj_fields_list <- obj_dat[names(obj_dat) == "fields"] %>%
#' map(collapse_list_with_dupe_names)
#' }
#' @export
collapse_list_with_dupe_names <- function(x){
dupes_exist <- any(duplicated(names(x)))
if(dupes_exist){
dupe_field_names <- unique(names(x)[duplicated(names(x))])
for(f in dupe_field_names){
target_idx <- which(names(x) == f)
obj_field_dupes <- x[target_idx]
if(all(sapply(obj_field_dupes, length) == 1)){
collapsed <- paste0(unlist(obj_field_dupes), collapse = ",")
} else {
collapsed <- map_df(obj_field_dupes, as_tibble) %>%
type_convert(col_types = cols()) %>%
list()
}
# replace into first
x[head(target_idx,1)] <- collapsed
# remove the rest
x[tail(target_idx,-1)] <- NULL
}
}
return(x)
}
30 changes: 30 additions & 0 deletions man/collapse_list_with_dupe_names.Rd

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

0 comments on commit 9c10b48

Please sign in to comment.