Skip to content

add ?~> and ?:= combinators #11

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

Closed
wants to merge 1 commit into from
Closed

add ?~> and ?:= combinators #11

wants to merge 1 commit into from

Conversation

kanterov
Copy link

Example usage

instance encodeJsonOption :: EncodeJson Option where
  encodeJson (Option o)
    =   "backgroundColor" ?:= o.backgroundColor
    ?~> "color"           ?:= o.color
    ?~> jsonEmptyObject

If backgroundColor is Nothing, it results into

{
  "color": "#ff0000"
}

@jdegoes
Copy link

jdegoes commented Jan 26, 2015

These combinators are not necessary since Maybe already has EncodeJson and DecodeJson instances. Or am I missing something???

@kanterov
Copy link
Author

It has different behaviour, for this example it would result in:

{
  "color": "#ff0000",
  "backgroundColor": null
}

@jdegoes
Copy link

jdegoes commented Jan 26, 2015

Ah, I see, so this is undefined. Is the goal to compact the JSON objects as much as possible? Or just an alternate take on what Maybe should be serialized into?

@kanterov
Copy link
Author

My goal was to preserve semantic, in Javascript, there is a difference between {a: undefined} and {}. In my case, I was working with javascript library that somehow merges JSON object into its own. Most of the merge implementations that I've seen will append a new value no matter if it's null or undefined.

I've seen similar combinators in original argonaut https://github.com/argonaut-io/argonaut/blob/master/src/main/scala/argonaut/Json.scala#L159.

@jdegoes
Copy link

jdegoes commented Jan 26, 2015

OK, looks good. Can you add another example to README.md on how to use these combinators?

@kanterov
Copy link
Author

OK, I will wait for #10

@jdegoes
Copy link

jdegoes commented Feb 12, 2015

@kanterov #10 has been merged, can you updated this so we can merge it in?

@joneshf
Copy link
Member

joneshf commented Feb 12, 2015

Before this gets merged, I was thinking about these operators the other day. Maybe it would make sense to keep the same combinator names as the scala version, including changing the names of the combinators that are already there. It would probably be less jarring to people coming here from there. They should all be valid in ps.

@jdegoes
Copy link

jdegoes commented Feb 12, 2015

That sounds good to me.

@kanterov
Copy link
Author

I'm not quite familiar with haskell infix operator naming conventions, as I got the idea from sources question mark should be placed next to Maybe a, so convention is

(?xx) :: forall a. Maybe a -> a -> a
(xx?) :: forall a. a -> Maybe a -> a
(?xx?) :: forall a. a -> Maybe a -> Maybe a -> a

There is ?>>= that has the same convention:

(?>>=) :: forall a b. Maybe a -> String -> Either String a

What do you think? @jdegoes @joneshf

So changes are (?:=) -> (:=?), (?>>=) (?~>) remains the same

@kanterov
Copy link
Author

Probably prefixing with ? makes sence for Scala for proper operator precedence

@kanterov
Copy link
Author

@joneshf @jdegoes ping

@jdegoes
Copy link

jdegoes commented Apr 14, 2015

@kanterov Can you resolve conflicts?

@thomashoneyman
Copy link

Closing as resolved by purescript-contrib/purescript-argonaut-codecs#38

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

Successfully merging this pull request may close these issues.

4 participants