summaryrefslogtreecommitdiff
path: root/ddl/templates/data.purs.ede
blob: f8b3e5daae98ae28dcf624ab8dba77239e685fa8 (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
62
63
64
65
66
67
68
69
70
71
-- generated file, do not modify!
-- {{ dateTime }}

module {{ moduleName }} where
import Prelude
import Data.Generic
import Data.Either (Either(..))
import Data.Maybe (Maybe(..))
import Data.StrMap (StrMap(..))
import Data.Map (Map(..))
import Data.List (List(..))
import LambdaCube.LinearBase

import Data.Argonaut.Encode.Combinators ((~>), (:=))
import Data.Argonaut.Decode.Combinators ((.?))
import Data.Argonaut.Core (jsonEmptyObject)
import Data.Argonaut.Printer (printJson)
import Data.Argonaut.Encode (class EncodeJson, encodeJson)
import Data.Argonaut.Decode (class DecodeJson, decodeJson)

{% 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 | psType }}
{% endfor %}
  }
{% else %}
  {% if c.first %}={% else %}|{% endif %} {{ c.value.name }}{% for f in c.value.fields %} {{ f.value.fieldType | psType | parens }}{% endfor %}{% endif %}{% endfor %}

{% when "TypeAlias" %}
type {{ t.value.aliasName }} = {{ t.value.aliasType | psType }}

{% endcase %}
{% endfor %}
{% for t in definitions %}{% let l = t.value.instances | length %}{% if l > 0 %}{# FIXME!!! #}
derive instance generic{{ t.value.dataName }} :: Generic {{ t.value.dataName }}
instance show{{ t.value.dataName }} :: Show {{ t.value.dataName }} where show = gShow
instance eq{{ t.value.dataName }}   :: Eq {{ t.value.dataName }}   where eq = gEq
{% endif %}{% endlet %}{% endfor %}

{# JSON Encode and Decode #}
{% for t in definitions %}
instance encodeJson{{ t.value.dataName }} :: EncodeJson {{ t.value.dataName }} where
  encodeJson v = case v of{% for c in t.value.constructors %}{% if c.value.fields | hasFieldNames %}
    {{ c.value.name }} r ->
      "tag" := "{{ c.value.name }}" ~>{% for f in c.value.fields %}
      "{{ f.value.fieldName }}" := r.{{ f.value.fieldName }} ~>{% endfor %}
      jsonEmptyObject{% else %}
    {{ c.value.name }}{% for f in c.value.fields %} arg{{ f.index0 }}{% endfor %} -> "tag" := "{{ c.value.name }}"{% for f in c.value.fields %} ~> "arg{{ f.index0 }}" := arg{{ f.index0 }}{% endfor %} ~> jsonEmptyObject{% endif %}{% endfor %}

instance decodeJson{{ t.value.dataName }} :: DecodeJson {{ t.value.dataName }} where
  decodeJson json = do
    obj <- decodeJson json
    tag <- obj .? "tag"
    case tag 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 %}
      _ -> Left ("decodeJson{{ t.value.dataName }} - unknown tag: " <> tag)

{% endfor %}