All Projects → wlisac → Environment

wlisac / Environment

Licence: mit
Type-safe environment variables in Swift.

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Environment

envmnt
Environment variables utility functions.
Stars: ✭ 16 (-33.33%)
Mutual labels:  environment-variables
Confex
Useful helper to read and use application configuration from environment variables.
Stars: ✭ 272 (+1033.33%)
Mutual labels:  environment-variables
Dotenv Flow
Loads environment variables from .env.[development|test|production][.local] files for Node.js® projects.
Stars: ✭ 537 (+2137.5%)
Mutual labels:  environment-variables
dotenv.net
A library to read .env files in a .NET Core environment
Stars: ✭ 126 (+425%)
Mutual labels:  environment-variables
envkeygo
EnvKey's official Go client library
Stars: ✭ 36 (+50%)
Mutual labels:  environment-variables
Dotenv Kotlin
🗝️ Dotenv is a module that loads environment variables from a .env file
Stars: ✭ 326 (+1258.33%)
Mutual labels:  environment-variables
awssecret2env
Convert secrets stored in AWS Secrets Manager to environment variables
Stars: ✭ 44 (+83.33%)
Mutual labels:  environment-variables
Go Envparser
a go generator based env to go-struct decoder.
Stars: ✭ 17 (-29.17%)
Mutual labels:  environment-variables
localenvironment
Simply and optionally apply environment variables if they exist in env.json.
Stars: ✭ 14 (-41.67%)
Mutual labels:  environment-variables
Sync Dotenv
Keep your .env in sync with .env.example
Stars: ✭ 393 (+1537.5%)
Mutual labels:  environment-variables
envfile
Parse and write environment files with Node.js
Stars: ✭ 42 (+75%)
Mutual labels:  environment-variables
envy
Use envy to manage environment variables with your OS keychain
Stars: ✭ 23 (-4.17%)
Mutual labels:  environment-variables
Envied
Ensures presence and type of your app's ENV-variables (mirror)
Stars: ✭ 327 (+1262.5%)
Mutual labels:  environment-variables
angular-environment
AngularJS Environment Plugin
Stars: ✭ 78 (+225%)
Mutual labels:  environment-variables
Environs
simplified environment variable parsing
Stars: ✭ 631 (+2529.17%)
Mutual labels:  environment-variables
gatsby-plugin-dynamic-routes
Creating dynamic routes based on your environment and/or renaming existing routes
Stars: ✭ 14 (-41.67%)
Mutual labels:  environment-variables
Ionic Environment Variables
Easy to use environment variables for Ionic3!
Stars: ✭ 278 (+1058.33%)
Mutual labels:  environment-variables
Parameterhandler
Composer script handling your ignored parameter file
Stars: ✭ 906 (+3675%)
Mutual labels:  environment-variables
Musthave
A Node.js helper module for checking object elements against a list of required elements.
Stars: ✭ 5 (-79.17%)
Mutual labels:  environment-variables
Psl
PHP Standard Library - a modern, consistent, centralized, well-typed set of APIs for PHP programmers.
Stars: ✭ 329 (+1270.83%)
Mutual labels:  environment-variables

🌳 Environment

swift platforms version twitter
build jazzy codecov

Welcome to Environment – a nicer, type-safe way of working with environment variables in Swift.

Usage

Access Environment Variables

The Environment struct provides a type-safe API to get and set environment variables.

import Environment

let environment = Environment.environment

// Get "HOST" as String value
let host = environment["HOST"]

// Set "PORT" to String value of "8000"
environment["PORT"] = "8000"

Environment variables are accessed as String values by default, but can be converted to any type that conforms to EnvironmentStringConvertible.

// Get "HOST" as URL value
let host: URL? = environment["HOST"]

// Get "PORT" as Int value
let port: Int? = environment["PORT"]

// Set "PORT" to Int value of 8000
environment["PORT"] = 8000

// Get "APP_ID" as UUID value or use a default UUID if "APP_ID" is not set
let appID = environment["APP_ID"] ?? UUID()

Dynamic Member Lookup

The Environment struct also supports accessing environment variables using @dynamicMemberLookup.

// Get "HOST" as URL value using dynamic member lookup
let host: URL? = environment.HOST

// Set "PORT" to Int value of 8000 using dynamic member lookup
environment.PORT = 8000

Static Subscripts

The Environment struct supports static subscript access to environment variables in Swift 5.1.

import Environment
import Foundation

// Get "HOST" as URL value using static subscript
let host: URL? = Environment["HOST"]

// Set "PORT" to Int value of 8000 using static subscript
Environment["PORT"] = 8000

@dynamicMemberLookup also works with static subscripts.

// Get "HOST" as URL value using static dynamic member lookup
let host: URL? = Environment.HOST

// Set "PORT" to Int value of 8000 using static dynamic member lookup
Environment.PORT = 8000

Property Wrappers

The EnvironmentVariable property wrapper enables properties to be backed by environment variables in Swift 5.1.

The following example shows how to use the EnvironmentVariable property wrapper to expose static properties backed by enviornment variables ("HOST" and "PORT").

enum ServerSettings {
    @EnvironmentVariable(name: "HOST")
    static var host: URL?
    
    @EnvironmentVariable(name: "PORT", defaultValue: 8000)
    static var port: Int
}

Type-Safe Variables

Environment variables can be converted from a String representation to any type that conforms to the EnvironmentStringConvertible protocol.

Standard 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.

You can add conformance to other classes, structures, or enumerations to enable additional types to be used as environment variables.

Custom EnvironmentStringConvertible Conformance

enum Beverage {
    case coffee
    case tea
}

extension Beverage: EnvironmentStringConvertible {
    init?(environmentString: String) {
        switch environmentString {
        case "coffee":
            self = .coffee
        case "tea":
            self = .tea
        default:
            return nil
        }
    }
    
    var environmentString: String {
        switch self {
        case .coffee:
            return "coffee"
        case .tea:
            return "tea"
        }
    }
}

let beverage: Beverage? = environment["DEFAULT_BEVERAGE"]

Default EnvironmentStringConvertible Conformance

A default implementation of EnvironmentStringConvertible is provided for types that already conform to LosslessStringConvertible or RawRepresentable.

For example, String-backed enums are RawRepresentable and may use the default implementation for EnvironmentStringConvertible conformance.

enum CompassPoint: String {
    case north
    case south
    case east
    case west
}

extension CompassPoint: EnvironmentStringConvertible { }

let defaultDirection: CompassPoint? = environment["DEFAULT_DIRECTION"]

API Documentation

Visit the online API reference for full documentation of the public API.

Installation

Environment requires Xcode 10 or a Swift 5 toolchain with the Swift Package Manager.

Swift Package Manager

Add the Environment package as a dependency to your Package.swift file.

.package(url: "https://github.com/wlisac/environment.git", from: "0.11.1")

Add Environment to your target's dependencies.

.target(name: "Example", dependencies: ["Environment"])
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].