Skip to content

Commit

Permalink
Workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
wallymathieu committed Feb 18, 2024
1 parent 47a8a0c commit 0facd40
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
11 changes: 8 additions & 3 deletions MvcApp/Models.fs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ let parseId prefix str =
| None -> raise (FormatException str)

[<Struct>]
[<TypeConverter(typeof<ParseTypeConverter<CustomerId>>)>]
[<TypeConverter(typeof<CustomerId_T1>)>]
type CustomerId =
{ Value : Guid }
static member Default : CustomerId = { Value=Guid.Empty }
Expand All @@ -42,8 +42,10 @@ type CustomerId =
with _ ->
result <- Unchecked.defaultof<_>
false
and internal CustomerId_T1 = ParseTypeConverter<CustomerId>

[<Struct>]
[<TypeConverter(typeof<ParseTypeConverter<ProductId>>)>]
[<TypeConverter(typeof<ProductId_T1>)>]
type ProductId =
{ Value : Guid }
static member Default : ProductId = { Value=Guid.Empty }
Expand All @@ -58,8 +60,10 @@ type ProductId =
with _ ->
result <- Unchecked.defaultof<_>
false
and internal ProductId_T1 = ParseTypeConverter<ProductId>

[<Struct>]
[<TypeConverter(typeof<ParseTypeConverter<OrderId>>)>]
[<TypeConverter(typeof<OrderId_T1>)>]
type OrderId =
{ Value : Guid }
static member Default : OrderId = { Value=Guid.Empty }
Expand All @@ -74,6 +78,7 @@ type OrderId =
with _ ->
result <- Unchecked.defaultof<_>
false
and internal OrderId_T1 = ParseTypeConverter<OrderId>

type Customer = {Id:CustomerId; FirstName:string ; LastName:string; Version:int}

Expand Down
10 changes: 6 additions & 4 deletions Saithe/ParseTypeConverters.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ open System.ComponentModel
open System
open System.Reflection
open Newtonsoft.Json
module internal MethodInfos=
let matchParse (t:MethodInfo) = t.Name<>null && (t.Name.Equals("Parse") || t.Name.EndsWith(".Parse")) && t.GetParameters().Length = 2

type ParseTypeConverter<'T (* when IParsable<'T> *) >() =
type ParseTypeConverter<'T when 'T :> IParsable<'T> >() =
inherit TypeConverter()
let strT = typeof<string>
let t = typeof<'T>
let parse_method = t.GetInterface("IParsable`1").GetMethod("Parse")
let parse_method = t.GetMethods() |> Array.find MethodInfos.matchParse

let parse s =
try
Expand All @@ -30,11 +32,11 @@ type ParseTypeConverter<'T (* when IParsable<'T> *) >() =
if destinationType = t then box (parse value)
else box (value.ToString())

type public ParseTypeJsonConverter<'T (* when IParsable<'T> *) >() =
type public ParseTypeJsonConverter<'T when 'T :> IParsable<'T> >() =
inherit JsonConverter()
let t = typeof<'T>

let parse_method = t.GetInterface("IParsable`1").GetMethod("Parse")
let parse_method = t.GetMethods() |> Array.find MethodInfos.matchParse

let parse s =
try
Expand Down
8 changes: 5 additions & 3 deletions Tests/Handle_discriminated_union.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ open Newtonsoft.Json
open System.ComponentModel
open System.Globalization


[<TypeConverter(typeof<ParseTypeConverter<ParseValueType>>)>]
[<JsonConverter(typeof<ParseTypeJsonConverter<ParseValueType>>)>]
[<TypeConverter(typeof<ParseValueType_T1>)>]
[<JsonConverter(typeof<ParseValueType_T2>)>]
type ParseValueType =
| ValueType of string
| Empty
Expand All @@ -33,6 +32,9 @@ type ParseValueType =
with _ ->
result <- Unchecked.defaultof<_>
false
and internal ParseValueType_T1 = ParseTypeConverter<ParseValueType>
and internal ParseValueType_T2 = ParseTypeJsonConverter<ParseValueType>


[<Serializable>]
[<CLIMutable>]
Expand Down
3 changes: 2 additions & 1 deletion Tests/Parse_fs_type.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ open Newtonsoft.Json
open System.ComponentModel
open System.Globalization

[<TypeConverter(typeof<ParseTypeConverter<ParseValueType>>)>]
[<TypeConverter(typeof<ParseValueType_T1>)>]
type ParseValueType={ Value:string }
with
static member Parse (str:string)=
Expand All @@ -24,6 +24,7 @@ with
with _ ->
result <- Unchecked.defaultof<_>
false
and internal ParseValueType_T1 = ParseTypeConverter<ParseValueType>

[<Serializable>]
[<CLIMutable>]
Expand Down

0 comments on commit 0facd40

Please sign in to comment.