Skip to content
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

Moving down into an empty object or list fails. #38

Closed
luke-clifton opened this issue Dec 18, 2018 · 9 comments
Closed

Moving down into an empty object or list fails. #38

luke-clifton opened this issue Dec 18, 2018 · 9 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@luke-clifton
Copy link
Contributor

This isn't what I was expecting.

D.withCursor $ \c -> D.down c >> pure ()

will fail on "{}" or "[]". I was expecting the D.down to succeed, but then any D.moveRight to cause an error. Is this documented somewhere?

@mankyKitty
Copy link
Contributor

There isn't anything in the index for that position, so it is considered a failure. I don't think I have explicit documentation for this behaviour, I'll add some in.

@luke-clifton
Copy link
Contributor Author

So, what I want to do is, go down into an object, then do a foldCursor (or a rightwardSnoc). I'm perfectly happy for that to just give me the initial value of the accumulator if the object is empty. What is the alternative? If I check for failure on the down I still don't know why that is, it could be because it's a text or numeric field.

Or, rephrased, how can I check that my cursor is currently pointed at an object without parsing it all into a Json?

@luke-clifton
Copy link
Contributor Author

Which, looking at the code just now, is exactly what your listAt is trying to do. So, if you listAt and the cursor is sitting in front of a text or numeric field, it will result in an empty list, which seems slight counter-intuitive.

@mankyKitty
Copy link
Contributor

That listAt behaviour seems a bit more 'bug' than 'counter-intuitive', so thanks for that !

Currently there isn't a direct way to check if you're currently looking at an object or other specific type, without going to the Json structure then using the lenses and prisms to nose around. That's a useful feature though and wouldn't be difficult to include.

To keep you moving, you could take advantage of the Alt instance for Decoder to combine alternatives for what may exist at a given cursor position. Until I fix the listAt this will mean you need to be aware of ordering considerations, which isn't awesome.

Alternatively, you are able to use the functions from the library that powers the cursor itself hw-json. Specifically from the module HaskellWorks.Data.Json.Type.

jsonTypeAt (unJCurs cursor) :: Maybe JsonType

Using Alt would be my first preference though. Does any of that help?

@luke-clifton
Copy link
Contributor Author

It seems that exposing jsonTypeAt is what I want.

@luke-clifton
Copy link
Contributor Author

Ha, we posted at the same time. 👍

@mankyKitty
Copy link
Contributor

I'll leave this open until I've patched listAt others if I find more

@luke-clifton
Copy link
Contributor Author

@mankyKitty I've posted a patch.

@mankyKitty mankyKitty added bug Something isn't working enhancement New feature or request labels Dec 19, 2018
@luke-clifton
Copy link
Contributor Author

Fixed by #39

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants