type tags #7

mwotton opened this Issue Sep 9, 2011 · 2 comments


None yet

2 participants

mwotton commented Sep 9, 2011

I'm having a bit of trouble with a pattern.

If I have

data Foo = Bar Int | Baz Int

and I'd like

{ "type": "Bar", "id": 2 }

to be parsed to Bar 2

{"type": "Baz", "id": 2}

to be parsed to Baz 2, how would I arrange it?

Otherwise, a lovely piece of software, thank you:)


Hi Mark,

You can use the choice operator <> in combination with fixedProp for this:

foo1 :: Iso (Value :- t) (Foo :- t)
foo1 =  bar . object (fixedProp "type" "Bar" . prop "id")
     <> baz . object (fixedProp "type" "Baz" . prop "id")

But this definition contains some redundancy: the fact that we're parsing an object is mentioned twice, as well as the property "id". We can fix this. First we push the constructor isos bar and baz inside object's argument:

foo2 :: Iso (Value :- t) (Foo :- t)
foo2 =  object (duck bar . fixedProp "type" "Bar" . prop "id")
     <> object (duck baz . fixedProp "type" "Baz" . prop "id")

Then we push the <> inside:

foo3 :: Iso (Value :- t) (Foo :- t)
foo3 =  object (typeProp . prop "id")
    typeProp = duck bar . fixedProp "type" "Bar"
            <> duck baz . fixedProp "type" "Baz"

Hope this helps! Let me know if you have any other questions.

mwotton commented Sep 12, 2011

thanks, that worked great!

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