Skip to content
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
1 change: 1 addition & 0 deletions .swiftformat
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@

--disable spaceAroundOperators
--disable wrapMultilineStatementBraces
--disable noForceUnwrapInTests

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This resolves #150 (comment)

Looks like there's a bug in this rule when a test mixes force unwrapping and async.

6 changes: 3 additions & 3 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ let package = Package(
.target(
name: "PklSwift",
dependencies: ["PklMessagePack", "PklSwiftInternals", "SemanticVersion"],
swiftSettings: [.enableUpcomingFeature("StrictConcurrency")],
swiftSettings: [.enableUpcomingFeature("StrictConcurrency")]
),
.target(
name: "PklSwiftInternals",
Expand Down
11 changes: 5 additions & 6 deletions Sources/PklMessagePack/Decoder/KeyedDecodingContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ extension MessagePackValue {
}

extension _MessagePackDecoder {
final class KeyedContainer<Key> where Key: CodingKey {
final class KeyedContainer<Key: CodingKey> {
var value: [(MessagePackValue, MessagePackValue)]
var index: Int
var codingPath: [CodingKey]
Expand Down Expand Up @@ -248,10 +248,9 @@ extension _MessagePackDecoder.KeyedContainer: KeyedDecodingContainerProtocol {
}

var allEntries: [KeyMessagePackValueEntry] {
let keys = self.value.compactMap { kv in
self.value.compactMap { kv in
Key(stringValue: kv.0.stringValue).map { KeyMessagePackValueEntry(key: $0, value: kv.1) }
}
return keys
}

func contains(_ key: Key) -> Bool {
Expand All @@ -267,7 +266,7 @@ extension _MessagePackDecoder.KeyedContainer: KeyedDecodingContainerProtocol {
}
}

func decode<T>(_ typ: T.Type, forKey key: Key) throws -> T where T: Decodable {
func decode<T: Decodable>(_ typ: T.Type, forKey key: Key) throws -> T {
defer {
index += 1
}
Expand Down Expand Up @@ -305,8 +304,8 @@ extension _MessagePackDecoder.KeyedContainer: KeyedDecodingContainerProtocol {
}
}

func nestedContainer<NestedKey>(keyedBy nestedKeyType: NestedKey.Type, forKey key: Key) throws
-> KeyedDecodingContainer<NestedKey> where NestedKey: CodingKey {
func nestedContainer<NestedKey: CodingKey>(keyedBy nestedKeyType: NestedKey.Type, forKey key: Key) throws
-> KeyedDecodingContainer<NestedKey> {
try checkCanDecodeValue(forKey: key)

guard let msgPackValue = strMap[key.stringValue] else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import Foundation

extension _MessagePackDecoder {
final class KeyedSingleContainer<Key> where Key: CodingKey {
final class KeyedSingleContainer<Key: CodingKey> {
var value: MessagePackValue
var index: Int
var codingPath: [CodingKey]
Expand Down Expand Up @@ -62,7 +62,7 @@ extension _MessagePackDecoder.KeyedSingleContainer: KeyedDecodingContainerProtoc
self.value == MessagePackValue.nil
}

func decode<T>(_ typ: T.Type, forKey key: Key) throws -> T where T: Decodable {
func decode<T: Decodable>(_: T.Type, forKey key: Key) throws -> T {
_ = self.value.getAs(T.self) // FIXME: make optional

let context = DecodingError.Context(
Expand All @@ -75,8 +75,8 @@ extension _MessagePackDecoder.KeyedSingleContainer: KeyedDecodingContainerProtoc
fatalError("\(#function)")
}

func nestedContainer<NestedKey>(keyedBy nestedKeyType: NestedKey.Type, forKey key: Key) throws
-> KeyedDecodingContainer<NestedKey> where NestedKey: CodingKey {
func nestedContainer<NestedKey: CodingKey>(keyedBy nestedKeyType: NestedKey.Type, forKey key: Key) throws
-> KeyedDecodingContainer<NestedKey> {
fatalError("\(#function)")
}

Expand Down
64 changes: 40 additions & 24 deletions Sources/PklMessagePack/Decoder/MessagePackDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ public final class MessagePackDecoder {
throw DecodingError.dataCorrupted(
DecodingError.Context(
codingPath: [], debugDescription: "Unknown msgpack format: \(code.toHex())"
))
)
)
}
}

public func decode<T>(as type: T.Type) throws -> T where T: Decodable {
public func decode<T: Decodable>(as type: T.Type) throws -> T {
let value = try decodeGeneric()
let converted = try value.decodeInto(type)
return converted
return try value.decodeInto(type)
}

public func decodeBool() throws -> Bool {
Expand All @@ -83,7 +83,8 @@ public final class MessagePackDecoder {
case .true: return true
default:
throw DecodingError.dataCorrupted(
.init(codingPath: [], debugDescription: "Invalid code for bool: \(code.toHex())"))
.init(codingPath: [], debugDescription: "Invalid code for bool: \(code.toHex())")
)
}
}

Expand All @@ -93,7 +94,8 @@ public final class MessagePackDecoder {
return nil
}
throw DecodingError.dataCorrupted(
.init(codingPath: [], debugDescription: "Invalid code for nil: \(code.toHex())"))
.init(codingPath: [], debugDescription: "Invalid code for nil: \(code.toHex())")
)
}

public func decodeInt() throws -> Int {
Expand Down Expand Up @@ -156,7 +158,8 @@ public final class MessagePackDecoder {
throw DecodingError.dataCorrupted(
DecodingError.Context(
codingPath: [], debugDescription: "Invalid format for String length: \(code.toHex())"
))
)
)
}

let bytes = try read(length)
Expand Down Expand Up @@ -195,7 +198,8 @@ public final class MessagePackDecoder {
throw DecodingError.dataCorrupted(
DecodingError.Context(
codingPath: [], debugDescription: "Invalid format for date \(code.toHex())"
))
)
)
}

let timeInterval = TimeInterval(seconds) + nanoseconds / Double(NSEC_PER_SEC)
Expand All @@ -217,14 +221,16 @@ public final class MessagePackDecoder {
throw DecodingError.dataCorrupted(
DecodingError.Context(
codingPath: [], debugDescription: "Invalid format for binary length \(code.toHex())"
))
)
)
}

let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: length, alignment: 1)
let readCount = try reader.read(into: buffer)
if readCount < length {
throw DecodingError.dataCorrupted(
DecodingError.Context(codingPath: [], debugDescription: "Unexpected end of input"))
DecodingError.Context(codingPath: [], debugDescription: "Unexpected end of input")
)
}
return Array(buffer)
}
Expand All @@ -251,7 +257,8 @@ public final class MessagePackDecoder {
throw DecodingError.dataCorrupted(
DecodingError.Context(
codingPath: [], debugDescription: "Invalid code \(code) for array header"
))
)
)
}
}

Expand All @@ -268,7 +275,8 @@ public final class MessagePackDecoder {
throw DecodingError.dataCorrupted(
DecodingError.Context(
codingPath: [], debugDescription: "Invalid format \(code) for maps"
))
)
)
}
}

Expand Down Expand Up @@ -301,7 +309,8 @@ public final class MessagePackDecoder {
throw DecodingError.dataCorrupted(
DecodingError.Context(
codingPath: [], debugDescription: "Invalid format \(code) for extensions"
))
)
)
}
}

Expand All @@ -312,12 +321,13 @@ public final class MessagePackDecoder {
let readCount = try reader.read(into: buffer)
if readCount < length {
throw DecodingError.dataCorrupted(
DecodingError.Context(codingPath: [], debugDescription: "Unexpected end of input"))
DecodingError.Context(codingPath: [], debugDescription: "Unexpected end of input")
)
}
return (type, Array(buffer))
}

private func decodeFloatingPoint<T>() throws -> T where T: BinaryFloatingPoint {
private func decodeFloatingPoint<T: BinaryFloatingPoint>() throws -> T {
let code = try readByte()
let t: T?
switch code {
Expand All @@ -333,18 +343,20 @@ public final class MessagePackDecoder {
throw DecodingError.dataCorrupted(
DecodingError.Context(
codingPath: [], debugDescription: "Invalid format for floating points: \(code.toHex())"
))
)
)
}
guard let t else {
throw DecodingError.dataCorrupted(
DecodingError.Context(
codingPath: [], debugDescription: "Unable to decode bytes into BinaryFloatingPoint"
))
)
)
}
return t
}

private func decodeBinaryInteger<T>() throws -> T where T: BinaryInteger {
private func decodeBinaryInteger<T: BinaryInteger>() throws -> T {
let code = try readByte()

var t: T?
Expand Down Expand Up @@ -374,14 +386,16 @@ public final class MessagePackDecoder {
throw DecodingError.dataCorrupted(
DecodingError.Context(
codingPath: [], debugDescription: "Invalid format for int: \(code.toHex())"
))
)
)
}

guard let value = t else {
throw DecodingError.dataCorrupted(
DecodingError.Context(
codingPath: [], debugDescription: "Unable to decode bytes into BinaryInteger"
))
)
)
}
return value
}
Expand All @@ -392,7 +406,8 @@ public final class MessagePackDecoder {
let readCount = try reader.read(into: out)
if readCount == 0 {
throw DecodingError.dataCorrupted(
DecodingError.Context(codingPath: [], debugDescription: "Unexpected end of input"))
DecodingError.Context(codingPath: [], debugDescription: "Unexpected end of input")
)
}

return out[0]
Expand All @@ -404,12 +419,13 @@ public final class MessagePackDecoder {
let readCount = try reader.read(into: out)
if readCount < length {
throw DecodingError.dataCorrupted(
DecodingError.Context(codingPath: [], debugDescription: "Unexpected end of input"))
DecodingError.Context(codingPath: [], debugDescription: "Unexpected end of input")
)
}
return [UInt8](out)
}

private func read<T>(_: T.Type) throws -> T where T: FixedWidthInteger {
private func read<T: FixedWidthInteger>(_: T.Type) throws -> T {
let stride = MemoryLayout<T>.stride
let bytes = try read(stride)
return T(bytes: bytes)
Expand Down Expand Up @@ -461,7 +477,7 @@ extension MessagePackValue {
}

extension MessagePackValue {
public func decode<T>(_: T.Type) throws -> T where T: Decodable {
public func decode<T: Decodable>(_: T.Type) throws -> T {
try T(from: _MessagePackDecoder(value: self))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ extension _MessagePackDecoder.SingleValueContainer: SingleValueDecodingContainer
}
}

func decode<T>(_: T.Type) throws -> T where T: BinaryInteger & Decodable {
func decode<T: BinaryInteger & Decodable>(_: T.Type) throws -> T {
switch value {
case .int(let value):
guard let value = value as? T else {
Expand Down Expand Up @@ -142,7 +142,7 @@ extension _MessagePackDecoder.SingleValueContainer: SingleValueDecodingContainer
}
}

func decode<T>(_ type: T.Type) throws -> T where T: Decodable {
func decode<T: Decodable>(_ type: T.Type) throws -> T {
switch type {
case is Data.Type:
return try self.decode(Data.self) as! T
Expand All @@ -154,8 +154,7 @@ extension _MessagePackDecoder.SingleValueContainer: SingleValueDecodingContainer
return try self.decode(URL.self) as! T
default:
let decoder = _MessagePackDecoder(value: value)
let value = try T(from: decoder)
return value
return try T(from: decoder)
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/PklMessagePack/Decoder/UnkeyedDecodingContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ extension _MessagePackDecoder.UnkeyedContainer: UnkeyedDecodingContainer {
}
}

func decode<T>(_ typ: T.Type) throws -> T where T: Decodable {
func decode<T: Decodable>(_ typ: T.Type) throws -> T {
try checkCanDecodeValue()
defer {
currentIndex += 1
Expand All @@ -90,7 +90,7 @@ extension _MessagePackDecoder.UnkeyedContainer: UnkeyedDecodingContainer {
}
}

func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type) throws -> KeyedDecodingContainer<NestedKey> where NestedKey: CodingKey {
func nestedContainer<NestedKey: CodingKey>(keyedBy type: NestedKey.Type) throws -> KeyedDecodingContainer<NestedKey> {
try checkCanDecodeValue()
defer { currentIndex += 1 }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ extension _MessagePackDecoder {
self.value.count
}

// var value: [(MessagePackValue, MessagePackValue)]
/// var value: [(MessagePackValue, MessagePackValue)]
var value: [MessagePackValue] // flattened

var currentIndex: Int = 0
Expand Down Expand Up @@ -97,7 +97,7 @@ extension _MessagePackDecoder.MapUnkeyedContainer: UnkeyedDecodingContainer {
fatalError("Not implemented: \(#function)")
}

func decode<T>(_ typ: T.Type) throws -> T where T: Decodable {
func decode<T: Decodable>(_ typ: T.Type) throws -> T {
defer {
currentIndex += 1
}
Expand All @@ -110,7 +110,7 @@ extension _MessagePackDecoder.MapUnkeyedContainer: UnkeyedDecodingContainer {
fatalError("Not implemented: \(#function)")
}

func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type) throws -> KeyedDecodingContainer<NestedKey> where NestedKey: CodingKey {
func nestedContainer<NestedKey: CodingKey>(keyedBy type: NestedKey.Type) throws -> KeyedDecodingContainer<NestedKey> {
fatalError("Not implemented: \(#function)")
}

Expand Down
5 changes: 2 additions & 3 deletions Sources/PklMessagePack/Decoder/_MessagePackDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ final class _MessagePackDecoder: Decoder {
self.value = value
}

func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key>
where Key: CodingKey {
func container<Key: CodingKey>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> {
switch self.value {
case .map(let map):
let container = _MessagePackDecoder.KeyedContainer<Key>(
Expand Down Expand Up @@ -88,7 +87,7 @@ protocol MessagePackDecodingContainer: AnyObject {
}

extension MessagePackValue {
func decodeInto<T>(_ typ: T.Type) throws -> T where T: Decodable {
func decodeInto<T: Decodable>(_ typ: T.Type) throws -> T {
let decoder = _MessagePackDecoder(value: self)
switch typ {
case is Data.Type:
Expand Down
Loading
Loading