Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Avoid using Foundation when it is unavailable #32

Merged
merged 2 commits into from
Apr 23, 2020
Merged
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
2 changes: 0 additions & 2 deletions Sources/AnyCodable/AnyCodable.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import Foundation

/**
A type-erased `Codable` value.

Expand Down
10 changes: 9 additions & 1 deletion Sources/AnyCodable/AnyDecodable.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#if canImport(Foundation)
import Foundation
#endif

/**
A type-erased `Decodable` value.
Expand Down Expand Up @@ -56,7 +58,11 @@ extension _AnyDecodable {
let container = try decoder.singleValueContainer()

if container.decodeNil() {
self.init(NSNull())
#if canImport(Foundation)
self.init(NSNull())
#else
self.init(Optional<Self>.none)
#endif
} else if let bool = try? container.decode(Bool.self) {
self.init(bool)
} else if let int = try? container.decode(Int.self) {
Expand All @@ -80,8 +86,10 @@ extension _AnyDecodable {
extension AnyDecodable: Equatable {
public static func == (lhs: AnyDecodable, rhs: AnyDecodable) -> Bool {
switch (lhs.value, rhs.value) {
#if canImport(Foundation)
case is (NSNull, NSNull), is (Void, Void):
return true
#endif
case let (lhs as Bool, rhs as Bool):
return lhs == rhs
case let (lhs as Int, rhs as Int):
Expand Down
14 changes: 11 additions & 3 deletions Sources/AnyCodable/AnyEncodable.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#if canImport(Foundation)
import Foundation
#endif

/**
A type-erased `Encodable` value.
Expand Down Expand Up @@ -56,11 +58,15 @@ extension _AnyEncodable {
var container = encoder.singleValueContainer()

switch value {
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
case let number as NSNumber:
try encode(nsnumber: number, into: &container)
#endif
case is NSNull, is Void:
#endif
#if canImport(Foundation)
case is NSNull:
try container.encodeNil()
#endif
case is Void:
try container.encodeNil()
case let bool as Bool:
try container.encode(bool)
Expand Down Expand Up @@ -90,10 +96,12 @@ extension _AnyEncodable {
try container.encode(double)
case let string as String:
try container.encode(string)
#if canImport(Foundation)
case let date as Date:
try container.encode(date)
case let url as URL:
try container.encode(url)
#endif
case let array as [Any?]:
try container.encode(array.map { AnyCodable($0) })
case let dictionary as [String: Any?]:
Expand Down