working with multiple constructors #16

Closed
adzeitor opened this Issue Sep 5, 2012 · 2 comments

Comments

Projects
None yet
2 participants

adzeitor commented Sep 5, 2012

{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
import Data.Typeable
import Data.Data
import Data.Aeson
import GHC.Generics
import Data.Monoid

import Text.Hastache
import Text.Hastache.Context
import qualified Data.ByteString.Lazy.Char8 as LZ

data Hero = SuperHero { name :: String
                      , powers :: [String]
                      , companion :: String}
          | EvilHero { name :: String
                     , minion :: String}
             deriving(Show,Data, Typeable, Generic)
instance ToJSON Hero
instance FromJSON Hero

data SimpleHero = SimpleHero { simpleName :: String }
             deriving(Show,Data, Typeable, Generic)
instance ToJSON SimpleHero
instance FromJSON SimpleHero

template = mconcat [
  "{{#SuperHero}}\n",
  "Hero: {{name}}\n",
  " * Powers: {{#powers}}\n",
  "\n   - {{.}}{{/powers}} \n",
  " * Companion: {{companion}}\n",
  "{{/SuperHero}}\n",
  "{{#EvilHero}}\n",
  "Evil hero: {{name}}\n",
  " * Minion: {{minion}}\n",
  "{{/EvilHero}}"]
context = mkGenericContext


render a = hastacheStr defaultConfig (encodeStr template) ( context a)

main = do let batman = SuperHero "Batman" ["ht","ht"] "Robin"
          let doctorEvil = EvilHero "Doctor Evil" "Mini-Me"
          let simple = SimpleHero "Mermaid"
          putStrLn "\n------ JSON batman (multiple constructors)"
          LZ.putStrLn $ encode batman
          putStrLn "--------------------------"

          putStrLn "\n---- JSON doctorEvil (multiple constructors)"
          LZ.putStrLn $ encode doctorEvil
          putStrLn "--------------------------"

          putStrLn "\n------ JSON simple (one constructor)"
          LZ.putStrLn $ encode simple
          putStrLn "--------------------------"


          render batman >>= print
          render doctorEvil >>= print

and output

------ JSON batman (multiple constructors)
{"SuperHero":{"name":"Batman","companion":"Robin","powers":["ht","ht"]}}
--------------------------

---- JSON doctorEvil (multiple constructors)
{"EvilHero":{"name":"Doctor Evil","minion":"Mini-Me"}}
--------------------------

------ JSON simple (one constructor)
{"simpleName":"Mermaid"}
--------------------------
Empty
Empty

@co-dan co-dan added a commit that referenced this issue Jan 28, 2014

@co-dan co-dan Constructor name (with value True) added to the generic contexts
This allows for making generic contexts with multiple
constructors, as in #16
34b8445
Collaborator

co-dan commented Jan 28, 2014

The necessary code changes have been pushed, the example works in HEAD.

However, the documentation and tests need to be updated, so I am not closing this just yet.
@adzeitor would you mind if I put this example in the examples/ collection?

Sure. I'm very glad to see these changes (and my example :) ) in HEAD.

co-dan closed this in c3492de Jan 28, 2014

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