Skip to content

Commit

Permalink
Replicate Fable behaviour when encoding StringEnum when not NETFRAMEWORK
Browse files Browse the repository at this point in the history
Fix #11
  • Loading branch information
Maxime Mangel committed Jun 7, 2019
1 parent ff13c1b commit ed91c00
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
8 changes: 4 additions & 4 deletions paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ storage:none
nuget FSharp.Core redirects:force
nuget Newtonsoft.Json >= 11.0.2 lowest_matching:true
nuget Expecto
nuget Fable.Core
nuget Fable.Core framework: netstandard2.0

# Get the tests from Thoth.Json repo so both project are in sync
github thoth-org/Thoth.Json:develop tests/Types.fs
github thoth-org/Thoth.Json:develop tests/Decoders.fs
github thoth-org/Thoth.Json:develop tests/Encoders.fs
github thoth-org/Thoth.Json tests/Types.fs
github thoth-org/Thoth.Json tests/Decoders.fs
github thoth-org/Thoth.Json tests/Encoders.fs

group netcorebuild
source https://www.nuget.org/api/v2
Expand Down
8 changes: 4 additions & 4 deletions paket.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ NUGET
Expecto (8.10.1)
FSharp.Core (>= 4.3.4) - restriction: || (>= net461) (>= netstandard2.0)
Mono.Cecil (>= 0.10.3) - restriction: || (>= net461) (>= netstandard2.0)
Fable.Core (3.0)
Fable.Core (3.0) - restriction: == netstandard2.0
FSharp.Core (>= 4.5.2) - restriction: >= netstandard2.0
FSharp.Core (4.6.2) - redirects: force
System.Collections (>= 4.0.11) - restriction: && (< net45) (>= netstandard1.6) (< netstandard2.0)
Expand Down Expand Up @@ -673,9 +673,9 @@ NUGET
System.Xml.ReaderWriter (>= 4.3) - restriction: && (< monoandroid) (< monotouch) (< net46) (>= netstandard1.3) (< xamarinios) (< xamarinmac) (< xamarintvos) (< xamarinwatchos)
GITHUB
remote: thoth-org/Thoth.Json
tests/Decoders.fs (0640a98cd571e90ccd573dbf6f963f8c1a204fa7)
tests/Encoders.fs (0640a98cd571e90ccd573dbf6f963f8c1a204fa7)
tests/Types.fs (0640a98cd571e90ccd573dbf6f963f8c1a204fa7)
tests/Decoders.fs (ac975a26a0c9d611ec792d123b550f35fbc66ef4)
tests/Encoders.fs (ac975a26a0c9d611ec792d123b550f35fbc66ef4)
tests/Types.fs (ac975a26a0c9d611ec792d123b550f35fbc66ef4)
GROUP netcorebuild
RESTRICTION: == netstandard2.0
NUGET
Expand Down
15 changes: 12 additions & 3 deletions src/Encode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,15 @@ module Encode =
Some(fun (v: obj) -> (v :?> System.Guid).ToString())
else None

#if !NETFRAMEWORK
let private (|StringEnum|_|) (typ : System.Type) =
typ.CustomAttributes
|> Seq.tryPick (function
| attr when attr.AttributeType.FullName = "Fable.Core.StringEnumAttribute" -> Some attr
| attr when attr.AttributeType.FullName = typeof<Fable.Core.StringEnumAttribute>.FullName -> Some attr
| _ -> None
)


let private (|CompiledName|_|) (caseInfo : UnionCaseInfo) =
caseInfo.GetCustomAttributes()
|> Seq.tryPick (function
Expand All @@ -344,19 +346,22 @@ module Encode =
let private (|LowerFirst|Forward|) (args : IList<System.Reflection.CustomAttributeTypedArgument>) =
args
|> Seq.tryPick (function
| rule when rule.ArgumentType.FullName = "Fable.Core.CaseRules" -> Some rule
| rule when rule.ArgumentType.FullName = typeof<Fable.Core.CaseRules>.FullName -> Some rule
| _ -> None
)
|> function
| Some rule ->
match rule.Value with
| :? int as value ->
printfn "%A" value
match value with
| 0 -> Forward
| 1 -> LowerFirst
| _ -> LowerFirst // should not happen
| _ -> LowerFirst // should not happen
| None -> LowerFirst
| None ->
LowerFirst
#endif

let rec private autoEncodeRecordsAndUnions extra (isCamelCase : bool) (t: System.Type) : BoxedEncoder =
// Add the encoder to extra in case one of the fields is recursive
Expand All @@ -383,6 +388,7 @@ module Encode =
let info, fields = FSharpValue.GetUnionFields(value, t, allowAccessToPrivateRepresentation=true)
match fields.Length with
| 0 ->
#if !NETFRAMEWORK
match t with
// Replicate Fable behaviour when using StringEnum
| StringEnum t ->
Expand All @@ -396,6 +402,9 @@ module Encode =
| Forward -> string info.Name

| _ -> string info.Name
#else
string info.Name
#endif

| len ->
let fieldTypes = info.GetFields()
Expand Down
1 change: 1 addition & 0 deletions src/paket.references
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
FSharp.Core
Newtonsoft.Json
Fable.Core

0 comments on commit ed91c00

Please sign in to comment.