this repo has no description
at main 60 lines 1.7 kB view raw
1// 2// StringCodingKey.swift 3// URLQueryItemCoder 4// 5// Created by Kyle Hughes on 1/15/23. 6// 7 8/// A basic coding key implementation for stringly-keyed collections. 9internal struct StringCodingKey { 10 /// The value to use in an integer-indexed collection (e.g. an int-keyed dictionary). 11 internal let intValue: Int? 12 13 /// The string to use in a named collection (e.g. a string-keyed dictionary). 14 internal let stringValue: String 15 16 // MARK: Private Initialization 17 18 private init(intValue: Int?, stringValue: String) { 19 self.intValue = intValue 20 self.stringValue = stringValue 21 } 22} 23 24// MARK: - CodingKey Extension 25 26extension StringCodingKey: CodingKey { 27 // MARK: Internal Initialization 28 29 /// Creates a new instance from the specified integer. 30 /// 31 /// - Parameter intValue: The integer value of the desired key. 32 internal init(intValue: Int) { 33 self.intValue = intValue 34 35 stringValue = String(intValue) 36 } 37 38 /// Creates a new instance from the given string. 39 /// 40 /// - Parameter stringValue: The string value of the desired key. 41 internal init(stringValue: String) { 42 self.stringValue = stringValue 43 44 intValue = nil 45 } 46} 47 48// MARK: - ExpressibleByStringLiteral Extension 49 50extension StringCodingKey: ExpressibleByStringLiteral { 51 // MARK: Internal Initialization 52 53 /// Creates an instance initialized to the given string value. 54 /// 55 /// - Parameter stringLiteral: The value of the new instance. 56 @inlinable 57 internal init(stringLiteral value: String) { 58 self.init(stringValue: value) 59 } 60}