Compare commits

...

2 Commits

Author SHA1 Message Date
Patrick Stevens
f944953384 Enforce non-nullability in more cases during JSON parse (#367) 2025-04-20 17:20:22 +00:00
Patrick Stevens
7930039ad1 Revert "Some fixes to nullability (#365)" (#366)
This reverts commit 8d275f0047.
2025-04-20 17:02:40 +00:00
16 changed files with 960 additions and 1707 deletions

View File

@@ -1,5 +1,10 @@
Notable changes are recorded here.
# WoofWare.Myriad.Plugins 5.0.1
We now enforce non-nullability on more types during JSON parse.
We have always expected you to consume nullable types wrapped in an `option`, but now we enforce this in more cases by throwing `ArgumentNullException`.
# WoofWare.Myriad.Plugins 3.0.1
Semantics of `HttpClient`'s URI component composition changed:

View File

@@ -11,10 +11,6 @@
<ItemGroup>
<None Include="myriad.toml"/>
<Compile Include="JsonParseNullness.fs" />
<Compile Include="GeneratedJsonParseNullness.fs">
<MyriadFile>JsonParseNullness.fs</MyriadFile>
</Compile>
<Compile Include="AssemblyInfo.fs" />
<Compile Include="RecordFile.fs"/>
<Compile Include="GeneratedRecord.fs">

File diff suppressed because it is too large Load Diff

View File

@@ -71,8 +71,11 @@ module JsonRecordType =
)
| v -> v)
.AsArray ()
|> Seq.cast<System.Text.Json.Nodes.JsonNode>
|> Seq.map (fun elt -> elt.AsValue().GetValue<System.Int32> ())
|> Seq.map (fun elt ->
(match elt with
| null -> raise (System.ArgumentNullException ())
| elt -> elt.AsValue().GetValue<System.Int32> ())
)
|> Array.ofSeq
let arg_4 =
@@ -85,8 +88,11 @@ module JsonRecordType =
)
| v -> v)
.AsArray ()
|> Seq.cast<System.Text.Json.Nodes.JsonNode>
|> Seq.map (fun elt -> elt.AsValue().GetValue<System.String> ())
|> Seq.map (fun elt ->
(match elt with
| null -> raise (System.ArgumentNullException ())
| elt -> elt.AsValue().GetValue<System.String> ())
)
|> Array.ofSeq
let arg_3 =
@@ -111,8 +117,11 @@ module JsonRecordType =
)
| v -> v)
.AsArray ()
|> Seq.cast<System.Text.Json.Nodes.JsonNode>
|> Seq.map (fun elt -> elt.AsValue().GetValue<System.Int32> ())
|> Seq.map (fun elt ->
(match elt with
| null -> raise (System.ArgumentNullException ())
| elt -> elt.AsValue().GetValue<System.Int32> ())
)
|> List.ofSeq
let arg_1 =
@@ -204,15 +213,7 @@ module ToGetExtensionMethodJsonParseExtension =
/// Parse from a JSON node.
static member jsonParse (node : System.Text.Json.Nodes.JsonNode) : ToGetExtensionMethod =
let arg_20 =
let v = node.["whiskey"]
System.Numerics.BigInteger.Parse (
(match v with
| null -> raise (System.ArgumentNullException ())
| v -> v)
.ToJsonString ()
)
let arg_20 = System.Numerics.BigInteger.Parse (node.["whiskey"].ToJsonString ())
let arg_19 =
(match node.["victor"] with

View File

@@ -1,53 +0,0 @@
//------------------------------------------------------------------------------
// This code was generated by myriad.
// Changes to this file will be lost when the code is regenerated.
//------------------------------------------------------------------------------
namespace ConsumePlugin
/// Module containing JSON parsing methods for the InnerStruct type
[<RequireQualifiedAccess ; CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module InnerStruct =
/// Parse from a JSON node.
let jsonParse (node : System.Text.Json.Nodes.JsonNode) : InnerStruct =
let arg_0 =
(match node.["a"] with
| null ->
raise (
System.Collections.Generic.KeyNotFoundException (
sprintf "Required key '%s' not found on JSON object" ("a")
)
)
| v -> v)
.AsValue()
.GetValue<System.Int32> ()
{
A = arg_0
}
namespace ConsumePlugin
/// Module containing JSON parsing methods for the ArrayOfInnerStruct type
[<RequireQualifiedAccess ; CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module ArrayOfInnerStruct =
/// Parse from a JSON node.
let jsonParse (node : System.Text.Json.Nodes.JsonNode) : ArrayOfInnerStruct =
let arg_0 =
(match node.["b"] with
| null ->
raise (
System.Collections.Generic.KeyNotFoundException (
sprintf "Required key '%s' not found on JSON object" ("b")
)
)
| v -> v)
.AsArray ()
|> Seq.cast<System.Text.Json.Nodes.JsonNode>
|> Seq.map (fun elt -> InnerStruct.jsonParse elt)
|> Array.ofSeq
{
B = arg_0
}

View File

@@ -60,8 +60,11 @@ module GymOpeningHours =
)
| v -> v)
.AsArray ()
|> Seq.cast<System.Text.Json.Nodes.JsonNode>
|> Seq.map (fun elt -> elt.AsValue().GetValue<System.String> ())
|> Seq.map (fun elt ->
(match elt with
| null -> raise (System.ArgumentNullException ())
| elt -> elt.AsValue().GetValue<System.String> ())
)
|> List.ofSeq
let arg_0 =
@@ -1039,8 +1042,11 @@ module Sessions =
)
| v -> v)
.AsArray ()
|> Seq.cast<System.Text.Json.Nodes.JsonNode>
|> Seq.map (fun elt -> Visit.jsonParse elt)
|> Seq.map (fun elt ->
(match elt with
| null -> raise (System.ArgumentNullException ())
| elt -> Visit.jsonParse elt)
)
|> List.ofSeq
let arg_0 =

View File

@@ -48,15 +48,13 @@ module PureGymApi =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return
jsonNode.AsArray ()
|> Seq.cast<System.Text.Json.Nodes.JsonNode>
|> Seq.map (fun elt -> Gym.jsonParse elt)
|> Seq.map (fun elt ->
(match elt with
| null -> raise (System.ArgumentNullException ())
| elt -> Gym.jsonParse elt)
)
|> List.ofSeq
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
@@ -91,11 +89,6 @@ module PureGymApi =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return GymAttendance.jsonParse jsonNode
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
@@ -130,11 +123,6 @@ module PureGymApi =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return GymAttendance.jsonParse jsonNode
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
@@ -165,11 +153,6 @@ module PureGymApi =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return Member.jsonParse jsonNode
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
@@ -203,11 +186,6 @@ module PureGymApi =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return Gym.jsonParse jsonNode
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
@@ -238,11 +216,6 @@ module PureGymApi =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return MemberActivityDto.jsonParse jsonNode
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
@@ -273,11 +246,6 @@ module PureGymApi =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return UriThing.jsonParse jsonNode
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
@@ -333,11 +301,6 @@ module PureGymApi =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return
match jsonNode with
| null -> None
@@ -390,11 +353,6 @@ module PureGymApi =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return Sessions.jsonParse jsonNode
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
@@ -436,11 +394,6 @@ module PureGymApi =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return Sessions.jsonParse jsonNode
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
@@ -932,11 +885,6 @@ module PureGymApi =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return
new RestEase.Response<_> (
responseString,
@@ -973,11 +921,6 @@ module PureGymApi =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return
new RestEase.Response<_> (
responseString,
@@ -1014,11 +957,6 @@ module PureGymApi =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return
new RestEase.Response<_> (
responseString,
@@ -1055,11 +993,6 @@ module PureGymApi =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return
new RestEase.Response<_> (
responseString,

View File

@@ -408,8 +408,11 @@ module InnerTypeWithBothJsonParseExtension =
let value =
(kvp.Value).AsArray ()
|> Seq.cast<System.Text.Json.Nodes.JsonNode>
|> Seq.map (fun elt -> elt.AsValue().GetValue<System.Char> ())
|> Seq.map (fun elt ->
(match elt with
| null -> raise (System.ArgumentNullException ())
| elt -> elt.AsValue().GetValue<System.Char> ())
)
|> List.ofSeq
key, value
@@ -677,8 +680,11 @@ module JsonRecordTypeWithBothJsonParseExtension =
)
| v -> v)
.AsArray ()
|> Seq.cast<System.Text.Json.Nodes.JsonNode>
|> Seq.map (fun elt -> elt.AsValue().GetValue<System.Int32> ())
|> Seq.map (fun elt ->
(match elt with
| null -> raise (System.ArgumentNullException ())
| elt -> elt.AsValue().GetValue<System.Int32> ())
)
|> Array.ofSeq
let arg_4 =
@@ -691,8 +697,11 @@ module JsonRecordTypeWithBothJsonParseExtension =
)
| v -> v)
.AsArray ()
|> Seq.cast<System.Text.Json.Nodes.JsonNode>
|> Seq.map (fun elt -> elt.AsValue().GetValue<System.String> ())
|> Seq.map (fun elt ->
(match elt with
| null -> raise (System.ArgumentNullException ())
| elt -> elt.AsValue().GetValue<System.String> ())
)
|> Array.ofSeq
let arg_3 =
@@ -717,8 +726,11 @@ module JsonRecordTypeWithBothJsonParseExtension =
)
| v -> v)
.AsArray ()
|> Seq.cast<System.Text.Json.Nodes.JsonNode>
|> Seq.map (fun elt -> elt.AsValue().GetValue<System.Int32> ())
|> Seq.map (fun elt ->
(match elt with
| null -> raise (System.ArgumentNullException ())
| elt -> elt.AsValue().GetValue<System.Int32> ())
)
|> List.ofSeq
let arg_1 =

View File

@@ -94,8 +94,11 @@ module JwtVaultAuthResponse =
)
| v -> v)
.AsArray ()
|> Seq.cast<System.Text.Json.Nodes.JsonNode>
|> Seq.map (fun elt -> elt.AsValue().GetValue<System.String> ())
|> Seq.map (fun elt ->
(match elt with
| null -> raise (System.ArgumentNullException ())
| elt -> elt.AsValue().GetValue<System.String> ())
)
|> List.ofSeq
let arg_3 =
@@ -108,8 +111,11 @@ module JwtVaultAuthResponse =
)
| v -> v)
.AsArray ()
|> Seq.cast<System.Text.Json.Nodes.JsonNode>
|> Seq.map (fun elt -> elt.AsValue().GetValue<System.String> ())
|> Seq.map (fun elt ->
(match elt with
| null -> raise (System.ArgumentNullException ())
| elt -> elt.AsValue().GetValue<System.String> ())
)
|> List.ofSeq
let arg_2 =
@@ -122,8 +128,11 @@ module JwtVaultAuthResponse =
)
| v -> v)
.AsArray ()
|> Seq.cast<System.Text.Json.Nodes.JsonNode>
|> Seq.map (fun elt -> elt.AsValue().GetValue<System.String> ())
|> Seq.map (fun elt ->
(match elt with
| null -> raise (System.ArgumentNullException ())
| elt -> elt.AsValue().GetValue<System.String> ())
)
|> List.ofSeq
let arg_1 =
@@ -499,11 +508,6 @@ module VaultClient =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return JwtSecretResponse.jsonParse jsonNode
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
@@ -540,11 +544,6 @@ module VaultClient =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return JwtVaultResponse.jsonParse jsonNode
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
@@ -603,11 +602,6 @@ module VaultClientNonExtensionMethod =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return JwtSecretResponse.jsonParse jsonNode
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
@@ -644,11 +638,6 @@ module VaultClientNonExtensionMethod =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return JwtVaultResponse.jsonParse jsonNode
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
@@ -710,11 +699,6 @@ module VaultClientExtensionMethodHttpClientExtension =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return JwtSecretResponse.jsonParse jsonNode
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
@@ -751,11 +735,6 @@ module VaultClientExtensionMethodHttpClientExtension =
System.Text.Json.Nodes.JsonNode.ParseAsync (responseStream, cancellationToken = ct)
|> Async.AwaitTask
let jsonNode =
match jsonNode with
| null -> raise (System.ArgumentNullException ())
| v -> v
return JwtVaultResponse.jsonParse jsonNode
}
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))

View File

@@ -1,13 +0,0 @@
namespace ConsumePlugin
[<WoofWare.Myriad.Plugins.JsonParse>]
type InnerStruct =
{
A : int
}
[<WoofWare.Myriad.Plugins.JsonParse>]
type ArrayOfInnerStruct =
{
B : InnerStruct array
}

View File

@@ -1,23 +0,0 @@
namespace WoofWare.Myriad.Plugins.Test
open System.Text.Json.Nodes
open FsUnitTyped
open NUnit.Framework
open ConsumePlugin
[<TestFixture>]
module TestJsonNullability =
[<Test>]
let ``Can consume JsonParseNullness`` () =
let options = JsonNodeOptions (PropertyNameCaseInsensitive = true)
"""{
"b": null
}"""
|> fun s -> JsonNode.Parse (s, options)
|> ArrayOfInnerStruct.jsonParse
|> shouldEqual
{
B = null
}

View File

@@ -18,7 +18,6 @@
<Compile Include="TestJsonParse\TestJsonParse.fs" />
<Compile Include="TestJsonParse\TestPureGymJson.fs" />
<Compile Include="TestJsonParse\TestExtensionMethod.fs" />
<Compile Include="TestJsonParse\TestJsonNullability.fs" />
<Compile Include="TestHttpClient\TestPureGymRestApi.fs" />
<Compile Include="TestHttpClient\TestPathParam.fs" />
<Compile Include="TestHttpClient\TestReturnTypes.fs" />

View File

@@ -356,19 +356,3 @@ module internal AstHelper =
}
)
| _ -> failwithf "Failed to get record elements for type that was: %+A" repr
let raiseIfNull (variable : Ident) : SynExpr =
SynExpr.createMatch
(SynExpr.createIdent' variable)
[
SynMatchClause.create
SynPat.createNull
(SynExpr.applyFunction
(SynExpr.createIdent "raise")
(SynExpr.paren (
SynExpr.applyFunction
(SynExpr.createLongIdent [ "System" ; "ArgumentNullException" ])
(SynExpr.CreateConst ())
)))
SynMatchClause.create (SynPat.named "v") (SynExpr.createIdent "v")
]

View File

@@ -1,5 +1,6 @@
namespace WoofWare.Myriad.Plugins
open System.IO
open System.Net.Http
open Fantomas.FCS.Syntax
open WoofWare.Whippet.Fantomas
@@ -13,6 +14,17 @@ type internal HttpClientGeneratorOutputSpec =
module internal HttpClientGenerator =
open Fantomas.FCS.Text.Range
let outputFile = FileInfo "/tmp/output.txt"
// do
// use _ = File.Create outputFile.FullName
// ()
let log (line : string) =
// use w = outputFile.AppendText ()
// w.WriteLine line
()
[<RequireQualifiedAccess>]
type PathSpec =
| Verbatim of string
@@ -556,9 +568,6 @@ module internal HttpClientGenerator =
)
)
let jsonNodeNotNull =
Let ("jsonNode", AstHelper.raiseIfNull (Ident.create "jsonNode"))
let setVariableHeaders =
variableHeaders
|> List.map (fun (headerName, callToGetValue) ->
@@ -633,7 +642,6 @@ module internal HttpClientGenerator =
yield responseString
yield responseStream
yield jsonNode
yield jsonNodeNotNull
| String -> yield responseString
| Stream -> yield responseStream
| UnitType ->
@@ -642,7 +650,6 @@ module internal HttpClientGenerator =
| _ ->
yield responseStream
yield jsonNode
yield jsonNodeNotNull
]
|> SynExpr.createCompExpr "async" returnExpr
|> SynExpr.startAsTask cancellationTokenArg
@@ -907,6 +914,10 @@ module internal HttpClientGenerator =
"Create a REST client. The input functions will be re-evaluated on every HTTP request to obtain the required values for the corresponding header properties."
|> PreXmlDoc.create
let functionName = Ident.create "client"
let pattern = SynLongIdent.createS "make"
let returnInfo = SynType.createLongIdent interfaceType.Name
let nameWithoutLeadingI =

View File

@@ -26,7 +26,7 @@ module internal JsonParseGenerator =
}
/// (match {indexed} with | null -> raise (System.Collections.Generic.KeyNotFoundException ({propertyName} not found)) | v -> v)
let assertNotNull (propertyName : SynExpr) (indexed : SynExpr) =
let assertPropertyExists (propertyName : SynExpr) (indexed : SynExpr) =
let raiseExpr =
SynExpr.applyFunction
(SynExpr.createIdent "sprintf")
@@ -46,28 +46,42 @@ module internal JsonParseGenerator =
|> SynExpr.createMatch indexed
|> SynExpr.paren
let assertNotNull (body : SynExpr) (boundIdent : Ident) : SynExpr =
let raiseExpr =
SynExpr.CreateConst ()
|> SynExpr.applyFunction (SynExpr.createLongIdent [ "System" ; "ArgumentNullException" ])
|> SynExpr.paren
|> SynExpr.applyFunction (SynExpr.createIdent "raise")
[
SynMatchClause.create SynPat.createNull raiseExpr
SynMatchClause.create (SynPat.namedI boundIdent) body
]
|> SynExpr.createMatch (SynExpr.createIdent' boundIdent)
|> SynExpr.paren
/// {node}.AsValue().GetValue<{typeName}> ()
/// If `propertyName` is Some, uses `assertNotNull {node}` instead of `{node}`.
/// If `propertyName` is Some, uses `assertPropertyExists {node}` instead of `{node}`.
let asValueGetValue (propertyName : SynExpr option) (typeName : string) (node : SynExpr) : SynExpr =
match propertyName with
| None -> node
| Some propertyName -> assertNotNull propertyName node
| Some propertyName -> assertPropertyExists propertyName node
|> SynExpr.callMethod "AsValue"
|> SynExpr.callGenericMethod' "GetValue" typeName
let asValueGetValueIdent (propertyName : SynExpr option) (typeName : LongIdent) (node : SynExpr) : SynExpr =
match propertyName with
| None -> node
| Some propertyName -> assertNotNull propertyName node
| Some propertyName -> assertPropertyExists propertyName node
|> SynExpr.callMethod "AsValue"
|> SynExpr.callGenericMethod (SynLongIdent.createS "GetValue") [ SynType.createLongIdent typeName ]
/// {node}.AsObject()
/// If `propertyName` is Some, uses `assertNotNull {node}` instead of `{node}`.
/// If `propertyName` is Some, uses `assertPropertyExists {node}` instead of `{node}`.
let asObject (propertyName : SynExpr option) (node : SynExpr) : SynExpr =
match propertyName with
| None -> node
| Some propertyName -> assertNotNull propertyName node
| Some propertyName -> assertPropertyExists propertyName node
|> SynExpr.callMethod "AsObject"
/// {type}.jsonParse {node}
@@ -77,9 +91,8 @@ module internal JsonParseGenerator =
/// collectionType is e.g. "List"; we'll be calling `ofSeq` on it.
/// body is the body of a lambda which takes a parameter `elt`.
/// {assertNotNull node}.AsArray()
/// |> Seq.cast<JsonNode>
/// |> Seq.map (fun elt -> {body})
/// {assertPropertyExists node}.AsArray()
/// |> Seq.map (fun elt -> {assertNotNull} {body})
/// |> {collectionType}.ofSeq
let asArrayMapped
(propertyName : SynExpr option)
@@ -90,17 +103,12 @@ module internal JsonParseGenerator =
=
match propertyName with
| None -> node
| Some propertyName -> assertNotNull propertyName node
| Some propertyName -> assertPropertyExists propertyName node
|> SynExpr.callMethod "AsArray"
|> SynExpr.pipeThroughFunction (
SynExpr.createLongIdent [ "Seq" ; "cast" ]
|> SynExpr.typeApp
[
SynType.createLongIdent' [ "System" ; "Text" ; "Json" ; "Nodes" ; "JsonNode" ]
]
)
|> SynExpr.pipeThroughFunction (
SynExpr.applyFunction (SynExpr.createLongIdent [ "Seq" ; "map" ]) (SynExpr.createLambda "elt" body)
SynExpr.applyFunction
(SynExpr.createLongIdent [ "Seq" ; "map" ])
(SynExpr.createLambda "elt" (assertNotNull body (Ident.create "elt")))
)
|> SynExpr.pipeThroughFunction (SynExpr.createLongIdent [ collectionType ; "ofSeq" ])
@@ -185,27 +193,29 @@ module internal JsonParseGenerator =
=
// TODO: parsing format for DateTime etc
match fieldType with
// Struct types
| DateOnly ->
node
|> asValueGetValue propertyName "string"
|> SynExpr.pipeThroughFunction (SynExpr.createLongIdent [ "System" ; "DateOnly" ; "Parse" ])
| Uri ->
node
|> asValueGetValue propertyName "string"
|> SynExpr.pipeThroughFunction (SynExpr.createLongIdent [ "System" ; "Uri" ])
| Guid ->
node
|> asValueGetValue propertyName "string"
|> SynExpr.pipeThroughFunction (SynExpr.createLongIdent [ "System" ; "Guid" ; "Parse" ])
| DateTime ->
node
|> asValueGetValue propertyName "string"
|> SynExpr.pipeThroughFunction (SynExpr.createLongIdent [ "System" ; "DateTime" ; "Parse" ])
| NumberType typeName -> parseNumberType options propertyName node typeName
| Guid ->
node
|> asValueGetValue propertyName "string"
|> SynExpr.pipeThroughFunction (SynExpr.createLongIdent [ "System" ; "Guid" ; "Parse" ])
// Reference types
| Uri ->
node
|> asValueGetValue propertyName "string"
|> SynExpr.pipeThroughFunction (SynExpr.createLongIdent [ "System" ; "Uri" ])
| DateTimeOffset ->
node
|> asValueGetValue propertyName "string"
|> SynExpr.pipeThroughFunction (SynExpr.createLongIdent [ "System" ; "DateTimeOffset" ; "Parse" ])
| NumberType typeName -> parseNumberType options propertyName node typeName
| PrimitiveType typeName -> asValueGetValueIdent propertyName typeName node
| OptionType ty ->
let someClause =
@@ -281,12 +291,10 @@ module internal JsonParseGenerator =
)
|> SynExpr.pipeThroughFunction (SynExpr.createLongIdent [ "Map" ; "ofSeq" ])
| BigInt ->
AstHelper.raiseIfNull (Ident.create "v")
|> SynExpr.paren
node
|> SynExpr.callMethod "ToJsonString"
|> SynExpr.paren
|> SynExpr.applyFunction (SynExpr.createLongIdent [ "System" ; "Numerics" ; "BigInteger" ; "Parse" ])
|> SynExpr.createLet [ SynBinding.basic [ Ident.create "v" ] [] node ]
| Measure (_measure, primType) ->
parseNumberType options propertyName node primType
|> SynExpr.pipeThroughFunction (Measure.getLanguagePrimitivesMeasure primType)
@@ -301,7 +309,7 @@ module internal JsonParseGenerator =
match propertyName with
| None -> node
| Some propertyName -> assertNotNull propertyName node
| Some propertyName -> assertPropertyExists propertyName node
|> typeJsonParse typeName
/// propertyName is probably a string literal, but it could be a [<Literal>] variable
@@ -515,7 +523,7 @@ module internal JsonParseGenerator =
|> SynExpr.createLet
[
SynExpr.index (SynExpr.CreateConst "data") (SynExpr.createIdent "node")
|> assertNotNull (SynExpr.CreateConst "data")
|> assertPropertyExists (SynExpr.CreateConst "data")
|> SynBinding.basic [ Ident.create "node" ] []
]
@@ -563,7 +571,7 @@ module internal JsonParseGenerator =
SynExpr.createIdent "node"
|> SynExpr.index property
|> assertNotNull property
|> assertPropertyExists property
|> SynExpr.pipeThroughFunction (
SynExpr.createLambda "v" (SynExpr.callGenericMethod' "GetValue" "string" (SynExpr.createIdent "v"))
)

View File

@@ -1,5 +1,5 @@
{
"version": "4.0",
"version": "5.0",
"publicReleaseRefSpec": [
"^refs/heads/main$"
],
@@ -11,4 +11,4 @@
":/README.md",
":/Directory.Build.props"
]
}
}