Skip to content

Commit

Permalink
FEAT: can name params in a url format
Browse files Browse the repository at this point in the history
  • Loading branch information
rflechner committed Jul 27, 2017
1 parent 21edbd3 commit f33d831
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
3 changes: 3 additions & 0 deletions examples/Suave.Swagger.PetStoreAPi/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ let api =

for route in getting (urlFormat "/bonjour/%s" (fun x -> OK (sprintf "Bonjour %s" x))) do
yield description Of route is "Say hello in french"

for route in getting (pathTemplate "/hello/%s/%s" ["lastname";"firstname"] (fun (lastname, firstname) -> OK (sprintf "Bonjour %s %s" lastname firstname))) do
yield description Of route is "Say hello"

// another syntax
for route in getOf (path "/time2" >=> now) do
Expand Down
18 changes: 13 additions & 5 deletions src/Suave.Swagger/FunnyDsl.fs
Original file line number Diff line number Diff line change
Expand Up @@ -140,22 +140,28 @@ module FunnyDsl =

let simpleUrl (p:string) =
(DocBuildState.Of <| path p).Documents(fun doc -> { doc with Template = p})

let urlFormat (pf : PrintfFormat<_,_,_,_,'t>) f =
let pathTemplate (pf : PrintfFormat<_,_,_,_,'t>) (names:string list) f =
let ty = typeof<'t>
let parts = FormatParser.Parse pf.Value

let getName i =
if i < names.Length
then names.Item i
else sprintf "param%d" i

let _,tmpl =
parts
|> List.fold (
fun ((i,acc):(int*string)) (p:FormatPart) ->
match p with
| Constant c -> i, acc + c
| Parsed _ -> (i+1), sprintf "%s{param%d}" acc i
| Parsed _ -> (i+1), sprintf "%s{%s}" acc (getName i)
) (0,"")

let doc = DocBuildState.Of <| pathScan pf f
let pname (pt:Type) i =
let name = sprintf "param%d" i
let name = getName i
let tn = match pt.FormatAndName with | Some (_,v) -> v | None -> "object"
name,tn

Expand All @@ -180,7 +186,9 @@ module FunnyDsl =
)
|> fun d ->
d.Documents(fun doc -> { doc with Template = tmpl})


let urlFormat (pf : PrintfFormat<_,_,_,_,'t>) f =
pathTemplate pf [] f

let thenReturns w (d:DocBuildState) =
{ d with Current = { d.Current with WebPart=(d.Current.WebPart >=> w) } }

0 comments on commit f33d831

Please sign in to comment.