Skip to content

SwiftPM BuildToolPlugin for type-safe configuration schemas #116

@yuukiw00w

Description

@yuukiw00w

swift-configuration is focused on runtime configuration, which works very well for feature flags and other dynamic settings.

In client applications such as iOS apps, however, many configuration values are effectively fixed per build artifact and rarely change at runtime.
For these cases, having stronger static guarantees—such as type-safe keys and accessors—can improve safety and maintainability.

Proposal

I would like to propose using a Swift Package Manager Build Tool Plugin to generate type-safe configuration schemas at build time.
The idea is to read configuration files (e.g. YAML or plist) and generate:

  • type information
  • key definitions
  • schema / accessors

at build time.
Important constraints of this proposal:

  • The plugin would generate only schemas, key definitions, and accessors
  • Actual values are still resolved at runtime via existing providers

This keeps the runtime-focused design of swift-configuration intact, while adding stronger static guarantees for configurations that are effectively build-time–fixed.

Possible Interface

As a concrete example, the plugin could generate typed keys like the following:

public struct ConfigKey<Value> {
  public let name: String
}

public enum AppConfigKeys {
  public static let httpTimeout = ConfigKey<Int>(name: "http.timeout")
  public static let apiBaseURL  = ConfigKey<String>(name: "api.base_url")
}

ConfigReader could then provide additional APIs that accept these typed keys:

extension ConfigReader {
  func value<T>(_ key: ConfigKey<T>, default defaultValue: T) -> T
  func value<T>(_ key: ConfigKey<T>) -> T?
}

Usage would look like this:

let config = ConfigReader(provider: EnvironmentVariablesProvider())
let httpTimeout = config.value(AppConfigKeys.httpTimeout, default: 60)

This approach:

  • avoids stringly-typed keys and typos
  • allows the expected value type to be checked at compile time
  • can coexist with the existing int(forKey:), string(forKey:), etc. APIs

Metadata

Metadata

Assignees

No one assigned

    Labels

    status/triageCollecting information required to triage the issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions