https://toitlang.org/ logo
Code generation options
# help
a
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?
f
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.
a
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
f
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.
I might have slightly more recent code. Let me have a look and push it if necessary.
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
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.
a
very nice 🙂 I expect I'll be taking a look at this in the coming days!
f
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...).
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.
repository has been created and PR is uploaded: https://github.com/toitware/toit-mustache/pull/1
a
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?
I always forget where it installs it
but maybe i should just install toit 😛
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 :/
f
jag toit
is already available in the latest Jaguar release.
build/mustache -h
should definitely give you a help. If it doesn't then something went wrong.
argh.
my mistake.
fixing it.
Fix is uploaded.
a
Amazing, I might try and take another look at it today 😛
2 Views