Skip to content

Instantly share code, notes, and snippets.

View aainaj's full-sized avatar

Aaina Jain aainaj

  • GoJek Tech
  • Bangalore, India
View GitHub Profile
@aainaj
aainaj / HTTPStatusCode.swift
Created July 9, 2020 04:08 — forked from ollieatkinson/HTTPStatusCode.swift
HTTP status codes as a Swift enum.
/// This is a list of Hypertext Transfer Protocol (HTTP) response status codes.
/// It includes codes from IETF internet standards, other IETF RFCs, other specifications, and some additional commonly used codes.
/// The first digit of the status code specifies one of five classes of response; an HTTP client must recognise these five classes at a minimum.
enum HTTPStatusCode: Int, Error {
/// The response class representation of status codes, these get grouped by their first digit.
enum ResponseType {
/// - informational: This class of status code indicates a provisional response, consisting only of the Status-Line and optional headers, and is terminated by an empty line.
case informational
@aainaj
aainaj / DefaultEmptyArray.swift
Created June 20, 2020 06:49
DefaultEmptyArray Property Wrapper
import Foundation
@propertyWrapper
public struct DefaultEmptyArray<Element: Codable>: Codable {
public var wrappedValue: [Element]
public init(wrappedValue: [Element]) {
self.wrappedValue = wrappedValue
}
@aainaj
aainaj / FailableCodableArray.swift
Last active June 20, 2020 06:47
FailableCodableArray Property wrapper
import Foundation
@propertyWrapper
struct FailableCodableArray<Element: Codable>: Codable {
private struct AnyDecodableValue: Codable {}
private struct FailableDecodableValue<Value: Codable>: Codable {
let value: Value
public init(from decoder: Decoder) throws {
@aainaj
aainaj / SortByKeyPath.swift
Created June 13, 2020 13:16
sort by keypath
extension Collection {
func sorted<Value: Comparable>(on property: KeyPath<Element, Value>, by areInIncreasingOrder: (Value, Value) -> Bool) -> [Element] {
return sorted { currentElement, nextElement in
areInIncreasingOrder(currentElement[keyPath: property], nextElement[keyPath: property])
}
}
}
extension MutableCollection where Self: RandomAccessCollection {
mutating func sort<Value: Comparable>(on property: KeyPath<Element, Value>, by order: (Value, Value) throws -> Bool) rethrows {
@aainaj
aainaj / ModifyStructPropertyUsingKeyPath.swift
Last active December 15, 2021 21:34
KeyPathEditable Complete Source code including consumption
enum KeyPathError: Error {
case unableToCast(String)
}
protocol KeyPathEditable {
func update<KeyPathType>(path: PartialKeyPath<Self>, to value: KeyPathType) throws -> Self
}
extension KeyPathEditable {
func update<KeyPathType>(path: PartialKeyPath<Self>, to value: KeyPathType) throws -> Self {
@aainaj
aainaj / ModifyClassPropertyUsingKeyPath.swift
Created June 13, 2020 12:43
ReferenceKeyPathEditable Complete Source code including consumption
enum KeyPathError: Error {
case unableToCast(String)
}
protocol ReferenceKeyPathEditable {
associatedtype Root
func update<KeyPathType>(type: Root, path: PartialKeyPath<Root>, to value: KeyPathType) throws
}
extension ReferenceKeyPathEditable {
@aainaj
aainaj / ConsumeReferenceKeyPathEditable.swift
Last active June 13, 2020 12:52
Consumption of ReferenceKeyPathEditable by Class
class Merchant: ReferenceKeyPathEditable {
typealias Root = Merchant
private(set) var name: String
private(set) var occupation: String
init(name: String, occupation: String) {
self.name = name
self.occupation = occupation
}
@aainaj
aainaj / ReferenceKeyPathEditable.swift
Last active June 13, 2020 12:36
ReferenceKeyPathEditable Protocol
enum KeyPathError: Error {
case unableToCast(String)
}
protocol ReferenceKeyPathEditable {
associatedtype Root
func update<KeyPathType>(type: Root, path: PartialKeyPath<Root>, to value: KeyPathType) throws
}
extension ReferenceKeyPathEditable {
@aainaj
aainaj / KeyPathEditable.swift
Created June 13, 2020 09:42
KeyPathEditable Protocol
enum KeyPathError: Error {
case unableToCast(String)
}
protocol KeyPathEditable {
func update<KeyPathType>(path: PartialKeyPath<Self>, to value: KeyPathType) throws -> Self
}
extension KeyPathEditable {
func update<KeyPathType>(path: PartialKeyPath<Self>, to value: KeyPathType) throws -> Self {
@aainaj
aainaj / ThreadSafeSingleton.swift
Created June 2, 2020 04:41
ThreadSafe Singleton
final class FeaturedTopics {
private let concurrentQueue = DispatchQueue(label: "concurrentQueue", attributes: .concurrent)
private var topics: [String: Any] = [:]
static let shared = FeaturedTopics()
private init() {}
subscript(key: String) -> Any? {
get {