guild icon
Toit
#Code generation options
Thread channel in help
addshore
addshore 05/15/2025 09:57 AM
I have a few hundred lines of custom yaml that defines a comms protocol
Currently for go I use https://github.com/dave/jennifer to generate structs for all parts of this protocol
What sort of options might there be for toit / do any exist yet / should I try and handroll something for now?
Jennifer is a code generator for Go. Contribute to dave/jennifer development by creating an account on GitHub.
floitsch
floitsch 05/15/2025 10:00 AM
Good question. It depends a bit on the protocol.
I have been working on a Toit mustache library. If your protocol is structured enough you could just generate code this way.
I don't think there is any Toit AST library otherwise. Could be interesting.

If you are interested in the mustache library ping me, and I will share what I have so far.
addshore
addshore 05/15/2025 10:23 AM
defiantly interested in that library
A simple version of what I plan on generating (currently hand crafted) https://github.com/lightbug-io/toit-lightbug/blob/main/src/messages/m11-open.toit
And a more complex one https://github.com/lightbug-io/toit-lightbug/blob/main/src/messages/m15-last-position.toit
Generated from yaml that looks something like this https://github.com/lightbug-io/docs/blob/main/public/files/protocol-v3.yaml
A Toit package for working with Lightbug devices in Toit - lightbug-io/toit-lightbug
A Toit package for working with Lightbug devices in Toit - lightbug-io/toit-lightbug
Documentation site. Contribute to lightbug-io/docs development by creating an account on GitHub.
floitsch
floitsch 05/15/2025 10:27 AM
Contribute to floitsch/toit-hue development by creating an account on GitHub.
floitsch
floitsch 05/15/2025 10:28 AM
It's in a branch, and not everything is finished (which is why I haven't published it yet).
The mustache library should be good, though.
floitsch
floitsch 05/15/2025 10:29 AM
I might have slightly more recent code. Let me have a look and push it if necessary.
floitsch
floitsch 05/15/2025 10:32 AM
So the way I set this up is: I write Toit code "normally", with "// MUSTACHE" comments.
I then replace these strings with the corresponding mustache templates.
The result is then used as input for the mustache library.
This avoids writing Toit code in a mustache file where there is no code completion...

Here is an example: https://github.com/floitsch/toit-hue/blob/floitsch/2025-03-13/tools/openapi-template/api.toit
Contribute to floitsch/toit-hue development by creating an account on GitHub.
💟1
floitsch
floitsch 05/15/2025 10:34 AM
Here is the conversion from Toit file to mustach template.
https://github.com/floitsch/toit-hue/blob/floitsch/2025-03-13/tools/openapi-gen.toit#L284
And immediately afterwards is the application.
Contribute to floitsch/toit-hue development by creating an account on GitHub.
addshore
addshore 05/15/2025 10:35 AM
very nice :🙂: I expect I'll be taking a look at this in the coming days!
floitsch
floitsch 05/15/2025 10:36 AM
I hope it works for you.
Eventually all of these will become their own packages, etc. But that will take time. (I have been working on this for more than a year now...).
floitsch
floitsch 05/15/2025 10:38 AM
I usually work on this project when I travel, which is happening this weekend. I will probably start by making the mustache library its own package. That's probably the most useful now.
💟1
addshore
addshore 05/28/2025 09:38 AM
So, giving it a go, but i still have never really had toit installed, only primarily use it via jag.
the makefile hardcodes @toit
What do you think is the path of least resistance for me? :😛:

I always thought it might be neat to have a jag toit command that just passes through to the toit CLI from jag, as jag fetches the whole of toit somewhere anyway?
addshore
addshore 05/28/2025 09:38 AM
I always forget where it installs it
addshore
addshore 05/28/2025 09:39 AM
but maybe i should just install toit :😛:
addshore
addshore 05/28/2025 10:13 AM
Infact, I might just generally need some more guidance on that repo and how to build the tool.
I managed to get make to work for me with a makefile alteration https://github.com/addshore/toit-mustache/commit/0f07d8818ecfc4c668e8bbe0c2e6d3d5d9f5fc02
however ./build/mustache and ./build/mustache --help seem to do nothing for me :/
addshoreOPaddshore
So, giving it a go, but i still have never really had toit installed, only primarily use it via jag. the makefile hardcodes @toit What do you think is the path of least resistance ...
floitsch
floitsch 05/28/2025 10:30 AM
jag toit is already available in the latest Jaguar release.
💟1
floitsch
floitsch 05/28/2025 10:32 AM
build/mustache -h should definitely give you a help. If it doesn't then something went wrong.
floitsch
floitsch 05/28/2025 10:33 AM
argh.
floitsch
floitsch 05/28/2025 10:33 AM
my mistake.
floitsch
floitsch 05/28/2025 10:33 AM
fixing it.
💟1
floitsch
floitsch 05/28/2025 10:35 AM
Fix is uploaded.
addshore
addshore 05/28/2025 10:57 AM
Amazing, I might try and take another look at it today :😛:
23 messages in total