Skip to content

Commit

Permalink
Simplify API for subscripting
Browse files Browse the repository at this point in the history
  • Loading branch information
ollieatkinson committed Jun 17, 2022
1 parent 831e100 commit 6d33bbb
Showing 1 changed file with 5 additions and 87 deletions.
92 changes: 5 additions & 87 deletions Sources/AnyCoding/util/Subscript.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,15 @@ enum OptionalSubscriptError: Error {
}

extension Optional where Wrapped == Any {

subscript(first: AnyCodingKey, rest: AnyCodingKey...) -> Any? {
get { self[[first] + rest] }
set { self[[first] + rest] = newValue }
}


subscript<C: Collection>(path: C) -> Any? where C.Element == CodingKey {
get { try? get(path, from: self) }
set { set(newValue, at: path, on: &self) }
}
}

extension Dictionary where Key == String, Value == Any {

subscript(first: AnyCodingKey, rest: AnyCodingKey...) -> Any? {
get { self[[first] + rest] }
set { self[[first] + rest] = newValue }
}


subscript<C: Collection>(path: C) -> Value? where C.Element == CodingKey {
get { try? get(path) }
set { set(newValue, at: path) }
Expand All @@ -48,12 +38,7 @@ extension Dictionary where Key == String, Value == Any {
}

extension Array where Element == Any {

subscript(first: AnyCodingKey, rest: AnyCodingKey...) -> Any? {
get { self[[first] + rest] }
set { self[[first] + rest] = newValue }
}


subscript<C: Collection>(path: C) -> Element? where C.Element == CodingKey {
get { try? get(path) }
set { set(newValue, at: path) }
Expand Down Expand Up @@ -100,8 +85,8 @@ private func get<T, C: Collection>(
from any: Any?,
as _: T.Type = T.self
) throws -> T? where C.Element == CodingKey {
let any: Any = try _get(path, from: any)
return try (any as? T).or(throw: OptionalSubscriptError.message("\(type(of: any)) is not \(T.self)"))
return try (_get(path, from: any) as? T)
.or(throw: OptionalSubscriptError.message("\(type(of: any)) is not \(T.self)"))
}

private func _get<C: Collection>(_ path: C, from any: Any?) throws -> Any where C.Element == CodingKey {
Expand Down Expand Up @@ -147,70 +132,3 @@ extension Collection {
return (head, dropFirst())
}
}

extension String {

fileprivate func splitDotPath() -> [String] {
isEmpty
? []
: split(separator: ".", omittingEmptySubsequences: true).map(String.init)
}
}

extension Collection where Element == String {
fileprivate var codingPath: [CodingKey] { map { AnyCodingKey($0) } }
}

extension Optional where Wrapped == Any {

subscript(first: String, rest: String...) -> Any? {
get { self[[first] + rest] }
set { self[[first] + rest] = newValue }
}

subscript(dotPath string: String) -> Any? {
get { self[string.splitDotPath()] }
set { self[string.splitDotPath()] = newValue }
}

subscript<C: Collection>(_ collection: C) -> Any? where C.Element == String {
get { self[collection.codingPath] }
set { self[collection.codingPath] = newValue }
}
}

extension Dictionary where Key == String, Value == Any {

subscript(first: String, rest: String...) -> Any? {
get { self[[first] + rest] }
set { self[[first] + rest] = newValue }
}

subscript(dotPath string: String) -> Any? {
get { self[string.splitDotPath()] }
set { self[string.splitDotPath()] = newValue }
}

subscript<C: Collection>(_ collection: C) -> Value? where C.Element == String {
get { self[collection.codingPath] }
set { self[collection.codingPath] = newValue }
}
}

extension Array where Element == Any {

subscript(first: String, rest: String...) -> Any? {
get { self[[first] + rest] }
set { self[[first] + rest] = newValue }
}

subscript(dotPath string: String) -> Any? {
get { self[string.splitDotPath()] }
set { self[string.splitDotPath()] = newValue }
}

subscript<C: Collection>(_ collection: C) -> Element? where C.Element == String {
get { self[collection.codingPath] }
set { self[collection.codingPath] = newValue }
}
}

0 comments on commit 6d33bbb

Please sign in to comment.