Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AnyEncodable support #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions AnyCodable/DecodingContainer+AnyCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ extension KeyedDecodingContainer {
let values = try nestedContainer(keyedBy: AnyCodingKey.self, forKey: key)
return try values.decode(type)
}

public func decode<T: Decodable>(_ type: [String: Any].Type, forKey key: KeyedDecodingContainer<K>.Key) throws -> [String: T] {
let values = try nestedContainer(keyedBy: AnyCodingKey.self, forKey: key)
return try values.decode(type)
}

/// Decodes a value of the given type for the given key, if present.
///
Expand Down Expand Up @@ -94,6 +99,12 @@ extension KeyedDecodingContainer {
try decodeNil(forKey: key) == false else { return nil }
return try decode(type, forKey: key)
}

public func decodeIfPresent<T: Decodable>(_ type: [String: Any].Type, forKey key: KeyedDecodingContainer<K>.Key) throws -> [String: T]? {
guard contains(key),
try decodeNil(forKey: key) == false else { return nil }
return try decode(type, forKey: key)
}
}

private extension KeyedDecodingContainer {
Expand All @@ -118,6 +129,16 @@ private extension KeyedDecodingContainer {
}
return dictionary
}

func decode<T: Decodable>(_ type: [String: Any].Type) throws -> [String: T] {
var dictionary: [String: T] = [:]
for key in allKeys {
if let object = try? decode(T.self, forKey: key) {
dictionary[key.stringValue] = object
}
}
return dictionary
}
}

private extension UnkeyedDecodingContainer {
Expand Down Expand Up @@ -145,3 +166,4 @@ private extension UnkeyedDecodingContainer {
return elements
}
}

16 changes: 16 additions & 0 deletions AnyCodable/EncodingContainer+AnyCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ private extension KeyedEncodingContainer where K == AnyCodingKey {
try encode(dict, forKey: key)
case let array as [Any]:
try encode(array, forKey: key)
case let encodable as AnyEncodable:
try encode(encodable, forKey: key)
default:
debugPrint("⚠️ Unsuported type!", v)
continue
Expand Down Expand Up @@ -113,6 +115,8 @@ private extension UnkeyedEncodingContainer {
case let array as [Any]:
var values = nestedUnkeyedContainer()
try values.encode(array)
case let encodable as AnyEncodable:
try encode(encodable)
default:
debugPrint("⚠️ Unsuported type!", v)
}
Expand All @@ -129,3 +133,15 @@ private extension UnkeyedEncodingContainer {
try container.encode(value)
}
}

public struct AnyEncodable: Encodable {

private let _encode: (Encoder) throws -> Void
public init<T: Encodable>(_ wrapped: T) {
_encode = wrapped.encode
}

public func encode(to encoder: Encoder) throws {
try _encode(encoder)
}
}