jeudi 6 mai 2021

Get value of optional types when retrieving value of record fields in F#

I have a type defined as follows:

type Employee = {
    Id:    Guid
    Name:  string
    Phone: string
    Email: Option<string>
}

and an instance of this type:

let emp = {
    Id = Guid "bc07e94c-b376-45a2-928b-508b888802c9"
    Name = "A"
    Phone = "B"
    Email = Some "E"
}

I want to extract the field names and values from this record type using reflection like the following:

let getFieldValueMappingOfARecordType (data: 'T) : seq<string * obj> =
    let fieldValueMapping =
        data.GetType()
        |> FSharpType.GetRecordFields
        |> Seq.map (
            fun propertyInfo ->

                    (propertyInfo.Name, data |> propertyInfo.GetValue)
        )
    fieldValueMapping

Then invoking the above function with the instance of employee type

let mapping = getFieldValueMappingOfARecordType emp
            |> Seq.toList

gives us:

val mapping : (string * obj) list =
  [("Id", bc07e94c-b376-45a2-928b-508b888802c9); ("Name", "A"); ("Phone", "B");
   ("Email", Some "E")]

So far it's working well with non-optional type. But in case of optional types, it's returning the value of the field as either Some value or None. What I would like to do is to get the value when the field has Some value or make it null when it's None.

Essentially like the follwing:

val mapping : (string * obj) list =
  [("Id", bc07e94c-b376-45a2-928b-508b888802c9); ("Name", "A"); ("Phone", "B");
   ("Email", "E")]

Or if the employee instance is like the following:

let emp = {
    Id = Guid "bc07e94c-b376-45a2-928b-508b888802c9"
    Name = "A"
    Phone = "B"
    Email = None
}

Then,

val mapping : (string * obj) list =
  [("Id", bc07e94c-b376-45a2-928b-508b888802c9); ("Name", "A"); ("Phone", "B");
   ("Email", null)]

This is what I have so far (non-working code):

open System
open Microsoft.FSharp.Reflection
open System.Reflection

type Employee = {
    Id:    Guid
    Name:  string
    Phone: string
    Email: Option<string>
}

let emp = {
    Id = Guid "bc07e94c-b376-45a2-928b-508b888802c9"
    Name = "A"
    Phone = "B"
    Email = Some "E"
}

let getSomeOrNull (t: Type) (o: obj) =
    let opt = typedefof<option<_>>.MakeGenericType [| t |]
    match (o :?> opt) with
    | Some s ->
        s
    | None ->
        null

let getValues (data: 'T) =
    let values =
        data.GetType()
        |> FSharpType.GetRecordFields
        |> Array.map (
            fun propertyInfo ->
                let value =
                    data |> propertyInfo.GetValue

                let isOption =
                    propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition() = typedefof<Option<_>>

                match isOption with
                | true ->
                    (propertyInfo.Name, (getSomeOrNull propertyInfo.PropertyType value))
                | false ->
                    (propertyInfo.Name, value)
        )
    values

getValues emp
|> printfn "%A"




Aucun commentaire:

Enregistrer un commentaire