@@ -1,7 +1,9 @@
1{-# LANGUAGE DataKinds #-}
1{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE DeriveGeneric #-}
2{-# LANGUAGE KindSignatures #-}
3{-# LANGUAGE KindSignatures #-}
3{-# LANGUAGE LambdaCase #-}
4{-# LANGUAGE LambdaCase #-}
4{-# LANGUAGE OverloadedStrings #-}
5{-# LANGUAGE OverloadedStrings #-}
6+{-# LANGUAGE RecordWildCards #-}
5{-# LANGUAGE TemplateHaskell #-}
7{-# LANGUAGE TemplateHaskell #-}
7-- Module : Gen.Types
9-- Module : Gen.Types
@@ -11,9 +13,11 @@
11-- Stability : provisional
13-- Stability : provisional
12-- Portability : non-portable (GHC extensions)
14-- Portability : non-portable (GHC extensions)
14−module Gen.Types where
16+module Gen.Types
18+ , module Gen.Types.NS
17import Data.Aeson
21import Data.Aeson
18import Data.Aeson.TH
22import Data.Aeson.TH
19import qualified Data.Attoparsec.Text as A
23import qualified Data.Attoparsec.Text as A
@@ -28,16 +32,20 @@ import Data.Semigroup
28import Data.Text (Text)
32import Data.Text (Text)
29import qualified Data.Text as Text
33import qualified Data.Text as Text
30import qualified Data.Text.Lazy as LText
34import qualified Data.Text.Lazy as LText
35+import qualified Data.Text.Lazy.Builder as Build
31import Data.Text.Manipulate
36import Data.Text.Manipulate
32import qualified Filesystem.Path.CurrentOS as Path
37import qualified Filesystem.Path.CurrentOS as Path
33−import Gen.JSON
38+import Formatting
34import Gen.Types.NS
40import Gen.Types.NS
35import GHC.TypeLits
42import GHC.TypeLits
36import Text.EDE (Template)
43import Text.EDE (Template)
38−type Map = Map.HashMap
45+type Map = Map.HashMap
39−type Error = LText.Text
46+type Error = LText.Text
47+type Rendered = LText.Text
40−type Path = Path.FilePath
48+type Path = Path.FilePath
42toTextIgnore :: Path -> Text
50toTextIgnore :: Path -> Text
43toTextIgnore = either id id . Path.toText
51toTextIgnore = either id id . Path.toText
@@ -53,6 +61,12 @@ data Templates = Templates
53newtype Version (v :: Symbol) = Version Text
61newtype Version (v :: Symbol) = Version Text
54 deriving (Eq, Show)
62 deriving (Eq, Show)
64+instance ToJSON (Version v) where
65+ toJSON (Version v) = toJSON v
67+semver :: Format a (Version v -> a)
68+semver = later (\(Version v) -> Build.fromText v)
56type LibraryVer = Version "library"
70type LibraryVer = Version "library"
57type ClientVer = Version "client"
71type ClientVer = Version "client"
58type CoreVer = Version "core"
72type CoreVer = Version "core"
@@ -63,8 +77,6 @@ data Versions = Versions
63 , _coreVersion :: CoreVer
77 , _coreVersion :: CoreVer
64 } deriving (Show)
78 } deriving (Show)
66−-- makeClassy ''Versions
68-- FIXME: need a more comprehensive 'vm_alpha' vs 'vm1.1' etc check.
80-- FIXME: need a more comprehensive 'vm_alpha' vs 'vm1.1' etc check.
69data Spec = Spec
81data Spec = Spec
70 { _specName :: Text
82 { _specName :: Text
@@ -102,8 +114,12 @@ instance FromJSON Protocol where
102 "rest" -> pure REST
114 "rest" -> pure REST
103 e -> fail $ "Unable to parse protocol from " ++ Text.unpack e
115 e -> fail $ "Unable to parse protocol from " ++ Text.unpack e
117+instance ToJSON Protocol where
118+ toJSON = toJSON . map toLower . show
105data Service = Service
120data Service = Service
121+ { _svcLibrary :: Text
106− { _svcTitle :: Text
122+ , _svcTitle :: Text
107 , _svcName :: Text
123 , _svcName :: Text
108 , _svcCanonicalName :: Maybe Text
124 , _svcCanonicalName :: Maybe Text
109 , _svcDescription :: Text
125 , _svcDescription :: Text
@@ -121,30 +137,57 @@ data Service = Service
121 , _svcParameters :: Object
137 , _svcParameters :: Object
122 , _svcAuth :: Maybe Object
138 , _svcAuth :: Maybe Object
123 , _svcSchemas :: Object
139 , _svcSchemas :: Object
124− } deriving (Show)
140+ } deriving (Show, Generic)
126deriveFromJSON (js "_svc") ''Service
142deriveFromJSON (js "_svc") ''Service
144+instance ToJSON Service where
145+ toJSON s = Object (x <> y)
147+ Object x = genericToJSON (js "_svc") s
149+ [ "exposedModules" .= exposedModules s
152+svcAbbrev :: Service -> Text
153+svcAbbrev = renameAbbrev . _svcTitle
155+typeImports :: Service -> [NS]
156+typeImports _ = ["Network.Google.Prelude"]
158+tocNS, typesNS :: Service -> NS
159+tocNS s = NS ["Network", "Google", svcAbbrev s]
160+typesNS = (<> "Types") . tocNS
162+exposedModules :: Service -> [NS]
163+exposedModules s = [tocNS s, typesNS s]
128data Library = Library
165data Library = Library
129− { _libName :: Text
166+ { _libName :: Text
130− , _libTitle :: Text
167+ , _libTitle :: Text
131− , _libDescriptions :: [Text]
132− , _libNamespace :: NS
133− , _libVersions :: Versions
168+ , _libVersions :: Versions
134− , _libServices :: NonEmpty Service
169+ , _libServices :: NonEmpty Service
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)
137mergeLibraries :: Versions -> [Service] -> [Library]
182mergeLibraries :: Versions -> [Service] -> [Library]
138−mergeLibraries v = map go . groupBy (on (==) _svcName)
183+mergeLibraries v = map go . groupBy (on (==) _svcLibrary)
140 go [x] = mk x
185 go [x] = mk x
141 go (x:xs) = (mk x) { _libServices = x :| xs }
186 go (x:xs) = (mk x) { _libServices = x :| xs }
143 mk x = Library
188 mk x = Library
144− { _libName = "gozen-" <> Text.toLower (_svcName x)
189+ { _libName = _svcLibrary x
145− , _libTitle = _svcTitle x
190+ , _libTitle = renameTitle (_svcTitle x)
146− , _libDescriptions = []
147− , _libNamespace = "Network.Google" <> mkNS (toPascal (_svcName x))
148− , _libVersions = v
191+ , _libVersions = v
149− , _libServices = x :| []
192+ , _libServices = x :| []