What are the plans to enable macros to read from files or access the network?
I understand the performance and security concerns, as well as the "how should a macro know you changed a file so it can update the code?", but I don't think any of these reasons are blocking.
After all, build plugins already have ways to acquire those permissions.
Use case
As a Server-Side Swift person, It's been a while I'm considering creating a macro for sql queries.
After some time, I noticed basically the same thing that I was thinking of, is already available in Rust: query in sqlx - Rust
This macro either creates a live connection to the db to read table schema and all (I assume it does some caching as well), or read from a static file that you've generated for it from the db using a cli command, which contains the necessary info about the db for the macro.
This is clearly not possible to do in Swift, and that's a bummer.
My more-immediate use case is related to my new project, EnumeratorMacro, which generates code for enums, based on Mustache templates.
For example:
@Enumerator("""
var caseName: String {
switch self {
{{#cases}}
case .{{name}}: "{{snakeCased(name)}}"
{{/cases}}
}
}
""")
enum TestEnum {
case a(val1: String, val2: Int)
case b
case testCase(testValue: String)
}
Is expanded to:
enum TestEnum {
case a(val1: String, val2: Int)
case b
case testCase(testValue: String)
+ var caseName: String {
+ switch self {
+ case .a: "a"
+ case .b: "b"
+ case .testCase: "test_case"
+ }
+ }
}
The ability to read files could enable users to create shared templates to be read from Disk. It'll kind of enable users to create their own custom macro.
Imagine you could do @CreateCaseName
and it'd automatically create the same code as above for you.
See this issue for more info.
Sooo ... am I missing something or does it seem like Swift should lift some limits from macros now that they're more-established?
Related Open Source Slack thread since some discussion went on there.