문제

I just read the tutorial at https://www.fpcomplete.com/user/tel/lens-aeson-traversals-prisms, and I have successfully written a query into a json bytestring. However, I am not getting the kind of result value I want.

I'd like to do something along the lines of

if (j^? key "some key" == Just "Google") then ...
                                         else ...

But (j^? key "some key") has the type "Maybe Value".

This must be a common enough pattern that I'd be surprised if there wasn't a utility function to turn a Value into a Text. Any ideas?

도움이 되었습니까?

해결책

There is! The _String Prism has type Prism' Value Text, i.e. it attempts to traverse down the branch of Value containing a Text. So you can do j ^? key "some key" . _String == Just "Google".

다른 팁

My lens-fu is pretty limited, but looks like you need _String method of AsPrimitive:

if (j^? key "some key" >>= (^? _String)) == Just "Google"

Or you can convert the right part to Value:

if (j^? key "some key" == Just (String "Google"))
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top