Created
December 31, 2020 16:20
-
-
Save Luiz-Monad/747c0a0f7e7dd2e00b0a81062c1d51e9 to your computer and use it in GitHub Desktop.
failed try on structural automapping
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
open System.Reflection | |
open System.Collections | |
open Microsoft.FSharp.Reflection | |
open Microsoft.FSharp.Quotations | |
open Microsoft.FSharp.Quotations.ExprShape | |
open Microsoft.FSharp.Quotations.DerivedPatterns | |
let dump tag s = (string s).Dump(string tag); s | |
let getMethodInfo ( expr: Expr<'T> ) ( target: Type ) = | |
let rec traverse quotation = | |
match quotation with | |
| SpecificCall expr ( e, types, exprs ) -> | |
( expr, e, types, expr ) |> Dump | |
Expr.Call ( Option.get e, null, exprs ) | |
| ShapeVar v -> Expr.Var v |> dump "ShapeVar" | |
| ShapeLambda ( v, expr ) -> Expr.Lambda ( v, traverse expr ) |> dump "ShapeLambda" | |
| ShapeCombination ( o, exprs ) -> | |
RebuildShapeCombination ( o, List.map traverse exprs ) |> dump "ShapComb" | |
match traverse expr with | |
| Patterns.Call ( _, mi, _ ) -> Some mi | |
| _ -> None | |
type CustomizedEntity () = class end | |
with | |
member inline this.String = "" | |
static member inline Parse ( _: string ) = CustomizedEntity () | |
let private (|CustomEntityString|_|) o = | |
let target = o.GetType () | |
let dummy = CustomizedEntity () | |
match getMethodInfo <@ dummy.String @> target with | |
| Some meth -> | |
meth.Invoke ( o, [||] ) |> Some | |
| _ -> None | |
let writeObject = function | |
| CustomEntityString value -> | |
Some value | |
| _ -> None | |
module Option = | |
let ofTupleBool (b, v) = | |
if b | |
then Some v else None | |
let either orSome orNone = function | |
| Some s -> orSome s | |
| _ -> orNone | |
type Url = Url of System.Uri | Invalid | |
with | |
member inline this.IsValid = ( function | Url _ -> true | _ -> false ) this | |
member inline this.String = ( function | Url u -> string u | _ -> "" ) this | |
member inline this.Value = ( function | Url u -> u | _ -> failwith "invalid" ) this | |
static member inline Parse (s: string) = | |
Uri.TryCreate ( s, UriKind.Absolute ) | |
|> Option.ofTupleBool |> Option.either Url Invalid | |
static member inline empty = Invalid | |
writeObject ( Url.Parse "http://test" ) |> Dump |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment