Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hope support the "|" separator #1126

Open
tospery opened this issue Mar 4, 2022 · 0 comments
Open

hope support the "|" separator #1126

tospery opened this issue Mar 4, 2022 · 0 comments

Comments

@tospery
Copy link

tospery commented Mar 4, 2022

like,
mutating func mapping(map: Map) {
id <- map["areaId|serverId"]
}
when has not areaId, use serverId.
can modify the follow code to support.
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 ?? 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 keyIsNested {
			// break down the components of the key that are separated by delimiter
			(isKeyPresent, currentValue) = valueFor(ArraySlice(key.components(separatedBy: delimiter)), dictionary: JSON)
		} else {
			var object = JSON[key]

// add this code
if object == nil && key.contains(delimiter) {
let components = key.components(separatedBy: delimiter)
for item in components {
object = JSON[item]
if object != nil {
break
}
}
}
// end add

            var isNSNull = object is NSNull
			isKeyPresent = isNSNull ? true : object != nil
			currentValue = isNSNull ? nil : object
		}
		
		// update isKeyPresent if ignoreNil is true
		if ignoreNil && currentValue == nil {
			isKeyPresent = false
		}
	}
	
	return self
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant