Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


working with multiple constructors #16

adzeitor opened this Issue · 2 comments

2 participants

{-# 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 [
  "Hero: {{name}}\n",
  " * Powers: {{#powers}}\n",
  "\n   - {{.}}{{/powers}} \n",
  " * Companion: {{companion}}\n",
  "Evil hero: {{name}}\n",
  " * Minion: {{minion}}\n",
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)

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

------ JSON simple (one constructor)

@co-dan co-dan referenced this issue from a commit
@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

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 co-dan closed this issue from a commit
@co-dan co-dan Adding documentation, tests and an example for multiple constructor
support in generic contexts

This resolves #16
@co-dan co-dan closed this in c3492de
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.