Is there an easy way to read the values from a toml file?
I’m using otoml, the library allow you to read the values directly from the toml document, but does not provide any high level function for parsing a whole configuration file (like declaring a field as mandatory, transforming a table into an OCaml record and so one).
For that, I’m using decoders, which gives me what I need to declare my configuration in a verbose way (using a generic decoder):
let source =
let* file = D.field "file" S.string
and* name = D.field "name" S.string
and* tab = D.field_opt_or ~default:1 "tab" D.int in
D.succeed { Table.file; name; tab }
Please, if you’re writing a new application, consider a nicer configuration format - like JSON5. I’m working (on/off) on a JSON5 parser. But dhall would be even cooler - I am thinking of writing a parser for that, unless someone else has already started?
–
Ian
I completely agree, I try to only use scfg nowadays (disclaimer: I wrote the OCaml implementation).
But if you have to stick with toml, I recommend ez_toml.
Thank you for that information
I looked at ez_toml and searched for an example without success.
There is example code for scfg.
I would simply like to read a simple config file and save its values in variables.
In golang it’s one line of code and I was hoping it would be just as easy in ocaml.
Overall, I think it would be desirable if the libraries had corresponding examples
Did you file an example in the ez_toml repo asking for examples?
Edit: sorry I mean ‘did you file an issue’
I would be very grateful for examples
I am not sure if this is a good example, but here is a composition over ez_toml
:
-
We define first some kind of abstract idea of a configuration file with sections and options:
superbol-studio-oss/src/lsp/ezr_toml/ezr_toml.ml at 9dc319fc9d680d506de21fa5297aac8eda5c7d03 · OCamlPro/superbol-studio-oss · GitHub -
We then use it to define a configuration file in toml format:
superbol-studio-oss/src/lsp/superbol_free_lib/command_switch.ml at 9dc319fc9d680d506de21fa5297aac8eda5c7d03 · OCamlPro/superbol-studio-oss · GitHub
If you have an example in Golang of how TOML files are accessed in a typed-fashion in a few lines, I would be interested in seeing it to get some inspiration to improve the interface of ez_toml
.
I forgot to say, but the main advantage of ez_toml
over other implementation of TOML in OCaml is that it also manages comments in the file.
FWIW, I updated the README.md with an example similar to the one in OToml: GitHub - OCamlPro/ez_toml: A library to parse and print TOML files