@@ -9,6 +9,7 @@
9{-# LANGUAGE RankNTypes #-}
9{-# LANGUAGE RankNTypes #-}
10{-# LANGUAGE RecordWildCards #-}
10{-# LANGUAGE RecordWildCards #-}
11{-# LANGUAGE TemplateHaskell #-}
11{-# LANGUAGE TemplateHaskell #-}
12+{-# LANGUAGE ViewPatterns #-}
13-- Module : Gen.Solve
14-- Module : Gen.Solve
14-- Copyright : (c) 2015 Brendan Hay
15-- Copyright : (c) 2015 Brendan Hay
@@ -27,6 +28,8 @@ import Control.Monad.State.Strict
27import Data.CaseInsensitive (CI)
28import Data.CaseInsensitive (CI)
28import qualified Data.CaseInsensitive as CI
29import qualified Data.CaseInsensitive as CI
29import Data.Char (toLower)
30import Data.Char (toLower)
32+import qualified Data.HashMap.Strict as Map
30import qualified Data.HashMap.Strict as Map
33import qualified Data.HashMap.Strict as Map
31import qualified Data.HashSet as Set
34import qualified Data.HashSet as Set
32import Data.List (intersect)
35import Data.List (intersect)
@@ -36,6 +39,7 @@ import Data.Text (Text)
36import qualified Data.Text as Text
39import qualified Data.Text as Text
37import Data.Text.Manipulate
40import Data.Text.Manipulate
38import Gen.Formatting
41import Gen.Formatting
39import Gen.Types
43import Gen.Types
40import Prelude hiding (sum)
44import Prelude hiding (sum)
@@ -154,17 +158,80 @@ derive k = loc "derive" k $ memo derived k go
154 base = [DEq, DRead, DShow, DData, DTypeable, DGeneric]
158 base = [DEq, DRead, DShow, DData, DTypeable, DGeneric]
156prefix :: Id -> AST Pre
160prefix :: Id -> AST Pre
157−prefix k = loc "prefix" k $ memo prefixed k go
161+prefix k = loc "prefix" k $ memo prefixed k typ
164+ Obj i rs -> field rs >> pure (Pre k)
165+ Enum i vs ds -> pure (Pre k)
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
176+ uniq :: Lens' Memo Seen
180+ uniq _ [] ks = failure ("Unable to generate unique prefix: " % fid) k
181+ uniq seen (x:xs) ks = do
182+ m <- uses seen (Map.lookup x)
184+ Just ys | overlap ys ks
187+ seen %= Map.insertWith (<>) x ks
190+overlap :: (Eq a, Hashable a) => Set a -> Set a -> Bool
191+overlap 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
196+acronymPrefixes :: Id -> [CI Text]
197+acronymPrefixes (idToText -> n) = map CI.mk (xs ++ map suffix ys)
159− go (Obj i rs) = Map.traverseWithKey prop rs >> pure (Pre k)
199+ -- Take the next char
160− go s = pure (Pre k)
200+ suffix x = Text.snoc x c
202+ c | Text.length x >= 2 = Text.head (Text.drop 1 x)
203+ | otherwise = Text.head x
205+ xs = catMaybes [r1, r2, r3, r4, r5, r6]
206+ ys = catMaybes [r1, r2, r3, r4, r6]
213+ -- Full name if leq limit
214+ r1 | Text.length n <= limit = Just n
215+ | otherwise = Nothing
217+ -- VpcPeeringInfo -> VPI
220+ -- VpcPeeringInfo -> VPCPI
222+ | otherwise = Nothing
226+ -- SomeTestTType -> S
227+ r4 = Text.toUpper <$> safeHead n
162− prop n _ = do
229+ -- SomeTypes -> STS (retain pural)
163− let p = Pre (idFromText (acronym $ idToText k) <> n)
230+ r5 | Text.isSuffixOf "s" n = flip Text.snoc 's' <$> (r2 <|> r3)
164− prefixed %= Map.insert (k <> n) p
231+ | otherwise = Nothing
167− acronym = foldMap (Text.singleton . toLower . Text.head) . splitWords
233+ -- SomeTestTType -> Som
234+ r6 = Text.take limit <$> listToMaybe (splitWords a)
169loc :: String -> Id -> a -> a
236loc :: String -> Id -> a -> a
170loc _ _ = id -- trace (n ++ ": " ++ Text.unpack (refToText r))
237loc _ _ = id -- trace (n ++ ": " ++ Text.unpack (refToText r))