on develop help sign in
Casing enum branches
commit46ba06535dbc472dba4c8c29a077aa3df2b30e25 authorBrendan Hay <brendan.g.hay@gmail.com> dateFri 25 Sep 2015, 17:30 (11 years ago) parentd430777
5 files changed+87−153
gen/gozen-gen.cabal−1 gen/src/Gen/Prefix.hs−146 gen/src/Gen/Solve.hs+4−2 gen/src/Gen/Text.hs+82−3 gen/src/Gen/Types/Id.hs+1−1
gen/gozen-gen.cabal−1
@@ -26,7 +26,6 @@ executable gozen-gen
2626 , Gen.IO
2727 , Gen.JSON
2828 , Gen.Orphans
29− , Gen.Prefix
3029 , Gen.Solve
3130 , Gen.Syntax
3231 , Gen.Text
gen/src/Gen/Prefix.hsdeleted−146
@@ -1,146 +0,0 @@
1−{-# LANGUAGE FlexibleContexts #-}
2−{-# LANGUAGE LambdaCase #-}
3−{-# LANGUAGE OverloadedStrings #-}
4−{-# LANGUAGE RankNTypes #-}
5−{-# LANGUAGE TemplateHaskell #-}
6−{-# LANGUAGE TypeOperators #-}
7−
8−-- Module : Gen.Prefix
9−-- Copyright : (c) 2015 Brendan Hay
10−-- License : Mozilla Public License, v. 2.0.
11−-- Maintainer : Brendan Hay <brendan.g.hay@gmail.com>
12−-- Stability : provisional
13−-- Portability : non-portable (GHC extensions)
14−
15−module Gen.Prefix where
16−
17−-- type Seen = Map (CI Text) (Set (CI Text))
18−
19−-- data Env = Env
20−-- { _memo :: Map Id (Maybe Text)
21−-- , _branches :: Seen
22−-- , _fields :: Seen
23−-- }
24−
25−-- makeLenses ''Env
26−
27−-- type MemoP = StateT Env (Either Error)
28−
29−-- prefixes :: Map Id (Shape Related) -> Either Error (Map Id (Shape Prefixed))
30−-- prefixes ss = evalStateT (traverse assignPrefix ss) env
31−-- where
32−-- env = Env mempty mempty (smartCtors ss)
33−
34−-- -- | Record projected smart constructors in set of seen field names.
35−-- smartCtors :: Map Id (Shape a) -> Seen
36−-- smartCtors = Map.fromListWith (<>) . mapMaybe go . Map.toList
37−-- where
38−-- go :: (Id, Shape a) -> Maybe (CI Text, Set (CI Text))
39−-- go (s, _ :< Struct {}) = Just (k, Set.singleton v)
40−-- where
41−-- n = smartCtorId s
42−-- k = CI.mk (Text.takeWhile isLower n)
43−-- v = CI.mk (dropLower n)
44−-- go _ = Nothing
45−
46−-- assignPrefix :: Shape Related -> MemoP (Shape Prefixed)
47−-- assignPrefix = annotate Prefixed memo go
48−-- where
49−-- go :: (HasId a, HasRelation a) => Shape a -> MemoP (Maybe Text)
50−-- go (x :< s) =
51−-- let n = typeId (identifier x)
52−-- r = x ^. relation
53−-- in case s of
54−-- Enum _ vs -> Just <$> do
55−-- let hs = mempty : acronymPrefixes r n
56−-- ks = keys vs
57−-- unique r branches n hs ks
58−
59−-- Struct st -> Just <$> do
60−-- let hs = acronymPrefixes r n
61−-- ks = keys (st ^. members)
62−-- unique r fields n hs ks
63−
64−-- _ -> return Nothing
65−
66−-- unique :: Relation
67−-- -> Lens' Env Seen
68−-- -> Text
69−-- -> [CI Text]
70−-- -> Set (CI Text)
71−-- -> MemoP Text
72−-- unique r seen n [] ks = do
73−-- s <- use seen
74−-- let hs = acronymPrefixes r n
75−-- f x = sformat ("\n" % soriginal % " => " % shown) x (Map.lookup x s)
76−-- throwError $
77−-- format ("Error prefixing: " % stext %
78−-- ", fields: " % shown %
79−-- scomma)
80−-- n (Set.toList ks) (map f hs)
81−
82−-- unique r seen n (h:hs) ks = do
83−-- m <- uses seen (Map.lookup h)
84−-- -- Find if this particular naming heuristic is used already, and if
85−-- -- it is, then is there overlap with this set of ks?
86−-- case m of
87−-- Just ys | overlap ys ks
88−-- -> unique r seen n hs ks
89−-- _ -> do
90−-- seen %= Map.insertWith (<>) h ks
91−-- return (CI.original h)
92−
93−-- overlap :: (Eq a, Hashable a) => Set a -> Set a -> Bool
94−-- overlap xs ys = not . Set.null $ Set.intersection xs ys
95−
96−-- keys :: Map Id a -> Set (CI Text)
97−-- keys = Set.fromList . map (CI.mk . typeId) . Map.keys
98−
99−-- | Acronym preference list.
100−--
101−-- Prefixing occurs as follows:
102−-- * Requests - prefer acronyms
103−-- * Responses - prefer acronyms, and append 'rs'
104−-- * Shapes - prefer single letters, then acronyms
105−-- acronymPrefixes :: Id -> Id -- Text]
106−-- acronymPrefixes n =
107−-- where
108−-- -- rs = map (<> "rs") ss
109−-- ss = xs ++ map suffix ys
110−
111−-- -- Take the next char
112−-- suffix x = Text.snoc x c
113−-- where
114−-- c | Text.length x >= 2 = Text.head (Text.drop 1 x)
115−-- | otherwise = Text.head x
116−
117−-- xs = catMaybes [r1, r2, r3, r4, r5, r6]
118−-- ys = catMaybes [r1, r2, r3, r4, r6]
119−
120−-- a = camelAcronym n
121−-- a' = upperAcronym n
122−
123−-- limit = 3
124−
125−-- -- Full name if leq limit
126−-- r1 | Text.length n <= limit = Just n
127−-- | otherwise = Nothing
128−
129−-- -- VpcPeeringInfo -> VPI
130−-- r2 = toAcronym a
131−
132−-- -- VpcPeeringInfo -> VPCPI
133−-- r3 | x /= r2 = x
134−-- | otherwise = Nothing
135−-- where
136−-- x = toAcronym a'
137−
138−-- -- SomeTestTType -> S
139−-- r4 = Text.toUpper <$> safeHead n
140−
141−-- -- SomeTypes -> STS (retain pural)
142−-- r5 | Text.isSuffixOf "s" n = flip Text.snoc 's' <$> (r2 <|> r3)
143−-- | otherwise = Nothing
144−
145−-- -- SomeTestTType -> Som
146−-- r6 = Text.take limit <$> listToMaybe (splitWords a)
gen/src/Gen/Solve.hs+4−2
@@ -158,10 +158,12 @@ prefix :: Id -> AST Pre
158158prefix n = memo prefixed n typ
159159 where
160160 typ = \case
161− Obj i rs -> field rs
162− Enum i vs ds -> pure mempty
161+ Obj i rs -> field rs
162+ Enum i vs _ -> branch vs
163163 _ -> pure mempty
164164
165+ branch vs = pure (Pre "")
166+
165167 field rs = do
166168 let ls = Map.keys rs
167169 ks = Set.fromList (map (CI.mk . local) ls)
gen/src/Gen/Text.hs+82−3
@@ -14,7 +14,6 @@
1414module Gen.Text where
1515
1616import Control.Error
17−import Control.Monad
1817import Data.Bifunctor
1918import Data.Char
2019import qualified Data.Foldable as Fold
@@ -57,6 +56,35 @@ renameAbbrev = mconcat
5756 . stripPrefix "Google "
5857 . stripSuffix " API"
5958
59+renameBranch :: Text -> Text
60+renameBranch = renameReserved . go
61+ where
62+ go x | Text.length x <= 2 = Text.toUpper x
63+ | otherwise = upperAcronym . cat $ split x
64+
65+ cat = Fold.foldMap (Text.intercalate "_" . map component . Text.split dot)
66+ split = Text.split seperator
67+
68+ dot x = x == '.'
69+
70+ seperator x =
71+ x == '\\'
72+ || x == '/'
73+ || x == '+'
74+ || x == ' '
75+ || x == '('
76+ || x == ')'
77+ || x == ':'
78+ || x == '-'
79+ || x == '_'
80+ || x == '*'
81+
82+ component x
83+ | Text.length x <= 1 = x
84+ | isDigit (Text.last x) = Text.toUpper x
85+ | Text.all isUpper x = toPascal (Text.toLower x)
86+ | otherwise = toPascal x
87+
6088renameReserved :: Text -> Text
6189renameReserved x
6290 | x `Set.member` xs = x <> "'"
@@ -94,7 +122,58 @@ upperAcronym :: Text -> Text
94122upperAcronym x = Fold.foldl' (flip (uncurry RE.replaceAll)) x xs
95123 where
96124 xs :: [(Regex, Replace)]
97− xs = []
125+ xs = [ ("Acl", "ACL")
126+ , ("Adm([^i]|$)", "ADM$1")
127+ , ("Aes", "AES")
128+ , ("Api", "API")
129+ , ("Apns", "APNS")
130+ , ("Asn", "ASN")
131+ , ("Bgp", "BGP")
132+ , ("Cidr", "CIDR")
133+ , ("Cors", "CORS")
134+ , ("Csv", "CSV")
135+ , ("Cpu", "CPU")
136+ , ("Db", "DB")
137+ , ("Dhcp", "DHCP")
138+ , ("Dns", "DNS")
139+ , ("Gcm", "GCM")
140+ , ("Html", "HTML")
141+ , ("Https", "HTTPS")
142+ , ("Http([^s]|$)", "HTTP$1")
143+ , ("Hsm", "HSM")
144+ , ("Hvm", "HVM")
145+ , ("Iam", "IAM")
146+ , ("Icmp", "ICMP")
147+ , ("Ip", "IP")
148+ , ("Json", "JSON")
149+ , ("Jvm", "JVM")
150+ , ("Mac([^h]|$)", "MAC$1")
151+ , ("Md5", "MD5")
152+ , ("Raid", "RAID")
153+ , ("Ramdisk", "RAMDisk")
154+ , ("Sgd", "SGD")
155+ , ("Sni", "SNI")
156+ , ("Ssh", "SSH")
157+ , ("Ssl", "SSL")
158+ , ("Svn", "SVN")
159+ , ("Tar([^g]|$)", "TAR$1")
160+ , ("Tcp", "TCP")
161+ , ("Tgz", "TGZ")
162+ , ("Tls", "TLS")
163+ , ("Uri", "URI")
164+ , ("Url", "URL")
165+ , ("Vip", "VIP")
166+ , ("Vlan", "VLAN")
167+ , ("Vm([^d]|$)", "VM$1")
168+ , ("Vpc", "VPC")
169+ , ("Vpn", "VPN")
170+ , ("Xml", "XML")
171+ , ("ID", "Id")
172+ , ("Eq([^u]|$)", "EQ$1")
173+ , ("Lt$", "LT")
174+ , ("Gt$", "GT")
175+ , ("Nat", "NAT")
176+ ]
98177
99178acronyms :: [(String, String)]
100−acronyms = []
179+acronyms = [("NAT", "Nat")]
gen/src/Gen/Types/Id.hs+1−1
@@ -77,7 +77,7 @@ bname :: Pre -> Text -> Name
7777bname (Pre p) = name
7878 . Text.unpack
7979 . mappend (Text.toUpper p)
80− . upperHead
80+ . renameBranch
8181
8282fname, lname, pname :: Pre -> Local -> Name
8383fname = pre (Text.cons '_' . lowerHead)