-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.fs
More file actions
54 lines (41 loc) · 1.87 KB
/
Program.fs
File metadata and controls
54 lines (41 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
open System
open System.IO
open System.Threading
open FsFlow
type ReadmeEnv =
{ Root: string }
type FileReadError =
| NotFound of path: string
let readTextFile (path: string) : TaskFlow<ReadmeEnv, FileReadError, string> =
taskFlow {
// In production, map access and path exceptions separately at the boundary.
do! Check.okIf (File.Exists path)
|> Check.orError (NotFound path)
return! ColdTask(fun ct -> File.ReadAllTextAsync(path, ct)) // ColdTask<string>
}
let program : TaskFlow<ReadmeEnv, FileReadError, string * string> =
taskFlow {
let! root = TaskFlow.read _.Root // ReadmeEnv.Root -> string
let settingsFile = Path.Combine(root, "settings.json")
let featureFlagsFile = Path.Combine(root, "feature-flags.json")
// The cancellation token is passed implicitly through both file reads.
let! settings = readTextFile settingsFile // TaskFlow<ReadmeEnv, FileReadError, string>
let! featureFlags = readTextFile featureFlagsFile // TaskFlow<ReadmeEnv, FileReadError, string>
return settings, featureFlags // TaskFlow<ReadmeEnv, FileReadError, string * string>
}
[<EntryPoint>]
let main _ =
let root =
Path.Combine(Path.GetTempPath(), "FsFlow.ReadmeExample", Guid.NewGuid().ToString "N")
Directory.CreateDirectory root |> ignore
let settingsPath = Path.Combine(root, "settings.json")
let featureFlagsPath = Path.Combine(root, "feature-flags.json")
File.WriteAllText(settingsPath, """{"name":"Ada"}""")
File.WriteAllText(featureFlagsPath, """{"darkMode":true}""")
let readPairResult =
program
|> TaskFlow.run { Root = root } CancellationToken.None
|> fun task -> task.GetAwaiter().GetResult()
printfn "Config pair result: %A" readPairResult
// Config pair result: Ok ("{\"name\":\"Ada\"}", "{\"darkMode\":true}")
0