Skip to content

Commit

Permalink
add param global.vars.field
Browse files Browse the repository at this point in the history
  • Loading branch information
Miachol committed Nov 13, 2018
1 parent c262b8d commit a95e9bb
Show file tree
Hide file tree
Showing 16 changed files with 86 additions and 18 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
@@ -1,3 +1,7 @@
2018-11-13 Li Jianfeng <lee_jianfeng@sjtu.edu.cn>

* add params global.vars.field

2018-09-19 Li Jianfeng <lee_jianfeng@sjtu.edu.cn>

* fix failed names selectioon in config.help
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
@@ -1,7 +1,7 @@
Package: configr
Type: Package
Title: An Implementation of Parsing and Writing Configuration File (JSON/INI/YAML/TOML)
Version: 0.3.3.0001
Version: 0.3.4
Authors@R: person("Jianfeng", "Li", email = "lee_jianfeng@sjtu.edu.cn", role = c("aut", "cre"), comment = c(ORCID = "0000-0003-2349-208X"))
Description:
Implements the JSON, INI, YAML and TOML parser for R setting and writing of configuration file. The functionality of this package is similar to that of package 'config'.
Expand All @@ -20,7 +20,7 @@ Imports:
stringr (>= 1.2.0),
utils,
glue
RoxygenNote: 6.0.1
RoxygenNote: 6.1.1
Suggests: testthat,
knitr,
rmarkdown,
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
@@ -1,3 +1,7 @@
# configr v0.3.4

- new feature `global.vars.field` in parse.extra replacing global vars in configuration

# configr v0.3.3

- new function `str2config` to parse configuration string to R list object.
Expand Down
22 changes: 20 additions & 2 deletions R/parse.R
Expand Up @@ -7,7 +7,8 @@
#' @param bash.parse Logical wheather parse '#>#echo $HOME#<#' in config to your HOME PATH
#' @param glue.parse Logical wheather parse '!!glue{1:5}' in config to ['1','2','3','4','5'];
#' ['nochange', '!!glue(1:5)', 'nochange'] => ['nochange', '1', '2', '3', '4', '5', 'nochange']
#' @param glue.flag A character flage indicating wheater run glue() function to parse (Default is !!glue)
#' @param glue.flag A character flag indicating wheater run glue() function to parse (Default is !!glue)
#' @param global.vars.field All vars defined in global.vars.field will as the extra.list params [gloval_var]
#' @return A list
#' @export
#' @examples
Expand All @@ -31,7 +32,7 @@
#' list.raw <- list(glue = raw, nochange = 1:10)
#' parsed <- parse.extra(list.raw, glue.parse = TRUE)
parse.extra <- function(config, extra.list = list(), other.config = "", rcmd.parse = FALSE,
bash.parse = FALSE, glue.parse = FALSE, glue.flag = "!!glue") {
bash.parse = FALSE, glue.parse = FALSE, glue.flag = "!!glue", global.vars.field = "global_vars") {
if (length(config) == 0) {
return(config)
}
Expand All @@ -50,6 +51,9 @@ parse.extra <- function(config, extra.list = list(), other.config = "", rcmd.par
if (glue.parse) {
config <- parse.extra.glue(config, glue.flag = glue.flag)
}
if (is.character(global.vars.field)) {
config <- parse.extra.global.vars(config, global.vars.field = global.vars.field)
}
return(config)
}

Expand Down Expand Up @@ -271,6 +275,20 @@ str2multiple <- function(input = "", glue.flag = "!!glue") {
}
}

parse.extra.global.vars <- function(config, global.vars.field = "global_vars") {
if (is.character(global.vars.field) && global.vars.field %in% names(config) &&
length(config[[global.vars.field]]) > 0) {
global.vars <- config[[global.vars.field]]
extra.list <- paste0(global.vars, '="', unname(sapply(config[names(config) %in% global.vars],
function(x){return(x)[1]})), '"', collapse = ", ")
extra.list <- eval(parse(text = sprintf("list(%s)", extra.list)))
config <- parse.extra(config, extra.list = extra.list, global.vars.field = NULL)
return(config)
} else {
return(config)
}
}

# Use glue to parse character
parse.extra.glue <- function(config, glue.flag = "!!glue") {
list.names <- 1:length(config)
Expand Down
5 changes: 3 additions & 2 deletions R/read.R
Expand Up @@ -11,6 +11,7 @@
#' @param glue.parse Logical wheather parse '!!glue{1:5}' in config to ['1','2','3','4','5'];
#' ['nochange', '!!glue(1:5)', 'nochange'] => ['nochange', '1', '2', '3', '4', '5', 'nochange']
#' @param glue.flag A character flage indicating wheater run glue() function to parse (Default is !!glue)
#' @param global.vars.field All vars defined in global.vars.field will as the extra.list params [gloval_var]
#' @param file.type Default is no need to specify the variable, file.type will be automatically
#' identify by \code{\link{get.config.type}}. If the value be specified, the step of filetype identification will be skipped.
#' @param ... Arguments for \code{\link{get.config.type}},
Expand All @@ -36,7 +37,7 @@
#' config.extra.parsed.2 <- read.config(config.json, list(debug = 'TRUE'), other.config)
read.config <- function(file = Sys.getenv("R_CONFIGFILE_ACTIVE", "config.cfg"), extra.list = list(),
other.config = "", rcmd.parse = FALSE, bash.parse = FALSE, glue.parse = FALSE,
glue.flag = "!!glue", file.type = NULL, ...) {
glue.flag = "!!glue", global.vars.field = "global_vars", file.type = NULL, ...) {
status <- check.file.parameter(file)
if (status == FALSE) {
return(FALSE)
Expand All @@ -49,7 +50,7 @@ read.config <- function(file = Sys.getenv("R_CONFIGFILE_ACTIVE", "config.cfg"),
}
config.list <- get.config.list(file = file, file.type = file.type, extra.list = extra.list,
other.config = other.config, rcmd.parse = rcmd.parse, bash.parse = bash.parse,
glue.parse = glue.parse, glue.flag = glue.flag, ...)
glue.parse = glue.parse, glue.flag = glue.flag, global.vars.field = global.vars.field, ...)
return(config.list)
}

Expand Down
4 changes: 2 additions & 2 deletions R/utils.R
Expand Up @@ -34,7 +34,7 @@ config.funs.par <- function(fun = "", ...) {
# Function get config.list
get.config.list <- function(file, file.type = "json", json.file.debug = FALSE, ini.file.debug = FALSE,
yaml.file.debug = FALSE, toml.file.debug = FALSE, extra.list = list(), other.config = "",
rcmd.parse = FALSE, bash.parse = FALSE, glue.parse = FALSE, glue.flag = "!!glue",
rcmd.parse = FALSE, bash.parse = FALSE, glue.parse = FALSE, glue.flag = "!!glue", global.vars.field = "global_vars",
...) {
if (file.type == "json") {
readLines.par <- config.funs.par("readLines", ...)
Expand Down Expand Up @@ -122,7 +122,7 @@ get.config.list <- function(file, file.type = "json", json.file.debug = FALSE, i
return(FALSE)
} else {
config.list <- parse.extra(config.list, extra.list, other.config, rcmd.parse,
bash.parse, glue.parse, glue.flag)
bash.parse, glue.parse, glue.flag, global.vars.field)
return(config.list)
}
}
Expand Down
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -49,6 +49,7 @@ config.json <- system.file('extdata', 'config.json', package='configr')
config.ini <- system.file('extdata', 'config.ini', package='configr')
config.yaml <- system.file('extdata', 'config.yaml', package='configr')
config.toml <- system.file('extdata', 'config.toml', package='configr')
config.glob <- system.file('extdata', 'config.global.toml', package='configr')

# Check specific configuration file type
is.json <- is.json.file(file = config.json)
Expand Down Expand Up @@ -188,6 +189,14 @@ parsed <- parse.extra(list.raw, glue.parse = TRUE, glue.flag = "!!glue")
expect.parsed.1 <- c("a", "1", "2", "3", "4", "5", "c")
expect.parsed.2 <- list(glue = expect.parsed.1, nochange = 1:10)


# Global vars parse (new feature in v0.3.4)
#
```
config_no_parsed_global <- read.config(config.glob, global.vars.field = NULL)
config_parsed <- read.config(config.glob)
```
# Delete a section in a configuration object
config.partial <- config.sections.del(config.1, "default")
```
Expand Down
9 changes: 9 additions & 0 deletions inst/extdata/config.global.toml
@@ -0,0 +1,9 @@
title = "Demo of global vars of configuration files"

global_vars = ["gvar_1", "gvar_2"]
gvar_1 = "G1"
gvar_2 = "G2"

[subsection]
value_1 = "{{gvar_1}}/value_1"
value_2 = "{{gvar_2}}/value_2"
5 changes: 3 additions & 2 deletions man/eval.config.Rd

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

4 changes: 2 additions & 2 deletions man/eval.config.sections.Rd

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

4 changes: 2 additions & 2 deletions man/fetch.config.Rd

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

3 changes: 2 additions & 1 deletion man/get.config.type.Rd

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

6 changes: 4 additions & 2 deletions man/parse.extra.Rd

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

4 changes: 3 additions & 1 deletion man/read.config.Rd

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

10 changes: 10 additions & 0 deletions tests/testthat/test_k_parse.R
Expand Up @@ -3,6 +3,7 @@ config.yaml <- system.file("extdata", "config.yaml", package = "configr")
config.ini <- system.file("extdata", "config.ini", package = "configr")
config.toml <- system.file("extdata", "config.toml", package = "configr")
config.bio <- system.file("extdata", "complex.toml", package = "configr")
config.global <- system.file("extdata", "config.global.toml", package = "configr")
config.list <- list(ini = config.ini, json = config.json, toml = config.toml, yaml = config.yaml)

test_that("parse.config", {
Expand Down Expand Up @@ -78,6 +79,15 @@ test_that("parse.extra glue", {
}
})

test_that("parse.extra global var", {
config <- read.config(config.global)
expect_that(config$subsection$value_1, equals("G1/value_1"))
expect_that(config$subsection$value_2, equals("G2/value_2"))
config <- read.config(config.global, global.vars.field = NULL)
expect_that(config$subsection$value_1, equals("{{gvar_1}}/value_1"))
expect_that(config$subsection$value_2, equals("{{gvar_2}}/value_2"))
})

test_that("BioInstaller parse", {
config <- read.config(config.bio, rcmd.parse = TRUE, glue.parse = TRUE, extra.list = list(version = 'v1.1.0'))
expect_that(is.list(config), equals(TRUE))
Expand Down
7 changes: 7 additions & 0 deletions vignettes/configr.Rmd
Expand Up @@ -32,6 +32,7 @@ config.json <- system.file('extdata', 'config.json', package='configr')
config.ini <- system.file('extdata', 'config.ini', package='configr')
config.yaml <- system.file('extdata', 'config.yaml', package='configr')
config.toml <- system.file('extdata', 'config.toml', package='configr')
config.glob <- system.file('extdata', 'config.global.toml', package='configr')
```

## Check the configuration file type
Expand Down Expand Up @@ -133,6 +134,7 @@ configr own several userful extra parse function, you can use the `parse.extra`
- `rcmd.parse` can be used to parse the value of `@>@str_replace('config','g$','gr')@<@` to `configr` if you setted `rcmd.parse = TRUE`.
- `bash.parse` can be used to parse the value of `#>#echo bash#<#` to `bash` if you setted `bash.parse = TRUE`.
- `glue.parse` can be used to paste the value of `!!glue {1:5}` to `["1", "2", "3", "4", "5"]`; `!!glue_numeric {1:5}` to [1, 2, 3, 4, 5]
- `global.vars.field` can be used to parse the internal values using the given top level fields default use `global_vars` (new feature in v0.3.4)

**Note:** `glue.parse` using the `glue` package `glue` function to do that. Just like glue('{1:5}') and be processed by unname(unlist(x)).
The `!!glue` can be changed if you setted `glue.flag`. It is a remarkable fact that only contain the `glue.flag` character be parsed and the order of item will be changed if the `glue` result were multiple values. e.g. `['{a}', '!!glue {1:5}', '{{a}}']` will be parsed to `['{a}', '1', '2', '3', '4', '5', '{{a}}']`
Expand Down Expand Up @@ -173,6 +175,11 @@ expect.parsed.1 <- c("a", "1", "2", "3", "4", "5", "c")
expect.parsed.2 <- list(glue = expect.parsed.1, nochange = 1:10)
parse.extra(list.raw, glue.parse = TRUE, glue.flag = "!!glue")
read.config(config.glob, global.vars.field = NULL)
read.config(config.glob)
```

## External urls about configuration format and others
Expand Down

0 comments on commit a95e9bb

Please sign in to comment.