Skip to content

Commit 04d4b5e

Browse files
committed
Update Giraffe router to support named parameters
Notice that the names are not used, so they're discarded
1 parent efdf64f commit 04d4b5e

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

src/Giraffe/FormatExpressions.fs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ let private formatStringMap =
4343
'd', ("(-?\d+)", int64 >> box) // int64
4444
'f', ("(-?\d+\.{1}\d+)", float >> box) // float
4545
'O', (guidPattern, parseGuid >> box) // Guid
46-
'u', (shortIdPattern, ShortId.toUInt64 >> box)
47-
] // uint64
46+
'u', (shortIdPattern, ShortId.toUInt64 >> box) // uint64
47+
]
4848

4949
type MatchMode =
5050
| Exact // Will try to match entire string from start to end.
@@ -72,6 +72,13 @@ let private convertToRegexPatternAndFormatChars (mode: MatchMode) (formatString:
7272
| '%' :: '%' :: tail ->
7373
let pattern, formatChars = convert tail
7474
"%" + pattern, formatChars
75+
| '%' :: c :: ':' :: tail ->
76+
// Skip until next '/' or end of string
77+
let tailWithoutParamName = tail |> List.skipWhile (fun ch -> ch <> '/')
78+
79+
let pattern, formatChars = convert tailWithoutParamName
80+
let regex, _ = formatStringMap.[c]
81+
regex + pattern, c :: formatChars
7582
| '%' :: c :: tail ->
7683
let pattern, formatChars = convert tail
7784
let regex, _ = formatStringMap.[c]
@@ -187,8 +194,8 @@ let validateFormat (format: PrintfFormat<_, _, _, _, 'T>) =
187194
'd', typeof<int64> // int64
188195
'f', typeof<float> // float
189196
'O', typeof<System.Guid> // guid
190-
'u', typeof<uint64>
191-
] // guid
197+
'u', typeof<uint64> // uint64
198+
]
192199

193200
let tuplePrint pos last name =
194201
let mutable result = "("
@@ -216,6 +223,7 @@ let validateFormat (format: PrintfFormat<_, _, _, _, 'T>) =
216223
let mutable parseChars = []
217224
let mutable matchNext = false
218225
let mutable matches = 0
226+
let mutable isNamedParameter = false
219227

220228
let rec charTypeMatch ls mChar =
221229
match ls with
@@ -239,7 +247,15 @@ let validateFormat (format: PrintfFormat<_, _, _, _, 'T>) =
239247
for i in 0 .. path.Length - 1 do
240248
let mChar = path.[i]
241249

242-
if matchNext then
250+
if mChar = ':' then
251+
isNamedParameter <- true
252+
else if isNamedParameter then
253+
// Skip until next '/' or end of string
254+
if mChar = '/' then
255+
isNamedParameter <- false
256+
257+
()
258+
else if matchNext then
243259
charTypeMatch mapping mChar
244260
matchNext <- false
245261
else if mChar = '%' then

0 commit comments

Comments
 (0)