-
Notifications
You must be signed in to change notification settings - Fork 0
/
Primitives.fs
181 lines (158 loc) 路 5.7 KB
/
Primitives.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
namespace Songhay.Modules.Models
open System
open System.Text.Json
open Songhay.Modules.JsonDocumentUtility
open Songhay.Modules.StringUtility
/// <summary>
/// Defines the conventional identifier or sort ordinal.
/// </summary>
type Identifier =
| Alphanumeric of string
| Numeric of int
/// <summary>
/// Generates an instance of <see cref="Identifier" />
/// from conventional JSON input.
/// </summary>
static member fromInputElementName elementName (element: JsonElement) =
element
|> tryGetProperty elementName
|> Result.bind
(
fun el ->
match el.ValueKind with
| JsonValueKind.String -> el.GetString() |> fun idString -> Identifier.fromString(idString) |> Ok
| JsonValueKind.Number -> el.GetInt32() |> fun id -> Identifier.fromInt32(id) |> Ok
| _ -> JsonException("Only alphanumeric or integer identifiers are supported.") |> Error
)
/// <summary>
/// Generates an instance of <see cref="Identifier.Numeric" />
/// from conventional JSON input.
/// </summary>
static member fromInt32 i = Numeric i
/// <summary>
/// Generates an instance of <see cref="Identifier.Alphanumeric" />
/// from conventional JSON input.
/// </summary>
static member fromString s = Alphanumeric s
/// <summary>
/// Reduces this instance
/// to <c>Some</c> <see cref="int" /> value.
/// </summary>
member this.Int32Value =
match this with
| Alphanumeric a ->
match Int32.TryParse(a) with
| true, n -> Some n
| false, _ -> None
| Numeric n -> Some n
/// <summary>
/// Reduces this instance
/// to <c>Some</c> <see cref="string" /> value.
/// </summary>
member this.StringValue =
match this with
| Alphanumeric a -> a
| Numeric n -> $"{n}"
/// <summary>
/// Defines the conventional identifier exposed to a Studio Client.
/// </summary>
type ClientId =
| ClientId of Identifier
/// <summary>
/// Generates an instance of this type
/// from conventional JSON input.
/// </summary>
static member fromInput (useCamelCase: bool) (element: JsonElement) =
let elementName = ((useCamelCase, (nameof ClientId)) ||> toCamelCaseOrDefault)
match elementName with
| None -> Error(JsonException("The expected element-name input is not here."))
| Some name ->
element
|> tryGetProperty name
|> toResultFromStringElement (fun el -> ClientId.fromString(el.GetString()))
/// <summary>
/// Generates an instance of this type
/// from a <see cref="string" />.
/// </summary>
static member fromIdentifier i = ClientId i
/// <summary>
/// Generates an instance of this type
/// from a <see cref="string" />.
/// </summary>
static member fromString s = ClientId (Identifier.fromString(s))
/// <summary>
/// Converts this instance
/// to <see cref="Identifier" />.
/// </summary>
member this.toIdentifier = let (ClientId v) = this in v
/// <summary>
/// Defines the display text of an item.
/// </summary>
type DisplayText =
| DisplayText of string
/// <summary>
/// Reduces this instance
/// to <c>Some</c> <see cref="string" /> value.
/// </summary>
member this.Value = let (DisplayText v) = this in v
/// <summary>
/// Defines the Temporal end date of an item.
/// </summary>
type EndDate =
| EndDate of DateTime
/// <summary>
/// Generates an instance of this type
/// from conventional JSON input.
/// </summary>
static member fromInput (useCamelCase: bool) (element: JsonElement) =
let elementName = ((useCamelCase, (nameof EndDate))||> toCamelCaseOrDefault)
match elementName with
| None -> Error(JsonException("The expected element-name input is not here."))
| Some name ->
element
|> tryGetProperty name
|> toResultFromStringElement(fun el -> (EndDate (el.GetDateTime())))
/// <summary>
/// Defines the Temporal incept date of an item.
/// </summary>
type InceptDate =
| InceptDate of DateTime
/// <summary>
/// Generates an instance of this type
/// from conventional JSON input.
/// </summary>
static member fromInput (useCamelCase: bool) (element: JsonElement) =
let elementName = ((useCamelCase, (nameof InceptDate)) ||> toCamelCaseOrDefault)
match elementName with
| None -> Error(JsonException("The expected element-name input is not here."))
| Some name ->
element
|> tryGetProperty name
|> toResultFromStringElement(fun el -> (InceptDate (el.GetDateTime())))
/// <summary>
/// Defines the name of an item.
/// </summary>
type ItemName =
| ItemName of string
/// <summary>
/// Reduces this instance
/// to <c>Some</c> <see cref="string" /> value.
/// </summary>
member this.Value = let (ItemName v) = this in v
/// <summary>
/// Defines the Temporal modification date of an item.
/// </summary>
type ModificationDate =
| ModificationDate of DateTime
/// <summary>
/// Generates an instance of this type
/// from conventional JSON input.
/// </summary>
static member fromInput (useCamelCase: bool) (element: JsonElement) =
let elementName = ((useCamelCase, (nameof ModificationDate)) ||> toCamelCaseOrDefault)
match elementName with
| None -> Error(JsonException("The expected element-name input is not here."))
| Some name ->
element
|> tryGetProperty name
|> toResultFromStringElement(fun el -> ModificationDate (el.GetDateTime()))