MyNixOS website logo
Description

A Config Package with No "Con".

Work with configs with a source precedence. Either create own R6 instance or work with convenient functions at a package level.

fig

A Config Package with No "Con"

CRAN status R build status Codecov test coverage License: MIT

Installation

To install latest available version from GitHub use remotes package:

# install.packages("remotes")
remotes::install_github("TymekDev/fig")

Usage

fig can be used in two different ways:

  1. On a global level using fig_* functions
  2. On a class instance level

Check out the following sections to find the way that suits your needs.

Using fig_* Functions

This approach works with no additional prep. It is especially useful if you want to access stored values without the hassle to pass an argument through entire codebase. Values stored with fig_* live inside fig package making them accessible from any place in the code.

fig_store("foo", 123)
fig_get("foo") # == 123

For more examples see package documentation.

Using a Fig Class Instance

Using Fig class allows storing values separately in several distinct class instances. If you need a config separation in your code, then Fig class is for you. Note that, unlike with fig_* functions, you have to pass the instance around to access values stored in it.

fig <- Fig$new()
fig$store("foo", 123)
fig$get("foo") # == 123

fig2 <- Fig$new()$store("foo", 456)
fig2$get("foo") # == 456

For more examples see package documentation.

Features

Precedence

:information_source: This feature is useful if you want to override a stored value without introducing a code change (e.g. log level, API URL, ...).

fig supports a two level precedence. Every fig_get() function and get() method call performs a lookup in a following order:

  1. System environment variables
  2. Stored values
fig_store_many(foo = 123, bar = 456)
fig_get_many("foo", "bar") # == list(123, 456)

# Environment variable value gets picked over a stored value
withr::with_envvar(list(foo = "xyz"), {
  fig_get_many("foo", "bar")) # == list("xyz", 456)
})

fig_delete("foo")

# Environment variable value gets picked over a missing stored value
withr::with_envvar(list(foo = "xyz"), {
  fig_get("foo")) # == "xyz"
})

fig_get("foo") # == NULL

Notes

  • As seen in the above example fig does not perform any type coercion. System environment variables are returned as characters, regardless of stored value type.
  • Dots (.) in keys are replaced with underscores (_) during system environment lookup.
  • System environment lookup is case sensitive.

Environment Variable Prefix

This feature goes in pair with precedence. fig can be configured with env_prefix argument (default: ""). It can be provided via fig_configure() (or configure() method) or during Fig instance creation.

env_prefix determines value prepended to the key before performing a system environment lookup.

fig <- Fig$new(env_prefix = "RCONNECT_")
withr::with_envvar(list(RCONNECT_SERVER = "example.com", {
  fig$get("SERVER") # == "example.com"
})

Key Splitting

:information_source: This feature is useful if you want interact (dynamically) with a nested config without a hassle.

fig can be configured with split_on argument (default: "."). It can be provided via fig_configure() (or configure() method) or during Fig instance creation.

split_on determines a level delimiter for keys, i.e. with split_on set "foo.bar" is treated as bar nested under foo.

fig_store("foo", list(bar = 1))
fig_get("foo.bar") # == 1

# Storage
# └── foo
#     └── bar
#         └── 1

fig_configure(split_on = "") # Disable this functionality
fig_store("foo.bar", 2)
fig_get("foo.bar") # == 2

# Storage
# ├── foo
# │   └── bar
# │       └── 1
# └── foo.bar
#     └── 2

Note: this behavior currently is not supported by delete functions and methods.

Examples

Working with config

If you are using config package and would like to enjoy fig's features, then the suggested approach is to wrap your main config::get() call in fig_store_list() (or store_list() method).

# default:
#   foo: 123
fig_store_list(config::get())

fig_get("foo") # == 123

withr::with_envvar(list(foo = "xyz"), {
  fig_get("foo")) # == "xyz"
})

This way you can use fig functions to have precedence and key splitting working for retrieving values from your config.

About

Licensed under MIT. Inspired by viper. Written in Neovim. Tested with testthat. Used in radian.

Metadata

Version

1.0.0

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows