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
55 changes: 39 additions & 16 deletions Sources/PotentCodables/AnyValue/AnyValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ public enum AnyValue: Sendable {
return uint
}

public func integerValue<I: FixedWidthInteger>(_ type: I.Type) -> I? {
public typealias AnyIntegerType = FixedWidthInteger

public func integerValue<I: AnyIntegerType>(_ type: I.Type) -> I? {
switch self {
case .int8(let value): return I(value)
case .int16(let value): return I(value)
Expand Down Expand Up @@ -262,7 +264,9 @@ public enum AnyValue: Sendable {
return decimal
}

public func floatingPointValue<F: BinaryFloatingPoint & LosslessStringConvertible>(_ type: F.Type) -> F? {
public typealias AnyFloatingPointType = BinaryFloatingPoint & LosslessStringConvertible

public func floatingPointValue<F: AnyFloatingPointType>(_ type: F.Type) -> F? {
switch self {
case .int8(let value): return F(value)
case .int16(let value): return F(value)
Expand All @@ -280,6 +284,17 @@ public enum AnyValue: Sendable {
return nil
}
}

public func numericValue<N: Numeric>(_ type: N.Type) -> N? {
switch type {
case let floatType as any AnyFloatingPointType.Type:
return floatingPointValue(floatType) as? N
case let intType as any AnyIntegerType.Type:
return integerValue(intType) as? N
default:
return nil
}
}
}


Expand Down Expand Up @@ -339,23 +354,31 @@ extension AnyValue {
switch value {
case let val as AnyValue: return val
case let val as String: return .string(val)
case let val as Int: return .int(val)
case let val as UInt: return .uint(val)
case let val as Bool: return .bool(val)
case let val as Int8: return .int8(val)
case let val as UInt8: return .uint8(val)
case let val as Int16: return .int16(val)
case let val as UInt16: return .uint16(val)
case let val as Int32: return .int32(val)
case let val as UInt32: return .uint32(val)
case let val as Int64: return .int64(val)
case let val as UInt64: return .uint64(val)
case let val as Decimal: return .decimal(val) // Before other floats (Swift Decimal -> Double allowed)
case let val as Float16: return .float16(val)
case let val as Float: return .float(val)
case let val as Double: return .double(val)
case let val as any FixedWidthInteger:
switch val {
case let val as Int8: return .int8(val)
case let val as UInt8: return .uint8(val)
case let val as Int16: return .int16(val)
case let val as UInt16: return .uint16(val)
case let val as Int32: return .int32(val)
case let val as UInt32: return .uint32(val)
case let val as Int64: return .int64(val)
case let val as UInt64: return .uint64(val)
case let val as Int: return .int(val)
case let val as UInt: return .uint(val)
default: throw Error.unsupportedValue(value)
}
case let val as BigInt: return .integer(val)
case let val as BigUInt: return .unsignedInteger(val)
case let val as any BinaryFloatingPoint:
switch val {
case let val as Float16: return .float16(val)
case let val as Float32: return .float(val)
case let val as Float64: return .double(val)
default: throw Error.unsupportedValue(value)
}
case let val as Decimal: return .decimal(val) // Before other floats (Swift Decimal -> Double allowed)
case let val as Data: return .data(val)
case let val as URL: return .url(val)
case let val as UUID: return .uuid(val)
Expand Down
3 changes: 2 additions & 1 deletion Sources/PotentCodables/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public extension DecodingError {
else {
if let nonOptionalValue = value {
return String(describing: type(of: nonOptionalValue))
} else {
}
else {
return "nil"
}
}
Expand Down
4 changes: 0 additions & 4 deletions Sources/PotentJSON/JSONReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ internal struct JSONReader {
struct UTF8Source {
let buffer: UnsafeBufferPointer<UInt8>

init(buffer: UnsafeBufferPointer<UInt8>) {
self.buffer = buffer
}

func takeASCII(_ input: Index) -> (UInt8, Index)? {
guard hasNext(input) else {
return nil
Expand Down
4 changes: 2 additions & 2 deletions Tests/ASN1AnyStringTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class ASN1AnyStringTests: XCTestCase {

func testCodable() {

XCTAssertEqual(try JSONEncoder().encode(AnyString("test")), #""test""#.data(using: .utf8))
XCTAssertEqual(try JSONDecoder().decode(AnyString.self, from: #""test""#.data(using: .utf8)!), AnyString("test"))
XCTAssertEqual(try JSONEncoder().encode(AnyString("test")), Data(#""test""#.utf8))
XCTAssertEqual(try JSONDecoder().decode(AnyString.self, from: Data(#""test""#.utf8)), AnyString("test"))
}

func testUppercased() {
Expand Down
2 changes: 1 addition & 1 deletion Tests/ASN1AnyValueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class ASN1AnyValueTests: XCTestCase {
var pndec: AnyValue = .decimal(Decimal(sign: .plus, exponent: -3, significand: 1234567))
var npdec: AnyValue = .decimal(Decimal(sign: .minus, exponent: 1, significand: 1234567))
var nndec: AnyValue = .decimal(Decimal(sign: .minus, exponent: -3, significand: 1234567))
var data: AnyValue = .data("Binary Data".data(using: .utf8)!)
var data: AnyValue = .data(Data("Binary Data".utf8))
var url: AnyValue = .url(URL(string: "https://example.com/some/thing")!)
var uuid: AnyValue = .uuid(UUID(uuidString: "46076D06-86E8-4B3B-80EF-B24115D4C609")!)
var date: AnyValue = .date(Date(timeIntervalSinceReferenceDate: 1234567.89))
Expand Down
5 changes: 4 additions & 1 deletion Tests/AnyValueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ class AnyValueTests: XCTestCase {
XCTAssertEqual(try AnyValue.wrapped(Float16(1.5)), .float16(1.5))
XCTAssertEqual(try AnyValue.wrapped(Float(1.5)), .float(1.5))
XCTAssertEqual(try AnyValue.wrapped(Double(1.5)), .double(1.5))
XCTAssertEqual(try AnyValue.wrapped(Float16(0.7)), .float16(0.7))
XCTAssertEqual(try AnyValue.wrapped(Float(0.7)), .float(0.7))
XCTAssertEqual(try AnyValue.wrapped(Double(0.7)), .double(0.7))
XCTAssertEqual(try AnyValue.wrapped(Decimal(1.23)), .decimal(1.23))
XCTAssertEqual(try AnyValue.wrapped(Data([1, 2, 3])), .data(Data([1, 2, 3])))
let url = URL(string: "https://example.com")!
Expand Down Expand Up @@ -410,7 +413,7 @@ class AnyValueTests: XCTestCase {
var f64: AnyValue = .double(123.4567)
var pdec: AnyValue = .decimal(Decimal(sign: .plus, exponent: -3, significand: 1234567))
var ndec: AnyValue = .decimal(Decimal(sign: .minus, exponent: -3, significand: 1234567))
var data: AnyValue = .data("Binary Data".data(using: .utf8)!)
var data: AnyValue = .data(Data("Binary Data".utf8))
var url: AnyValue = .url(URL(string: "https://example.com/some/thing")!)
var uuid: AnyValue = .uuid(UUID(uuidString: "46076D06-86E8-4B3B-80EF-B24115D4C609")!)
var date: AnyValue = .date(Date(timeIntervalSinceReferenceDate: 1234567.89))
Expand Down
6 changes: 3 additions & 3 deletions Tests/CBORAnyValueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class CBORAnyValueTests: XCTestCase {
let value = try CBOR.Decoder.default.decode(TestValue.self, from: cbor)
XCTAssertEqual(value.unsigned, AnyValue.int64(123))
XCTAssertEqual(value.negative, AnyValue.int64(-456))
XCTAssertEqual(value.byteString, AnyValue.data("Binary Data".data(using: .utf8)!))
XCTAssertEqual(value.byteString, AnyValue.data(Data("Binary Data".utf8)))
XCTAssertEqual(value.utf8String, AnyValue.string("Hello World!"))
XCTAssertEqual(value.array, AnyValue.array([.nil, .bool(false), .int64(456), .string("a")]))
XCTAssertEqual(value.map, AnyValue.dictionary(["c": .int64(1), "a": .int64(2), "d": .int64(3), "b": .int64(4)]))
Expand Down Expand Up @@ -84,7 +84,7 @@ class CBORAnyValueTests: XCTestCase {
var pndec: AnyValue = .decimal(Decimal(sign: .plus, exponent: -3, significand: 1234567))
var npdec: AnyValue = .decimal(Decimal(sign: .minus, exponent: 1, significand: 1234567))
var nndec: AnyValue = .decimal(Decimal(sign: .minus, exponent: -3, significand: 1234567))
var data: AnyValue = .data("Binary Data".data(using: .utf8)!)
var data: AnyValue = .data(Data("Binary Data".utf8))
var url: AnyValue = .url(URL(string: "https://example.com/some/thing")!)
var uuid: AnyValue = .uuid(UUID(uuidString: "46076D06-86E8-4B3B-80EF-B24115D4C609")!)
var date: AnyValue = .date(Date(timeIntervalSinceReferenceDate: 1234567.89))
Expand Down Expand Up @@ -146,7 +146,7 @@ class CBORAnyValueTests: XCTestCase {
let cbor = Data(hexEncoded: "A16762363444617461D82270516D6C7559584A35494552686447453D")

let dstValue = try CBOR.Decoder.default.decode(TestValue.self, from: cbor)
XCTAssertEqual(dstValue.b64Data, .data("Binary Data".data(using: .utf8)!))
XCTAssertEqual(dstValue.b64Data, .data(Data("Binary Data".utf8)))
}

func testDecodeTaggedInt() throws {
Expand Down
4 changes: 2 additions & 2 deletions Tests/JSONAnyValueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class JSONAnyValueTests: XCTestCase {
var f64: AnyValue = .double(123.4567)
var pdec: AnyValue = .decimal(Decimal(sign: .plus, exponent: -3, significand: 1234567))
var ndec: AnyValue = .decimal(Decimal(sign: .minus, exponent: -3, significand: 1234567))
var data: AnyValue = .data("Binary Data".data(using: .utf8)!)
var data: AnyValue = .data(Data("Binary Data".utf8))
var url: AnyValue = .url(URL(string: "https://example.com/some/thing")!)
var uuid: AnyValue = .uuid(UUID(uuidString: "46076D06-86E8-4B3B-80EF-B24115D4C609")!)
var date: AnyValue = .date(Date(timeIntervalSinceReferenceDate: 1234567.89))
Expand Down Expand Up @@ -195,7 +195,7 @@ class JSONAnyValueTests: XCTestCase {

struct TestValue: Codable {
var invalid: AnyValue = .dictionary([
.data("Bad Key".data(using: .utf8)!): "Shouldn't Work!"
.data(Data("Bad Key".utf8)): "Shouldn't Work!"
])
}

Expand Down
10 changes: 5 additions & 5 deletions Tests/JSONDecoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class JSONDecoderTests: XCTestCase {
let decoder = JSON.Decoder()
decoder.keyDecodingStrategy = .useDefaultKeys

XCTAssertNoThrow(try decoder.decode(TestValue.self, from: json.data(using: .utf8)!))
XCTAssertNoThrow(try decoder.decode(TestValue.self, from: Data(json.utf8)))
}

func testDecodeWithDefaultKeyStrategy() {
Expand Down Expand Up @@ -253,7 +253,7 @@ class JSONDecoderTests: XCTestCase {

func testDecodeBase64Data() throws {

let data = "Hello World!".data(using: .utf8)!
let data = Data("Hello World!".utf8)

struct TestValue: Codable {
var data: Data
Expand All @@ -270,7 +270,7 @@ class JSONDecoderTests: XCTestCase {

func testDecodeBase64DataUnpadded() throws {

let data = "1234".data(using: .utf8)!
let data = Data("1234".utf8)

struct TestValue: Codable {
var data: Data
Expand All @@ -287,7 +287,7 @@ class JSONDecoderTests: XCTestCase {

func testDecodeDeferredToData() throws {

let data = "Hello World!".data(using: .utf8)!
let data = Data("Hello World!".utf8)

struct TestValue: Codable {
var data: Data
Expand All @@ -304,7 +304,7 @@ class JSONDecoderTests: XCTestCase {

func testDecodeCustomData() throws {

let data = "Hello World!".data(using: .utf8)!
let data = Data("Hello World!".utf8)

struct TestValue: Codable {
var data: Data
Expand Down
4 changes: 0 additions & 4 deletions Tests/RefTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ protocol RefTestValue {}

struct AValue: RefTestValue, Codable {
let name: String

init(name: String) {
self.name = name
}
}

struct BValue: RefTestValue, Codable {
Expand Down
2 changes: 1 addition & 1 deletion Tests/YAMLAnyValueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class YAMLAnyValueTests: XCTestCase {
var f64: AnyValue = .double(123.4567)
var pdec: AnyValue = .decimal(Decimal(sign: .plus, exponent: -3, significand: 1234567))
var ndec: AnyValue = .decimal(Decimal(sign: .minus, exponent: -3, significand: 1234567))
var data: AnyValue = .data("Binary Data".data(using: .utf8)!)
var data: AnyValue = .data(Data("Binary Data".utf8))
var url: AnyValue = .url(URL(string: "https://example.com/some/thing")!)
var uuid: AnyValue = .uuid(UUID(uuidString: "46076D06-86E8-4B3B-80EF-B24115D4C609")!)
var date: AnyValue = .date(Date(timeIntervalSinceReferenceDate: 1234567.89))
Expand Down
Loading
Loading