Skip to content

Commit ed29839

Browse files
committed
Add CodingKey extensions
1 parent 92097a1 commit ed29839

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// CodingKey.swift
3+
//
4+
//
5+
// Created by Alsey Coleman Miller on 8/18/23.
6+
//
7+
8+
internal extension Sequence where Element == CodingKey {
9+
10+
/// KVC path string for current coding path.
11+
var path: String {
12+
return reduce("", { $0 + "\($0.isEmpty ? "" : ".")" + $1.stringValue })
13+
}
14+
}
15+
16+
internal extension CodingKey {
17+
18+
static var sanitizedName: String {
19+
20+
let rawName = String(reflecting: self)
21+
var elements = rawName.split(separator: ".")
22+
guard elements.count > 2
23+
else { return rawName }
24+
elements.removeFirst()
25+
elements.removeAll { $0.contains("(unknown context") }
26+
return elements.reduce("", { $0 + ($0.isEmpty ? "" : ".") + $1 })
27+
}
28+
}

0 commit comments

Comments
 (0)