F#Yaml类型提供者

时间:2016-07-28 14:29:57

标签: f# yaml fsharp.data.typeproviders

我尝试在配置文件中使用Yaml地图集合:

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

但是,当尝试使用类型提供程序读取它时,它只会找到列表的第一个结果。

使用:

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

输出是:

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

尝试解析在线工作的代码,因此我想知道这是图书馆限制还是......?

感谢您的帮助

1 个答案:

答案 0 :(得分:3)

您需要实际加载文件,如果您想直接使用该模式,则不仅要获取模式:config.Load(yamlFile)。这应该在文档中更明确。我在链接中使用了示例文件。

#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

我得到两件物品:

> 
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;}]
>