on develop help sign in
Adding more verbose prefix debug logging
commit0a5dc667155191c844402ac215c2659ce3ef8a10 authorBrendan Hay <brendan.g.hay@gmail.com> dateSat 26 Sep 2015, 08:22 (11 years ago) parent0111d28
7 files changed+148−46
gen/src/Gen/AST.hs+2−1 gen/src/Gen/Solve.hs+14−2 gen/src/Gen/Syntax.hs+4 gen/src/Gen/Text.hs+79−13 gen/src/Gen/Types.hs+24−5 gen/src/Gen/Types/Id.hs+2−2 gen/template/toc.ede+23−23
gen/src/Gen/AST.hs+2−1
@@ -98,7 +98,8 @@ render svc = do
9898 res p = \case
9999 Parent rs -> traverse (parent p) (Map.toList rs) <&> concat
100100 Sub ms | Just x <- p
101− -> traverse (sub x) (Map.toList ms)
101+ -> traverse (sub x) (Map.toList ms)
102+ Sub ms -> traverse (sub "") (Map.toList ms)
102103
103104 parent :: Maybe Id -> (Local, Resource) -> AST [(Name, [Rendered])]
104105 parent p (n, r) = res (Just (pid p n)) r
gen/src/Gen/Solve.hs+14−2
@@ -177,7 +177,17 @@ prefix n = loc "prefix" n $ memo prefixed n typ
177177 -> [CI Text]
178178 -> Set (CI Text)
179179 -> AST Text
180− uniq _ [] ks = failure ("Unable to generate unique prefix: " % fid) n
180+ uniq seen [] ks = do
181+ s <- use seen
182+ let hs = acronymPrefixes n
183+ f x = sformat ("\n" % stext % " => " % shown)
184+ (CI.foldedCase x) (Map.lookup x s)
185+ throwError $
186+ format ("Error prefixing: " % fid %
187+ "\n Fields: " % shown %
188+ "\n Matches: " % stext)
189+ n (Set.toList ks) (foldMap f hs)
190+
181191 uniq seen (x:xs) ks = do
182192 m <- uses seen (Map.lookup x)
183193 case m of
@@ -191,7 +201,7 @@ overlap :: (Eq a, Hashable a) => Set a -> Set a -> Bool
191201overlap xs ys = not . Set.null $ Set.intersection xs ys
192202
193203acronymPrefixes :: Id -> [CI Text]
194−acronymPrefixes (idToText -> n) = map CI.mk (xs ++ map suffix ys)
204+acronymPrefixes (idToText -> n) = map CI.mk (xs ++ map suffix ys ++ zs)
195205 where
196206 -- Take the next char
197207 suffix x = Text.snoc x c
@@ -199,6 +209,8 @@ acronymPrefixes (idToText -> n) = map CI.mk (xs ++ map suffix ys)
199209 c | Text.length x >= 2 = Text.head (Text.drop 1 x)
200210 | otherwise = Text.head x
201211
212+ zs = zipWith (\n x -> Text.snoc x (head (show n))) ([1..] :: [Int]) xs
213+
202214 xs = catMaybes [r1, r2, r3, r4, r5, r6]
203215 ys = catMaybes [r1, r2, r3, r4, r6]
204216
gen/src/Gen/Syntax.hs+4
@@ -107,10 +107,14 @@ fieldUpdate p l s = FieldUpdate (UnQual (fname p l)) rhs
107107
108108 def x = \case
109109 Enum {} -> var (bname (_prefix s) x)
110+ Lit _ Bool -> lit (upperHead x)
110111 Lit _ Text -> str x
112+ Lit _ _ -> lit x
111113 --- FIXME: lift to AST
112114 s -> error $ "Unmatched fieldUpdate:\n" ++ show s
113115
116+ lit = var . name . Text.unpack
117+
114118lensSig :: Id -> Pre -> Local -> Solved -> Decl
115119lensSig n p l s = TypeSig noLoc [lname p l] $
116120 TyApp (TyApp (tycon "Lens'") (tycon n))
gen/src/Gen/Text.hs+79−13
@@ -56,6 +56,9 @@ renameAbbrev = mconcat
5656 . stripPrefix "Google "
5757 . stripSuffix " API"
5858
59+renameField :: Text -> Text
60+renameField = lowerHead . toCamel
61+
5962renameBranch :: Text -> Text
6063renameBranch = renameReserved . go
6164 where
@@ -67,24 +70,25 @@ renameBranch = renameReserved . go
6770
6871 dot x = x == '.'
6972
70− seperator x =
71− x == '\\'
72− || x == '/'
73− || x == '+'
74− || x == ' '
75− || x == '('
76− || x == ')'
77− || x == ':'
78− || x == '-'
79− || x == '_'
80− || x == '*'
81−
8273 component x
8374 | Text.length x <= 1 = x
8475 | isDigit (Text.last x) = Text.toUpper x
8576 | Text.all isUpper x = toPascal (Text.toLower x)
8677 | otherwise = toPascal x
8778
79+seperator :: Char -> Bool
80+seperator x =
81+ x == '\\'
82+ || x == '/'
83+ || x == '+'
84+ || x == ' '
85+ || x == '('
86+ || x == ')'
87+ || x == ':'
88+ || x == '-'
89+ || x == '_'
90+ || x == '*'
91+
8892renameReserved :: Text -> Text
8993renameReserved x
9094 | x `Set.member` xs = x <> "'"
@@ -176,4 +180,66 @@ upperAcronym x = Fold.foldl' (flip (uncurry RE.replaceAll)) x xs
176180 ]
177181
178182acronyms :: [(String, String)]
179−acronyms = [("NAT", "Nat")]
183+acronyms =
184+ [ ("ACL", "Acl")
185+ , ("AES", "Aes")
186+ , ("API", "Api")
187+ , ("APNS", "Apns")
188+ , ("ASN", "Asn")
189+ , ("BGP", "Bgp")
190+ , ("CIDR", "Cidr")
191+ , ("CORS", "Cors")
192+ , ("CSV", "Csv")
193+ , ("CPU", "Cpu")
194+ , ("DB", "Db")
195+ , ("DHCP", "Dhcp")
196+ , ("DNS", "Dns")
197+ , ("EBS", "Ebs")
198+ , ("EC2", "Ec2")
199+ , ("EIP", "Eip")
200+ , ("GCM", "Gcm")
201+ , ("HTML", "Html")
202+ , ("HTTPS", "Https")
203+ , ("HSM", "Hsm")
204+ , ("HVM", "Hvm")
205+ , ("IAM", "Iam")
206+ , ("ICMP", "Icmp")
207+ , ("IDN", "Idn")
208+ , ("IOPS", "Iops")
209+ , ("IP", "Ip")
210+ , ("JAR", "Jar")
211+ , ("JSON", "Json")
212+ , ("JVM", "Jvm")
213+ , ("KMS", "Kms")
214+ , ("MD5", "Md5")
215+ , ("MFA", "Mfa")
216+ , ("OK", "Ok")
217+ , ("OS", "Os")
218+ , ("PHP", "Php")
219+ , ("RAID", "Raid")
220+ , ("RAMDisk", "Ramdisk")
221+ , ("RDS", "Rds")
222+ , ("SGD", "Sgd")
223+ , ("SNI", "Sni")
224+ , ("SNS", "Sns")
225+ , ("SRIOV", "Sriov")
226+ , ("SSH", "Ssh")
227+ , ("SSL", "Ssl")
228+ , ("SSO", "Sso")
229+ , ("SVN", "Svn")
230+ , ("TDE", "Tde")
231+ , ("TCP", "Tcp")
232+ , ("TGZ", "Tgz")
233+ , ("TLS", "Tls")
234+ , ("URI", "Uri")
235+ , ("URL", "Url")
236+ , ("VGW", "Vgw")
237+ , ("VHD", "Vhd")
238+ , ("VIP", "Vip")
239+ , ("VLAN", "Vlan")
240+ , ("VMDK", "Vmdk")
241+ , ("VPC", "Vpc")
242+ , ("VPN", "Vpn")
243+ , ("XML", "Xml")
244+ , ("ID", "Id")
245+ ]
gen/src/Gen/Types.hs+24−5
@@ -369,8 +369,8 @@ data Resource
369369
370370instance FromJSON Resource where
371371 parseJSON = withObject "resource" $ \o ->
372− Sub <$> o .: "methods"
373− <|> Parent <$> o .: "resources"
372+ Parent <$> o .: "resources"
373+ <|> Sub <$> o .: "methods"
374374
375375data Service s r = Service
376376 { _svcLibrary :: Text
@@ -395,9 +395,28 @@ data Service s r = Service
395395 , _svcResources :: r
396396 } deriving (Generic)
397397
398−Have to manually write the fromjson instance due to resources?
399−
400−deriveFromJSON (js "_svc") ''Service
398+instance FromJSON s => FromJSON (Service s Resource) where
399+ parseJSON = withObject "service" $ \o -> Service
400+ <$> o .: "library"
401+ <*> o .: "title"
402+ <*> o .: "name"
403+ <*> o .:? "canonicalName"
404+ <*> o .: "description"
405+ <*> o .:? "revision"
406+ <*> o .: "version"
407+ <*> o .: "ownerName"
408+ <*> o .: "ownerDomain"
409+ <*> o .:? "packagePath"
410+ <*> o .: "documentationLink"
411+ <*> o .: "protocol"
412+ <*> o .: "baseUrl"
413+ <*> o .: "rootUrl"
414+ <*> o .: "servicePath"
415+ <*> o .: "batchPath"
416+ <*> o .:? "auth"
417+ <*> o .: "parameters"
418+ <*> o .: "schemas"
419+ <*> parseJSON (Object o)
401420
402421instance ToJSON (Service Data API) where
403422 toJSON s = Object (x <> y)
gen/src/Gen/Types/Id.hs+2−2
@@ -79,8 +79,8 @@ bname (Pre p) = name
7979 . renameBranch
8080
8181fname, lname, pname :: Pre -> Local -> Name
82−fname = pre (Text.cons '_' . lowerHead)
83−lname = pre lowerHead
82+fname = pre (Text.cons '_' . renameField)
83+lname = pre renameField
8484pname = pre (flip Text.snoc '_' . Text.cons 'p' . upperHead)
8585
8686pre :: (Text -> Text) -> Pre -> Local -> Name
gen/template/toc.ede+23−23
@@ -50,26 +50,26 @@ TODO
5050-}
5151
5252type {{ api }} = ()
53−--
54−-- {{ api | lowerHead }} :: Proxy {{ api }}
55−-- {{ api | lowerHead }} = Proxy
56−--
57−-- type {{ api }} =
58−-- {% for resource in resources %}
59−-- {% if resource.first %} {% else %}:<|>{% endif %} {{ resource.value.name }}
60−-- {% endfor %}
61−-- {% for resource in resources %}
62−--
63−-- type {{ resource.value.name }} =
64−-- {% for method in resource.value.methods %}
65−-- {% if !method.first %}:<|>{% else %} {% endif %} {{ method.key }}
66−-- {% endfor %}
67−-- {% for method in resource.value.methods %}
68−--
69−-- type {{ method.key }} =
70−-- {% for type in method.value %}
71−-- {% if !type.first %}:>{% else %} {% endif %} {{ type.value }}
72−-- {% endfor %}
73−-- {% endfor %}
74−-- {% endfor %}
75−--
53+
54+{{ api | lowerHead }} :: Proxy {{ api }}
55+{{ api | lowerHead }} = Proxy
56+
57+{# type {{ api }} = #}
58+{# {% for resource in resources %} #}
59+{# {% if resource.first %} {% else %}:<|>{% endif %} {{ resource.value.name }} #}
60+{# {% endfor %} #}
61+{# {% for resource in resources %} #}
62+
63+{# type {{ resource.value.name }} = #}
64+{# {% for method in resource.value.methods %} #}
65+{# {% if !method.first %}:<|>{% else %} {% endif %} {{ method.key }} #}
66+{# {% endfor %} #}
67+{# {% for method in resource.value.methods %} #}
68+
69+{# type {{ method.key }} = #}
70+{# {% for type in method.value %} #}
71+{# {% if !type.first %}:>{% else %} {% endif %} {{ type.value }} #}
72+{# {% endfor %} #}
73+{# {% endfor %} #}
74+{# {% endfor %} #}
75+