Skip to content

Commit ebf6d5e

Browse files
committed
Detect odd inputs to key/value builders
...
1 parent bcad35e commit ebf6d5e

4 files changed

Lines changed: 29 additions & 3 deletions

File tree

Sources/ZeeQL/Access/AccessDataSource.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ public extension AccessDataSourceType {
285285
_ firstKey: String, _ firstValue: Any,
286286
_ keysAndValues: Any...) throws -> [ Object ]
287287
{
288-
var binds = [ String: Any ].createArgs(keysAndValues)
288+
var binds = try [ String: Any ].createArgs(keysAndValues)
289289
binds[firstKey] = firstValue
290290
return try fetchObjects(fetchSpecificationName, binds)
291291
}

Sources/ZeeQL/Access/AccessDataSourceFinders.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ public extension AccessDataSource { // Finders
284284
func find(_ name: String, _ firstBinding: String, _ firstValue: Any,
285285
_ bindings: Any...) throws -> Object?
286286
{
287-
var bindings = [ String: Any ].createArgs(bindings)
287+
var bindings = try [ String: Any ].createArgs(bindings)
288288
assert(bindings[firstBinding] == nil, "Duplicate binding.")
289289
bindings[firstBinding] = firstValue
290290
return try find(name, bindings)

Sources/ZeeQL/Foundation/Dictionary+Extensions.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,22 @@
55
// Created by Helge Heß on 24.11.24.
66
//
77

8+
enum DictionaryArgumentError: Error, Equatable {
9+
10+
case oddArgumentCount(Int)
11+
}
12+
813
extension Dictionary where Key == String, Value == Any {
914

1015
/**
1116
* This method creates a new `Dictionary` from a set of given array containing
1217
* key/value arguments.
1318
*/
14-
static func createArgs(_ values: [ Any ]) -> Self {
19+
static func createArgs(_ values: [ Any ]) throws -> Self {
1520
guard !values.isEmpty else { return [:] }
21+
guard values.count.isMultiple(of: 2) else {
22+
throw DictionaryArgumentError.oddArgumentCount(values.count)
23+
}
1624
var me = Self()
1725
me.reserveCapacity(values.count / 2 + 1)
1826

Tests/ZeeQLTests/AccessDataSourceFinderTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@ final class AccessDataSourceFinderTests: XCTestCase {
2929
XCTAssertEqual(qualifier.qualifiers.count, 2)
3030
}
3131

32+
func testVariadicFinderRejectsOddBindingArguments() throws {
33+
let dataSource = makeCompositeDataSource()
34+
35+
XCTAssertThrowsError(
36+
try dataSource.find("named", "first", 1, "dangling")) { error in
37+
XCTAssertEqual(error as? DictionaryArgumentError, .oddArgumentCount(1))
38+
}
39+
}
40+
41+
func testVariadicFetchRejectsOddBindingArguments() throws {
42+
let dataSource = makeCompositeDataSource()
43+
44+
XCTAssertThrowsError(
45+
try dataSource.fetchObjects("named", "first", 1, "dangling")) { error in
46+
XCTAssertEqual(error as? DictionaryArgumentError, .oddArgumentCount(1))
47+
}
48+
}
49+
3250
private func makeCompositeDataSource() -> AdaptorDataSource {
3351
let entity = ModelEntity(name: "LocalizedItem")
3452
entity.attributes = [

0 commit comments

Comments
 (0)