Coder Social home page Coder Social logo

confx's Introduction

confx

Lifecycle: experimental CRAN status

Extends the scope of {config} by offering path-like retrieval, queries for unnamed config entities and referencing of entities. Config file content can be cached in-memory and can then either be retrieved from cache or from file as desired.

Installation

Development version from GitHub with:

remotes::install_github("rappster/confx")

Usage

TL;DR

Suppose you have a config file config.yml in your package’s root directory, you either call

conf_get("path/to/named/entity")

or

conf_get("path/to/unnamed/entity/with/query/<query>")

depending on what type of config entity you want to retrieve (named vs. unnamed entity)

For more detailed explanation of the package continue reading

Demo preliminaries

library(confx)
#> Warning: replacing previous import 'magrittr::set_names' by 'purrr::set_names'
#> when loading 'confx'

The package ships with a demo YAML config file:

(path_to_config <- fs::path_package("confx", "configs/config.yml"))
#> /media/janko/Shared/Code/R/Packages/confx/renv/library/R-4.0/x86_64-pc-linux-gnu/confx/configs/config.yml

You can use this file by setting the following environment variable:

Sys.setenv(R_CONFIG_DIR = fs::path_package("confx", "configs"))

Get named entities

Entire config content:

conf_get()
#> $host
#> $host$server_001
#> $host$server_001$url
#> [1] "https://dev-server-001.com"
#> 
#> $host$server_001$port
#> [1] 8000
#> 
#> 
#> 
#> $settings_versions
#> $settings_versions[[1]]
#> $settings_versions[[1]]$id
#> [1] "v1"
#> 
#> $settings_versions[[1]]$valid_from
#> [1] "2020-01-01"
#> 
#> $settings_versions[[1]]$valid_until
#> [1] "2020-03-31"
#> 
#> $settings_versions[[1]]$content
#> [1] "hello world!"
#> 
#> 
#> $settings_versions[[2]]
#> $settings_versions[[2]]$id
#> [1] "v2"
#> 
#> $settings_versions[[2]]$valid_from
#> [1] "2020-04-01"
#> 
#> $settings_versions[[2]]$valid_until
#> [1] "2020-09-30"
#> 
#> $settings_versions[[2]]$content
#> [1] "Hello World!"
#> 
#> 
#> $settings_versions[[3]]
#> $settings_versions[[3]]$id
#> [1] "v3"
#> 
#> $settings_versions[[3]]$valid_from
#> [1] "2020-10-01"
#> 
#> $settings_versions[[3]]$valid_until
#> [1] "2020-12-31"
#> 
#> $settings_versions[[3]]$content
#> [1] "HELLO WORLD!"
#> 
#> 
#> 
#> attr(,"config")
#> [1] "default"
#> attr(,"file")
#> [1] "/media/janko/Shared/Code/R/Packages/confx/renv/library/R-4.0/x86_64-pc-linux-gnu/confx/configs/config.yml"

Entity host:

conf_get("host")
#> $server_001
#> $server_001$url
#> [1] "https://dev-server-001.com"
#> 
#> $server_001$port
#> [1] 8000

Entity host but from different config environment:

conf_get("host", config = "prod")
#> $server_001
#> $server_001$url
#> [1] "https://prod-server-001.com"
#> 
#> $server_001$port
#> [1] 8000
#> 
#> 
#> $server_002
#> $server_002$url
#> [1] "https://prod-server-002.com"
#> 
#> $server_002$port
#> [1] 8000

Entity host/server_001:

conf_get("host/server_001")
#> $url
#> [1] "https://dev-server-001.com"
#> 
#> $port
#> [1] 8000

Entity host/server_001/url

conf_get("host/server_001/url")
#> [1] "https://dev-server-001.com"

Get unnamed entities

For unnamed entities (which parse into unnamed lists), you can specify a query consisting of a standard R expression written out as a string:

conf_get("settings_versions/id == 'v1'")
#> [[1]]
#> [[1]]$id
#> [1] "v1"
#> 
#> [[1]]$valid_from
#> [1] "2020-01-01"
#> 
#> [[1]]$valid_until
#> [1] "2020-03-31"
#> 
#> [[1]]$content
#> [1] "hello world!"
conf_get("settings_versions/valid_from >= '2020-03-01'")
#> [[1]]
#> [[1]]$id
#> [1] "v2"
#> 
#> [[1]]$valid_from
#> [1] "2020-04-01"
#> 
#> [[1]]$valid_until
#> [1] "2020-09-30"
#> 
#> [[1]]$content
#> [1] "Hello World!"
#> 
#> 
#> [[2]]
#> [[2]]$id
#> [1] "v3"
#> 
#> [[2]]$valid_from
#> [1] "2020-10-01"
#> 
#> [[2]]$valid_until
#> [1] "2020-12-31"
#> 
#> [[2]]$content
#> [1] "HELLO WORLD!"
conf_get("settings_versions/
  valid_from >= '2020-03-01' & 
  valid_until >= '2020-10-01'")
#> [[1]]
#> [[1]]$id
#> [1] "v3"
#> 
#> [[1]]$valid_from
#> [1] "2020-10-01"
#> 
#> [[1]]$valid_until
#> [1] "2020-12-31"
#> 
#> [[1]]$content
#> [1] "HELLO WORLD!"

DISCLAIMER

When I said standard R expressions, this does not yet reflect the full picture as I started with simple expressions as defined in valid_operators_logical()

confx:::valid_operators_logical()
#>      ==      !=    %in%   %!in%       <      <=       >      >= 
#>    "=="    "!="  "%in%" "%!in%"     "<"    "<="     ">"    ">="

In future releases, you will also (hopefully) be able to write something like this:

conf_get("settings_versions/
  stringr::str_detect(content, 'HELLO')")
#> NULL

Entity references

TODO

confx's People

Watchers

James Cloos avatar Janko Thyson avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.