Skip to content

Revisit whether a missing file should throw an error or default to empty in file providers #66

@czechboy0

Description

@czechboy0

When setting up a config provider hierarchy, you might treat config files as optional - read them if they're there, but ignore if they're missing. This allows a nice linear setup like this:

let config = ConfigReader(providers: [
    EnvironmentVariablesProvider(),
    JSONProvider(filePath: "config.json")
])

The above will throw an error if config.json is missing, so today you need to handle it manually with:

var providers: [any ConfigProvider] = [
    EnvironmentVariablesProvider()
]
if FileManager.default.fileExists(atPath: "config.json") {
    providers.append(JSONProvider(filePath: "config.json"))
}
let config = ConfigReader(providers: providers)

While not the end of the world, the verbosity and the repeating of the path makes this slightly painful.

Do we want to change the file providers to instead handle missing files gracefully, for example using an optional flag in the initializer that specifies the behavior?

let config = ConfigReader(providers: [
    EnvironmentVariablesProvider(),
    JSONProvider(filePath: "config.json", isRequired: true), // will throw if file is missing or malformed
    JSONProvider(filePath: "optional-config.json", isRequired: false), // will become an empty provider if file is missing or malformed
])

We'd still default isRequired: Bool = true, but folks can make it more graceful by setting it to false.

Opinions?

Metadata

Metadata

Assignees

Labels

kind/enhancementImprovements to existing feature.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions