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

A key should be able to contain dot #21

Merged
merged 1 commit into from
Sep 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions KakaJSON/Extension/Dictionary+KJ.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ extension Dictionary where Key == String {
}

private func _value(stringKey: String) -> Any? {
if let value = self[stringKey] {
return value
}

let subkeys = stringKey.split(separator: ".")
var value: Any? = self
for subKey in subkeys {
Expand Down
25 changes: 25 additions & 0 deletions KakaJSONTests/JSON_To_Model/JTM_05_KeyMapping.swift
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,31 @@ class JTM_05_KeyMapping: XCTestCase {
XCTAssert(dog.toy?.price == toy.price)
}

func testComplex_JSONKeyWithDot() {
struct Team: Convertible {
var name: String?
var captainName: String?

func kj_modelKey(from property: Property) -> ModelPropertyKey {
switch property.name {
case "captainName": return "captain.name"
default: return property.name
}
}
}

let teamName = "V"
let captainName = "Quentin"

let json: [String: Any] = [
"name": teamName,
"captain.name": captainName,
]
let team = json.kj.model(Team.self)
XCTAssert(team.name == teamName)
XCTAssert(team.captainName == captainName)
}

func testConfig1() {
// Global Config
ConvertibleConfig.setModelKey { property in
Expand Down