Skip to content

Instantly share code, notes, and snippets.

@smoothdeveloper
Created June 21, 2016 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smoothdeveloper/94332dbd7b894d2dc45cbc1f47a75ac5 to your computer and use it in GitHub Desktop.
Save smoothdeveloper/94332dbd7b894d2dc45cbc1f47a75ac5 to your computer and use it in GitHub Desktop.
CsvDistinctValuesProvider
namespace MyTypeProviders
open System
open System.IO
open System.Reflection
open ProviderImplementation.ProvidedTypes
open Microsoft.FSharp.Core.CompilerServices
open CsvHelper
open System.Collections.Generic
open Microsoft.FSharp.Quotations
[<TypeProvider>]
type TheCsvDistinctValuesProvider (config: TypeProviderConfig) as this =
inherit TypeProviderForNamespaces()
let ns = this.GetType().Namespace
let asm = Assembly.LoadFrom(config.RuntimeAssembly)
let providerType = ProvidedTypeDefinition(asm, ns, "CsvDistinctValuesProvider", Some typeof<obj> , HideObjectMethods = true, IsErased = false)
let tempAssembly = ProvidedAssembly(Path.ChangeExtension(Path.GetTempFileName(), ".dll"))
do
tempAssembly.AddTypes [providerType]
do
providerType.DefineStaticParameters(
parameters = [
ProvidedStaticParameter("CsvFile", typeof<string>)
ProvidedStaticParameter("FieldName", typeof<string>)
ProvidedStaticParameter("Delimiter", typeof<string>)
],
instantiationFunction = fun typename args -> this.instanciate(typename, (unbox args.[0]), (unbox args.[1]), (unbox args.[2]))
)
do
this.AddNamespace(ns, [providerType])
member this.instanciate(typename, csvFile, (fieldName: string), delimiter) =
let tempAssembly = ProvidedAssembly(Path.ChangeExtension(Path.GetTempFileName(), ".dll"))
let providedCsvLiteralsType = ProvidedTypeDefinition(asm, ns, typename, baseType = Some typeof<obj> , HideObjectMethods = true, IsErased = false)
tempAssembly.AddTypes [ providedCsvLiteralsType ]
let values =
let values = HashSet<_>()
begin
use textReader = File.OpenText(csvFile)
use csvReader = new CsvReader(textReader)
csvReader.Configuration.Delimiter <- delimiter
while csvReader.Read() do
let value = csvReader.GetField (fieldName)
if not (String.IsNullOrEmpty value) then
values.Add value |> ignore
end
values
values
|> Seq.map (fun value -> ProvidedLiteralField(value, typeof<string>, value))
|> Seq.iter providedCsvLiteralsType.AddMember
let typeInit = ProvidedConstructor([], IsTypeInitializer = true)
typeInit.InvokeCode <- fun _ -> <@@ () @@>
providedCsvLiteralsType.AddMember typeInit
providedCsvLiteralsType
@jpierson
Copy link

Looking at this for the first time and after pulling this code into a local project I get compiler errors on references to ProviderImplementation.ProvidedTypes and TypeProviderForNamespaces as they don't appear to be defined.

@jpierson
Copy link

Ah, I found a reference to the FSharp.TypeProviders.StarterPack Nuget package and on the GitHub project README it gives a pretty good explanation of the current state of things wrt writing type providers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment