Skip to content

Commit

Permalink
Cope with unit type in JSON (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
Smaug123 authored Sep 15, 2024
1 parent e22525c commit 9a3ebbf
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 1 deletion.
5 changes: 5 additions & 0 deletions ConsumePlugin/GeneratedSerde.fs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ module JsonRecordTypeWithBothJsonSerializeExtension =
|> (fun field -> field.ToString "o" |> System.Text.Json.Nodes.JsonValue.Create<string>))
)

node.Add ("unit", (input.Unit |> (fun value -> System.Text.Json.Nodes.JsonObject ())))

node :> _
namespace ConsumePlugin

Expand Down Expand Up @@ -478,6 +480,8 @@ module JsonRecordTypeWithBothJsonParseExtension =

/// Parse from a JSON node.
static member jsonParse (node : System.Text.Json.Nodes.JsonNode) : JsonRecordTypeWithBoth =
let arg_21 = ()

let arg_20 =
(match node.["timestamp"] with
| null ->
Expand Down Expand Up @@ -759,6 +763,7 @@ module JsonRecordTypeWithBothJsonParseExtension =
IntMeasureNullable = arg_18
Enum = arg_19
Timestamp = arg_20
Unit = arg_21
}
namespace ConsumePlugin

Expand Down
1 change: 1 addition & 0 deletions ConsumePlugin/SerializationAndDeserialization.fs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type JsonRecordTypeWithBoth =
IntMeasureNullable : int<measure> Nullable
Enum : SomeEnum
Timestamp : DateTimeOffset
Unit : unit
}

[<WoofWare.Myriad.Plugins.JsonSerialize true>]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ module TestJsonSerde =
IntMeasureNullable = intMeasureNullable
Enum = enum<SomeEnum> someEnum
Timestamp = timestamp
Unit = ()
}
}

Expand Down Expand Up @@ -168,6 +169,7 @@ module TestJsonSerde =
IntMeasureNullable = Nullable -883<measure>
Enum = enum<SomeEnum> 1
Timestamp = DateTimeOffset (2024, 07, 01, 17, 54, 00, TimeSpan.FromHours 1.0)
Unit = ()
}

let expected =
Expand Down Expand Up @@ -198,7 +200,8 @@ module TestJsonSerde =
"intMeasureOption": 981,
"intMeasureNullable": -883,
"enum": 1,
"timestamp": "2024-07-01T17:54:00.0000000\u002B01:00"
"timestamp": "2024-07-01T17:54:00.0000000\u002B01:00",
"unit": {}
}
"""
|> fun s -> s.ToCharArray ()
Expand Down
1 change: 1 addition & 0 deletions WoofWare.Myriad.Plugins/JsonParseGenerator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ module internal JsonParseGenerator =
parseNumberType options propertyName node primType
|> SynExpr.pipeThroughFunction (Measure.getLanguagePrimitivesMeasure primType)
| JsonNode -> node
| Unit -> SynExpr.CreateConst ()
| _ ->
// Let's just hope that we've also got our own type annotation!
let typeName =
Expand Down
6 changes: 6 additions & 0 deletions WoofWare.Myriad.Plugins/JsonSerializeGenerator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ module internal JsonSerializeGenerator =
|> SynExpr.createLambda "field"
|> fun e -> e, false
| JsonNode -> SynExpr.createIdent "id", true
| Unit ->
SynExpr.createLambda
"value"
(SynExpr.createLongIdent [ "System" ; "Text" ; "Json" ; "Nodes" ; "JsonObject" ]
|> SynExpr.applyTo (SynExpr.CreateConst ())),
false
| _ ->
// {type}.toJsonNode
let typeName =
Expand Down
1 change: 1 addition & 0 deletions WoofWare.Myriad.Plugins/SynExpr/Ident.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module internal Ident =
let createSanitisedParamName (s : string) =
match s with
| "type" -> create "type'"
| "private" -> create "private'"
| _ ->

let result = StringBuilder ()
Expand Down
11 changes: 11 additions & 0 deletions WoofWare.Myriad.Plugins/SynExpr/SynType.fs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,17 @@ module internal SynTypePatterns =
| _ -> None
| _ -> None

let (|Unit|_|) (fieldType : SynType) : unit option =
match fieldType with
| SynType.LongIdent (SynLongIdent.SynLongIdent (ident, _, _)) ->
match ident |> List.map (fun i -> i.idText.ToLowerInvariant ()) with
| [ "microsoft" ; "fsharp" ; "core" ; "unit" ]
| [ "fsharp" ; "core" ; "unit" ]
| [ "core" ; "unit" ]
| [ "unit" ] -> Some ()
| _ -> None
| _ -> None

let (|DateOnly|_|) (fieldType : SynType) =
match fieldType with
| SynType.LongIdent (SynLongIdent.SynLongIdent (ident, _, _)) ->
Expand Down

0 comments on commit 9a3ebbf

Please sign in to comment.