Skip to content
Open
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
106 changes: 106 additions & 0 deletions packages/Thoth.Json.Core/Syntax.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
namespace Thoth.Json.Core

[<AutoOpen>]
module Syntax =

type DecoderBuilder internal () =
member inline this.Bind(m, f) = Decode.andThen f m

member this.BindReturn(m, f) = Decode.map f m

member this.Bind2Return(d1, d2, ctor: struct ('a * 'b) -> 't) =
Decode.map2 (fun a b -> ctor struct (a, b)) d1 d2

member this.Bind3Return(d1, d2, d3, ctor: struct ('a * 'b * 'c) -> 't) =
Decode.map3 (fun a b c -> ctor struct (a, b, c)) d1 d2 d3

member this.Bind4Return
(d1, d2, d3, d4, ctor: struct ('a * 'b * 'c * 'd) -> 't)
=
Decode.map4 (fun a b c d -> ctor struct (a, b, c, d)) d1 d2 d3 d4

member this.Bind5Return
(d1, d2, d3, d4, d5, ctor: struct ('a * 'b * 'c * 'd * 'e) -> 't)
=
Decode.map5
(fun a b c d e -> ctor struct (a, b, c, d, e))
d1
d2
d3
d4
d5

member this.Bind6Return
(
d1,
d2,
d3,
d4,
d5,
d6,
ctor: struct ('a * 'b * 'c * 'd * 'e * 'f) -> 't
)
=
Decode.map6
(fun a b c d e f -> ctor struct (a, b, c, d, e, f))
d1
d2
d3
d4
d5
d6

member this.Bind7Return
(
d1,
d2,
d3,
d4,
d5,
d6,
d7,
ctor: struct ('a * 'b * 'c * 'd * 'e * 'f * 'g) -> 't
)
=
Decode.map7
(fun a b c d e f g -> ctor struct (a, b, c, d, e, f, g))
d1
d2
d3
d4
d5
d6
d7

member this.Bind8Return
(
d1,
d2,
d3,
d4,
d5,
d6,
d7,
d8,
ctor: struct ('a * 'b * 'c * 'd * 'e * 'f * 'g * 'h) -> 't
)
=
Decode.map8
(fun a b c d e f g h -> ctor struct (a, b, c, d, e, f, g, h))
d1
d2
d3
d4
d5
d6
d7
d8

member this.MergeSources(d1, d2) =
Decode.map2 (fun a b -> struct (a, b)) d1 d2

member this.Return(x) = Decode.succeed x

member this.ReturnFrom(x: Decoder<'a>) = x

let decoder = DecoderBuilder()
1 change: 1 addition & 0 deletions packages/Thoth.Json.Core/Thoth.Json.Core.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Pick the right additional package for your runtime:
<Compile Include="Decode.fs" />
<Compile Include="Encode.fs" />
<!-- <Compile Include="Extra.fs" /> -->
<Compile Include="Syntax.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="EasyBuild.PackageReleaseNotes.Tasks">
Expand Down
1 change: 1 addition & 0 deletions tests/Thoth.Json.Tests.JavaScript/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ let main args =
Decoders.tests runner
Encoders.tests runner
BackAndForth.tests runner
Syntax.tests runner

]
|> Pyxpecto.runTests [||]
2 changes: 1 addition & 1 deletion tests/Thoth.Json.Tests.Newtonsoft/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ let main args =
Decoders.tests runner
Encoders.tests runner
BackAndForth.tests runner

Syntax.tests runner
]
|> Pyxpecto.runTests [||]
1 change: 1 addition & 0 deletions tests/Thoth.Json.Tests.Python/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ let main args =
Decoders.tests runner
Encoders.tests runner
BackAndForth.tests runner
Syntax.tests runner
]
|> Pyxpecto.runTests [||]
2 changes: 2 additions & 0 deletions tests/Thoth.Json.Tests.System.Text.Json/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,7 @@ let main args =
[
Decoders.tests runner
Encoders.tests runner
BackAndForth.tests runner
Syntax.tests runner
]
|> Pyxpecto.runTests [||]
36 changes: 36 additions & 0 deletions tests/Thoth.Json.Tests/Syntax.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Thoth.Json.Tests.Syntax

open Thoth.Json.Tests.Testing
open Thoth.Json.Core
open Fable.Pyxpecto

let tests (runner: TestRunner<_, _>) =
testList
"Thoth.Json - Syntax"
[

testCase "decoder syntax works for bind"
<| fun _ ->
let expected = 123, "abc"

let json =
Encode.object
[
"x", Encode.int 123
"y", Encode.string "abc"
]
|> runner.Encode.toString 0

let decoded =
runner.Decode.fromString
(decoder {
let! x = Decode.field "x" Decode.int
let! y = Decode.field "y" Decode.string

return x, y
})
json

equal (Ok expected) decoded

]
1 change: 1 addition & 0 deletions tests/Thoth.Json.Tests/Thoth.Json.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<Compile Include="Decoders.fs" />
<Compile Include="Encoders.fs" />
<Compile Include="BackAndForth.fs" />
<Compile Include="Syntax.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\packages\Thoth.Json.Core\Thoth.Json.Core.fsproj" />
Expand Down
Loading