evancz / url-parser

Parse URLs into nicely structured data
http://package.elm-lang.org/packages/evancz/url-parser/latest/
BSD 3-Clause "New" or "Revised" License
114 stars 29 forks source link

Invalid example code for url-parser 1.0.0 under parse docs #14

Closed ckoster22 closed 7 years ago

ckoster22 commented 7 years ago

The parse function docs show this example:

blog : Parser (Int -> String -> a) a
blog =
  s "blog" </> int </> string

result : Result String (Int, String)
result =
  parse (,) blog "/blog/42/cat-herding-techniques"

-- result == OK (42, "cat-herding-techniques")

It's implied that code produces an OK result. I've put together an SSCCE with the same example code that says otherwise using Elm v0.17.1.

module Main exposing (..)

import Html exposing (Html, div, text)
import Navigation
import UrlParser exposing (Parser, parse, (</>), format, int, oneOf, s, string)

main : Program Never
main =
    Navigation.program (Navigation.makeParser result)
        { init = init
        , view = view
        , update = update
        , urlUpdate = urlUpdate
        , subscriptions = subscriptions
        }

blog : Parser (Int -> String -> a) a
blog =
    s "blog" </> int </> string

result : Navigation.Location -> Result String ( Int, String )
result ignoredLocation =
    Debug.log "Result" <| parse (,) blog "/blog/42/cat-herding-techniques"

type alias Model =
    Int

init : Result String ( Int, String ) -> ( Model, Cmd Msg )
init result =
    urlUpdate result 0

type Msg
    = NoOp

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    0 ! [ Cmd.none ]

urlUpdate : Result String ( Int, String ) -> Model -> ( Model, Cmd Msg )
urlUpdate result model =
    0 ! [ Cmd.none ]

subscriptions : Model -> Sub Msg
subscriptions model =
    Sub.none

view : Model -> Html Msg
view model =
    div [] [ text "No compile errors but check the console!" ]

The code compiles fine but the parser produces an error that I display in the console.

Result: Err "Wanted /blog but got //blog/42/cat-herding-techniques"

Note the extra slash. This error is fixed by changing this line:

blog =
    s "blog" </> int </> string

to this

blog =
    s "" </> s "blog" </> int </> string
process-bot commented 7 years ago

Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

evancz commented 7 years ago

Thanks for the report! A new version of this library is coming out with 0.18 (in the next week or so) and I recall fixing this issue in particular. So I think it makes sense to try to reproduce this with the 0.18 beta from elm-dev. If it can be reproduced, please open a new issue and we can fix the bug before the final release!