New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter Json.Decode.keyValuePairs with hasOwnProperty #768

Closed
wants to merge 1 commit into
base: master
from

Conversation

Projects
None yet
5 participants
@lsjroberts

lsjroberts commented Dec 2, 2016

If I wish to decode target.childNodes[...].textContent I would expect the following to work:

childrenContentDecoder : Decode.Decoder (List String)
childrenContentDecoder =
    Decode.at [ "target", "childNodes" ] (Decode.keyValuePairs textContentDecoder)
        |> Decode.map (\nodes -> List.map (\( key, text ) -> text) nodes)

textContentDecoder : Decode.Decoder String
textContentDecoder =
    Decode.field "textContent" Decode.string

However, since target.childNodes is an instance of NodeList this silently fails due to it having some inherited props, such as .length, .entries etc, in addition to the expected indices.

(Unless I'm missing something) the solution to this in Elm is:

childrenContentDecoder : Decode.Decoder (List String)
childrenContentDecoder =
    Decode.at [ "target", "childNodes" ] (Decode.keyValuePairs textContentDecoder)
        |> Decode.map
            (\nodes ->
                nodes
                    |> List.filterMap
                        (\( key, text ) ->
                            case String.toInt key of
                                Err msg ->
                                    Nothing

                                Ok value ->
                                    Just text
                        )
                    |> List.reverse
            )

textContentDecoder : Decode.Decoder String
textContentDecoder =
    Decode.oneOf [ Decode.field "textContent" Decode.string, Decode.succeed "nope" ]

This is rather convoluted, and indeed since no errors were thrown I had to step debugger through Elm itself to discover the cause. I believe doing a .hasOwnProperty check would solve this for the common use case where the expected values are all of the same type.

(The List.reverse is a possibly unrelated curiosity, but does appear to be required in all my tests).

(This may be worth a separate issue, but using Decode.list instead of Decode.keyValuePairs also resulted in a silent error).

I can provide a full demo app if required.

@process-bot

This comment has been minimized.

Show comment
Hide comment
@process-bot

process-bot Dec 2, 2016

Thanks for the pull request! 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.

process-bot commented Dec 2, 2016

Thanks for the pull request! 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.

@mgold

This comment has been minimized.

Show comment
Hide comment
@mgold

mgold Dec 2, 2016

Contributor

+1, this is stupid JS garbage. Whitespace diff is instructive: append ?w=1 to the URL.

Contributor

mgold commented Dec 2, 2016

+1, this is stupid JS garbage. Whitespace diff is instructive: append ?w=1 to the URL.

@rtfeldman

This comment has been minimized.

Show comment
Hide comment
@rtfeldman

rtfeldman Dec 5, 2016

Member

Yep, this seems like the right thing to do here. 👍

Screenshot of the diff without whitespace, per Max's comment:

screen shot 2016-12-04 at 4 03 47 pm

Member

rtfeldman commented Dec 5, 2016

Yep, this seems like the right thing to do here. 👍

Screenshot of the diff without whitespace, per Max's comment:

screen shot 2016-12-04 at 4 03 47 pm

@lsjroberts

This comment has been minimized.

Show comment
Hide comment
@lsjroberts

lsjroberts Dec 6, 2016

Just rebasing from master to get the elm-test fixes and put the brace on a new line.

lsjroberts commented Dec 6, 2016

Just rebasing from master to get the elm-test fixes and put the brace on a new line.

@evancz evancz changed the title from Fix key value pairs to filter out non-own properties to Filter Json.Decode.keyValuePairs with hasOwnProperty Mar 26, 2017

@evancz evancz closed this in 4158c7f Jul 9, 2017

@evancz

This comment has been minimized.

Show comment
Hide comment
@evancz

evancz Jul 9, 2017

Member

Thank you for the suggestion! The code moved around and changed a bit for 0.19 so I added the check myself.

Member

evancz commented Jul 9, 2017

Thank you for the suggestion! The code moved around and changed a bit for 0.19 so I added the check myself.

@lsjroberts lsjroberts deleted the lsjroberts:feature/key-value-pairs-own-property branch Jul 10, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment