mirror of
https://github.com/NorfairKing/autodocodec.git
synced 2025-01-07 10:56:34 +03:00
simplified the way default values work
This commit is contained in:
parent
c8c3b179b5
commit
a8eb840bdf
@ -52,9 +52,6 @@ data JSONSchema
|
||||
ObjectSchema ![(Text, (KeyRequirement, JSONSchema, Maybe Text))]
|
||||
| ValueSchema !JSON.Value
|
||||
| ChoiceSchema !(NonEmpty JSONSchema)
|
||||
| DefaultSchema
|
||||
!JSON.Value -- Machine-readible version of the default value
|
||||
JSONSchema
|
||||
| CommentSchema !Text !JSONSchema
|
||||
| RefSchema !Text
|
||||
| WithDefSchema !(Map Text JSONSchema) !JSONSchema
|
||||
@ -99,7 +96,6 @@ validateAccordingTo val schema = (`evalState` M.empty) $ go val schema
|
||||
_ -> pure False
|
||||
ValueSchema v -> pure $ v == value
|
||||
ChoiceSchema ss -> or <$> mapM (go value) ss
|
||||
DefaultSchema _ s -> go value s
|
||||
CommentSchema _ s -> go value s
|
||||
RefSchema name -> do
|
||||
mSchema <- gets (M.lookup name)
|
||||
@ -128,7 +124,7 @@ instance Validity JSONSchema where
|
||||
|
||||
data KeyRequirement
|
||||
= Required
|
||||
| Optional (Maybe (Text, JSON.Value)) -- Default value, human readable and machine readible
|
||||
| Optional (Maybe JSON.Value) -- Default value
|
||||
deriving (Show, Eq, Generic)
|
||||
|
||||
instance Validity KeyRequirement
|
||||
@ -179,7 +175,6 @@ instance ToJSON JSONSchema where
|
||||
val :: JSON.Value
|
||||
val = (JSON.toJSON :: [JSON.Value] -> JSON.Value) svals
|
||||
in [("anyOf", val)]
|
||||
DefaultSchema value s -> ("default", value) : go s
|
||||
CommentSchema comment s -> ("$comment" JSON..= comment) : go s
|
||||
RefSchema name -> ["$ref" JSON..= (defsPrefix <> name :: Text)]
|
||||
WithDefSchema defs s -> ("$defs" JSON..= defs) : go s
|
||||
@ -189,11 +184,9 @@ instance FromJSON JSONSchema where
|
||||
mt <- o JSON..:? "type"
|
||||
mc <- o JSON..:? "$comment"
|
||||
let commentFunc = maybe id CommentSchema mc
|
||||
let mDefault = HM.lookup "default" o
|
||||
let defaultFunc = maybe id DefaultSchema mDefault
|
||||
mdefs <- o JSON..:? "$defs"
|
||||
let defsFunc = maybe id WithDefSchema mdefs
|
||||
fmap (commentFunc . defaultFunc . defsFunc) $ case mt :: Maybe Text of
|
||||
fmap (commentFunc . defsFunc) $ case mt :: Maybe Text of
|
||||
Just "null" -> pure NullSchema
|
||||
Just "boolean" -> pure BoolSchema
|
||||
Just "string" -> pure StringSchema
|
||||
@ -293,9 +286,9 @@ jsonSchemaVia = (`evalState` S.empty) . go
|
||||
OptionalKeyCodec k c mdoc -> do
|
||||
s <- go c
|
||||
pure [(k, (Optional Nothing, s, mdoc))]
|
||||
OptionalKeyWithDefaultCodec k c hr mr mdoc -> do
|
||||
OptionalKeyWithDefaultCodec k c mr mdoc -> do
|
||||
s <- go c
|
||||
pure [(k, (Optional (Just (hr, toJSONVia c mr)), s, mdoc))]
|
||||
pure [(k, (Optional (Just (toJSONVia c mr)), s, mdoc))]
|
||||
MapCodec _ _ c -> goObject c
|
||||
PureCodec _ -> pure []
|
||||
ApCodec oc1 oc2 -> liftA2 (++) (goObject oc1) (goObject oc2)
|
||||
|
@ -74,7 +74,7 @@ parseContextVia = flip go
|
||||
OptionalKeyCodec k c _ -> do
|
||||
let mValueAtKey = HM.lookup k (value :: JSON.Object)
|
||||
forM mValueAtKey $ \valueAtKey -> go (valueAtKey :: JSON.Value) c
|
||||
OptionalKeyWithDefaultCodec k c _ defaultValue _ -> do
|
||||
OptionalKeyWithDefaultCodec k c defaultValue _ -> do
|
||||
let mValueAtKey = HM.lookup k (value :: JSON.Object)
|
||||
case mValueAtKey of
|
||||
Nothing -> pure defaultValue
|
||||
|
@ -49,6 +49,6 @@ toContextVia = flip go
|
||||
OptionalKeyCodec k c _ -> case (a :: Maybe _) of
|
||||
Nothing -> mempty
|
||||
Just b -> k JSON..= go b c
|
||||
OptionalKeyWithDefaultCodec k c _ _ mdoc -> go (Just a) (OptionalKeyCodec k c mdoc)
|
||||
OptionalKeyWithDefaultCodec k c _ mdoc -> go (Just a) (OptionalKeyCodec k c mdoc)
|
||||
PureCodec _ -> error "Cannot toJSON a pure object codec."
|
||||
ApCodec oc1 oc2 -> go a oc1 <> go a oc2
|
||||
|
@ -85,9 +85,6 @@ instance GenValid JSONSchema where
|
||||
ChoiceSchema ss -> case ss of
|
||||
s :| [] -> [s]
|
||||
_ -> ChoiceSchema <$> shrinkValid ss
|
||||
DefaultSchema mr s -> (s :) $ do
|
||||
(mr', s') <- shrinkValid (mr, s)
|
||||
pure $ DefaultSchema mr' s'
|
||||
CommentSchema k s -> (s :) $ do
|
||||
(k', s') <- shrinkValid (k, s)
|
||||
pure $ CommentSchema k' s'
|
||||
@ -111,9 +108,6 @@ instance GenValid JSONSchema where
|
||||
pure $
|
||||
ChoiceSchema $
|
||||
choice1 :| (choice2 : rest),
|
||||
do
|
||||
(a, b) <- genSplit (n -1)
|
||||
DefaultSchema <$> resize a genValid <*> resize b genValid,
|
||||
do
|
||||
(a, b) <- genSplit (n -1)
|
||||
(CommentSchema <$> resize a genValid <*> resize b genValid) `suchThat` isValid,
|
||||
|
@ -19,7 +19,7 @@
|
||||
, [33m<string>[m
|
||||
]
|
||||
[37moptional-with-default[m: # [34moptional[m
|
||||
# default: [35m"foobar"[m
|
||||
# default: [35mfoobar[m
|
||||
# an optional text with a default
|
||||
[33m<string>[m
|
||||
[37mfruit[m: # [31mrequired[m
|
||||
|
@ -65,14 +65,14 @@ jsonSchemaChunks = concatMap (\l -> l ++ ["\n"]) . go
|
||||
Optional _ -> fore blue "optional"
|
||||
mDefaultValue = \case
|
||||
Required -> Nothing
|
||||
Optional mdv -> fst <$> mdv
|
||||
Optional mdv -> mdv
|
||||
keySchemaFor k (kr, ks, mdoc) =
|
||||
let keySchemaChunks = go ks
|
||||
docToLines :: Text -> [[Chunk]]
|
||||
docToLines doc = map (\line -> [chunk "# ", chunk line]) (T.lines doc)
|
||||
defaultValueLine = case mDefaultValue kr of
|
||||
Nothing -> []
|
||||
Just defaultValue -> [[chunk "# default: ", fore magenta $ chunk defaultValue]]
|
||||
Just defaultValue -> [[chunk "# default: ", fore magenta $ jsonValueChunk defaultValue]]
|
||||
prefixLines = ["# ", requirementComment kr] : defaultValueLine ++ maybe [] docToLines mdoc
|
||||
in addInFrontOfFirstInList [fore white $ chunk k, ":", " "] (prefixLines ++ keySchemaChunks)
|
||||
in if null s
|
||||
@ -90,7 +90,6 @@ jsonSchemaChunks = concatMap (\l -> l ++ ["\n"]) . go
|
||||
map (addInFrontOfFirstInList [", "]) restChunks
|
||||
++ [[["]"]]]
|
||||
in addListAround s
|
||||
DefaultSchema v s -> [chunk "# default: ", fore magenta $ jsonValueChunk v] : go s
|
||||
CommentSchema comment s -> [chunk $ "# " <> comment] : go s
|
||||
RefSchema name -> [[fore cyan $ chunk $ "ref: " <> name]]
|
||||
WithDefSchema defs s -> concatMap (\(name, s') -> [fore cyan $ chunk $ "def: " <> name] : go s') (M.toList defs) ++ go s
|
||||
|
@ -161,8 +161,6 @@ data Codec context input output where
|
||||
Text ->
|
||||
-- | Codec for the value
|
||||
ValueCodec value value ->
|
||||
-- | Human-readible version of the default value
|
||||
Text ->
|
||||
-- | Default value
|
||||
value ->
|
||||
-- | Documentation
|
||||
@ -245,7 +243,7 @@ showCodecABit = ($ "") . (`evalState` S.empty) . go 0
|
||||
pure $ showParen (d > 10) $ showString "ReferenceCodec " . showsPrec d name . showString " " . s
|
||||
RequiredKeyCodec k c mdoc -> (\s -> showParen (d > 10) $ showString "RequiredKeyCodec " . showsPrec d k . showString " " . showsPrec d mdoc . showString " " . s) <$> go 11 c
|
||||
OptionalKeyCodec k c mdoc -> (\s -> showParen (d > 10) $ showString "OptionalKeyCodec " . showsPrec d k . showString " " . showsPrec d mdoc . showString " " . s) <$> go 11 c
|
||||
OptionalKeyWithDefaultCodec k c shownDefault _ mdoc -> (\s -> showParen (d > 10) $ showString "OptionalKeyWithDefaultCodec " . showsPrec d k . showString " " . s . showString " " . showsPrec d shownDefault . showString " " . showsPrec d mdoc) <$> go 11 c
|
||||
OptionalKeyWithDefaultCodec k c _ mdoc -> (\s -> showParen (d > 10) $ showString "OptionalKeyWithDefaultCodec " . showsPrec d k . showString " " . s . showString " " . showsPrec d mdoc) <$> go 11 c
|
||||
PureCodec _ -> pure $ showString "PureCodec"
|
||||
ApCodec oc1 oc2 -> (\s1 s2 -> showParen (d > 10) $ showString "ApCodec " . s1 . showString " " . s2) <$> go 11 oc1 <*> go 11 oc2
|
||||
|
||||
@ -464,7 +462,6 @@ optionalFieldWith' key c = OptionalKeyCodec key c Nothing
|
||||
--
|
||||
-- The shown version of the default value will appear in the documentation.
|
||||
optionalFieldWithDefaultWith ::
|
||||
Show output =>
|
||||
-- | Key
|
||||
Text ->
|
||||
-- | Codec for the value
|
||||
@ -474,11 +471,10 @@ optionalFieldWithDefaultWith ::
|
||||
-- | Documentation
|
||||
Text ->
|
||||
ObjectCodec output output
|
||||
optionalFieldWithDefaultWith key c defaultValue doc = OptionalKeyWithDefaultCodec key c (T.pack (show defaultValue)) defaultValue (Just doc)
|
||||
optionalFieldWithDefaultWith key c defaultValue doc = OptionalKeyWithDefaultCodec key c defaultValue (Just doc)
|
||||
|
||||
-- | Like 'optionalFieldWithDefaultWith', but without documentation.
|
||||
optionalFieldWithDefaultWith' ::
|
||||
Show output =>
|
||||
-- | Key
|
||||
Text ->
|
||||
-- | Codec for the value
|
||||
@ -486,7 +482,7 @@ optionalFieldWithDefaultWith' ::
|
||||
-- | Default value
|
||||
output ->
|
||||
ObjectCodec output output
|
||||
optionalFieldWithDefaultWith' key c defaultValue = OptionalKeyWithDefaultCodec key c (T.pack (show defaultValue)) defaultValue Nothing
|
||||
optionalFieldWithDefaultWith' key c defaultValue = OptionalKeyWithDefaultCodec key c defaultValue Nothing
|
||||
|
||||
-- | An optional, or null, field
|
||||
--
|
||||
|
Loading…
Reference in New Issue
Block a user