Skip to content

Commit

Permalink
Simplify code, fix loading non-existing file
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoVIII committed Sep 12, 2020
1 parent 2ab8870 commit 6199b59
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 15 deletions.
17 changes: 17 additions & 0 deletions src/TypedPersistence/Helper.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace TypedPersistence

open FSharp.Json

module Helper =
type OptionBuilder() =
member x.Bind(v, f) = Option.bind f v
member x.Return v = Some v
member x.ReturnFrom o = o
member x.Zero() = None

let opt = OptionBuilder()

let deserializeJson<'a> content =
try
Json.deserialize<'a> content |> Some
with :? JsonDeserializationError -> None
27 changes: 15 additions & 12 deletions src/TypedPersistence/Loading.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ namespace TypedPersistence
open FSharp.Json
open System.IO

open TypedPersistence.Helper

[<AutoOpen>]
module Loading =
let private getFileContent filepath =
if File.Exists filepath then File.ReadAllText filepath |> Some else None

let getVersion (filepath: string) =
let content = File.ReadAllText filepath
try
Json.deserialize<OnlyVersion> content
|> (fun v -> v.version)
|> Some
with :? JsonDeserializationError -> None
opt {
let! content = getFileContent filepath
let! parsed = deserializeJson<OnlyVersion> content
return parsed.version
}

let load<'a> (filepath: string) =
let content = File.ReadAllText filepath
try
Json.deserialize<VersionAndData<'a>> content
|> (fun d -> d.data)
|> Some
with :? JsonDeserializationError -> None
opt {
let! content = getFileContent filepath
let! parsed = deserializeJson<VersionAndData<'a>> content
return parsed.data
}
4 changes: 2 additions & 2 deletions src/TypedPersistence/Saving.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ open System.IO

[<AutoOpen>]
module Saving =
let saveVersion (filePath: string) (version: uint) (data: 'a) =
let saveVersion<'a> (filePath: string) (version: uint) (data: 'a) =
// Alias for writing text with single arguments and not with tuple
let writeAllText filePath content =
// Create path, if not existing
Expand All @@ -22,4 +22,4 @@ module Saving =
|> (fun text -> text + Environment.NewLine) // Somehow this final new line is missing
|> writeAllText filePath

let save (filePath: string) (data: 'a) = saveVersion filePath 1u data
let save<'a> (filePath: string) (data: 'a) = saveVersion filePath 1u data
1 change: 1 addition & 0 deletions src/TypedPersistence/TypedPersistence.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="Types.fs" />
<Compile Include="Helper.fs" />
<Compile Include="Saving.fs" />
<Compile Include="Loading.fs" />
</ItemGroup>
Expand Down
8 changes: 7 additions & 1 deletion tests/TypedPersistence.UnitTests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ let tests =
testList
"Test List"
[ testList
"Save and load"
"Misc"
[ test "Load nonexisting file" {
let value = load<string> "lalelu.txt"
Expect.equal value None "Load on non-existing file should return None"
} ]
testList
"Save and load"
[ basicSaveLoadTest<NonNull<string>> ()
basicSaveLoadTest<int> ()
basicSaveLoadTest<NonNull<string> list> ()
Expand Down

0 comments on commit 6199b59

Please sign in to comment.