@@ -13,7 +13,8 @@
1313
1414module Gen.Syntax where
1515
16−import Control.Lens hiding (iso, mapping, op, strict)
16+import Control.Lens hiding (iso, mapping, op, pre,
17+ strict)
1718import Data.Foldable (foldr')
1819import Data.Hashable
1920import qualified Data.HashMap.Strict as Map
@@ -23,6 +24,7 @@ import Data.String
2324import Data.Text (Text)
2425import qualified Data.Text as Text
2526import Data.Text.Manipulate
27+import Gen.Orphans
2628import Gen.Solve
2729import Gen.Text
2830import Gen.Types
@@ -30,12 +32,6 @@ import Language.Haskell.Exts.Build
3032import Language.Haskell.Exts.SrcLoc
3133import Language.Haskell.Exts.Syntax hiding (Int)
3234
33−instance Hashable Name
34−
35−instance IsString Name where fromString = name
36−instance IsString QName where fromString = UnQual . name
37−instance IsString QOp where fromString = op . sym
38−
3935apiTypes :: Service s r -> Id -> Method -> [Type]
4036apiTypes s r m = map sing ps ++ qs ++ [end]
4137 where
@@ -59,14 +55,14 @@ apiTypes s r m = map sing ps ++ qs ++ [end]
5955
6056 meth = TyCon . unqual . Text.unpack $ Text.toTitle (_mMethod m)
6157 typ = tycon "'[JSON]"
62− res = maybe (tycon "()") tycon (_mResponse m)
58+ res = maybe (tycon "()") (tycon . Pre) (_mResponse m)
6359
6460 -- method (titlecase) '[JSON] (responsetype)
6561
66−objDecl :: Id -> Map Id Solved -> AST Decl
67−objDecl r rs = DataDecl noLoc arity [] d [] [conDecl d rs] <$> derivings r
62+objDecl :: Id -> Pre -> Map Id Solved -> AST Decl
63+objDecl r p rs = DataDecl noLoc arity [] d [] [conDecl d rs] <$> derivings r
6864 where
69− d = dname r
65+ d = dname p
7066
7167 arity | Map.size rs == 1 = NewType
7268 | otherwise = DataType
@@ -74,14 +70,14 @@ objDecl r rs = DataDecl noLoc arity [] d [] [conDecl d rs] <$> derivings r
7470conDecl :: Name -> Map Id Solved -> QualConDecl
7571conDecl n rs = QualConDecl noLoc [] [] body
7672 where
77− body = case Map.toList rs of
73+ body = case Map.elems rs of
7874 [] -> ConDecl n []
7975 [x] -> RecDecl n [field internal x]
8076 xs -> RecDecl n (map (field (strict . internal)) xs)
8177
82− field f (k, v) = ([fname k], f (_type v))
78+ field f v = ([fname (_prefix v)], f (_type v))
8379
84−ctorSig :: Id -> Map Id Solved -> Decl
80+ctorSig :: Pre -> Map Id Solved -> Decl
8581ctorSig r rs = TypeSig noLoc [c] ts
8682 where
8783 c = cname r
@@ -90,7 +86,7 @@ ctorSig r rs = TypeSig noLoc [c] ts
9086 ts = foldr' TyFun (TyCon (UnQual d)) ps
9187 ps = parameters (Map.elems rs)
9288
93−ctorDecl :: Id -> Map Id Solved -> Decl
89+ctorDecl :: Pre -> Map Id Solved -> Decl
9490ctorDecl r rs = sfun noLoc c ps (UnGuardedRhs rhs) noBinds
9591 where
9692 c = cname r
@@ -98,30 +94,31 @@ ctorDecl r rs = sfun noLoc c ps (UnGuardedRhs rhs) noBinds
9894
9995 rhs | Map.null rs = var d
10096 | otherwise = RecConstr (UnQual d) $
101− map (uncurry fieldUpdate) (Map.toList rs)
97+ map fieldUpdate (Map.elems rs)
10298
103− ps = map pname . Map.keys $ Map.filter (view required) rs
99+ ps = map (pname . _prefix) . Map.elems $ Map.filter (view required) rs
104100
105−fieldUpdate :: Id -> Solved -> FieldUpdate
106−fieldUpdate r s = FieldUpdate (UnQual (fname r)) rhs
101+fieldUpdate :: Solved -> FieldUpdate
102+fieldUpdate s = FieldUpdate (UnQual (fname n)) rhs
107103 where
108104 rhs | s ^. required = p
109105 | Just x <- s ^. defaulted = str x
110106 | Just v <- iso (_type s) = infixApp v "#" p
111107 | otherwise = var (name "Nothing")
112108
113− p = var (pname r)
109+ p = var (pname n)
110+ n = _prefix s
114111
115−lensSig :: Id -> Id -> Solved -> Decl
116−lensSig n r s = TypeSig noLoc [lname r] $
112+lensSig :: Pre -> Solved -> Decl
113+lensSig n s = TypeSig noLoc [lname (_prefix s)] $
117114 TyApp (TyApp (tycon "Lens'") (tycon n))
118115 (external (_type s))
119116
120−lensDecl :: Id -> Solved -> Decl
117+lensDecl :: Pre -> Solved -> Decl
121118lensDecl r s = sfun noLoc l [] (UnGuardedRhs rhs) noBinds
122119 where
123− l = lname r
124120 f = fname r
121+ l = lname (_prefix s)
125122 t = _type s
126123
127124 rhs = mapping t $
@@ -129,14 +126,8 @@ lensDecl r s = sfun noLoc l [] (UnGuardedRhs rhs) noBinds
129126 (paren (lamE noLoc [pvar "s", pvar "a"]
130127 (RecUpdate (var "s") [FieldUpdate (UnQual f) (var "a")])))
131128
132−enumDecl :: Decl
133−enumDecl = emptyDecl
134−
135−emptyDecl :: Decl
136−emptyDecl = DataDecl noLoc DataType [] (name "Empty") [] [] []
137−
138129derivings :: Id -> AST [Deriving]
139−derivings = fmap (map ((,[]) . unqual . drop 1 . show)) . derive
130+derivings k = map ((,[]) . unqual . drop 1 . show) <$> derive k
140131
141132parameters :: [Solved] -> [Type]
142133parameters = map (external . _type) . filter (view required)
@@ -144,7 +135,7 @@ parameters = map (external . _type) . filter (view required)
144135external, internal :: TType -> Type
145136external = internal
146137internal = \case
147− TType r -> tycon r
138+ TType r -> tycon (Pre r)
148139 TMaybe t -> TyApp (tycon "Maybe") (internal t)
149140 TEither a b -> TyApp (TyApp (tycon "Either") (internal a)) (internal b)
150141 TList t -> TyList (internal t)
@@ -188,8 +179,8 @@ strict = TyBang BangedTy . \case
188179sing :: Text -> Type
189180sing = TyCon . unqual . Text.unpack . flip mappend "\"" . mappend "\""
190181
191−tycon :: Id -> Type
192−tycon = TyCon . unqual . ref upperHead
182+tycon :: Pre -> Type
183+tycon = TyCon . unqual . pre upperHead
193184
194185unqual :: String -> QName
195186unqual = UnQual . name