I like the Design of Swift Package Manager.
More specifically how the configuration/specification is done via the
This design i think has some advantages over doing configuration with something like json.
- its strongly typed
- its more expressive (for instance one can use a enum to express mutually exclusive options)
- its the same language you are already using
- could potentially provide autocompletion
While this is possible in current swift its a bit complicated to setup.
I was wondering if we could provide some facilities to make this kind of API Design easier.
To explain the problem i will use a example project of a webserver.
Following the Design of the SwiftPackageManager the project setup would be something like this:
| Package.swift | WebServer.swift | Sources |__ WebServer | |__ ... |__ WebServerDescription |__ ...
// Package.swift let package = Package( ... targets: [ .target("WebServerDescription"), .target("WebServer", dependencies: [ "WebServerDescription"] ) ] .... )
The goal is to somehow allow the
WebServer target to load its Configuration from a
In this example it could be
// WebServer.swift import WebServerDescription let server = WebServer( url: "127.0.0.1", port: 8080 )
Im sure theres lots of ways this can be done and i dont really know whats feasible.
Heres a couple of ideas that i think could work.
Convert File to Machine Readable Format
This is the approach SwiftPM is using
From what i could gather this is
Package.swift And linking the
PackageDescription and passing some arguments that causes the package defined in
Package.swift to be encoded as json and written to disk to be then consumed later.
This has the limitation that the code doesn’t run as part of your executable, so you cannot directly interact with the code in the configuration file (like providing a delegate)
For SwiftPM i think is by design, to protect against arbitrary code execution.
But i think "arbitrary code execution" could also be a feature.
Dynamically "link"/ load the configuration file
I don’t exactly know whats possible in that regard.
But maybe something like "Dynamic Method Replacement" from this thread could be utilised to load this "directly" into your code
Any Feedback is welcomed