3 people like it.

Showing generic record array from server to html client

Including generic abstraction to manage a RPC call returning either an error or an array of record types from DB server to client browser.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
open Microsoft.FSharp.Reflection

let recordHeader<'record>  
    : string [] = 
    FSharpType.GetRecordFields(typeof<'record>) 
    |> Array.map (fun field -> field.Name ) 

let recordValues<'record> (record:'record) 
    : obj [] = 
    FSharpType.GetRecordFields(record.GetType()) 
    |> Array.map (fun field -> FSharpValue.GetRecordField(record,field) ) 

type GenericEntityArray = {Values: obj array array; Header: string array}

type GenericExtract =
    Result<GenericEntityArray, string>

let toGenericResult<'record> (result: Result<'record[], string>) 
    : GenericExtract =
    match result with
    | Error error -> Error error
    | Ok array ->
        Ok {
            Header = recordHeader<'record>;
            Values = array |> Array.map recordValues; 
        } 
namespace Microsoft
namespace Microsoft.FSharp
namespace Microsoft.FSharp.Reflection
val recordHeader<'record> : string []

Full name: Script.recordHeader
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
type FSharpType =
  static member GetExceptionFields : exceptionType:Type * ?bindingFlags:BindingFlags -> PropertyInfo []
  static member GetFunctionElements : functionType:Type -> Type * Type
  static member GetRecordFields : recordType:Type * ?bindingFlags:BindingFlags -> PropertyInfo []
  static member GetTupleElements : tupleType:Type -> Type []
  static member GetUnionCases : unionType:Type * ?bindingFlags:BindingFlags -> UnionCaseInfo []
  static member IsExceptionRepresentation : exceptionType:Type * ?bindingFlags:BindingFlags -> bool
  static member IsFunction : typ:Type -> bool
  static member IsModule : typ:Type -> bool
  static member IsRecord : typ:Type * ?bindingFlags:BindingFlags -> bool
  static member IsTuple : typ:Type -> bool
  ...

Full name: Microsoft.FSharp.Reflection.FSharpType
static member FSharpType.GetRecordFields : recordType:System.Type * ?allowAccessToPrivateRepresentation:bool -> System.Reflection.PropertyInfo []
static member FSharpType.GetRecordFields : recordType:System.Type * ?bindingFlags:System.Reflection.BindingFlags -> System.Reflection.PropertyInfo []
val typeof<'T> : System.Type

Full name: Microsoft.FSharp.Core.Operators.typeof
module Array

from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> array:'T [] -> 'U []

Full name: Microsoft.FSharp.Collections.Array.map
val field : System.Reflection.PropertyInfo
property System.Reflection.MemberInfo.Name: string
val recordValues : record:'record -> obj []

Full name: Script.recordValues
val record : 'record
type obj = System.Object

Full name: Microsoft.FSharp.Core.obj
System.Object.GetType() : System.Type
type FSharpValue =
  static member GetExceptionFields : exn:obj * ?bindingFlags:BindingFlags -> obj []
  static member GetRecordField : record:obj * info:PropertyInfo -> obj
  static member GetRecordFields : record:obj * ?bindingFlags:BindingFlags -> obj []
  static member GetTupleField : tuple:obj * index:int -> obj
  static member GetTupleFields : tuple:obj -> obj []
  static member GetUnionFields : value:obj * unionType:Type * ?bindingFlags:BindingFlags -> UnionCaseInfo * obj []
  static member MakeFunction : functionType:Type * implementation:(obj -> obj) -> obj
  static member MakeRecord : recordType:Type * values:obj [] * ?bindingFlags:BindingFlags -> obj
  static member MakeTuple : tupleElements:obj [] * tupleType:Type -> obj
  static member MakeUnion : unionCase:UnionCaseInfo * args:obj [] * ?bindingFlags:BindingFlags -> obj
  ...

Full name: Microsoft.FSharp.Reflection.FSharpValue
static member FSharpValue.GetRecordField : record:obj * info:System.Reflection.PropertyInfo -> obj
type GenericEntityArray =
  {Values: obj array array;
   Header: string array;}

Full name: Script.GenericEntityArray
GenericEntityArray.Values: obj array array
type 'T array = 'T []

Full name: Microsoft.FSharp.Core.array<_>
GenericEntityArray.Header: string array
type GenericExtract = obj

Full name: Script.GenericExtract
val toGenericResult<'record> : result:obj -> GenericExtract

Full name: Script.toGenericResult
val result : obj

More information

Link:http://fssnip.net/7Xk
Posted:5 years ago
Author:giuliohome
Tags: generic , record types