summaryrefslogtreecommitdiff
path: root/ddl/templates/data.swift.ede
blob: f2a9039449b596b4f0844d62013d76dd9ee14d1f (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// generated file, do not modify!
// {{ dateTime }}

{% for t in dataAndType %}
{% case t.value | constType %}
{% when "DataDef" %}
enum {{ t.value.dataName }} {
{% for c in t.value.constructors %}
{% if c.value.fields | hasFieldNames %}
  case {{ c.value.name }}({{ c.value.name }}_Data)
    struct {{ c.value.name }}_Data {
{% for f in c.value.fields %}
      var {{ f.value.fieldName }} : {{ f.value.fieldType | swiftType }}
{% endfor %}
    }
{% else %}
  case {{ c.value.name }}{% if !(c.value.fields | empty) %}({% for f in c.value.fields %}{{ f.value.fieldType | swiftType }}{% if !f.last %},{% endif %}{% endfor %}){% endif %}
{% endif %}
{% endfor %}
}
{% when "TypeAlias" %}
typealias {{ t.value.aliasName }} = {{ t.value.aliasType | swiftType }}
{% endcase %}

{% endfor %}
{#
protocol ToJSON {
    func toJSON(item: ItemType) -> [String: AnyObject]
}
#}

extension Int {
    var toJSON : [String: AnyObject] {
        return ["":""]
    }
}
extension Int32 {
    var toJSON : [String: AnyObject] {
        return ["":""]
    }
}
extension UInt {
    var toJSON : [String: AnyObject] {
        return ["":""]
    }
}
extension UInt32 {
    var toJSON : [String: AnyObject] {
        return ["":""]
    }
}
extension Float {
    var toJSON : [String: AnyObject] {
        return ["":""]
    }
}
extension Bool {
    var toJSON : [String: AnyObject] {
        return ["":""]
    }
}
extension String {
    var toJSON : [String: AnyObject] {
        return ["":""]
    }
}
extension Array {
    var toJSON : [String: AnyObject] {
        return ["":""]
    }
}
extension Dictionary {
    var toJSON : [String: AnyObject] {
        return ["":""]
    }
}
extension Maybe {
    var toJSON : [String: AnyObject] {
        return ["":""]
    }
}



{% for t in definitions %}
extension {{ t.value.dataName }} {
  var toJSON : [String: AnyObject] {
    switch self {
{% for c in t.value.constructors %}
{% if c.value.fields | hasFieldNames %}
      case .{{ c.value.name }}(let v):
        return [ "tag" : "{{ c.value.name }}"{% for f in c.value.fields %}
               , "{{ f.value.fieldName }}" : v.{{ f.value.fieldName }}.toJSON{% endfor %}
               ]
{% else %}
      case .{{ c.value.name }}{% if !(c.value.fields | empty) %}({% for f in c.value.fields %}let arg{{ f.index0 }}{% if !f.last %}, {% endif %}{% endfor %}){% endif %}:
        return [ "tag" : "{{ c.value.name }}"{% for f in c.value.fields %}, "arg{{ f.index0 }}" : arg{{ f.index0 }}.toJSON{% endfor %}]
{% endif %}
{% endfor %}
    }
  }
}
{% endfor %}

enum Maybe<T> {
  case Nothing
  case Just(T)
}

enum Type {
  case Int
  case Int32
  case Word
  case Word32
  case Float
  case Bool
  case String
  case Array(Type)
  case List(Type)
  case Maybe(Type)
  case Map(Type,Type)
{% for t in definitions %}
  case {{ t.value.dataName }}
{% endfor %}
}

func fromJSON(type: Type, personName: String) -> Any {
  switch type {
    case .Int:    return 0
    case .Int32:  return 0
    case .Word:   return 0
    case .Word32: return 0
    case .Float:  return 0.0
    case .Bool:   return false
    case .String: return ""
    case .Array(let a): return fromJSON(a,personName)
    case .List(let a):  return fromJSON(a,personName)
    case .Maybe(let a): return fromJSON(a,personName)
  }
  return 0;
}