Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
zachary822 committed Dec 29, 2024
1 parent d1a41cb commit 692ece0
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 44 deletions.
39 changes: 32 additions & 7 deletions app/Lib/Common/Types.hs
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
module Lib.Common.Types where

import Data.Aeson
import GHC.Generics (Generic)
import Lib.OpenApi.Types
import Lib.Path.Types
import Data.Scientific
import Data.Text qualified as T
import Data.Text.Encoding qualified as T
import Data.Text.Read (decimal)
import Network.HTTP.Types.Method
import Network.HTTP.Types.Status

data ServerConfig = PathConf Paths | OpenApiConf OpenApi
deriving (Generic, Show, Eq)
newtype HTTPStatus = HTTPStatus {unStatus :: Status} deriving (Show, Eq, Ord)

instance FromJSON ServerConfig where
parseJSON = genericParseJSON defaultOptions{sumEncoding = UntaggedValue}
instance FromJSON HTTPStatus where
parseJSON = withScientific "Status" $ \s ->
case toBoundedInteger s of
Nothing -> fail "Bad status code"
Just i -> return (HTTPStatus . toEnum $ i)

instance FromJSONKey HTTPStatus where
fromJSONKey = FromJSONKeyTextParser $ \s ->
case decimal s of
Left e -> fail e
Right (i, _) -> return (HTTPStatus . toEnum $ i)

newtype HTTPMethod = HTTPMethod {unMethod :: StdMethod} deriving (Show, Eq, Ord)

instance FromJSON HTTPMethod where
parseJSON = withText "Method" $ \s ->
case (parseMethod . T.encodeUtf8 . T.toUpper) s of
Left e -> fail $ (T.unpack . T.decodeUtf8) e
Right m -> return $ HTTPMethod m

instance FromJSONKey HTTPMethod where
fromJSONKey = FromJSONKeyTextParser $ \s ->
case (parseMethod . T.encodeUtf8 . T.toUpper) s of
Left e -> fail $ (T.unpack . T.decodeUtf8) e
Right m -> return $ HTTPMethod m
4 changes: 2 additions & 2 deletions app/Lib/OpenApi/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Data.Text (Text)
import Data.Text qualified as T
import Data.Text.Read (decimal)
import GHC.Generics (Generic)
import Lib.Types
import Lib.Common.Types
import Network.HTTP.Types (Status, ok200)

data OpenApi = OpenApi
Expand Down Expand Up @@ -91,7 +91,7 @@ schemaLookup schemaContent (SchemaRef (Ref r)) =
ref@(SchemaRef _) -> schemaLookup schemaContent ref
s -> return s
schemaLookup schemaContent (SchemaObject (Just props)) =
(sequenceA $ M.map (schemaLookup schemaContent) props)
traverse (schemaLookup schemaContent) props
>>= return
. SchemaObject
. return
Expand Down
2 changes: 1 addition & 1 deletion app/Lib/Path/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Data.Aeson
import Data.Map.Strict (Map)
import Data.Text.Lazy qualified as TL
import GHC.Generics
import Lib.Types
import Lib.Common.Types

type Paths = Map String PathConfig

Expand Down
39 changes: 7 additions & 32 deletions app/Lib/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,12 @@
module Lib.Types where

import Data.Aeson
import Data.Scientific
import Data.Text qualified as T
import Data.Text.Encoding qualified as T
import Data.Text.Read (decimal)
import Network.HTTP.Types.Method
import Network.HTTP.Types.Status
import GHC.Generics (Generic)
import Lib.OpenApi.Types
import Lib.Path.Types

newtype HTTPStatus = HTTPStatus {unStatus :: Status} deriving (Show, Eq, Ord)
data ServerConfig = PathConf Paths | OpenApiConf OpenApi
deriving (Generic, Show, Eq)

instance FromJSON HTTPStatus where
parseJSON = withScientific "Status" $ \s ->
case toBoundedInteger s of
Nothing -> fail "Bad status code"
Just i -> return (HTTPStatus . toEnum $ i)

instance FromJSONKey HTTPStatus where
fromJSONKey = FromJSONKeyTextParser $ \s ->
case decimal s of
Left e -> fail e
Right (i, _) -> return (HTTPStatus . toEnum $ i)

newtype HTTPMethod = HTTPMethod {unMethod :: StdMethod} deriving (Show, Eq, Ord)

instance FromJSON HTTPMethod where
parseJSON = withText "Method" $ \s ->
case (parseMethod . T.encodeUtf8 . T.toUpper) s of
Left e -> fail $ (T.unpack . T.decodeUtf8) e
Right m -> return $ HTTPMethod m

instance FromJSONKey HTTPMethod where
fromJSONKey = FromJSONKeyTextParser $ \s ->
case (parseMethod . T.encodeUtf8 . T.toUpper) s of
Left e -> fail $ (T.unpack . T.decodeUtf8) e
Right m -> return $ HTTPMethod m
instance FromJSON ServerConfig where
parseJSON = genericParseJSON defaultOptions{sumEncoding = UntaggedValue}
3 changes: 1 addition & 2 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,7 @@ jsonFromSchema (SchemaObject props) =
fromMaybe (Object mempty)
<$> traverse
( fmap (object . M.foldlWithKey (\a k v -> (fromText k, v) : a) [])
. sequenceA
. M.map jsonFromSchema
. traverse jsonFromSchema
)
props
jsonFromSchema (SchemaArray item) =
Expand Down

0 comments on commit 692ece0

Please sign in to comment.