forked from microsoft/fsharplu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parsing.fs
51 lines (41 loc) · 1.69 KB
/
Parsing.fs
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
/// Parsing text to F# data types
module Microsoft.FSharpLu.Parsing
open Microsoft.FSharpLu.Option
/// Try to parse an int32
let public tryParseInt =
System.Int32.TryParse >> ofPair
/// A parser for string parameter
let public tryParseString = Some
/// Try to parse a boolean
let public tryParseBoolean = function
| "0" -> Some false
| "1" -> Some true
| b -> System.Boolean.TryParse b |> ofPair
/// Parse a boolean
let public parseBoolean =
tryParseBoolean >> orDo (fun() -> invalidOp "Invalid boolean format")
/// Parse a C#-like enumeration (i.e. of the form type MyEnum = One = 1 | Two = 2)
let public tryParseEnum<'T when 'T : struct
and 'T : (new : unit -> 'T)
and 'T :> System.ValueType> e =
System.Enum.TryParse<'T>(e, true) |> ofPair
/// Lookup value from a dictionary and try to parse it with the provided parser
let public tryParseDictValue dict key parser =
Collections.Dictionary.tryGetValue dict key
|> Option.bind parser
/// Try to parse a Guid
let public tryParseGuid value =
System.Guid.TryParse value |> ofPair
module Union =
open FSharp.Reflection
/// Parse a field-less discriminated union of type 'T from string
/// This only works with simple *field-less* discriminated union of
/// the form "type Bla = Foo | Bar"
let tryParse<'T> (string:string) =
let fields =
typeof<'T>
|> FSharpType.GetUnionCases
|> Array.filter (fun case -> case.Name.Equals(string, System.StringComparison.InvariantCultureIgnoreCase))
match fields with
| [| case |] -> Some(FSharpValue.MakeUnion(case,[||]) :?> 'T)
| _ -> None