Skip to content

Commit

Permalink
Merge pull request #936 from Skoti/feature/subscripts
Browse files Browse the repository at this point in the history
unifying default args of and simplifying map subscripts
  • Loading branch information
tristanhimmelman authored May 29, 2018
2 parents af5ff5d + 73406a5 commit 9523b40
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions Sources/Map.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,52 +62,54 @@ public final class Map {
/// The Key paramater can be a period separated string (ex. "distance.value") to access sub objects.
public subscript(key: String) -> Map {
// save key and value associated to it
return self[key, delimiter: ".", ignoreNil: false]
return self.subscript(key: key)
}

public subscript(key: String, delimiter delimiter: String) -> Map {
let nested = key.contains(delimiter)
return self[key, nested: nested, delimiter: delimiter, ignoreNil: false]
return self.subscript(key: key, delimiter: delimiter)
}

public subscript(key: String, nested nested: Bool) -> Map {
return self[key, nested: nested, delimiter: ".", ignoreNil: false]
return self.subscript(key: key, nested: nested)
}

public subscript(key: String, nested nested: Bool, delimiter delimiter: String) -> Map {
return self[key, nested: nested, delimiter: delimiter, ignoreNil: false]
return self.subscript(key: key, nested: nested, delimiter: delimiter)
}

public subscript(key: String, ignoreNil ignoreNil: Bool) -> Map {
return self[key, delimiter: ".", ignoreNil: ignoreNil]
return self.subscript(key: key, ignoreNil: ignoreNil)
}

public subscript(key: String, delimiter delimiter: String, ignoreNil ignoreNil: Bool) -> Map {
let nested = key.contains(delimiter)
return self[key, nested: nested, delimiter: delimiter, ignoreNil: ignoreNil]
return self.subscript(key: key, delimiter: delimiter, ignoreNil: ignoreNil)
}

public subscript(key: String, nested nested: Bool, ignoreNil ignoreNil: Bool) -> Map {
return self[key, nested: nested, delimiter: ".", ignoreNil: ignoreNil]
return self.subscript(key: key, nested: nested, ignoreNil: ignoreNil)
}

public subscript(key: String, nested nested: Bool, delimiter delimiter: String, ignoreNil ignoreNil: Bool) -> Map {
public subscript(key: String, nested nested: Bool?, delimiter delimiter: String, ignoreNil ignoreNil: Bool) -> Map {
return self.subscript(key: key, nested: nested, delimiter: delimiter, ignoreNil: ignoreNil)
}

private func `subscript`(key: String, nested: Bool? = nil, delimiter: String = ".", ignoreNil: Bool = false) -> Map {
// save key and value associated to it
currentKey = key
keyIsNested = nested
keyIsNested = nested ?? key.contains(delimiter)
nestedKeyDelimiter = delimiter

if mappingType == .fromJSON {
// check if a value exists for the current key
// do this pre-check for performance reasons
if nested == false {
if keyIsNested {
// break down the components of the key that are separated by delimiter
(isKeyPresent, currentValue) = valueFor(ArraySlice(key.components(separatedBy: delimiter)), dictionary: JSON)
} else {
let object = JSON[key]
let isNSNull = object is NSNull
isKeyPresent = isNSNull ? true : object != nil
currentValue = isNSNull ? nil : object
} else {
// break down the components of the key that are separated by .
(isKeyPresent, currentValue) = valueFor(ArraySlice(key.components(separatedBy: delimiter)), dictionary: JSON)
}

// update isKeyPresent if ignoreNil is true
Expand Down

0 comments on commit 9523b40

Please sign in to comment.