Cascading scoped value lookup for Swift Configuration.
CascadingProvider is a Swift Configuration provider that falls back to less
specific keys when its upstream providers have no value for the requested key
(e.g., a.b.setting, then a.setting, then setting), so that a setting can
be set once for every scope, then overridden for a specific scope.
The API documentation describes every public symbol.
import CascadingConfiguration
let config = ConfigReader(
provider: CascadingProvider(
providers: .init(EnvironmentVariablesProvider(), baseScope: ["tool"]),
.init(try await FileProvider<JSONSnapshot>(filePath: "tool.json")),
),
)
// Tries, in order:
// 1. TOOL_INSTALL_LIMIT from the environment
// 2. "install.limit" from the file
// 3. TOOL_LIMIT from the environment
// 4. "limit" from the file
let limit = config.int(forKey: "install.limit")FileProvider<JSONSnapshot> needs the JSON trait of Swift Configuration (see
Traits).
CascadingConfiguration re-exports Swift Configuration, so
import CascadingConfiguration also imports Configuration.
A cascading lookup treats every component of a key except the last as a scope. Key specificity takes precedence over provider order: each step of a cascade removes 1 more scope from the key of every upstream provider, then queries each upstream provider whose floors (see Limiting the Cascade) allow its shorter key, in the order in which they were provided, before the next step.
The encoded key of a lookup's result also names its upstream provider (by index
& name) & the key that the upstream provider was asked for, so that, e.g., an
AccessReporter reports which key of which upstream provider supplied each
value:
providers[0]: EnvironmentVariablesProvider: tool.install.limit as TOOL_INSTALL_LIMIT
An upstream provider can have a base scope (e.g., ["tool"]): scopes that are
prepended to every key that it is asked for, like a namespace. Unlike a key
prefix (see ConfigProvider.prefixKeys(with:)), a base scope can be removed by
a cascade, but only once no other scopes remain & only down to a separate floor:
the base scope cascade scope floor, the minimum number of components of its base
scope that a key tried by the cascade keeps. That floor applies only if the
cascade scope floor (see Limiting the Cascade) is 0;
the default base scope cascade scope floor, .noCascading, keeps the whole base
scope, while 0 lets the cascade remove it (e.g., 0 reaches PAGER for
pager, with base scope ["tool"]). A key overrides that floor via its
context:
// Tries TOOL_PAGER, then PAGER, in an environment provider whose base scope is ["tool"]
let pager = config.string(forKey: .init("pager", context: .baseScopeCascadeScopeFloor(0)))A key overrides the base scope of upstream providers via its context, too: an empty base scope removes every base scope, while any other base scope replaces each base scope that is not empty:
// Looks up only PAGER, even in an environment provider whose base scope is ["tool"]
let pager = config.string(forKey: .init("pager", context: .baseScope([])))A cascade scope floor is the minimum number of scopes, not counting the
components of its base scope, that a key tried by the cascade keeps: 0 (the
default) cascades until no scopes remain, while .noCascading turns cascading
off. Each upstream provider has its own floors (e.g.,
.init(file, cascadeScopeFloor: 1), or
.init(env, baseScope: ["tool"], baseScopeCascadeScopeFloor: 0)), which a key
overrides via its context:
// Tries TOOL_A_B_SETTING, "a.b.setting", TOOL_A_SETTING, then "a.setting"
let setting = config.string(forKey: .init("a.b.setting", context: .cascadeScopeFloor(1)))Chain these context functions to set several entries (e.g.,
.cascadeScopeFloor(1).baseScope(["git"])), or call them on a context that
holds other entries (e.g., ["environment": "production"].baseScope([])).
CascadingProvider is an ordinary provider, so a ConfigReader can combine it
with others. A provider that follows it is only consulted after the whole
cascade finds no value.
CascadingProvider removes the cascade context (the floors & the base scope)
from the context of each key before it queries its upstream providers; wrap each
other provider in the same ConfigReader (except another CascadingProvider or
a provider that wraps one, either of which would then ignore the cascade
context) with ignoringCascadeContext(), so that the cascade context cannot
affect its lookups:
let config = ConfigReader(
providers: [
CommandLineArgumentsProvider().ignoringCascadeContext(), // Exact keys only
CascadingProvider(providers: .init(env), .init(userFile)), // Cascades across both
CascadingProvider(providers: .init(defaultsFile)), // Cascades after the above find nothing
],
)ignoringCascadeContextUnlessCascading wraps each provider that needs it, once
passingCascadeContext() marks each provider that wraps a CascadingProvider:
let providers: [any ConfigProvider] = [
CommandLineArgumentsProvider(),
CascadingProvider(providers: .init(env), .init(userFile)).prefixKeys(with: "tool").passingCascadeContext(),
]
let config = ConfigReader(providers: providers.map(\.ignoringCascadeContextUnlessCascading))CommandLineArgumentsProvider needs the CommandLineArguments trait of Swift
Configuration (see Traits).
Snapshots, fetching values, watching values & watching snapshots follow the same
cascade as a synchronous lookup. When an upstream provider throws an error for a
key that the cascade tries, CascadingProvider propagates it instead of the
result of any later provider or key.
Add the package to the dependencies of your Package.swift:
.package(url: "https://github.com/ro-ss/swift-cascading-configuration", .upToNextMinor(from: "0.1.0")),Until version 1.0.0, each minor version (e.g., 0.2.0) may break the API, so
.upToNextMinor(from: "0.1.0") accepts only 0.1 versions (e.g., 0.1.1).
Then add the CascadingConfiguration product to the dependencies of each target
that uses it:
.product(name: "CascadingConfiguration", package: "swift-cascading-configuration"),CascadingConfiguration enables no trait of Swift Configuration, so that a
package that depends on it enables only the traits that it uses. A package
enables each trait that it uses (e.g., JSON for JSONSnapshot, or
CommandLineArguments for CommandLineArgumentsProvider) by depending on Swift
Configuration directly:
.package(
url: "https://github.com/apple/swift-configuration",
from: "1.0.2",
traits: ["CommandLineArguments", "JSON"],
),| Requirement | Minimum |
|---|---|
| Build | Swift 6.2 (6.3 for Android) or Xcode 26 |
| Run | macOS 15, iOS 18, tvOS 18, watchOS 11, visionOS 2, Linux, Windows, or Android API 28 |
| Swift Configuration | 1.0.2 (1.3.0 for Windows) |
Swift Cascading Configuration declares no platforms, so a package that
supports older platforms can depend on CascadingConfiguration, each of whose
declarations is available wherever Swift Configuration is.
Pull requests are welcome; see the contribution guidelines & the code of conduct. Report vulnerabilities as described in the security policy.
Swift Cascading Configuration is available under the MIT License, except for the files copied from other projects, which the notices list with their licenses.