Skip to content
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
18 changes: 9 additions & 9 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,36 @@ let package = Package(
products: [
.library(
name: "KarrotCodableKit",
targets: ["KarrotCodableKit"]
),
targets: ["KarrotCodableKit"],
)
],
dependencies: [
.package(url: "https://github.com/swiftlang/swift-syntax.git", "509.0.0"..<"604.0.0"),
.package(url: "https://github.com/swiftlang/swift-syntax.git", "509.0.0"..<"604.0.0")
],
targets: [
.target(
name: "KarrotCodableKit",
dependencies: ["KarrotCodableKitMacros"]
dependencies: ["KarrotCodableKitMacros"],
),
.macro(
name: "KarrotCodableKitMacros",
dependencies: [
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
]
],
),
.testTarget(
name: "KarrotCodableKitTests",
dependencies: [
"KarrotCodableKit",
]
"KarrotCodableKit"
],
),
.testTarget(
name: "KarrotCodableMacrosTests",
dependencies: [
"KarrotCodableKitMacros",
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
]
],
),
]
],
)
77 changes: 37 additions & 40 deletions Sources/KarrotCodableKit/AnyCodable/AnyCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@

import Foundation

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

The `AnyCodable` type forwards encoding and decoding responsibilities
to an underlying value, hiding its specific underlying type.

You can encode or decode mixed-type values in dictionaries
and other collections that require `Encodable` or `Decodable` conformance
by declaring their contained type to be `AnyCodable`.

- SeeAlso: `AnyEncodable`
- SeeAlso: `AnyDecodable`
*/
/// A type-erased `Codable` value.
///
/// The `AnyCodable` type forwards encoding and decoding responsibilities
/// to an underlying value, hiding its specific underlying type.
///
/// You can encode or decode mixed-type values in dictionaries
/// and other collections that require `Encodable` or `Decodable` conformance
/// by declaring their contained type to be `AnyCodable`.
///
/// - SeeAlso: `AnyEncodable`
/// - SeeAlso: `AnyDecodable`
@frozen
public struct AnyCodable: Codable {
public let value: Any
Expand All @@ -35,47 +33,47 @@ extension AnyCodable: Equatable {
public static func == (lhs: AnyCodable, rhs: AnyCodable) -> Bool {
switch (lhs.value, rhs.value) {
case is (Void, Void):
return true
true
case (let lhs as Bool, let rhs as Bool):
return lhs == rhs
lhs == rhs
case (let lhs as Int, let rhs as Int):
return lhs == rhs
lhs == rhs
case (let lhs as Int8, let rhs as Int8):
return lhs == rhs
lhs == rhs
case (let lhs as Int16, let rhs as Int16):
return lhs == rhs
lhs == rhs
case (let lhs as Int32, let rhs as Int32):
return lhs == rhs
lhs == rhs
case (let lhs as Int64, let rhs as Int64):
return lhs == rhs
lhs == rhs
case (let lhs as UInt, let rhs as UInt):
return lhs == rhs
lhs == rhs
case (let lhs as UInt8, let rhs as UInt8):
return lhs == rhs
lhs == rhs
case (let lhs as UInt16, let rhs as UInt16):
return lhs == rhs
lhs == rhs
case (let lhs as UInt32, let rhs as UInt32):
return lhs == rhs
lhs == rhs
case (let lhs as UInt64, let rhs as UInt64):
return lhs == rhs
lhs == rhs
case (let lhs as Float, let rhs as Float):
return lhs == rhs
lhs == rhs
case (let lhs as Double, let rhs as Double):
return lhs == rhs
lhs == rhs
case (let lhs as String, let rhs as String):
return lhs == rhs
lhs == rhs
case (let lhs as [String: AnyCodable], let rhs as [String: AnyCodable]):
return lhs == rhs
lhs == rhs
case (let lhs as [AnyCodable], let rhs as [AnyCodable]):
return lhs == rhs
lhs == rhs
case (let lhs as [String: Any], let rhs as [String: Any]):
return NSDictionary(dictionary: lhs) == NSDictionary(dictionary: rhs)
NSDictionary(dictionary: lhs) == NSDictionary(dictionary: rhs)
case (let lhs as [Any], let rhs as [Any]):
return NSArray(array: lhs) == NSArray(array: rhs)
NSArray(array: lhs) == NSArray(array: rhs)
case is (NSNull, NSNull):
return true
true
default:
return false
false
}
}
}
Expand All @@ -84,11 +82,11 @@ extension AnyCodable: CustomStringConvertible {
public var description: String {
switch value {
case is Void:
return String(describing: nil as Any?)
String(describing: nil as Any?)
case let value as CustomStringConvertible:
return value.description
value.description
default:
return String(describing: value)
String(describing: value)
}
}
}
Expand All @@ -97,9 +95,9 @@ extension AnyCodable: CustomDebugStringConvertible {
public var debugDescription: String {
switch value {
case let value as CustomDebugStringConvertible:
return "AnyCodable(\(value.debugDescription))"
"AnyCodable(\(value.debugDescription))"
default:
return "AnyCodable(\(description))"
"AnyCodable(\(description))"
}
}
}
Expand All @@ -113,7 +111,6 @@ extension AnyCodable: ExpressibleByStringInterpolation {}
extension AnyCodable: ExpressibleByArrayLiteral {}
extension AnyCodable: ExpressibleByDictionaryLiteral {}


extension AnyCodable: Hashable {
public func hash(into hasher: inout Hasher) {
switch value {
Expand Down
68 changes: 33 additions & 35 deletions Sources/KarrotCodableKit/AnyCodable/AnyDecodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,33 @@

import Foundation

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

The `AnyDecodable` type forwards decoding responsibilities
to an underlying value, hiding its specific underlying type.

You can decode mixed-type values in dictionaries
and other collections that require `Decodable` conformance
by declaring their contained type to be `AnyDecodable`:

let json = """
{
"boolean": true,
"integer": 42,
"double": 3.141592653589793,
"string": "string",
"array": [1, 2, 3],
"nested": {
"a": "alpha",
"b": "bravo",
"c": "charlie"
},
"null": null
}
""".data(using: .utf8)!

let decoder = JSONDecoder()
let dictionary = try! decoder.decode([String: AnyDecodable].self, from: json)
*/
/// A type-erased `Decodable` value.
///
/// The `AnyDecodable` type forwards decoding responsibilities
/// to an underlying value, hiding its specific underlying type.
///
/// You can decode mixed-type values in dictionaries
/// and other collections that require `Decodable` conformance
/// by declaring their contained type to be `AnyDecodable`:
///
/// let json = """
/// {
/// "boolean": true,
/// "integer": 42,
/// "double": 3.141592653589793,
/// "string": "string",
/// "array": [1, 2, 3],
/// "nested": {
/// "a": "alpha",
/// "b": "bravo",
/// "c": "charlie"
/// },
/// "null": null
/// }
/// """.data(using: .utf8)!
///
/// let decoder = JSONDecoder()
/// let dictionary = try! decoder.decode([String: AnyDecodable].self, from: json)
@frozen
public struct AnyDecodable: Decodable {
public let value: Any
Expand Down Expand Up @@ -80,7 +78,7 @@ extension _AnyDecodable {
} else {
throw DecodingError.dataCorruptedError(
in: container,
debugDescription: "AnyDecodable value cannot be decoded"
debugDescription: "AnyDecodable value cannot be decoded",
)
}
}
Expand Down Expand Up @@ -135,11 +133,11 @@ extension AnyDecodable: CustomStringConvertible {
public var description: String {
switch value {
case is Void:
return String(describing: nil as Any?)
String(describing: nil as Any?)
case let value as CustomStringConvertible:
return value.description
value.description
default:
return String(describing: value)
String(describing: value)
}
}
}
Expand All @@ -148,9 +146,9 @@ extension AnyDecodable: CustomDebugStringConvertible {
public var debugDescription: String {
switch value {
case let value as CustomDebugStringConvertible:
return "AnyDecodable(\(value.debugDescription))"
"AnyDecodable(\(value.debugDescription))"
default:
return "AnyDecodable(\(description))"
"AnyDecodable(\(description))"
}
}
}
Expand Down
Loading
Loading