Skip to content

Commit d6ac567

Browse files
authored
Merge pull request #289 from frictionlessdata/attributes
Use attributes
2 parents 97f91a2 + b887ac3 commit d6ac567

18 files changed

Lines changed: 107 additions & 71 deletions

NEWS.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,29 @@
11
# frictionless (development version)
22

3+
## For users
4+
5+
* `read_resource()` now supports reading from remote zip files, thanks to support in {vroom} (1.3.0) (#291).
36
* `resources()` is soft-deprecated, please use `resource_names()` instead (#282).
47
* `get_schema()` is soft-deprecated, please use `schema()` instead (#282).
5-
* `read_resource()` now supports reading from remote zip files, thanks to support in {vroom} (1.3.0) (#291).
8+
9+
## Changes for developers
10+
11+
* Internal frictionless properties are now _attributes_, to better separate them from public Data Package properties (#289). If you use these internal properties, then update:
12+
13+
```R
14+
package$directory
15+
r <- frictionless:::get_resource(package, "resource_name") # Internal function
16+
r$read_from
17+
```
18+
19+
to:
20+
21+
```R
22+
attr(package, "directory")
23+
r <- frictionless:::resource(package, "resource_name") # Renamed!
24+
attr(r, "data_location") # Renamed!
25+
```
26+
627
* frictionless now relies on R >= 4.1.0 (because of an indirect {vroom} dependency) (#291) and uses base pipes (`|>` rather than `%>%`) (#292).
728

829
# frictionless 1.2.1

R/check_package.R

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,28 @@ check_package <- function(package) {
4444
)
4545
}
4646

47-
# Check package has directory (character)
48-
if (!is.character(package$directory)) {
47+
# Check all resources (if any) have a name
48+
if (purrr::some(package$resources, ~ is.null(.x$name))) {
49+
cli::cli_abort(
50+
"All {.field resources} in {.arg package} must have a {.field name}
51+
property.",
52+
class = "frictionless_error_resources_without_name"
53+
)
54+
}
55+
56+
# Check package has directory attribute (character)
57+
if (!is.character(attr(package, "directory"))) {
4958
cli::cli_abort(
5059
c(
5160
general_message,
52-
"x" = "{.arg package} is missing a {.field directory} property or it is
61+
"x" = "{.arg package} is missing a {.field directory} attribute or it is
5362
not a character.",
5463
"i" = tip_message
5564
),
5665
class = "frictionless_error_package_invalid"
5766
)
5867
}
5968

60-
# Check all resources (if any) have a name
61-
if (purrr::some(package$resources, ~ is.null(.x$name))) {
62-
cli::cli_abort(
63-
"All {.field resources} in {.arg package} must have a {.field name}
64-
property.",
65-
class = "frictionless_error_resources_without_name"
66-
)
67-
}
68-
6969
# Warn if class is missing
7070
if (!"datapackage" %in% class(package)) {
7171
cli::cli_warn(

R/col_types.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ cols <- function(schema) {
1616

1717
# Create col_types
1818
col_types <- purrr::map(fields, field_to_col)
19-
# Assign names: list("name1" = <collector_character>, "name2" = ...)
19+
# Add names: list("name1" = <collector_character>, "name2" = ...)
2020
names(col_types) <- field_names
2121

2222
# Replicate structure of readr::col_spec
@@ -44,7 +44,7 @@ field_to_col <- function(field) {
4444
bare_number <- if (field$bareNumber %||% "" != FALSE) TRUE else FALSE
4545
format <- field$format %||% "default" # Undefined => default
4646

47-
# Assign types and formats
47+
# Add types and formats
4848
col_type <- switch(
4949
type,
5050
"string" = col_string(enum),

R/create_package.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
#' Initiates a Data Package object, either from scratch or from an existing
44
#' list.
55
#' This Data Package object is a list with the following characteristics:
6-
#' - A `datapackage` subclass.
76
#' - All properties of the original `descriptor`.
87
#' - A `resources` property, set to an empty list if undefined.
9-
#' - A `directory` property, set to `"."` for the current directory if
8+
#' - A `directory` attribute, set to `"."` for the current directory if
109
#' undefined.
1110
#' It is used as the base path to access resources with [read_resource()].
11+
#' - A `datapackage` subclass.
1212
#'
1313
#' See `vignette("data-package")` to learn how this function implements the
1414
#' Data Package standard.
@@ -36,9 +36,11 @@ create_package <- function(descriptor = NULL) {
3636
)
3737
}
3838

39-
# Add properties
39+
# Add resources property (also creates descriptor if NULL)
4040
descriptor$resources <- descriptor$resources %||% list()
41-
descriptor$directory <- descriptor$directory %||% "." # Current directory
41+
42+
# Add directory attribute
43+
attr(descriptor, "directory") <- attr(descriptor, "directory") %||% "."
4244

4345
# Add datapackage class
4446
if (!"datapackage" %in% class(descriptor)) {

R/read_from_path.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ read_from_path <- function(package, resource_name, col_select) {
3333
col_types <- cols(schema)
3434

3535
# Get dialect (can be NULL)
36-
dialect <- read_descriptor(resource$dialect, package$directory, safe = TRUE)
36+
dialect <- read_descriptor(
37+
resource$dialect,
38+
attr(package, "directory"),
39+
safe = TRUE
40+
)
3741
escape_backslash <- if (dialect$escapeChar %||% "not set" == "\\") {
3842
TRUE
3943
} else {

R/read_package.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ read_package <- function(file = "datapackage.json") {
4545
}
4646

4747
# Add directory
48-
descriptor$directory <- dirname(file) # Also works for URLs
48+
attr(descriptor, "directory") <- dirname(file) # Also works for URLs
4949

5050
# Create package
5151
create_package(descriptor)

R/read_resource.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,20 @@ read_resource <- function(package, resource_name, col_select = NULL) {
5050
resource <- resource(package, resource_name)
5151

5252
# Read data directly
53-
if (resource$read_from == "df") {
53+
data_location <- attr(resource, "data_location")
54+
if (data_location == "df") {
5455
df <- dplyr::as_tibble(resource$data)
5556

5657
# Read data from data
57-
} else if (resource$read_from == "data") {
58+
} else if (data_location == "data") {
5859
df <- do.call(
5960
function(...) rbind.data.frame(..., stringsAsFactors = FALSE),
6061
resource$data
6162
)
6263
df <- dplyr::as_tibble(df)
6364

6465
# Read data from path(s)
65-
} else if (resource$read_from == "path" || resource$read_from == "url") {
66+
} else if (data_location == "path" || data_location == "url") {
6667
df <- read_from_path(package, resource_name, col_select)
6768
}
6869
return(df)

R/resource.R

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#' described `resources`.
55
#'
66
#' @inheritParams read_resource
7-
#' @return List describing a Data Resource, with new property `read_from` to
8-
#' indicate how data should be read.
7+
#' @return List describing a Data Resource, with new attribute `data_location`
8+
#' to indicate how the data are attached.
99
#' If present, `path` will be updated to contain the full path(s).
1010
#' @family accessor functions
1111
#' @noRd
@@ -47,25 +47,26 @@ resource <- function(package, resource_name) {
4747
)
4848
}
4949

50-
# Assign read_from property (based on path, then df, then data)
50+
# Add data_location attribute (based on path, then df, then data)
5151
if (length(resource$path) != 0) {
5252
if (all(is_url(resource$path))) {
53-
resource$read_from <- "url"
53+
data_location <- "url"
5454
} else {
55-
resource$read_from <- "path"
55+
data_location <- "path"
5656
}
5757
# Expand paths to full paths, check if file exists and check path safety,
5858
# unless those paths were willingly added by user in add_resource()
5959
if (attr(resource, "path") %||% "" != "added") {
6060
resource$path <- purrr::map_chr(
61-
resource$path, ~ check_path(.x, package$directory, safe = TRUE)
61+
resource$path, ~ check_path(.x, attr(package, "directory"), safe = TRUE)
6262
)
6363
}
6464
} else if (is.data.frame(resource$data)) {
65-
resource$read_from <- "df"
65+
data_location <- "df"
6666
} else if (!is.null(resource$data)) {
67-
resource$read_from <- "data"
67+
data_location <- "data"
6868
}
69+
attr(resource, "data_location") <- data_location
6970

7071
return(resource)
7172
}

R/schema.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ schema <- function(package, resource_name) {
3939
class = "frictionless_error_resource_without_schema"
4040
)
4141
}
42-
schema <- read_descriptor(resource$schema, package$directory, safe = TRUE)
42+
schema <- read_descriptor(
43+
resource$schema,
44+
attr(package, "directory"),
45+
safe = TRUE
46+
)
4347

4448
# Check schema
4549
check_schema(schema)

R/write_package.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ write_package <- function(package, directory, compress = FALSE) {
7373
return_package <- package # Needs directory to remain valid
7474

7575
# Write datapackage.json
76-
package$directory <- NULL
76+
attr(package, "directory") <- NULL
7777
package_json <- jsonlite::toJSON(
7878
package,
7979
pretty = TRUE,

0 commit comments

Comments
 (0)