I have been using the @dfinity/candid npm package to encode and decode javascript values into candid values for Azle. It’s going great! From here I want to be able to convert those candid types to the textual syntax for a candid file. I know that the agent can convert a candid file to javascript so I am hoping that there is something that would be able to do essentially the reverse. I was hoping that the display() function that is built into the IDL classes would work, it is almost what I want, but it doesn’t always display valid candid types. For example when I try to display a recursive class I get a bunch of μ prefixed names and for fucs it will diplay → instead of → and other things like that that prevent it from being valid candid.
Are there any libraries that would help me out with this?
Just backing Ben up here, we’ve made really good progress with this for Azle, but there are a few issues like the recursion and not being able to escape Candid keys with quotes.
We’re really hoping to have a robust way to go from IDL → Candid.
Going from IDL to candid is straighforward when you have the interfaceFactory.
In the example JSON lib linked below the IDL interfaceFactory to instantiate custom classes for each IDL type is used. And then toJSON/fromJSON is called for each IDL custom class within the instance tree.
You can basically implement custom classes that get instantiated with a toCandid method. That should be able to output your Candid string without too many complications. Keep in mind there 2 pieces of data for every node within your IDL instance tree, the arguments passed when the node is created e.g. “the nat is 8 bits” and the data the node holds e.g. 231, see candid lib source code for the exact args passed to the IDL node class and the data it holds.
In theory, these additional methods could be added to the candid lib if they’re useful in the candid library for a larger audience.
@lastmjs The RecClass is used for recursive types in the candid lib.
If you use IDL from @dfinity/candid you’ll be limited to functionality within those IDL classes. Only way I’ve been able to work around that is by calling the interfaceFactory with my own custom list of classes with different functionality.
Currently trying to make an example for generating candid from a interfaceFactory input. I’ll link a gist when I’ve managed to get something working.