Skip to content

Swift macro that can get KeyPath of all properties of struct, class, enum or actor

License

Notifications You must be signed in to change notification settings

Ryu0118/KeyPathIterable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

KeyPathIterable

Swift macro that can get KeyPath of all properties of struct, class, enum or actor

Installation

.package(url: "https://github.com/Ryu0118/KeyPathIterable.git", branch: "main")

Usage

import KeyPathIterable

@KeyPathIterable
struct Hoge {
  let hoge: Int
  let fuga: Int
}

Hoge.allKeyPaths // [\.hoge, \.fuga]

Recursively get KeyPath of all properties

@KeyPathIterable
struct Person {
  let name: String
  let age: Int
}

@KeyPathIterable 
struct Parents {
  let mother: Person
  let father: Person
}

let parents = Parents(
  mother: .init(name: "Mary", age: 30),
  father: .init(name: "Jon", age: 30)
)

parents.recursivelyAllKeyPaths // [\.mother, \.mother.name, \.mother.age, \.father, \.father.name, \.father.age]

Known Issue

Computed properties added by extension are not included in allKeyPaths due to macro specifications. You need to add them manually in this way.

@KeyPathIterable
struct Hoge {
  let hoge: String
}

extension Hoge {
  var fuga: Int { 0 }
  static var additionalKeyPaths: [PartialKeyPath<Self>] {
    [\.fuga]
  }
}

Hoge.allKeyPaths // [\.hoge, \.fuga]

About

Swift macro that can get KeyPath of all properties of struct, class, enum or actor

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages