user1251614 user1251614 - 4 months ago 15
YAML Question

F# Yaml type provider

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

Companies:
- 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 =
Companies:
- 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

Answer

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.

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

open FSharp.Configuration
open System.IO

/// https://github.com/fsprojects/FSharp.Configuration/blob/master/tests/FSharp.Configuration.Tests/Lists.yaml

[<Literal>]
let yamlFile = __SOURCE_DIRECTORY__ + "..\Lists.yaml"

File.Exists yamlFile

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

config.Load(yamlFile)
config.items.Count
config.items

And I get both items:

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