Structure
FileProvider
A configuration provider that reads from a file on disk using a configurable snapshot type.
- iOS 18.0+
- macOS 15.0+
- tvOS 18.0+
- visionOS 2.0+
- watchOS 11.0+
struct FileProvider<Snapshot> where Snapshot : FileConfigSnapshot
Mentioned In
Overview
FileProvider is a generic file-based configuration provider that works with different file formats by using different snapshot types that conform to FileConfigSnapshot. This allows for a unified interface for reading JSON, YAML, or other structured configuration files.
Usage
Create a provider by specifying the snapshot type and file path:
// Using with a JSON snapshot
let jsonProvider = try await FileProvider<JSONSnapshot>(
filePath: "/etc/config.json"
)
// Using with a YAML snapshot
let yamlProvider = try await FileProvider<YAMLSnapshot>(
filePath: "/etc/config.yaml"
)
The provider reads the file once during initialization and creates an immutable snapshot of the configuration values. For auto-reloading behavior, use ReloadingFileProvider.
Configuration from a reader
You can also initialize the provider using a configuration reader that specifies the file path through environment variables or other configuration sources:
let envConfig = ConfigReader(provider: EnvironmentVariablesProvider())
let provider = try await FileProvider<JSONSnapshot>(config: envConfig)
This expects a filePath key in the configuration that specifies the path to the file. For a full list of configuration keys, check out init(snapshotType:parsingOptions:config:).
Topics
Creating a file provider
Reading configuration files
Relationships
Conforms To