@@ -27,10 +27,8 @@ import Control.Monad.Except
27import Control.Monad.State.Strict
27import Control.Monad.State.Strict
28import Data.CaseInsensitive (CI)
28import Data.CaseInsensitive (CI)
29import qualified Data.CaseInsensitive as CI
29import qualified Data.CaseInsensitive as CI
30−import Data.Char (toLower)
31import Data.Hashable
30import Data.Hashable
32import qualified Data.HashMap.Strict as Map
31import qualified Data.HashMap.Strict as Map
33−import qualified Data.HashMap.Strict as Map
34import qualified Data.HashSet as Set
32import qualified Data.HashSet as Set
35import Data.List (intersect)
33import Data.List (intersect)
36import Data.Maybe
34import Data.Maybe
@@ -70,8 +68,7 @@ data Derive
70 deriving (Eq, Show)
68 deriving (Eq, Show)
72data Solved = Solved
70data Solved = Solved
74− , _schema :: Schema Id
71+ { _schema :: Schema Id
75 , _type :: TType
72 , _type :: TType
76 , _deriving :: [Derive]
73 , _deriving :: [Derive]
@@ -101,14 +98,14 @@ type AST = ExceptT Error (State Memo)
102schema :: Id -> AST (Schema Id)
99schema :: Id -> AST (Schema Id)
103schema k = do
100schema k = do
104− m <- gets (Map.lookup k . view schemas)
101+ m <- uses schemas (Map.lookup k)
105 case m of
102 case m of
106 Nothing -> failure ("Missing Schema: " % fid) k
103 Nothing -> failure ("Missing Schema: " % fid) k
107 Just v -> pure v
104 Just v -> pure v
109memo :: Lens' Memo (Map Id v) -> Id -> (Schema Id -> AST v) -> AST v
106memo :: Lens' Memo (Map Id v) -> Id -> (Schema Id -> AST v) -> AST v
110memo l k f = do
107memo l k f = do
111− m <- gets (Map.lookup k . view l)
108+ m <- uses l (Map.lookup k)
112 case m of
109 case m of
113 Just x -> return x
110 Just x -> return x
114 Nothing -> do
111 Nothing -> do
@@ -117,16 +114,16 @@ memo l k f = do
119solve :: Id -> AST Solved
116solve :: Id -> AST Solved
120−solve k = Solved <$> prefix k <*> schema k <*> typeOf k <*> derive k
117+solve k = Solved <$> schema k <*> typeOf k <*> derive k
122typeOf :: Id -> AST TType
119typeOf :: Id -> AST TType
123−typeOf k = loc "typeOf" k $ memo typed k go
120+typeOf k = memo typed k go
125 go s = fmap may $ case s of
122 go s = fmap may $ case s of
126 Obj {} -> pure (TType k)
123 Obj {} -> pure (TType k)
127 Arr _ r -> TList <$> typeOf r
124 Arr _ r -> TList <$> typeOf r
128 Enum {} -> pure (TType k)
125 Enum {} -> pure (TType k)
129− Ref _ r -> typeOf r
126+ Ref _ r -> typeOf (Free r)
130 Any {} -> pure (TEither (TLit Text) (TLit Int64))
127 Any {} -> pure (TEither (TLit Text) (TLit Int64))
131 Lit _ l -> pure (TLit l)
128 Lit _ l -> pure (TLit l)
@@ -135,13 +132,13 @@ typeOf k = loc "typeOf" k $ memo typed k go
135 | otherwise = TMaybe
132 | otherwise = TMaybe
137derive :: Id -> AST [Derive]
134derive :: Id -> AST [Derive]
138−derive k = loc "derive" k $ memo derived k go
135+derive k = memo derived k go
140 go = \case
137 go = \case
141 Obj _ rs -> foldM props base (Map.elems rs)
138 Obj _ rs -> foldM props base (Map.elems rs)
142 Arr _ r -> mappend monoid . intersect base <$> derive r
139 Arr _ r -> mappend monoid . intersect base <$> derive r
143 Enum {} -> pure enum
140 Enum {} -> pure enum
144− Ref _ r -> derive r
141+ Ref _ r -> derive (Free r)
145 Any _ -> pure base
142 Any _ -> pure base
146 Lit _ l -> pure $
143 Lit _ l -> pure $
147 case l of
144 case l of
@@ -158,26 +155,24 @@ derive k = loc "derive" k $ memo derived k go
158 base = [DEq, DRead, DShow, DData, DTypeable, DGeneric]
155 base = [DEq, DRead, DShow, DData, DTypeable, DGeneric]
160prefix :: Id -> AST Pre
157prefix :: Id -> AST Pre
161−prefix k = loc "prefix" k $ memo prefixed k typ
158+prefix n = memo prefixed n typ
163 typ = \case
160 typ = \case
164− Obj i rs -> field rs >> pure (Pre k)
161+ Obj i rs -> field rs
165− Enum i vs ds -> pure (Pre k)
162+ Enum i vs ds -> pure mempty
166− _ -> pure (Pre k)
163+ _ -> pure mempty
168− field (keys -> ks) = do
169− p <- uniq fields (acronymPrefixes k) ks
171− prefixed %= Map.insert (k <> key n) (Pre (key p <> key n))
174− key = idFromText . CI.foldedCase
165+ field rs = do
166+ let ls = Map.keys rs
167+ ks = Set.fromList (map (CI.mk . local) ls)
168+ p <- uniq fields (acronymPrefixes n) ks
176 uniq :: Lens' Memo Seen
171 uniq :: Lens' Memo Seen
177 -> [CI Text]
172 -> [CI Text]
178 -> Set (CI Text)
173 -> Set (CI Text)
179− -> AST (CI Text)
174+ -> AST Text
180− uniq _ [] ks = failure ("Unable to generate unique prefix: " % fid) k
175+ uniq _ [] ks = failure ("Unable to generate unique prefix: " % fid) n
181 uniq seen (x:xs) ks = do
176 uniq seen (x:xs) ks = do
182 m <- uses seen (Map.lookup x)
177 m <- uses seen (Map.lookup x)
183 case m of
178 case m of
@@ -185,14 +180,11 @@ prefix k = loc "prefix" k $ memo prefixed k typ
185 -> uniq seen xs ks
180 -> uniq seen xs ks
187 seen %= Map.insertWith (<>) x ks
182 seen %= Map.insertWith (<>) x ks
188− return x
183+ return (CI.foldedCase x)
190overlap :: (Eq a, Hashable a) => Set a -> Set a -> Bool
185overlap :: (Eq a, Hashable a) => Set a -> Set a -> Bool
191overlap xs ys = not . Set.null $ Set.intersection xs ys
186overlap xs ys = not . Set.null $ Set.intersection xs ys
193−keys :: Map Id a -> Set (CI Text)
194−keys = Set.fromList . map (CI.mk . idToText) . Map.keys
196acronymPrefixes :: Id -> [CI Text]
188acronymPrefixes :: Id -> [CI Text]
197acronymPrefixes (idToText -> n) = map CI.mk (xs ++ map suffix ys)
189acronymPrefixes (idToText -> n) = map CI.mk (xs ++ map suffix ys)
@@ -232,6 +224,3 @@ acronymPrefixes (idToText -> n) = map CI.mk (xs ++ map suffix ys)
233 -- SomeTestTType -> Som
225 -- SomeTestTType -> Som
234 r6 = Text.take limit <$> listToMaybe (splitWords a)
226 r6 = Text.take limit <$> listToMaybe (splitWords a)
236−loc :: String -> Id -> a -> a
237−loc _ _ = id -- trace (n ++ ": " ++ Text.unpack (refToText r))