summaryrefslogtreecommitdiff
path: root/ddl/templates/data.hs.ede
blob: 8e1115def81d136514d5859ec7a18f6402e8ca9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
-- generated file, do not modify!
-- {{ dateTime }}

{-# LANGUAGE OverloadedStrings, RecordWildCards #-}
module {{ moduleName }} where

import Data.Int
import Data.Word
import Data.Map
import Data.Vector (Vector(..))
import LambdaCube.Linear

import Data.Text
import Data.Aeson hiding (Value,Bool)
import Data.Aeson.Types hiding (Value,Bool)
import Control.Monad

{% for m in imports %}
import {{ m.value }}
{% endfor %}

{% for t in dataAndType %}
{% case t.value | constType %}
{% when "DataDef" %}
data {{ t.value.dataName }}{% for c in t.value.constructors %}
{% if c.value.fields | hasFieldNames %}
  {% if c.first %}={% else %}|{% endif %} {{ c.value.name }}
{% for f in c.value.fields %}{% if f.first %}  { {%else%}  , {%endif%}{{ f.value.fieldName }} :: {{ f.value.fieldType | hsType }}
{% endfor %}
  }
{% else %}
  {% if c.first %}={% else %}|{% endif %} {{ c.value.name }}{% for f in c.value.fields %} {{ f.value.fieldType | hsType | parens }}{% endfor %}{% endif %}{% endfor %}
  deriving (Show, Eq, Ord)
{% when "TypeAlias" %}
type {{ t.value.aliasName }} = {{ t.value.aliasType | hsType }}
{% endcase %}

{% endfor %}

{% for t in definitions %}
instance ToJSON {{ t.value.dataName }} where
  toJSON v = case v of{% for c in t.value.constructors %}{% if c.value.fields | hasFieldNames %}
    {{ c.value.name }}{..} -> object
      [ "tag" .= ("{{ c.value.name }}" :: Text){% for f in c.value.fields %}
      , "{{ f.value.fieldName }}" .= {{ f.value.fieldName }}{% endfor %}
      ]{% else %}
    {{ c.value.name }}{% for f in c.value.fields %} arg{{ f.index0 }}{% endfor %} -> object [ "tag" .= ("{{ c.value.name }}" :: Text){% for f in c.value.fields %}, "arg{{ f.index0 }}" .= arg{{ f.index0 }}{% endfor %}]{% endif %}{% endfor %}

instance FromJSON {{ t.value.dataName }} where
  parseJSON (Object obj) = do
    tag <- obj .: "tag"
    case tag :: Text of{% for c in t.value.constructors %}{% if c.value.fields | hasFieldNames %}
      "{{ c.value.name }}" -> do{% for f in c.value.fields %}
        {{ f.value.fieldName }} <- obj .: "{{ f.value.fieldName }}"{% endfor %}
        pure $ {{ c.value.name }}{% for f in c.value.fields %}
          {% if f.first %}{ {% else %}, {%endif%}{{ f.value.fieldName }} = {{ f.value.fieldName }}{% endfor %}
          } {% else %}
      "{{ c.value.name }}" -> {% for f in c.value.fields %}{% if f.first %}{{ c.value.name }} <$>{% else %} <*>{% endif %} obj .: "arg{{ f.index0 }}"{%else%}pure {{ c.value.name }}{% endfor %}{% endif %}{% endfor %}
  parseJSON _ = mzero

{% endfor %}