/
Type.hs
185 lines (147 loc) · 5.2 KB
/
Type.hs
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TypeSynonymInstances #-}
module Servant.Kotlin.Type
( KotlinClass (..)
, KotlinPrimitiveClass (..)
, KotlinExClass (..)
, KotlinDataClass (..)
, KotlinFields (..)
, KotlinField (..)
, KotlinType (..)
, GenericKotlinType (..)
, GenericKotlinFields (..)
, toKotlinType'
) where
import Data.Int (Int16, Int32, Int64, Int8)
import Data.IntMap (IntMap)
import Data.Map (Map)
import Data.Maybe (fromMaybe)
import Data.Proxy (Proxy (..))
import Data.Text (Text, pack)
import Data.Time (UTCTime)
import GHC.Generics
import Servant.API (NoContent (..))
data KotlinClass
= PrimitiveClass KotlinPrimitiveClass
| DataClass KotlinDataClass
| ExClass KotlinExClass
deriving (Show, Eq)
data KotlinPrimitiveClass
= KDouble
| KFloat
| KLong
| KInt
| KShort
| KByte
| KChar
| KBoolean
| KArray KotlinClass
| KString
| KUnit
| KNullable KotlinClass
| KAny
deriving (Show, Eq)
data KotlinExClass
= KList KotlinClass
| KHashMap KotlinClass KotlinClass
| KPair KotlinClass KotlinClass
| KTime
deriving (Show, Eq)
data KotlinDataClass = KotlinDataClass Text KotlinFields deriving (Show, Eq)
data KotlinFields
= Node KotlinField
| Brunch KotlinFields KotlinFields
deriving (Show, Eq)
data KotlinField = KotlinField Text KotlinClass deriving (Show, Eq)
class KotlinType a where
toKotlinType :: a -> Maybe KotlinClass
toKotlinType = genericToKotlinType . from
default toKotlinType :: (Generic a, GenericKotlinType (Rep a)) =>
a -> Maybe KotlinClass
class GenericKotlinType f where
genericToKotlinType :: f a -> Maybe KotlinClass
instance (Datatype d, GenericKotlinFields f)
=> GenericKotlinType (D1 d f) where
genericToKotlinType datatype = fmap DataClass $
KotlinDataClass (pack $ datatypeName datatype)
<$> genericToKotlinFields (unM1 datatype)
class GenericKotlinFields f where
genericToKotlinFields :: f a -> Maybe KotlinFields
instance (Constructor c, GenericKotlinFields f)
=> GenericKotlinFields (C1 c f) where
genericToKotlinFields constructor =
if conIsRecord constructor then
genericToKotlinFields (unM1 constructor)
else
Nothing
instance GenericKotlinFields (f :+: g) where
genericToKotlinFields _ = Nothing
instance (Selector s, GenericKotlinType a)
=> GenericKotlinFields (S1 s a) where
genericToKotlinFields selector =
case selName selector of
"" -> Nothing
name ->
Node . KotlinField (pack name)
<$> genericToKotlinType (undefined :: a p)
instance (GenericKotlinFields f, GenericKotlinFields g)
=> GenericKotlinFields (f :*: g) where
genericToKotlinFields _ =
Brunch <$> genericToKotlinFields (undefined :: f p)
<*> genericToKotlinFields (undefined :: g p)
instance GenericKotlinFields U1 where
genericToKotlinFields _ = Nothing
instance KotlinType a => GenericKotlinType (Rec0 a) where
genericToKotlinType _ = toKotlinType (Proxy :: Proxy a)
instance KotlinType a => KotlinType [a] where
toKotlinType _ = ExClass . KList <$> toKotlinType (Proxy :: Proxy a)
instance KotlinType a => KotlinType (Maybe a) where
toKotlinType _ =
PrimitiveClass . KNullable <$> toKotlinType (Proxy :: Proxy a)
instance KotlinType () where
toKotlinType _ = Just $ PrimitiveClass KUnit
instance KotlinType Text where
toKotlinType _ = Just $ PrimitiveClass KString
instance KotlinType UTCTime where
toKotlinType _ = Just $ ExClass KTime
instance KotlinType Float where
toKotlinType _ = Just $ PrimitiveClass KFloat
instance KotlinType Double where
toKotlinType _ = Just $ PrimitiveClass KDouble
instance KotlinType Int where
toKotlinType _ = Just $ PrimitiveClass KInt
instance KotlinType Int8 where
toKotlinType _ = Just $ PrimitiveClass KByte
instance KotlinType Int16 where
toKotlinType _ = Just $ PrimitiveClass KShort
instance KotlinType Int32 where
toKotlinType _ = Just $ PrimitiveClass KInt
instance KotlinType Int64 where
toKotlinType _ = Just $ PrimitiveClass KLong
instance KotlinType Char where
toKotlinType _ = Just $ PrimitiveClass KChar
instance KotlinType Bool where
toKotlinType _ = Just $ PrimitiveClass KBoolean
instance (KotlinType a, KotlinType b) => KotlinType (a, b) where
toKotlinType _ = fmap ExClass $
KPair
<$> toKotlinType (Proxy :: Proxy a)
<*> toKotlinType (Proxy :: Proxy b)
instance (KotlinType a) => KotlinType (Proxy a) where
toKotlinType _ = toKotlinType (undefined :: a)
instance (KotlinType k, KotlinType v) => KotlinType (Map k v) where
toKotlinType _ = fmap ExClass $
KHashMap
<$> toKotlinType (Proxy :: Proxy k)
<*> toKotlinType (Proxy :: Proxy v)
instance (KotlinType v) => KotlinType (IntMap v) where
toKotlinType _ =
PrimitiveClass . KArray <$> toKotlinType (Proxy :: Proxy v)
instance KotlinType NoContent where
toKotlinType _ = Just $ PrimitiveClass KUnit
toKotlinType' :: (KotlinType a) => a -> KotlinClass
toKotlinType' = fromMaybe (PrimitiveClass KAny) . toKotlinType