/
TypeName.purs
58 lines (46 loc) · 1.68 KB
/
TypeName.purs
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
module GraphQL.Client.Variables.TypeName where
import Prelude
import Data.Argonaut.Core (Json)
import Data.Maybe (Maybe)
import Data.String.CodeUnits (dropRight, takeRight)
import Data.Symbol (class IsSymbol, reflectSymbol)
import Heterogeneous.Folding (class FoldingWithIndex, class HFoldlWithIndex, hfoldlWithIndex)
import Type.Proxy (Proxy(..))
class VarTypeName :: forall k. k -> Constraint
class VarTypeName var where
varTypeName :: Proxy var -> String
instance varTypeNameInt :: VarTypeName Int where
varTypeName _ = "Int!"
instance varTypeNameNumber :: VarTypeName Number where
varTypeName _ = "Float!"
instance varTypeNameString :: VarTypeName String where
varTypeName _ = "String!"
instance varTypeNameJson :: VarTypeName Json where
varTypeName _ = "Json!"
instance varTypeNameArray :: VarTypeName a => VarTypeName (Array a) where
varTypeName _ = "[" <> varTypeName (Proxy :: _ a) <> "]!"
instance varTypeNameMaybe :: VarTypeName a => VarTypeName (Maybe a) where
varTypeName _ =
let
inner = varTypeName (Proxy :: _ a)
in
if takeRight 1 inner == "!" then
dropRight 1 inner
else
inner
data VarTypeNameProps
= VarTypeNameProps
instance varTypeNameProps ::
(VarTypeName a, IsSymbol sym) =>
FoldingWithIndex VarTypeNameProps (Proxy sym) String a String where
foldingWithIndex VarTypeNameProps prop str _ = pre <> reflectSymbol prop <> ": " <> varTypeName (Proxy :: _ a)
where
pre
| str == "" = "$"
| otherwise = str <> ", $"
varTypeNameRecord ::
forall r.
HFoldlWithIndex VarTypeNameProps String { | r } String =>
{ | r } ->
String
varTypeNameRecord r = "( " <> hfoldlWithIndex VarTypeNameProps "" r <> " )"