How to evaluate symbols with swift macros

I'm trying to make a macro that has the following structure:

@SampleDataProviding(decoder: JSONDecoder(), bundlePath: Bundle.module.path(forResource:"Sample Data", ofType: nil), fileExtension: "json")

The primary issue that I am having is that I need to be able to scan a subdirectory of the provided path for files that match a certain criteria. However, when I try to access the path it literally spits back at me Bundle.module.path(forResource:"Sample Data", ofType: nil) which obviously doesn't get me anywhere. I've looked all over for a way to convert what I enter at the macro call site to an actual path that I can put in code that isn't generated. (This macro goes through a specific subdirectory looking for sample data files which it then uses to generate a variable expression for each of them so that it is easy to access this sample data (for UI work or testing).) Each time I try this it fails stating that the folder doesn't exist because it's looking for Bundle.module.path(forResource:"Sample Data", ofType: nil)/[subdirectory] when I want it to look in the path, not in Bundle.module.path(forResource:"Sample Data", ofType: nil). I've been able to get this to work in testing by wrapping the call in an interpolation from a string but this does not work in the real world because the compiler complains that the interpolation cannot be modified (it is also more clunky, I would prefer to avoid having to wrap the entire thing in an interpolation). It will also work if I manually type out the path, but the bundle path can change between computers, so this is something that I would really like to avoid.

1 Like

The primary issue with this that I'm having is that I need in order to generate the accessors for the sample data files that are in the bundle directory, I need to know where to look for the files. At the moment I'm using the FileManager type from foundation to perform the searches. I'm trying to find a way to do this, thus it isn't strictly necessary for me to ask for the path.

try FileManager.default.contentsOfDirectory(atPath: "\(sampleDataDirectoryPath)/\(fetcherName)")

I think there is multiple problems with you approach.

The first one being Swift macro explicitly forbids you to access the file system. (Expand on Swift macros - WWDC23 - Videos - Apple Developer)

Also the code you provide should execute at runtime to be valuable, however the macros execute at compile time so Bundle.module.path(forResource:"Sample Data", ofType: nil) doesn't exist.