Skip to content

Commit

Permalink
further test params structure
Browse files Browse the repository at this point in the history
  • Loading branch information
djbe committed Jan 28, 2017
1 parent 656b1c9 commit 83059c4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
15 changes: 11 additions & 4 deletions Sources/Parameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Foundation
public enum ParametersError: Error {
case invalidSyntax(value: String)
case invalidKey(key: String, value: String)
case invalidStructure(key: String, oldValue: Any, newValue: Any)
}

public enum Parameters {
Expand All @@ -33,25 +34,31 @@ public enum Parameters {
private static func parse(item: Parameter, result: StringDict) throws -> StringDict {
let parts = item.key.components(separatedBy: ".")
let key = parts.first ?? ""

var result = result

// validate key
guard validate(key: key) else { throw ParametersError.invalidKey(key: item.key, value: item.value) }

// no sub keys, may need to convert to array if repeat key
// no sub keys, may need to convert to array if repeat key if possible
if parts.count == 1 {
if let current = result[key] as? [String] {
result[key] = current + [item.value]
} else if let current = result[item.key] {
} else if let current = result[key] as? String {
result[key] = [current, item.value]
} else if let current = result[key] {
throw ParametersError.invalidStructure(key: key, oldValue: current, newValue: item.value)
} else {
result[key] = item.value
}
} else if parts.count > 1 {
guard result[key] is StringDict || result[key] == nil else {
throw ParametersError.invalidStructure(key: key, oldValue: result[key], newValue: item.value)
}

// recurse into sub keys
let sub = (key: parts.suffix(from: 1).joined(separator: "."), value: item.value)
let current = result[key] as? StringDict ?? StringDict()

let sub = (key: parts.suffix(from: 1).joined(separator: "."), value: item.value)
result[key] = try parse(item: sub, result: current)
}

Expand Down
29 changes: 29 additions & 0 deletions Tests/TestSuites/ParametersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,33 @@ class ParametersTests: XCTestCase {
XCTFail("Unexpected error occured while parsing: \(error)")
}
}

func testInvalidStructure() {
do {
let items = ["foo=1", "foo.bar=1"]
_ = try Parameters.parse(items: items)
} catch ParametersError.invalidStructure {
// That's the expected exception we want to happen
} catch let error {
XCTFail("Unexpected error occured while parsing: \(error)")
}

do {
let items = ["foo.bar=1", "foo=1"]
_ = try Parameters.parse(items: items)
} catch ParametersError.invalidStructure {
// That's the expected exception we want to happen
} catch let error {
XCTFail("Unexpected error occured while parsing: \(error)")
}

do {
let items = ["foo=1", "foo=2", "foo.bar=1"]
_ = try Parameters.parse(items: items)
} catch ParametersError.invalidStructure {
// That's the expected exception we want to happen
} catch let error {
XCTFail("Unexpected error occured while parsing: \(error)")
}
}
}

0 comments on commit 83059c4

Please sign in to comment.