F# Type Provider Dependent Nested Types

旧时模样 提交于 2019-12-11 03:19:08

问题


I am trying to build a TypeProvider where nested ProvidedProperty is generated based on parent's type value.

A result I want to have is following

#r @"bin/Debug/library.dll"
open Library.TypeProviders

type sdmx = SdmxDataProvider<WsEntryPoint="">
let dataflows = sdmx.GetDataFlowsContext()

let a = dataflows.A.A1 // A1,A2,A3 < should be generated based on A
let b = dataflows.B.B1 // B1,B2,B3 < should be generated based on B
let c = dataflows.C.C1 // C1,C2,C3 < should be generated based on C

I could generate all dimensionsTypes for all dataflowsTypes.

Then I tried to add argument dfId and then use it to choose properties.

let dimensionsType dfId =
    let t = ProvidedTypeDefinition("Dimensions", Some typeof<string>)
    t.AddMembersDelayed (fun () -> 
        [ for dimension in [(1, "A1"); (1, "A2"); (1, "A3");
                            (2, "B1"); (2, "B2"); (2, "B3");
                            (3, "C1"); (3, "C2"); (3, "C3")] do
              let dataflowId, dimensionName = dimension
              if dataflowId = dfId then
                  let prop = 
                      ProvidedProperty
                        ( dimensionName, typeof<string>, 
                          GetterCode = (fun args -> <@@ (%%args.[0]) :> string @@>))
                  yield prop                     
        ])
    serviceTypesType.AddMember t
    t

let dataflowsType =
    let t = ProvidedTypeDefinition("Dataflows", Some typeof<string>)
    t.AddMembersDelayed (fun () ->
        [ for dataflow in [(1, "A"); (2, "B") ; (3, "C")] do
            let dataflowId, dataflowName = dataflow
            let prop = ProvidedProperty(dataflowName, dimensionsType dataflowId, GetterCode = (fun args -> <@@ (%%args.[0]) :> string @@>))
            yield prop])
    serviceTypesType.AddMember t
    t

This approach raises error: error FS1109: A reference to the type 'Library.TypeProviders.SdmxDataProvider,WsEntryPoint="".ServiceTypes.Dimensions' in assembly 'Library' was found, but the type could not be found in that assembly

Is it possible to do this kind of type generation?

Full Source: https://gist.github.com/demonno/f9234ced664439986597cf7c3218d3b8

来源:https://stackoverflow.com/questions/53248969/f-type-provider-dependent-nested-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!