Given that type providers is not supported yet, I need some other convenient way to parse a YAML file in F#. However, as type providers are so awesome, I'm finding it difficult to find anything else when I search the internet for an alternative solution.
What's the simplest way to parse a config file in F#, given that type providers are off the table?
Using a library is fine, but the more OO that the interface of that library is, the less convenient it will be for use in F#.
I don't need a full "deserialize any yaml into a given type/object graph" either; something like xpath querying but for YAML is perfectly fine; I just don't want to have to read the stream and parse it manually.
Here is my current attempt, which fails at runtime because the discriminated type union OneOfSeveral
does not have a default constructor. It doesn't really surprise me that it requires some special handling, but I have no idea how to go about doing it.
open System
open YamlDotNet.Serialization
[<CLIMutable>]
type SpecificThing = {
foo : string
bar : int
}
type OneOfSeveral = Thing of SpecificThing
[<CLIMutable>]
type Root = {
option : OneOfSeveral
}
[<EntryPoint>]
let main argv =
let yaml = @"---
option:
thing:
foo: foobar
bar: 17
"
let deserializer = DeserializerBuilder().Build()
let config = deserializer.Deserialize<Root>(yaml)
printfn "%A" config
0
I'm also not sure how I want to represent the choice in the type union in the YAML; I've considered several options:
# omit the 'option' level completely
thing:
foo: foobar
bar: 17
# have a specific field to discriminate on
option:
type: thing
foo: foobar
bar: 17
In the end, it's more important to me to have a flexible object graph for the config, than to have a nice-looking YAML file, so whatever works...