-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Ryu0118/feature/allkeypath-recursively
Add feature to recursively get KeyPath of all properties
- Loading branch information
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,43 @@ | ||
public protocol KeyPathIterable { | ||
static var allKeyPaths: [PartialKeyPath<Self>] { get } | ||
static var allAnyKeyPaths: [AnyKeyPath] { get } | ||
|
||
var allKeyPaths: [PartialKeyPath<Self>] { get } | ||
var allAnyKeyPaths: [AnyKeyPath] { get } | ||
|
||
var recursivelyAllKeyPaths: [PartialKeyPath<Self>] { get } | ||
var recursivelyAllAnyKeyPaths: [AnyKeyPath] { get } | ||
} | ||
|
||
public extension KeyPathIterable { | ||
static var allAnyKeyPaths: [AnyKeyPath] { | ||
allKeyPaths.map { $0 as AnyKeyPath } | ||
} | ||
|
||
var allKeyPaths: [PartialKeyPath<Self>] { | ||
Self.allKeyPaths | ||
} | ||
|
||
var allAnyKeyPaths: [AnyKeyPath] { | ||
allKeyPaths.map { $0 as AnyKeyPath } | ||
} | ||
|
||
var recursivelyAllKeyPaths: [PartialKeyPath<Self>] { | ||
var recursivelyKeyPaths = [PartialKeyPath<Self>]() | ||
for keyPath in allKeyPaths { | ||
recursivelyKeyPaths.append(keyPath) | ||
if let anyKeyPathIterable = self[keyPath: keyPath] as? any KeyPathIterable { | ||
for childKeyPath in anyKeyPathIterable.recursivelyAllAnyKeyPaths { | ||
if let appendedKeyPath = keyPath.appending(path: childKeyPath) { | ||
recursivelyKeyPaths.append(appendedKeyPath) | ||
} | ||
} | ||
} | ||
} | ||
return recursivelyKeyPaths | ||
} | ||
|
||
var recursivelyAllAnyKeyPaths: [AnyKeyPath] { | ||
recursivelyAllKeyPaths.map { $0 as AnyKeyPath } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters