Skip to content

Commit

Permalink
Restyled by fourmolu
Browse files Browse the repository at this point in the history
  • Loading branch information
restyled-commits committed Sep 22, 2023
1 parent ed40e97 commit 3e79a1e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 34 deletions.
24 changes: 13 additions & 11 deletions src/Swarm/Doc/Schema/Refined.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,35 @@
-- all JSON Value types to their specific sum types
module Swarm.Doc.Schema.Refined where

import Data.Aeson
import Control.Applicative ((<|>))
import Data.Aeson
import Data.Text (Text)
import Text.Pandoc.Builder
import Data.Text qualified as T
import Text.Pandoc.Builder

newtype SingleOrList a = SingleOrList {
getList :: [a]
} deriving (Eq, Ord, Show)
newtype SingleOrList a = SingleOrList
{ getList :: [a]
}
deriving (Eq, Ord, Show)

instance (FromJSON a) => FromJSON (SingleOrList a) where
parseJSON x = SingleOrList <$> do
(pure <$> parseJSON x) <|> parseJSON x
parseJSON x =
SingleOrList <$> do
(pure <$> parseJSON x) <|> parseJSON x

type SchemaIdReference = Text

data SchemaType =
Simple (SingleOrList Text)
data SchemaType
= Simple (SingleOrList Text)
| Reference SchemaIdReference
| Alternates [Value]
deriving (Eq, Ord, Show)

fragmentHref :: SchemaIdReference -> Text
fragmentHref = T.cons '#' . T.filter (/= '.'). T.toLower
fragmentHref = T.cons '#' . T.filter (/= '.') . T.toLower

listToText :: SchemaType -> Inlines
listToText = \case
Simple xs -> code $ T.intercalate " | " $ getList xs
Reference x -> link (fragmentHref x) "Link to object properties" $ text "Object schema"
Alternates xs -> code $ T.intercalate " | " $ map (T.pack . show) xs
Alternates xs -> code $ T.intercalate " | " $ map (T.pack . show) xs
15 changes: 7 additions & 8 deletions src/Swarm/Doc/Schema/Scenario.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ module Swarm.Doc.Schema.Scenario where
import Control.Arrow (left, (&&&))
import Data.Aeson
import Data.Map.Strict qualified as M
import Swarm.Doc.Schema.Refined
import Data.Maybe (fromMaybe)
import Data.Text qualified as T
import Swarm.Doc.Schema.Refined
import Swarm.Doc.Schema.Surface
import Swarm.Util (quote, showT)
import System.FilePath ((<.>), (</>), splitExtension)
import System.Directory (listDirectory)
import System.FilePath (splitExtension, (<.>), (</>))
import Text.Pandoc
import Text.Pandoc.Builder
import System.Directory (listDirectory)

scenariosDir :: FilePath
scenariosDir = "data/scenarios"
Expand Down Expand Up @@ -76,14 +76,13 @@ genScenarioSchemaDocs = do
Left e -> print $ unwords ["Failed:", T.unpack e]
Right md -> writeFile (scenariosDir </> "README_NEW.md") $ T.unpack md
where

parseSchemaFile :: FileStemAndExtension -> IO (Either T.Text SwarmSchema)
parseSchemaFile stemAndExtension =
left (prependPath . T.pack) <$> eitherDecodeFileStrict fullPath
where
prependPath = ((T.unwords ["in", quote (T.pack filename)] <> ": ") <>)
filename = recombineExtension stemAndExtension
fullPath = schemasDir </> filename
where
prependPath = ((T.unwords ["in", quote (T.pack filename)] <> ": ") <>)
filename = recombineExtension stemAndExtension
fullPath = schemasDir </> filename

renderValue :: Value -> T.Text
renderValue = \case
Expand Down
30 changes: 15 additions & 15 deletions src/Swarm/Doc/Schema/Surface.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
-- simply for rendering Markdown documentation from Swarm's schema.
module Swarm.Doc.Schema.Surface where

import Control.Applicative ((<|>))
import Data.Aeson
import Data.List.Extra (replace)
import Data.Map (Map)
import Data.Text (Text)
import Control.Applicative ((<|>))
import GHC.Generics (Generic)
import Swarm.Doc.Schema.Refined

Expand Down Expand Up @@ -47,8 +47,8 @@ instance FromJSON SchemaRaw where
-- TODO: Conveniently, this extra layer of processing
-- is able to enforce that all "object" definitions in the schema
-- contain the @additionalProperties: false@ property.
data SwarmSchema = SwarmSchema {
schemaType :: SchemaType
data SwarmSchema = SwarmSchema
{ schemaType :: SchemaType
, defaultValue :: Maybe Value
, items :: Maybe Value
, description :: Maybe Text
Expand All @@ -59,20 +59,20 @@ data SwarmSchema = SwarmSchema {

instance FromJSON SwarmSchema where
parseJSON x = do

rawSchema :: rawSchema <- parseJSON x
let maybeType =
(Reference <$> _Sref rawSchema)
<|> (Simple <$> _type rawSchema)
<|> (Alternates <$> _oneOf rawSchema)
(Reference <$> _Sref rawSchema)
<|> (Simple <$> _type rawSchema)
<|> (Alternates <$> _oneOf rawSchema)

theType <- maybe (fail "Unspecified sub-schema type") return maybeType

return $ SwarmSchema {
schemaType = theType
, defaultValue = _default rawSchema
, items = _items rawSchema
, description = _description rawSchema
, examples = _examples rawSchema
, properties = _properties rawSchema
}
return $
SwarmSchema
{ schemaType = theType
, defaultValue = _default rawSchema
, items = _items rawSchema
, description = _description rawSchema
, examples = _examples rawSchema
, properties = _properties rawSchema
}

0 comments on commit 3e79a1e

Please sign in to comment.