krzysztofzablocki / Sourcery

Meta-programming for Swift, stop writing boilerplate code.
http://merowing.info
MIT License
7.65k stars 616 forks source link

Attach information for every property (compile time required) #1039

Open Nikoloutsos opened 2 years ago

Nikoloutsos commented 2 years ago

I'm trying to build an automatic way to create fixtures that are used in unit tests to easily create models. https://medium.com/@bruno.hcr/swift-tests-tips-tricks-fixture-object-pattern-5decefe6f10c

// sourcery: Autofixturable
class Person {
     // sourcery fixture = .init(fromString: "dasdsadsa-dasddede2-3rd2e")
     let id: UUID

     // sourcery fixture = "James Bond"
     let name: String

     // sourcery fixture = 18
     let age: Int

     // sourcery fixture = .male
     let sex: Sex // enum
}

The only way to pass information about a single property was to add a comment like // sourcery fixture = 18. But this is not compile time checked as it is a comment.

So I was wondering.. can Sourcery somehow read actual code and not declarations only? Or is there any workaround to pass information for a property but at the same time have compile time check!

krzysztofzablocki commented 2 years ago

We only analyze AST but I can imagine you could potentially leverage PropertyWrappers? we have basic support for them already and could potentially extend it