Skip to content

Commit

Permalink
renamed recursive method
Browse files Browse the repository at this point in the history
  • Loading branch information
djbe committed Jan 28, 2017
1 parent 83059c4 commit 7294fac
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions Sources/Parameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,41 +25,40 @@ public enum Parameters {

var result = StringDict()
for parameter in parameters {
result = try parse(item: parameter, result: result)
result = try parse(parameter: parameter, result: result)
}

return result
}

private static func parse(item: Parameter, result: StringDict) throws -> StringDict {
let parts = item.key.components(separatedBy: ".")
private static func parse(parameter: Parameter, result: StringDict) throws -> StringDict {
let parts = parameter.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) }
guard validate(key: key) else { throw ParametersError.invalidKey(key: parameter.key, value: parameter.value) }

// 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]
result[key] = current + [parameter.value]
} else if let current = result[key] as? String {
result[key] = [current, item.value]
result[key] = [current, parameter.value]
} else if let current = result[key] {
throw ParametersError.invalidStructure(key: key, oldValue: current, newValue: item.value)
throw ParametersError.invalidStructure(key: key, oldValue: current, newValue: parameter.value)
} else {
result[key] = item.value
result[key] = parameter.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)
throw ParametersError.invalidStructure(key: key, oldValue: result[key], newValue: parameter.value)
}

// recurse into sub keys
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)
let sub = (key: parts.suffix(from: 1).joined(separator: "."), value: parameter.value)
result[key] = try parse(parameter: sub, result: current)
}

return result
Expand Down

0 comments on commit 7294fac

Please sign in to comment.