@@ -1,7 +1,9 @@
11{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE DeriveGeneric #-}
23{-# LANGUAGE KindSignatures #-}
34{-# LANGUAGE LambdaCase #-}
45{-# LANGUAGE OverloadedStrings #-}
6+{-# LANGUAGE RecordWildCards #-}
57{-# LANGUAGE TemplateHaskell #-}
68
79-- Module : Gen.Types
@@ -11,9 +13,11 @@
1113-- Stability : provisional
1214-- Portability : non-portable (GHC extensions)
1315
14−module Gen.Types where
16+module Gen.Types
17+ ( module Gen.Types
18+ , module Gen.Types.NS
19+ ) where
1520
16−--import Control.Lens
1721import Data.Aeson
1822import Data.Aeson.TH
1923import qualified Data.Attoparsec.Text as A
@@ -28,16 +32,20 @@ import Data.Semigroup
2832import Data.Text (Text)
2933import qualified Data.Text as Text
3034import qualified Data.Text.Lazy as LText
35+import qualified Data.Text.Lazy.Builder as Build
3136import Data.Text.Manipulate
3237import qualified Filesystem.Path.CurrentOS as Path
33−import Gen.JSON
38+import Formatting
39+import Gen.Text
3440import Gen.Types.NS
41+import GHC.Generics
3542import GHC.TypeLits
3643import Text.EDE (Template)
3744
38−type Map = Map.HashMap
39−type Error = LText.Text
40−type Path = Path.FilePath
45+type Map = Map.HashMap
46+type Error = LText.Text
47+type Rendered = LText.Text
48+type Path = Path.FilePath
4149
4250toTextIgnore :: Path -> Text
4351toTextIgnore = either id id . Path.toText
@@ -53,6 +61,12 @@ data Templates = Templates
5361newtype Version (v :: Symbol) = Version Text
5462 deriving (Eq, Show)
5563
64+instance ToJSON (Version v) where
65+ toJSON (Version v) = toJSON v
66+
67+semver :: Format a (Version v -> a)
68+semver = later (\(Version v) -> Build.fromText v)
69+
5670type LibraryVer = Version "library"
5771type ClientVer = Version "client"
5872type CoreVer = Version "core"
@@ -63,8 +77,6 @@ data Versions = Versions
6377 , _coreVersion :: CoreVer
6478 } deriving (Show)
6579
66−-- makeClassy ''Versions
67−
6880-- FIXME: need a more comprehensive 'vm_alpha' vs 'vm1.1' etc check.
6981data Spec = Spec
7082 { _specName :: Text
@@ -102,8 +114,12 @@ instance FromJSON Protocol where
102114 "rest" -> pure REST
103115 e -> fail $ "Unable to parse protocol from " ++ Text.unpack e
104116
117+instance ToJSON Protocol where
118+ toJSON = toJSON . map toLower . show
119+
105120data Service = Service
106− { _svcTitle :: Text
121+ { _svcLibrary :: Text
122+ , _svcTitle :: Text
107123 , _svcName :: Text
108124 , _svcCanonicalName :: Maybe Text
109125 , _svcDescription :: Text
@@ -121,30 +137,57 @@ data Service = Service
121137 , _svcParameters :: Object
122138 , _svcAuth :: Maybe Object
123139 , _svcSchemas :: Object
124− } deriving (Show)
140+ } deriving (Show, Generic)
125141
126142deriveFromJSON (js "_svc") ''Service
127143
144+instance ToJSON Service where
145+ toJSON s = Object (x <> y)
146+ where
147+ Object x = genericToJSON (js "_svc") s
148+ Object y = object
149+ [ "exposedModules" .= exposedModules s
150+ ]
151+
152+svcAbbrev :: Service -> Text
153+svcAbbrev = renameAbbrev . _svcTitle
154+
155+typeImports :: Service -> [NS]
156+typeImports _ = ["Network.Google.Prelude"]
157+
158+tocNS, typesNS :: Service -> NS
159+tocNS s = NS ["Network", "Google", svcAbbrev s]
160+typesNS = (<> "Types") . tocNS
161+
162+exposedModules :: Service -> [NS]
163+exposedModules s = [tocNS s, typesNS s]
164+
128165data Library = Library
129− { _libName :: Text
130− , _libTitle :: Text
131− , _libDescriptions :: [Text]
132− , _libNamespace :: NS
133− , _libVersions :: Versions
134− , _libServices :: NonEmpty Service
166+ { _libName :: Text
167+ , _libTitle :: Text
168+ , _libVersions :: Versions
169+ , _libServices :: NonEmpty Service
135170 }
136171
172+instance ToJSON Library where
173+ toJSON Library{..} = object
174+ [ "libraryName" .= _libName
175+ , "libraryTitle" .= _libTitle
176+ , "libraryVersion" .= _libraryVersion _libVersions
177+ , "coreVersion" .= _coreVersion _libVersions
178+ , "clientVersion" .= _clientVersion _libVersions
179+ , "exposedModules" .= concatMap exposedModules (NE.toList _libServices)
180+ ]
181+
137182mergeLibraries :: Versions -> [Service] -> [Library]
138−mergeLibraries v = map go . groupBy (on (==) _svcName)
183+mergeLibraries v = map go . groupBy (on (==) _svcLibrary)
139184 where
140185 go [x] = mk x
141186 go (x:xs) = (mk x) { _libServices = x :| xs }
142187
143188 mk x = Library
144− { _libName = "gozen-" <> Text.toLower (_svcName x)
145− , _libTitle = _svcTitle x
146− , _libDescriptions = []
147− , _libNamespace = "Network.Google" <> mkNS (toPascal (_svcName x))
148− , _libVersions = v
149− , _libServices = x :| []
189+ { _libName = _svcLibrary x
190+ , _libTitle = renameTitle (_svcTitle x)
191+ , _libVersions = v
192+ , _libServices = x :| []
150193 }