user1251614 user1251614 - 6 months ago 61
YAML Question

F# Yaml type provider

I have tried using a Yaml collection of maps in my config file :

- code: 11
name: A
country: FR
functionalCurrency: EUR
- code: 12
name: B
country: GB
functionalCurrency: GBP

However, when trying to read it with the type provider, it only finds the first result of the list.

With :

open FSharp.Configuration
type CompaniesConfig = YamlConfig<"Config.yaml">
let config = CompaniesConfig()

the output is :

val config : CompaniesConfig =
- code: 11
name: A
country: FR
functionalCurrency: EUR

Trying to parse the code online worked, hence I wonder if that is a library limitation or... ?

Thanks for your help


You need to actually load the file, not only get the schema if you want to work with it directly: config.Load(yamlFile). This should probably more explicit in the documentation. I used the sample file in the link.

#r @"..\packages\FSharp.Configuration.0.6.1\lib\net40\FSharp.Configuration.dll"

open FSharp.Configuration
open System.IO


let yamlFile = __SOURCE_DIRECTORY__ + "..\Lists.yaml"

File.Exists yamlFile

type TestConfig = YamlConfig<yamlFile>
let config = TestConfig()


And I get both items:

val it : int = 2
val it : System.Collections.Generic.IList<TestConfig.items_Item_Type> =
       {descrip = "Water Bucket (Filled)";
        part_no = "A4786";
        price = 147;
        quantity = 4;};
       {descrip = "High Heeled "Ruby" Slippers";
        part_no = "E1628";
        price = 10027;
        quantity = 1;}]