Autogenerating API client models with dynamic Sourcery "Swift templates"

(JP) #1

I have a open-source Swift package that interfaces with a RESTful JSON API. One of the features of this SDK is that if your model classes conform to a specific protocol and follow the proper formatting, you can deserialize your own types from this JSON API (the mechanism is an extension of Swift.Decodable.

Anyway, I'm investigating auto-generating the source code for API clients so users can save themselves the time of writing files on their own and I came across Sourcery. I also tried SwiftSyntax, but it really doesn't seem stable yet and the documentation was very sparse.

Anyway, I'd like to use Swift templates instead of Stencil templates so that I can make the proper networking calls to infer the client's model and then dynamically create the templates in Swift—the API & SDK enable introspecting the model to grab the relevant type info and property names.

Is this something you (all) would recommend doing with Sourcery? If so, is there a place where I can find documentation on Swift templates?

Thanks

(JP) #2

Hi, thought I would post my "solution" here just in case:

I have never done much metaprogramming so finding an approach to solve my problem took me some time, but once it clicked, I realized it was quite simple. I'm just using a simple templating language, Mustache, to create my templates and pump the relevant data from my API to generate models.