@@ -24,36 +24,40 @@
2424
2525module Gen.Types
2626 ( module Gen.Types
27+ , module Gen.Types.Help
28+ , module Gen.Types.Id
2729 , module Gen.Types.Map
2830 , module Gen.Types.NS
29− , module Gen.Types.Id
3031 ) where
3132
3233import Control.Applicative
33−import Control.Lens hiding ((.=))
34−import Data.Aeson hiding (Bool, String)
34+import Control.Lens hiding ((.=))
35+import Data.Aeson hiding (Bool, String)
3536import Data.Aeson.TH
3637import Data.Char
37−import Data.Function (on)
38−import Data.List.NonEmpty (NonEmpty (..))
39−import qualified Data.List.NonEmpty as NE
38+import Data.Function (on)
39+import Data.List.NonEmpty (NonEmpty (..))
40+import qualified Data.List.NonEmpty as NE
4041import Data.Maybe
4142import Data.Ord
42−import Data.Semigroup ((<>))
43−import Data.Text (Text)
44−import qualified Data.Text as Text
45−import qualified Data.Text.Lazy as LText
46−import qualified Data.Text.Lazy.Builder as Build
47−import qualified Data.Text.Read as Read
48−import qualified Filesystem.Path.CurrentOS as Path
43+import Data.Semigroup ((<>))
44+import Data.Text (Text)
45+import qualified Data.Text as Text
46+import qualified Data.Text.Lazy as LText
47+import qualified Data.Text.Lazy.Builder as Build
48+import qualified Data.Text.Read as Read
49+import qualified Filesystem.Path.CurrentOS as Path
4950import Formatting
5051import Gen.Text
52+import Gen.Types.Help
5153import Gen.Types.Id
5254import Gen.Types.Map
5355import Gen.Types.NS
5456import GHC.Generics
5557import GHC.TypeLits
56−import Text.EDE (Template)
58+import Language.Haskell.Exts (Name)
59+import Language.Haskell.Exts.Pretty (Pretty, prettyPrint)
60+import Text.EDE (Template)
5761
5862default (Text)
5963
@@ -141,7 +145,7 @@ data Location
141145deriveJSON (js "") ''Location
142146
143147data Info = Info
144− { _description :: Maybe Text
148+ { _description :: Maybe Help
145149 , _defaulted :: Maybe Text
146150 , _required :: !Bool
147151 , _location :: Maybe Location
@@ -164,8 +168,13 @@ parameter s = s ^. required && isNothing (s ^. defaulted)
164168data Lit
165169 = Text
166170 | Bool
167− | Int
168− | Long
171+ | Float
172+ | Double
173+ | Byte
174+ | UInt32
175+ | UInt64
176+ | Int32
177+ | Int64
169178 | Nat
170179 | Time
171180 deriving (Eq, Show)
@@ -178,8 +187,13 @@ instance FromJSON Lit where
178187 "string" -> pure Text
179188 "boolean" -> pure Bool
180189 -- Formats
181− "uint64" -> pure (nat n Long)
182− "int32" -> pure (nat n Int)
190+ "float" -> pure Float
191+ "double" -> pure Double
192+ "byte" -> pure (nat n Byte)
193+ "uint32" -> pure (nat n UInt32)
194+ "uint64" -> pure (nat n UInt64)
195+ "int32" -> pure (nat n Int32)
196+ "int64" -> pure (nat n Int64)
183197 "date-time" -> pure Time
184198 e -> fail $
185199 "Unable to parse Lit from " ++ Text.unpack e
@@ -213,8 +227,8 @@ instance FromJSON (Fix Schema) where
213227 Fix <$> (ref i o <|> schema i o)
214228 where
215229 schema i o = o .: "type" >>= \case
216− "object" -> Obj i <$> o .: "properties"
217− "array" -> Arr i <$> o .: "items"
230+ "object" -> Obj i <$> o .:? "properties" .!= mempty
231+ "array" -> Arr i <$> o .: "items"
218232 "any" -> pure (Any i)
219233 _ -> enm i o <|> Lit i <$> parseJSON (Object o)
220234
@@ -250,30 +264,39 @@ instance FromJSON Param where
250264
251265deriveToJSON (js "") ''Param
252266
253−data Fun = Fun' Text Rendered Rendered
267+data Syn a = Syn { syntax :: a }
268+
269+instance Pretty a => ToJSON (Syn a) where
270+ toJSON = toJSON . prettyPrint . syntax
271+
272+data Fun = Fun' Name (Maybe Help) Rendered Rendered
254273
255274instance ToJSON Fun where
256− toJSON (Fun' h s d) = object
257− [ "help" .= h
275+ toJSON (Fun' n h s d) = object
276+ [ "name" .= Syn n
277+ , "help" .= h
258278 , "sig" .= s
259279 , "decl" .= d
260280 ]
261281
262282data Data
263− = Sum Text Rendered
264− | Prod Rendered Fun [Fun]
283+ = Sum Name (Maybe Help) Rendered
284+ | Prod Name (Maybe Help) Rendered Fun [Fun]
265285
266286instance ToJSON Data where
267287 toJSON = \case
268− Sum h d -> object
269− [ "type" .= "sum"
288+ Sum n h d -> object
289+ [ "name" .= Syn n
290+ , "type" .= "sum"
270291 , "decl" .= d
271292 , "help" .= h
272293 ]
273294
274− Prod d c ls -> object
275− [ "type" .= "prod"
295+ Prod n h d c ls -> object
296+ [ "name" .= Syn n
297+ , "type" .= "prod"
276298 , "decl" .= d
299+ , "help" .= h
277300 , "ctor" .= c
278301 , "lenses" .= ls
279302 ]