@@ -266,28 +266,65 @@ instance HasInfo (Schema a) where
266266 Ref _ r -> Ref i r
267267 Any {} -> Any i
268268
269−data Param = Param
269+data Param a = Param
270270 { _prmLocation :: !Location
271271 , _prmRepeated :: !Bool
272272 , _prmInfo :: Info
273− , _prmLiteral :: !Lit
274− }
273+ , _prmSchema :: a
274+ } deriving (Show, Functor, Foldable, Traversable)
275275
276−paramSchema :: Param -> Fix Schema
277−paramSchema p
278− | _prmRepeated p = Fix $ Arr (_prmInfo p) lit
279− | otherwise = lit
280− where
281− lit = Fix $ Lit (_prmInfo p) (_prmLiteral p)
276+-- paramSchema :: Param -> Fix Schema
277+-- paramSchema p
278+-- | _prmRepeated p = Fix $ Arr (_prmInfo p) lit
279+-- | otherwise = lit
280+-- where
281+-- lit = Fix $ Lit (_prmInfo p) (_prmLiteral p)
282282
283−instance FromJSON Param where
283+instance FromJSON a => FromJSON (Param a) where
284284 parseJSON = withObject "parameter" $ \o -> Param
285285 <$> o .: "location"
286286 <*> o .:? "repeated" .!= False
287287 <*> parseJSON (Object o)
288288 <*> parseJSON (Object o)
289289
290−deriveToJSON (js "") ''Param
290+data TType
291+ = TType Id
292+ | TLit Lit
293+ | TMaybe TType
294+ | TEither TType TType
295+ | TList TType
296+
297+data Derive
298+ = DEq
299+ | DOrd
300+ | DRead
301+ | DShow
302+ | DEnum
303+ | DNum
304+ | DIntegral
305+ | DReal
306+ | DMonoid
307+ | DIsString
308+ | DData
309+ | DTypeable
310+ | DGeneric
311+ deriving (Eq, Show)
312+
313+data Solved = Solved
314+ { _ident :: Id
315+ , _prefix :: Pre
316+ , _schema :: Schema Id
317+ , _type :: TType
318+ , _deriving :: [Derive]
319+ }
320+
321+instance HasInfo Solved where
322+ info = f . info
323+ where
324+ f = lens _schema (\s a -> s { _schema = a })
325+
326+monoid :: Solved -> Bool
327+monoid = elem DMonoid . _deriving
291328
292329data Syn a = Syn { syntax :: a }
293330
@@ -345,11 +382,12 @@ data Action = Action
345382 }
346383
347384instance ToJSON Action where
348− toJSON (Action n _ h a d) = object
349− [ "name" .= Syn n
350− , "help" .= h
351− , "alias" .= a
352− , "data" .= d
385+ toJSON Action {..} = object
386+ [ "name" .= Syn _actName
387+ , "ns" .= _actNS
388+ , "help" .= _actHelp
389+ , "alias" .= _actAlias
390+ , "type" .= _actData
353391 ]
354392
355393data API = API
@@ -359,25 +397,25 @@ data API = API
359397 }
360398
361399instance ToJSON API where
362− toJSON (API n d as) = object
363− [ "name" .= Syn n
364− , "decl" .= d
365− , "actions" .= Map.elems as
400+ toJSON API {..} = object
401+ [ "name" .= Syn _apiName
402+ , "decl" .= _apiDecl
403+ , "actions" .= Map.elems _apiActions
366404 ]
367405
368−data Method = Method
406+data Method a = Method
369407 { _mId :: Text
370408 , _mPath :: Text
371409 , _mMethod :: Text
372410 , _mDescription :: Maybe Help
373− , _mParameters :: Map Local Param
411+ , _mParameters :: Map Local (Param a)
374412 , _mParameterOrder :: [Local]
375413 , _mScopes :: [Text]
376414 , _mRequest :: Maybe Global
377415 , _mResponse :: Maybe Global
378− } deriving (Generic)
416+ } deriving (Generic, Functor, Foldable, Traversable)
379417
380−instance FromJSON Method where
418+instance FromJSON a => FromJSON (Method a) where
381419 parseJSON = withObject "method" $ \o -> Method
382420 <$> o .: "id"
383421 <*> o .: "path"
@@ -392,18 +430,17 @@ instance FromJSON Method where
392430 ref o k = Just <$> (o .: k >>= (.: "$ref"))
393431 <|> pure Nothing
394432
395−deriveToJSON (js "_m") ''Method
433+data Resource a
434+ = Top (Map Local (Resource a))
435+ | Sub (Map Local (Method a))
436+ deriving (Functor, Foldable, Traversable)
396437
397−data Resource
398− = Top (Map Local Resource)
399− | Sub (Map Local Method)
400−
401−instance FromJSON Resource where
438+instance FromJSON a => FromJSON (Resource a) where
402439 parseJSON = withObject "resource" $ \o ->
403440 Top <$> o .: "resources"
404441 <|> Sub <$> o .: "methods"
405442
406−data Service s r = Service
443+data Service s p r = Service
407444 { _svcLibrary :: Text
408445 , _svcTitle :: Text
409446 , _svcName :: Text
@@ -421,12 +458,12 @@ data Service s r = Service
421458 , _svcServicePath :: Text
422459 , _svcBatchPath :: Text
423460 , _svcAuth :: Maybe Object
424− , _svcParameters :: Map Local Param
425− , _svcSchemas :: Map Id s
461+ , _svcParameters :: p
462+ , _svcSchemas :: s
426463 , _svcApi :: r
427464 } deriving (Generic)
428465
429−instance FromJSON s => FromJSON (Service s Resource) where
466+instance (FromJSON s, FromJSON p, FromJSON r) => FromJSON (Service s p r) where
430467 parseJSON = withObject "service" $ \o -> Service
431468 <$> o .: "library"
432469 <*> o .: "title"
@@ -449,7 +486,7 @@ instance FromJSON s => FromJSON (Service s Resource) where
449486 <*> o .: "schemas"
450487 <*> parseJSON (Object o)
451488
452−instance ToJSON (Service Data API) where
489+instance (ToJSON s, ToJSON p) => ToJSON (Service s p API) where
453490 toJSON s = Object (x <> y)
454491 where
455492 Object x = genericToJSON (js "_svc") s
@@ -457,38 +494,46 @@ instance ToJSON (Service Data API) where
457494 [ "exposedModules" .= exposedModules s
458495 ]
459496
460−svcAbbrev :: Service s r -> Text
497+type Parsed = Service (Map Id (Fix Schema))
498+ (Map Local (Param (Fix Schema)))
499+ (Resource (Fix Schema))
500+
501+type Flattened = Service [Id] [Param Id] (Resource Id)
502+type Typed = Service [Solved] [Param Solved] (Resource Solved)
503+type Printed = Service [Data] () API
504+
505+svcAbbrev :: Service s p r -> Text
461506svcAbbrev = upperHead . renameAbbrev . _svcCanonicalName
462507
463−svcActions :: Service s API -> [NS]
464−svcActions = map _actNS . Map.elems . _apiActions . _svcApi
508+svcActions :: Service s p API -> [Action]
509+svcActions = Map.elems . _apiActions . _svcApi
465510
466−typeImports, prodImports, sumImports, actionImports :: Service s r -> [NS]
511+typeImports, prodImports, sumImports, actionImports :: Service s p r -> [NS]
467512typeImports s = sort ["Network.Google.Prelude", prodNS s, sumNS s]
468513prodImports s = sort ["Network.Google.Prelude", sumNS s]
469514sumImports _ = sort ["Network.Google.Prelude"]
470515actionImports = typeImports
471516
472−tocNS, typesNS, prodNS, sumNS :: Service s r -> NS
517+tocNS, typesNS, prodNS, sumNS :: Service s p r -> NS
473518tocNS s = NS $ "Network" : "Google" : Text.split (== '.') (_svcCanonicalName s)
474519typesNS = (<> "Types") . tocNS
475520prodNS = (<> "Product") . typesNS
476521sumNS = (<> "Sum") . typesNS
477522
478−actionNS :: Service s r -> Local -> Local -> NS
523+actionNS :: Service s p r -> Local -> Local -> NS
479524actionNS s x y = tocNS s <> NS [upperHead (local x), upperHead (local y)]
480525
481−exposedModules :: Service s API -> [NS]
482−exposedModules s = sort (tocNS s : typesNS s : svcActions s)
526+exposedModules :: Service s p API -> [NS]
527+exposedModules s = sort (tocNS s : typesNS s : map _actNS (svcActions s))
483528
484−otherModules :: Service s r -> [NS]
529+otherModules :: Service s p r -> [NS]
485530otherModules s = sort [prodNS s, sumNS s]
486531
487532data Library = Library
488533 { _libName :: Text
489534 , _libTitle :: Text
490535 , _libVersions :: Versions
491− , _libServices :: NonEmpty (Service Data API)
536+ , _libServices :: NonEmpty Printed
492537 }
493538
494539instance ToJSON Library where
@@ -503,7 +548,7 @@ instance ToJSON Library where
503548 , "otherModules" .= concatMap otherModules (NE.toList _libServices)
504549 ]
505550
506−merge :: Versions -> [Service Data API] -> [Library]
551+merge :: Versions -> [Printed] -> [Library]
507552merge v = map go . groupBy (on (==) _svcLibrary)
508553 where
509554 go [x] = mk x