forked from fsprojects/FSharp.Data.GraphQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.fs
More file actions
73 lines (62 loc) · 2.63 KB
/
Copy pathExtensions.fs
File metadata and controls
73 lines (62 loc) · 2.63 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// The MIT License (MIT)
// Copyright (c) 2016 Bazinga Technologies Inc
module internal FSharp.Data.GraphQL.Extensions
open System.Reflection
open System.Collections.Generic
type IDictionary<'TKey, 'TValue> with
member x.TryFind(key : 'TKey) =
match x.TryGetValue(key) with
| (true, value) -> Some value
| _ -> None
type TypeInfo with
/// If no property is found with the specified name, it will try changing the case of the first letter
member x.GetDeclaredProperty(propertyName: string, ignoreCase: bool) =
match x.GetDeclaredProperty(propertyName), ignoreCase with
| null, true ->
let first =
let first = propertyName.Substring(0,1)
match first.ToUpper() with
| upper when upper <> first -> upper
| _ -> first.ToLower()
x.GetDeclaredProperty(first + propertyName.Substring(1))
| prop, _ -> prop
/// If no method is found with the specified name, it will try changing the case of the first letter
member x.GetDeclaredMethod(propertyName: string, ignoreCase: bool) =
match x.GetDeclaredMethod(propertyName), ignoreCase with
| null, true ->
let first =
let first = propertyName.Substring(0,1)
match first.ToUpper() with
| upper when upper <> first -> upper
| _ -> first.ToLower()
x.GetDeclaredMethod(first + propertyName.Substring(1))
| prop, _ -> prop
module Option =
let mergeWith (f: 'T -> 'T -> 'T) (o1 : 'T option) (o2 : 'T option) : 'T option =
match (o1, o2) with
| Some a, Some b -> Some (f a b)
| Some a, _ -> Some a
| _, Some b -> Some b
| _, _ -> None
let unwrap (defaultValue : 'U) (onSome : 'T -> 'U) (o : 'T option) : 'U =
match o with
| Some t -> onSome t
| None -> defaultValue
module Result =
let catchError (handle : 'Err -> 'T) (r : Result<'T, 'Err>) : 'T =
match r with
| Ok x -> x
| Error err -> handle err
let fromOption (errValue : 'Err) (o : 'T option) : Result<'T, 'Err> =
match o with
| Some x -> Ok x
| None -> Error errValue
module Dictionary =
let adjust (f : 'V -> 'V) (key : 'K) (dict : Dictionary<'K, 'V>) : unit =
match dict.TryGetValue(key) with
| true, v -> dict.[key] <- f v
| false, _ -> ()
let addWith (f : 'V -> 'V -> 'V) (key : 'K) (value : 'V) (dict : Dictionary<'K, 'V>) : unit =
match dict.TryGetValue(key) with
| true, v -> dict.[key] <- f value v
| false, _ -> dict.Add(key, value)