@@ -26,32 +26,29 @@ module Gen.Types
26 ( module Gen.Types
26 ( module Gen.Types
27 , module Gen.Types.Map
27 , module Gen.Types.Map
28 , module Gen.Types.NS
28 , module Gen.Types.NS
29+ , module Gen.Types.Id
31import Control.Applicative
32import Control.Applicative
33+import Control.Lens hiding ((.=))
32import Data.Aeson hiding (Bool, String)
34import Data.Aeson hiding (Bool, String)
33import Data.Aeson.TH
35import Data.Aeson.TH
34−import qualified Data.Attoparsec.Text as A
36import Data.Char
36import Data.Char
37import Data.Function (on)
37import Data.Function (on)
39−import qualified Data.HashMap.Strict as Map
40−import Data.List (groupBy, nub, sort)
41−import Data.List.NonEmpty (NonEmpty (..), nonEmpty)
38+import Data.List.NonEmpty (NonEmpty (..))
42import qualified Data.List.NonEmpty as NE
39import qualified Data.List.NonEmpty as NE
43import Data.Ord
41import Data.Ord
44−import Data.Semigroup hiding (Sum)
42+import Data.Semigroup ((<>))
46import Data.Text (Text)
43import Data.Text (Text)
47import qualified Data.Text as Text
44import qualified Data.Text as Text
48import qualified Data.Text.Lazy as LText
45import qualified Data.Text.Lazy as LText
49import qualified Data.Text.Lazy.Builder as Build
46import qualified Data.Text.Lazy.Builder as Build
50−import Data.Text.Manipulate
51import qualified Data.Text.Read as Read
47import qualified Data.Text.Read as Read
52import qualified Filesystem.Path.CurrentOS as Path
48import qualified Filesystem.Path.CurrentOS as Path
53import Formatting
49import Formatting
54import Gen.Text
50import Gen.Text
55import Gen.Types.Map
52import Gen.Types.Map
56import Gen.Types.NS
53import Gen.Types.NS
57import GHC.Generics
54import GHC.Generics
@@ -144,10 +141,10 @@ data Location
144deriveJSON (js "") ''Location
141deriveJSON (js "") ''Location
146data Info = Info
143data Info = Info
147− { _infoDescription :: Maybe Text
144+ { _description :: Maybe Text
148− , _infoDefault :: Maybe Text
145+ , _defaulted :: Maybe Text
149− , _infoRequired :: !Bool
146+ , _required :: !Bool
150− , _infoLocation :: Maybe Location
147+ , _location :: Maybe Location
151 } deriving (Eq, Show)
148 } deriving (Eq, Show)
153instance FromJSON Info where
150instance FromJSON Info where
@@ -159,6 +156,11 @@ instance FromJSON Info where
160deriveToJSON (js "_info") ''Info
157deriveToJSON (js "_info") ''Info
161+parameter :: HasInfo a => a -> Bool
162+parameter s = s ^. required && isNothing (s ^. defaulted)
@@ -170,7 +172,7 @@ data Lit
171instance FromJSON Lit where
173instance FromJSON Lit where
172 parseJSON = withObject "literal" $ \o -> do
174 parseJSON = withObject "literal" $ \o -> do
173− n <- min =<< (o .:? "minimum" .!= "0")
175+ n <- num =<< (o .:? "minimum" .!= "0")
174 (o .: "format" <|> o .: "type") >>= \case
176 (o .: "format" <|> o .: "type") >>= \case
176 "string" -> pure Text
178 "string" -> pure Text
@@ -182,7 +184,7 @@ instance FromJSON Lit where
182 e -> fail $
184 e -> fail $
183 "Unable to parse Lit from " ++ Text.unpack e
185 "Unable to parse Lit from " ++ Text.unpack e
185− min = either fail (pure . fst) . Read.decimal
187+ num = either fail (pure . fst) . Read.decimal
187 nat :: Int -> Lit -> Lit
189 nat :: Int -> Lit -> Lit
188 nat n | n > 0 = const Nat
190 nat n | n > 0 = const Nat
@@ -196,65 +198,50 @@ instance FromJSON Lit where
197deriveToJSON (js "") ''Lit
199deriveToJSON (js "") ''Lit
202− deriving (Eq, Show, Generic)
204−instance Semigroup Ref where
205− a <> b = case (a, b) of
206− (Opaque xs, Opaque ys) -> Opaque (xs <> ys)
207− (Opaque xs, Direct y) -> Opaque (xs <> [y])
208− (Direct x, Opaque ys) -> Opaque (x : ys)
209− (Direct x, Direct y) -> Opaque [x, y]
211−instance Hashable Ref
213−instance IsString Ref where
214− fromString = Direct . fromString
216−instance FromJSON Ref where parseJSON = withText "ref" (pure . Direct)
217−instance ToJSON Ref where toJSON = toJSON . refToText
219−instance FromJSON a => FromJSON (Map Ref a) where
220− parseJSON = fmap (Map.fromList . map (first Direct) . Map.toList) . parseJSON
222−instance ToJSON a => ToJSON (Map Ref a) where
223− toJSON = toJSON . Map.fromList . map (first refToText) . Map.toList
225−refToText :: Ref -> Text
227− Opaque xs -> mconcat (map upperHead xs)
230−fref :: Format a (Ref -> a)
231−fref = later (Build.fromText . refToText)
233data Schema a
201data Schema a
234− = Obj Info (Map Ref a)
202+ = Obj Info (Map Id a)
235 | Arr Info a
203 | Arr Info a
236 | Enum Info [Text] [Text]
204 | Enum Info [Text] [Text]
237 | Lit Info Lit
205 | Lit Info Lit
238− | Ref Ref
206+ | Ref Info Id
239− | Any' -- String or Number
207+ | Any Info -- String or Number
240 deriving (Eq, Show, Functor, Foldable, Traversable)
208 deriving (Eq, Show, Functor, Foldable, Traversable)
242instance FromJSON (Fix Schema) where
210instance FromJSON (Fix Schema) where
243− parseJSON = withObject "schema" $ \o -> Fix <$> (ref o <|> schema o)
211+ parseJSON = withObject "schema" $ \o -> do
212+ i <- parseJSON (Object o)
213+ Fix <$> (ref i o <|> schema i o)
246− i <- parseJSON (Object o)
247− o .: "type" >>= \case
215+ schema i o = o .: "type" >>= \case
248− "object" -> Obj i <$> o .: "properties"
216+ "object" -> Obj i <$> o .: "properties"
249− "array" -> Arr i <$> o .: "items"
217+ "array" -> Arr i <$> o .: "items"
250− "any" -> pure Any'
218+ "any" -> pure (Any i)
251− _ -> enum i o <|> Lit i <$> parseJSON (Object o)
219+ _ -> enm i o <|> Lit i <$> parseJSON (Object o)
253− enum i o = Enum i <$> o .: "enum" <*> o .: "enumDescriptions"
221+ enm i o = Enum i <$> o .: "enum" <*> o .: "enumDescriptions"
254− ref o = Ref <$> o .: "$ref"
222+ ref i o = Ref i <$> o .: "$ref"
256deriveToJSON (js "") ''Schema
224deriveToJSON (js "") ''Schema
226+instance HasInfo (Schema a) where
227+ info = lens f (flip g)
240+ Enum _ v d -> Enum i v d
258data Param = Param Location -- (Fix Schema)
245data Param = Param Location -- (Fix Schema)
260instance FromJSON Param where
247instance FromJSON Param where
@@ -309,8 +296,8 @@ data Service a = Service
309 , _svcServicePath :: Text
296 , _svcServicePath :: Text
310 , _svcBatchPath :: Text
297 , _svcBatchPath :: Text
311 , _svcAuth :: Maybe Object
298 , _svcAuth :: Maybe Object
312− , _svcParameters :: Map Ref Param
299+ , _svcParameters :: Map Id Param
313− , _svcSchemas :: Map Ref a
300+ , _svcSchemas :: Map Id a
314 , _svcResources :: Object
301 , _svcResources :: Object
315 } deriving (Generic)
302 } deriving (Generic)