{"id":13995242,"url":"https://github.com/wlisac/environment","last_synced_at":"2025-10-15T13:10:41.829Z","repository":{"id":63921244,"uuid":"198764138","full_name":"wlisac/environment","owner":"wlisac","description":"Type-safe environment variables in Swift.","archived":false,"fork":false,"pushed_at":"2020-01-21T06:22:39.000Z","size":325,"stargazers_count":30,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-08-16T15:50:44.551Z","etag":null,"topics":["environment-variables","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wlisac.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-07-25T05:35:52.000Z","updated_at":"2024-03-04T04:10:17.000Z","dependencies_parsed_at":"2023-01-14T14:15:33.201Z","dependency_job_id":null,"html_url":"https://github.com/wlisac/environment","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wlisac%2Fenvironment","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wlisac%2Fenvironment/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wlisac%2Fenvironment/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wlisac%2Fenvironment/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wlisac","download_url":"https://codeload.github.com/wlisac/environment/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225063624,"owners_count":17415130,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["environment-variables","swift"],"created_at":"2024-08-09T14:03:19.167Z","updated_at":"2025-10-15T13:10:36.796Z","avatar_url":"https://github.com/wlisac.png","language":"Swift","readme":"# 🌳 Environment\n\n![swift](https://img.shields.io/badge/Swift-5.0%20%7C%205.1-orange.svg)\n![platforms](https://img.shields.io/badge/platforms-macOS%20%7C%20Linux%20%7C%20iOS%20%7C%20tvOS%20%7C%20watchOS-lightgrey.svg)\n![version](https://img.shields.io/badge/version-0.11.1-blue.svg)\n[![twitter](https://img.shields.io/badge/twitter-@wlisac-blue.svg)](https://twitter.com/wlisac)\n\u003cbr\u003e\n[![build](https://travis-ci.com/wlisac/environment.svg?branch=master)](https://travis-ci.com/wlisac/environment)\n[![jazzy](https://raw.githubusercontent.com/wlisac/environment/gh-pages/badge.svg?sanitize=true)](https://wlisac.github.io/environment/Structs/Environment.html)\n[![codecov](https://img.shields.io/codecov/c/github/wlisac/environment)](https://codecov.io/gh/wlisac/environment)\n\nWelcome to **Environment** – a nicer, type-safe way of working with environment variables in Swift.\n\n## Usage\n\n### Access Environment Variables\n\nThe `Environment` struct provides a type-safe API to get and set environment variables.\n\n```swift\nimport Environment\n\nlet environment = Environment.environment\n\n// Get \"HOST\" as String value\nlet host = environment[\"HOST\"]\n\n// Set \"PORT\" to String value of \"8000\"\nenvironment[\"PORT\"] = \"8000\"\n```\n\nEnvironment variables are accessed as `String` values by default, but can be [converted](#type-safe-variables) to any type that conforms to `EnvironmentStringConvertible`.\n\n```swift\n// Get \"HOST\" as URL value\nlet host: URL? = environment[\"HOST\"]\n\n// Get \"PORT\" as Int value\nlet port: Int? = environment[\"PORT\"]\n\n// Set \"PORT\" to Int value of 8000\nenvironment[\"PORT\"] = 8000\n\n// Get \"APP_ID\" as UUID value or use a default UUID if \"APP_ID\" is not set\nlet appID = environment[\"APP_ID\"] ?? UUID()\n```\n\n### Dynamic Member Lookup\n\nThe `Environment` struct also supports accessing environment variables using `@dynamicMemberLookup`.\n\n```swift\n// Get \"HOST\" as URL value using dynamic member lookup\nlet host: URL? = environment.HOST\n\n// Set \"PORT\" to Int value of 8000 using dynamic member lookup\nenvironment.PORT = 8000\n```\n\n### Static Subscripts\n\nThe `Environment` struct supports [static subscript](https://github.com/apple/swift-evolution/blob/master/proposals/0254-static-subscripts.md) access to environment variables in Swift 5.1.\n\n```swift\nimport Environment\nimport Foundation\n\n// Get \"HOST\" as URL value using static subscript\nlet host: URL? = Environment[\"HOST\"]\n\n// Set \"PORT\" to Int value of 8000 using static subscript\nEnvironment[\"PORT\"] = 8000\n```\n\n`@dynamicMemberLookup` also works with static subscripts.\n\n```swift\n// Get \"HOST\" as URL value using static dynamic member lookup\nlet host: URL? = Environment.HOST\n\n// Set \"PORT\" to Int value of 8000 using static dynamic member lookup\nEnvironment.PORT = 8000\n```\n\n### Property Wrappers\n\nThe `EnvironmentVariable` [property wrapper](https://github.com/apple/swift-evolution/blob/master/proposals/0258-property-wrappers.md) enables properties to be backed by environment variables in Swift 5.1.\n\nThe following example shows how to use the `EnvironmentVariable` property wrapper to expose static properties backed by enviornment variables (`\"HOST\"` and `\"PORT\"`).\n\n```swift\nenum ServerSettings {\n    @EnvironmentVariable(name: \"HOST\")\n    static var host: URL?\n    \n    @EnvironmentVariable(name: \"PORT\", defaultValue: 8000)\n    static var port: Int\n}\n```\n\n### Type-Safe Variables\n\nEnvironment variables can be converted from a `String` representation to any type that conforms to the `EnvironmentStringConvertible` protocol.\n\nStandard Library and Foundation types like `Int`, `Float`, `Double`, `Bool`, `URL`, `UUID`, `Data`, and more are already extended to conform to `EnvironmentStringConvertible`. Collection types like  `Array`, `Set`, and `Dictionary` are also extended with conditional conformance.\n\nYou can add conformance to other classes, structures, or enumerations to enable additional types to be used as environment variables.\n\n**Custom `EnvironmentStringConvertible` Conformance**\n\n```swift\nenum Beverage {\n    case coffee\n    case tea\n}\n\nextension Beverage: EnvironmentStringConvertible {\n    init?(environmentString: String) {\n        switch environmentString {\n        case \"coffee\":\n            self = .coffee\n        case \"tea\":\n            self = .tea\n        default:\n            return nil\n        }\n    }\n    \n    var environmentString: String {\n        switch self {\n        case .coffee:\n            return \"coffee\"\n        case .tea:\n            return \"tea\"\n        }\n    }\n}\n\nlet beverage: Beverage? = environment[\"DEFAULT_BEVERAGE\"]\n```\n\n**Default `EnvironmentStringConvertible` Conformance**\n\nA default implementation of `EnvironmentStringConvertible` is provided for types that already conform to `LosslessStringConvertible` or `RawRepresentable`.\n\nFor example, `String`-backed enums are `RawRepresentable` and may use the default implementation for `EnvironmentStringConvertible` conformance.\n\n```swift\nenum CompassPoint: String {\n    case north\n    case south\n    case east\n    case west\n}\n\nextension CompassPoint: EnvironmentStringConvertible { }\n\nlet defaultDirection: CompassPoint? = environment[\"DEFAULT_DIRECTION\"]\n```\n\n## API Documentation\n\nVisit the [online API reference](https://wlisac.github.io/environment/Structs/Environment.html) for full documentation of the public API.\n\n## Installation\n\nEnvironment requires Xcode 10 or a Swift 5 toolchain with the Swift Package Manager. \n\n### Swift Package Manager\n\nAdd the Environment package as a dependency to your `Package.swift` file.\n\n```swift\n.package(url: \"https://github.com/wlisac/environment.git\", from: \"0.11.1\")\n```\n\nAdd Environment to your target's dependencies.\n\n```swift\n.target(name: \"Example\", dependencies: [\"Environment\"])\n```\n","funding_links":[],"categories":["Swift"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwlisac%2Fenvironment","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwlisac%2Fenvironment","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwlisac%2Fenvironment/lists"}