Skip to content

Commit

Permalink
Start generifying stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
ptoffy committed Nov 7, 2024
1 parent d9a056e commit ad06379
Show file tree
Hide file tree
Showing 12 changed files with 977 additions and 882 deletions.
70 changes: 70 additions & 0 deletions .swift-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"fileScopedDeclarationPrivacy": {
"accessLevel": "private"
},
"indentation": {
"spaces": 4
},
"indentConditionalCompilationBlocks": true,
"indentSwitchCaseLabels": false,
"lineBreakAroundMultilineExpressionChainComponents": false,
"lineBreakBeforeControlFlowKeywords": false,
"lineBreakBeforeEachArgument": false,
"lineBreakBeforeEachGenericRequirement": false,
"lineLength": 140,
"maximumBlankLines": 1,
"multiElementCollectionTrailingCommas": true,
"noAssignmentInExpressions": {
"allowedFunctions": [
"XCTAssertNoThrow"
]
},
"prioritizeKeepingFunctionOutputTogether": false,
"respectsExistingLineBreaks": true,
"rules": {
"AllPublicDeclarationsHaveDocumentation": false,
"AlwaysUseLiteralForEmptyCollectionInit": false,
"AlwaysUseLowerCamelCase": true,
"AmbiguousTrailingClosureOverload": true,
"BeginDocumentationCommentWithOneLineSummary": false,
"DoNotUseSemicolons": true,
"DontRepeatTypeInStaticProperties": true,
"FileScopedDeclarationPrivacy": true,
"FullyIndirectEnum": true,
"GroupNumericLiterals": true,
"IdentifiersMustBeASCII": true,
"NeverForceUnwrap": false,
"NeverUseForceTry": false,
"NeverUseImplicitlyUnwrappedOptionals": false,
"NoAccessLevelOnExtensionDeclaration": true,
"NoAssignmentInExpressions": true,
"NoBlockComments": true,
"NoCasesWithOnlyFallthrough": true,
"NoEmptyTrailingClosureParentheses": true,
"NoLabelsInCasePatterns": true,
"NoLeadingUnderscores": false,
"NoParensAroundConditions": true,
"NoPlaygroundLiterals": true,
"NoVoidReturnOnFunctionSignature": true,
"OmitExplicitReturns": false,
"OneCasePerLine": true,
"OneVariableDeclarationPerLine": true,
"OnlyOneTrailingClosureArgument": true,
"OrderedImports": true,
"ReplaceForEachWithForLoop": true,
"ReturnVoidInsteadOfEmptyTuple": true,
"TypeNamesShouldBeCapitalized": true,
"UseEarlyExits": false,
"UseExplicitNilCheckInConditions": true,
"UseLetInEveryBoundCaseVariable": true,
"UseShorthandTypeNames": true,
"UseSingleLinePropertyGetter": true,
"UseSynthesizedInitializer": true,
"UseTripleSlashForDocumentationComments": true,
"UseWhereClausesInForLoops": false,
"ValidateDocumentationComments": false
},
"spacesAroundRangeFormationOperators": false,
"tabWidth": 4,
"version": 1
}
146 changes: 73 additions & 73 deletions Sources/MultipartKit/FormDataDecoder/FormDataDecoder.Decoder.swift
Original file line number Diff line number Diff line change
@@ -1,81 +1,81 @@
// extension FormDataDecoder {
// struct Decoder {
// let codingPath: [any CodingKey]
// let data: MultipartFormData
// let userInfo: [CodingUserInfoKey: Any]
// let previousCodingPath: [any CodingKey]?
// let previousType: (any Decodable.Type)?
extension FormDataDecoder {
struct Decoder {
let codingPath: [any CodingKey]
let data: MultipartFormData<Body>
let userInfo: [CodingUserInfoKey: Any]
let previousCodingPath: [any CodingKey]?
let previousType: (any Decodable.Type)?

// init(
// codingPath: [any CodingKey], data: MultipartFormData, userInfo: [CodingUserInfoKey: Any],
// previousCodingPath: [any CodingKey]? = nil, previousType: (any Decodable.Type)? = nil
// ) {
// self.codingPath = codingPath
// self.data = data
// self.userInfo = userInfo
// self.previousCodingPath = previousCodingPath
// self.previousType = previousType
// }
// }
// }
init(
codingPath: [any CodingKey], data: MultipartFormData<Body>, userInfo: [CodingUserInfoKey: Any],
previousCodingPath: [any CodingKey]? = nil, previousType: (any Decodable.Type)? = nil
) {
self.codingPath = codingPath
self.data = data
self.userInfo = userInfo
self.previousCodingPath = previousCodingPath
self.previousType = previousType
}
}
}

// extension FormDataDecoder.Decoder: Decoder {
// func container<Key: CodingKey>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> {
// guard let dictionary = data.dictionary else {
// throw decodingError(expectedType: "dictionary")
// }
// return KeyedDecodingContainer(FormDataDecoder.KeyedContainer(data: dictionary, decoder: self))
// }
extension FormDataDecoder.Decoder: Decoder {
func container<Key: CodingKey>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> {
guard let dictionary = data.dictionary else {
throw decodingError(expectedType: "dictionary")
}
return KeyedDecodingContainer(FormDataDecoder.KeyedContainer(data: dictionary, decoder: self))
}

// func unkeyedContainer() throws -> any UnkeyedDecodingContainer {
// guard let array = data.array else {
// throw decodingError(expectedType: "array")
// }
// return FormDataDecoder.UnkeyedContainer(data: array, decoder: self)
// }
func unkeyedContainer() throws -> any UnkeyedDecodingContainer {
guard let array = data.array else {
throw decodingError(expectedType: "array")
}
return FormDataDecoder.UnkeyedContainer(data: array, decoder: self)
}

// func singleValueContainer() throws -> any SingleValueDecodingContainer {
// self
// }
// }
func singleValueContainer() throws -> any SingleValueDecodingContainer {
self
}
}

// extension FormDataDecoder.Decoder {
// func nested(at key: any CodingKey, with data: MultipartFormData) -> Self {
// .init(codingPath: codingPath + [key], data: data, userInfo: userInfo)
// }
// }
extension FormDataDecoder.Decoder {
func nested(at key: any CodingKey, with data: MultipartFormData<Body>) -> Self {
.init(codingPath: codingPath + [key], data: data, userInfo: userInfo)
}
}

// extension FormDataDecoder.Decoder {
// fileprivate func decodingError(expectedType: String) -> any Error {
// let encounteredType: Any.Type
// let encounteredTypeDescription: String
extension FormDataDecoder.Decoder {
fileprivate func decodingError(expectedType: String) -> any Error {
let encounteredType: Any.Type
let encounteredTypeDescription: String

// switch data {
// case .nestingDepthExceeded:
// return DecodingError.dataCorrupted(
// .init(
// codingPath: codingPath,
// debugDescription: "Nesting depth exceeded while expecting \(expectedType).",
// underlyingError: nil
// ))
// case .array:
// encounteredType = [MultipartFormData].self
// encounteredTypeDescription = "array"
// case .keyed:
// encounteredType = MultipartFormData.Keyed.self
// encounteredTypeDescription = "dictionary"
// case .single:
// encounteredType = MultipartPart.self
// encounteredTypeDescription = "single value"
// }
switch data {
case .nestingDepthExceeded:
return DecodingError.dataCorrupted(
.init(
codingPath: codingPath,
debugDescription: "Nesting depth exceeded while expecting \(expectedType).",
underlyingError: nil
))
case .array:
encounteredType = [MultipartFormData<Body>].self
encounteredTypeDescription = "array"
case .keyed:
encounteredType = MultipartFormData<Body>.Keyed.self
encounteredTypeDescription = "dictionary"
case .single:
encounteredType = MultipartPart<Body>.self
encounteredTypeDescription = "single value"
}

// return DecodingError.typeMismatch(
// encounteredType,
// .init(
// codingPath: codingPath,
// debugDescription: "Expected \(expectedType) but encountered \(encounteredTypeDescription).",
// underlyingError: nil
// )
// )
// }
// }
return DecodingError.typeMismatch(
encounteredType,
.init(
codingPath: codingPath,
debugDescription: "Expected \(expectedType) but encountered \(encounteredTypeDescription).",
underlyingError: nil
)
)
}
}
Original file line number Diff line number Diff line change
@@ -1,61 +1,61 @@
// extension FormDataDecoder {
// struct KeyedContainer<K: CodingKey> {
// let data: MultipartFormData.Keyed
// let decoder: FormDataDecoder.Decoder
// }
// }

// extension FormDataDecoder.KeyedContainer: KeyedDecodingContainerProtocol {
// var allKeys: [K] {
// data.keys.compactMap(K.init(stringValue:))
// }

// var codingPath: [any CodingKey] {
// decoder.codingPath
// }

// func contains(_ key: K) -> Bool {
// data.keys.contains(key.stringValue)
// }

// func getValue(forKey key: any CodingKey) throws -> MultipartFormData {
// guard let value = data[key.stringValue] else {
// throw DecodingError.keyNotFound(
// key,
// .init(
// codingPath: codingPath,
// debugDescription: "No value associated with key \"\(key.stringValue)\"."
// )
// )
// }
// return value
// }

// func decodeNil(forKey key: K) throws -> Bool {
// false
// }

// func decode<T: Decodable>(_ type: T.Type, forKey key: K) throws -> T {
// try decoderForKey(key).decode()
// }

// func nestedContainer<NestedKey: CodingKey>(keyedBy keyType: NestedKey.Type, forKey key: K) throws -> KeyedDecodingContainer<NestedKey> {
// try decoderForKey(key).container(keyedBy: keyType)
// }

// func nestedUnkeyedContainer(forKey key: K) throws -> any UnkeyedDecodingContainer {
// try decoderForKey(key).unkeyedContainer()
// }

// func superDecoder() throws -> any Decoder {
// try decoderForKey(BasicCodingKey.super)
// }

// func superDecoder(forKey key: K) throws -> any Decoder {
// try decoderForKey(key)
// }

// func decoderForKey(_ key: any CodingKey) throws -> FormDataDecoder.Decoder {
// decoder.nested(at: key, with: try getValue(forKey: key))
// }
// }
extension FormDataDecoder {
struct KeyedContainer<K: CodingKey> {
let data: MultipartFormData<Body>.Keyed
let decoder: FormDataDecoder.Decoder
}
}

extension FormDataDecoder.KeyedContainer: KeyedDecodingContainerProtocol {
var allKeys: [K] {
data.keys.compactMap(K.init(stringValue:))
}

var codingPath: [any CodingKey] {
decoder.codingPath
}

func contains(_ key: K) -> Bool {
data.keys.contains(key.stringValue)
}

func getValue(forKey key: any CodingKey) throws -> MultipartFormData<Body> {
guard let value = data[key.stringValue] else {
throw DecodingError.keyNotFound(
key,
.init(
codingPath: codingPath,
debugDescription: "No value associated with key \"\(key.stringValue)\"."
)
)
}
return value
}

func decodeNil(forKey key: K) throws -> Bool {
false
}

func decode<T: Decodable>(_ type: T.Type, forKey key: K) throws -> T {
try decoderForKey(key).decode()
}

func nestedContainer<NestedKey: CodingKey>(keyedBy keyType: NestedKey.Type, forKey key: K) throws -> KeyedDecodingContainer<NestedKey> {
try decoderForKey(key).container(keyedBy: keyType)
}

func nestedUnkeyedContainer(forKey key: K) throws -> any UnkeyedDecodingContainer {
try decoderForKey(key).unkeyedContainer()
}

func superDecoder() throws -> any Decoder {
try decoderForKey(BasicCodingKey.super)
}

func superDecoder(forKey key: K) throws -> any Decoder {
try decoderForKey(key)
}

func decoderForKey(_ key: any CodingKey) throws -> FormDataDecoder.Decoder {
decoder.nested(at: key, with: try getValue(forKey: key))
}
}
Loading

0 comments on commit ad06379

Please sign in to comment.