Skip to content

Commit

Permalink
Add Constrained typs to JSON schema rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
paolino committed Sep 25, 2023
1 parent 6acd4eb commit 8182ad8
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib/fine-types/fine-types.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ test-suite unit
build-depends:
, aeson
, base
, bytestring
, containers
, deepseq
, filepath
Expand Down
23 changes: 18 additions & 5 deletions lib/fine-types/src/Language/FineTypes/Export/OpenAPI/Schema.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,21 @@ import Language.FineTypes.Module
, resolveVars
)
import Language.FineTypes.Typ
( ConstructorName
( Constraint
, ConstructorName
, FieldName
, OpOne (..)
, OpTwo (..)
, Typ (..)
, TypConst (..)
, TypName
, VarName
, everything
, everywhere
)
import Language.FineTypes.Typ.PrettyPrinter (prettyTyp)
import Prettyprinter (layoutCompact)
import Prettyprinter.Render.String (renderString)

import qualified Data.HashMap.Strict.InsOrd as MH
import qualified Data.Map as Map
Expand Down Expand Up @@ -104,8 +109,6 @@ supportsJSON =
isSupportedTyp = everything (&&) isSupported
isSupported (Two fun _ _) =
fun `notElem` [PartialFunction, FiniteSupport]
isSupported Constrained{} =
False
isSupported _ = True

-- | Convert 'Typ' definitions to JSON.
Expand Down Expand Up @@ -203,8 +206,18 @@ schemaFromTyp = go
schemaFromProductN fields
go (SumN constructors) =
schemaFromSumN constructors
go Constrained{} =
error "ConstrainedTyp is not supported by JSON schema"
go (Constrained v t c) =
schemaFromConstraint v t c

schemaFromConstraint :: VarName -> Typ -> Constraint -> Schema
schemaFromConstraint v t c = (schemaFromTyp t){_schemaFormat = Just format}
where
format =
T.pack
$ renderString
$ layoutCompact
$ prettyTyp (const mempty) "no-name"
$ Constrained v t c

-- | Map a record type to a JSON schema.
--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ jsonFromValue :: Typ -> Value -> JS.Value
jsonFromValue = go
where
go :: Typ -> Value -> JS.Value
go (Typ.Constrained _ typ _) value = go typ value
go (Typ.Two Typ.Product2 ta tb) (Product [a, b]) =
JS.object ["0" .= go ta a, "1" .= go tb b]
go (Typ.ProductN fields) (Product ps) =
Expand Down
2 changes: 2 additions & 0 deletions lib/fine-types/src/Language/FineTypes/Value.hs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ newtype TwoF a b

-- | Check whether a 'Value' inhabits the given 'Typ'.
hasTyp :: Value -> Typ -> Bool
hasTyp z (Typ.Constrained _ t _) =
z `hasTyp` t
hasTyp (Zero a) (Typ.Zero b) =
typOf0 a == b
hasTyp (Zero _) _ =
Expand Down
1 change: 1 addition & 0 deletions lib/fine-types/src/Language/FineTypes/Value/Gen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ genTypValue typ =
ix <- lift $ choose (0, length constructors - 1)
let (_cn, typ') = constructors !! ix
Sum ix <$> exceptGenValue typ'
Typ.Constrained _ typ' _ -> genTypValue typ'
typ' -> pure $ Left typ'

genTypAndValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import Language.FineTypes.Typ
)
import Language.FineTypes.Typ.Gen
( Mode (Concrete)
, WithConstraints (WithoutConstraints)
, WithConstraints (..)
)
import Language.FineTypes.Value.Gen (genTypAndValue)
import Test.Hspec
Expand Down Expand Up @@ -63,7 +63,7 @@ spec = do
( genTypAndValue
(const True)
unsupported
WithoutConstraints
WithConstraints
Concrete
6
)
Expand Down
2 changes: 1 addition & 1 deletion lib/fine-types/test/data/JsonUTxO.fine
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ DataHash = Bytes;
Quantity = ℤ;

Value =
{ ada : Quantity
{ ada : {x : Quantity | x > 0}
, assets : Asset*
};

Expand Down

0 comments on commit 8182ad8

Please sign in to comment.