Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions ConsumePlugin/GeneratedJson.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace ConsumePlugin

open System.Collections.Generic
open System.Text.Json.Serialization

/// Module containing JSON serializing methods for the InternalTypeNotExtensionSerial type
Expand Down Expand Up @@ -36,6 +37,7 @@ module internal InternalTypeNotExtensionSerial =
node :> _
namespace ConsumePlugin

open System.Collections.Generic
open System.Text.Json.Serialization

/// Module containing JSON serializing extension members for the InternalTypeExtension type
Expand Down Expand Up @@ -94,6 +96,27 @@ namespace ConsumePlugin
module JsonRecordType =
/// Parse from a JSON node.
let jsonParse (node : System.Text.Json.Nodes.JsonNode) : JsonRecordType =
let arg_6 =
match node.["g"] |> Option.ofObj with
| None ->
raise (
System.Collections.Generic.KeyNotFoundException (
sprintf "Required key '%s' not found on JSON object" ("g")
)
)
| Some node ->
node.AsObject ()
|> Seq.map (fun kvp ->
let key = (kvp.Key)
let value = kvp.Value |> Option.ofObj

key,
match value with
| None -> System.Nullable ()
| Some v -> v.AsValue().GetValue<System.Int32> () |> System.Nullable
)
|> dict

let arg_5 =
match node.["f"] |> Option.ofObj with
| None ->
Expand Down Expand Up @@ -194,6 +217,7 @@ module JsonRecordType =
D = arg_3
E = arg_4
F = arg_5
G = arg_6
}
namespace ConsumePlugin

Expand Down
2 changes: 2 additions & 0 deletions ConsumePlugin/JsonRecord.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace ConsumePlugin

open System.Collections.Generic
open System.Text.Json.Serialization

module Literals =
Expand Down Expand Up @@ -27,6 +28,7 @@ type JsonRecordType =
D : InnerType
E : string array
F : int[]
G : IDictionary<string, System.Nullable<int>>
}

[<WoofWare.Myriad.Plugins.JsonParse>]
Expand Down
59 changes: 58 additions & 1 deletion WoofWare.Myriad.Plugins.Test/TestJsonParse/TestJsonParse.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace WoofWare.Myriad.Plugins.Test

open System
open System.Collections.Generic
open System.Text.Json.Nodes
open ConsumePlugin
open NUnit.Framework
Expand All @@ -14,7 +16,7 @@ module TestJsonParse =
let s =
"""
{
"a": 3, "another-thing": "hello", "hi": [6, 1], "d": {"something": "oh hi"},
"a": 3, "another-thing": "hello", "hi": [6, 1], "d": {"something": "oh hi"}, "g": {},
"e": ["something", "else"], "f": []
}
"""
Expand All @@ -30,6 +32,61 @@ module TestJsonParse =
}
E = [| "something" ; "else" |]
F = [||]
G = dict []
}

let actual = s |> JsonNode.Parse |> JsonRecordType.jsonParse
actual |> shouldEqual expected

[<Test>]
let ``Single example with explicit null`` () =
let s =
"""
{
"a": 3, "another-thing": "hello", "hi": [6, 1], "d": {"something": "oh hi"}, "g": {"hi": null},
"e": ["something", "else"], "f": []
}
"""

let expected =
{
A = 3
B = "hello"
C = [ 6 ; 1 ]
D =
{
Thing = "oh hi"
}
E = [| "something" ; "else" |]
F = [||]
G = dict [ "hi", Nullable () ]
}

let actual = s |> JsonNode.Parse |> JsonRecordType.jsonParse
actual |> shouldEqual expected

[<Test>]
let ``Single example, nullable provided`` () =
let s =
"""
{
"a": 3, "another-thing": "hello", "hi": [6, 1], "d": {"something": "oh hi"}, "g": {"hi": 3},
"e": ["something", "else"], "f": []
}
"""

let expected =
{
A = 3
B = "hello"
C = [ 6 ; 1 ]
D =
{
Thing = "oh hi"
}
E = [| "something" ; "else" |]
F = [||]
G = dict [ "hi", Nullable 3 ]
}

let actual = s |> JsonNode.Parse |> JsonRecordType.jsonParse
Expand Down
9 changes: 8 additions & 1 deletion WoofWare.Myriad.Plugins/JsonParseGenerator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,14 @@ module internal JsonParseGenerator =
=
let keyArg = SynExpr.createLongIdent [ "kvp" ; "Key" ] |> SynExpr.paren

let valueArg = SynExpr.createLongIdent [ "kvp" ; "Value" ]
let valueArg =
let value = SynExpr.createLongIdent [ "kvp" ; "Value" ]

if valueTypeIsNullable then
value
|> SynExpr.pipeThroughFunction (SynExpr.createLongIdent [ "Option" ; "ofObj" ])
else
value

let value =
if valueTypeIsNullable then
Expand Down
Loading