on d54960d help sign in
40 files changed+607−1804
since splitsnapshots
unifiedsplit
.github/workflows/cabal.yml−64 .github/workflows/ci.yml+3−6 EXAMPLE.md−143 Example.lhs−1 Makefile+1−3 README.md+23−87 benchmarks/README.md−30 benchmarks/genFile.sh−26 benchmarks/prelude−61 benchmarks/runTiming.sh−10 benchmarks/runTimingNoisy.sh−10 package.yaml+3−24 roboservant.cabal+6−68 src/Roboservant.hs+3−6 src/Roboservant/Client.hs−127 src/Roboservant/Direct.hs+174−248 src/Roboservant/Server.hs−36 src/Roboservant/Types.hs+10−11 src/Roboservant/Types/Breakdown.hs+74−48 src/Roboservant/Types/BuildFrom.hs−107 src/Roboservant/Types/Config.hs−29 src/Roboservant/Types/FlattenServer.hs+45 src/Roboservant/Types/Internal.hs−53 src/Roboservant/Types/Orphans.hs−7 src/Roboservant/Types/ReifiedApi.hs+60−76 src/Roboservant/Types/ReifiedApi/Server.hs−114 stack.yaml+22−5 stack.yaml.lock+112−9 test/Breakdown.hs−50 test/Foo.hs−3 test/Headers.hs+2−4 test/Nested.hs−23 test/Post.hs−31 test/Product.hs−31 test/Put.hs−33 test/QueryParams.hs−15 test/Seeded.hs+4−10 test/Spec.hs+47−150 test/UnsafeIO.hs+15−13 test/Valid.hs+3−32
.github/workflows/cabal.ymldeleted−64
@@ -1,64 +0,0 @@
1−# modified from https://github.com/jgm/pandoc/blob/master/.github/workflows/ci.yml
2−name: Cabal CI
3−
4−on:
5− push:
6− branches:
7− - '**'
8− paths-ignore: []
9− pull_request:
10− paths-ignore: []
11−
12−jobs:
13− linux:
14−
15− runs-on: ubuntu-20.04
16− strategy:
17− fail-fast: false
18− matrix:
19− versions:
20− - ghc: '8.8.4'
21− cabal: '3.4'
22− - ghc: '8.10.4'
23− cabal: '3.4'
24− steps:
25− - uses: actions/checkout@v2
26−
27− # need to install older cabal/ghc versions from ppa repository
28−
29− - name: Install recent cabal/ghc
30− uses: actions/setup-haskell@v1.1.3
31− with:
32− ghc-version: ${{ matrix.versions.ghc }}
33− cabal-version: ${{ matrix.versions.cabal }}
34−
35− # declare/restore cached things
36− # caching doesn't work for scheduled runs yet
37− # https://github.com/actions/cache/issues/63
38−
39− - name: Cache cabal global package db
40− id: cabal-global
41− uses: actions/cache@v2
42− with:
43− path: |
44− ~/.cabal
45− key: ${{ runner.os }}-${{ matrix.versions.ghc }}-${{ matrix.versions.cabal }}-cabal-global-${{ hashFiles('cabal.project') }}
46−
47− - name: Cache cabal work
48− id: cabal-local
49− uses: actions/cache@v2
50− with:
51− path: |
52− dist-newstyle
53− key: ${{ runner.os }}-${{ matrix.versions.ghc }}-${{ matrix.versions.cabal }}-cabal-local
54−
55− - name: Install dependencies
56− run: |
57− cabal update
58− cabal build all --dependencies-only --enable-tests --disable-optimization
59− - name: Build
60− run: |
61− cabal build all --enable-tests --disable-optimization 2>&1 | tee build.log
62− - name: Test
63− run: |
64− cabal test all --disable-optimization
.github/workflows/ci.yml+3−6
@@ -1,4 +1,4 @@
1−name: Stack CI
1+name: CI
22
33# Trigger the workflow on push or pull request, but only for the master branch
44on:
@@ -12,14 +12,13 @@ jobs:
1212 strategy:
1313 matrix:
1414 stack: ["2.3.3"]
15− ghc: ["8.10.4"]
15+ ghc: ["8.8.3"]
1616 os: [ubuntu-latest]
1717 steps:
1818 - uses: actions/checkout@v2
1919 if: github.event.action == 'opened' || github.event.action == 'synchronize' || github.event.ref == 'refs/heads/master'
2020
21− - uses: actions/setup-haskell@v1.1.4
22− if: github.event.action == 'opened' || github.event.action == 'synchronize' || github.event.ref == 'refs/heads/master'
21+ - uses: actions/setup-haskell@v1.1
2322 name: Setup Haskell Stack
2423 with:
2524 ghc-version: ${{ matrix.ghc }}
@@ -27,13 +26,11 @@ jobs:
2726 enable-stack: true
2827
2928 - uses: actions/cache@v1
30− if: github.event.action == 'opened' || github.event.action == 'synchronize' || github.event.ref == 'refs/heads/master'
3129 name: Cache ~/.stack
3230 with:
3331 path: ~/.stack
3432 key: ${{ runner.os }}-${{ matrix.ghc }}-stack
3533
3634 - name: Test
37− if: github.event.action == 'opened' || github.event.action == 'synchronize' || github.event.ref == 'refs/heads/master'
3835 run: |
3936 stack test
EXAMPLE.mddeleted−143
@@ -1,143 +0,0 @@
1−# Example
2−
3−Our api under test:
4−
5−``` haskell
6−-- Obligatory fancy-types pragma tax
7−{-# LANGUAGE DataKinds #-}
8−{-# LANGUAGE TypeOperators #-}
9−{-# LANGUAGE DerivingStrategies #-}
10−{-# LANGUAGE StandaloneDeriving #-}
11−{-# LANGUAGE DerivingVia #-}
12−{-# LANGUAGE TypeApplications #-}
13−{-# LANGUAGE DeriveGeneric #-}
14−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
15−{-# LANGUAGE RecordWildCards #-}
16−{-# LANGUAGE ScopedTypeVariables #-}
17−{-# LANGUAGE TypeApplications #-}
18−
19−import qualified Roboservant.Server as RS
20−import qualified Roboservant.Client as RC
21−import Servant.Client(ClientEnv, baseUrlPort, parseBaseUrl, mkClientEnv)
22−import Network.HTTP.Client (newManager, defaultManagerSettings)
23−import Roboservant.Types
24−import Test.Hspec
25−import Servant
26−import GHC.Generics
27−import Data.Typeable
28−import Data.Hashable
29−import Data.Maybe(isNothing, isJust)
30−import qualified Network.Wai.Handler.Warp as Warp
31−import Data.Aeson(FromJSON,ToJSON)
32−
33−newtype A = A Int
34− deriving (Generic, Eq, Show, Typeable)
35− deriving newtype (Hashable, FromHttpApiData, ToHttpApiData)
36−
37−instance FromJSON A
38−instance ToJSON A
39−
40−newtype B = B Int
41− deriving (Generic, Eq, Show, Typeable)
42− deriving newtype (Hashable, FromHttpApiData, ToHttpApiData)
43−
44−instance FromJSON B
45−instance ToJSON B
46−
47−type Api =
48− "item" :> Get '[JSON] A
49− :<|> "itemAdd" :> Capture "one" B :> Capture "two" B :> Get '[JSON] B
50− :<|> "item" :> Capture "itemId" B :> Get '[JSON] ()
51−
52−server :: Handler A -> Server Api
53−server introduce = introduce :<|> combine :<|> eliminate
54− where
55− combine (B i) (B j) = pure $ B (i + j)
56− eliminate (B i)
57− | i > 10 = error "give up, eleven is way too big and probably not even real"
58− | otherwise = pure ()
59−```
60−
61−We have a "good" server, that never generates anything other than a 0. This means repeated application of
62−the combination/addition rule can never bring us to the dangerous state of numbers larger than 10.
63−
64−``` haskell
65−goodServer, badServer :: Server Api
66−goodServer = server (pure $ A 0)
67−badServer = server (pure $ A 1)
68−```
69−
70−In the test file, we first define the tests: the faulty server should fail and the good server should pass.
71−
72−```haskell
73−main :: IO ()
74−main = hspec spec
75−
76−spec :: Spec
77−spec = describe "example" $ do
78− it "good server should not fail" $ do
79− RS.fuzz @Api goodServer config
80− >>= (`shouldSatisfy` isNothing)
81− it "bad server should fail" $ do
82− RS.fuzz @Api badServer config
83− >>= (`shouldSatisfy` isJust)
84−```
85−
86−The previous test just picked apart the server and ran functions manually: sometimes, we want to test via
87−an honest-to-goodness network port, like so:
88−
89−```haskell
90− around (withServer (serve (Proxy :: Proxy Api) badServer)) $ do
91− it "we should also be able to run the _client_ to an independent server (ignore server error messages)" $ \(clientEnv::ClientEnv) -> do
92− RC.fuzz @Api clientEnv config >>= (`shouldSatisfy` isJust)
93−```
94−
95−(we use withApplication rather than testWithApplication because we don't primarily care what the server does here:
96−we want to check what a client does when presented with a faulty server.)
97−
98−We expect to be able to cover the whole api from our starting point, so let's set the coverage to 0.99.
99−There are other tweakable things in the config, like maximum runtime, reps,
100−per-request healthchecks, seeds, and verbose logging. Have a look at
101−Roboservant.Types.Config for details.
102−
103−``` haskell
104−config :: Config
105−config = defaultConfig
106− { coverageThreshold = 0.99
107− }
108−```
109−
110−Unless we want to ship roboservant and all its dependencies to production, we also need
111−some orphan instances: because As are the only value we can get without
112−an input, we need to be able to break them down.
113−
114−``` haskell
115−deriving via (Compound A) instance Breakdown A
116−```
117−
118−if we wanted to assemble As from parts as well, we'd derive using Compound, but in this case we don't care.
119−
120−``` haskell
121−deriving via (Atom A) instance BuildFrom A
122−
123−```
124−
125−Similarly, to generate the first B from the Ints we got from inside the A, we need to be able to
126−build it up from components.
127−
128−```haskell
129−deriving via (Compound B) instance BuildFrom B
130−deriving via (Atom B) instance Breakdown B
131−```
132−
133−
134−test utilities:
135−
136−``` haskell
137−withServer :: Application -> ActionWith ClientEnv -> IO ()
138−withServer app action = Warp.withApplication (pure app) (\p -> genClientEnv p >>= action)
139− where genClientEnv port = do
140− baseUrl <- parseBaseUrl "http://localhost"
141− manager <- newManager defaultManagerSettings
142− pure $ mkClientEnv manager (baseUrl { baseUrlPort = port })
143−```
Example.lhsdeleted−1
@@ -1 +0,0 @@
1−EXAMPLE.md
Makefile+1−3
@@ -1,4 +1,2 @@
11testwatch:
2− ghcid -T main -c 'stack repl roboservant:lib roboservant:test:roboservant-test --test --ghc-options=-fobject-code' --allow-eval --restart="stack.yaml" --restart="package.yaml" -W
3−pedanticwatch:
4− ghcid -c 'stack repl --test --ghc-options=-fobject-code' --allow-eval --restart="stack.yaml" --restart="package.yaml" -W
2+ ghcid -T main -c 'stack repl --test --ghc-options=-fobject-code' --allow-eval --restart="stack.yaml" --restart="package.yaml" -W
README.md+23−87
@@ -2,86 +2,45 @@
22
33Automatically fuzz your servant apis in a contextually-aware way.
44
5−[![Stack CI](https://github.com/mwotton/roboservant/actions/workflows/ci.yml/badge.svg)](https://github.com/mwotton/roboservant/actions/workflows/ci.yml)
6−[![Cabal CI](https://github.com/mwotton/roboservant/actions/workflows/cabal.yml/badge.svg)](https://github.com/mwotton/roboservant/actions/workflows/cabal.yml)
5+![CI](https://github.com/mwotton/roboservant/workflows/CI/badge.svg)
76
8−## example
97
10−see full example [here](EXAMPLE.md)
11−
12−## why?
8+# why?
139
1410Servant gives us a lot of information about what a server can do. We
1511use this information to generate arbitrarily long request/response
1612sessions and verify properties that should hold over them.
1713
18−## how?
19−
20−In essence, ```fuzz @Api yourServer config``` will make a bunch of
21−calls to your API, and record the results in a type-indexed
22−dictionary. This means that they are now available for the
23−prerequisites of other calls, so as you proceed, more and more api
24−calls become possible.
25−
26−We explicitly do not try to come up with plausible values that haven't
27−somehow come back from the API. That's straying into QC/Hedgehog
28−territory: if you want that, come up with the values on that side, and
29−set them as seeds in the configuration.
30−
31−### what does it mean to be "available"?
32−
33−In a simple API, you may make a call and get back a `Foo`, which will
34−allow you to make another call that requires a `Foo`. In a more
35−complicated app, it's likely that you'll send a request body that
36−includes many subcomponents, and it's likely you'll get a response
37−that needs to be broken down into pieces before it's useful.
14+# example
3815
39−To cope with this, we have the typeclasses `BuildFrom` and
40−`Breakdown`. You can write instances for them if you feel like it, and
41−indeed it's currently required for recursive datatypes if you don't
42−want the fuzzer to hang, but for the majority of your types it should
43−be sufficient to derive them generically. (Sensible instances are
44−provided for lists.)
16+Our api under test:
4517
46−There are two basic strategies here. In some cases, you want to regard
47−a type as indivisible: that's why we like newtypes, right? In this
48−case, we can derive using the `Atom` strategy.
49−
50−``` haskell
51−deriving via (Atom NewtypedKey) instance Breakdown NewtypedKey
52−deriving via (Atom NewtypedKey) instance BuildFrom NewtypedKey
5318```
54−
55−This is saying "A can neither be built from components or broken down
56−for spare parts. Hands off!". This is a good strategy for key types,
57−for instance.
58−
59−If instead it's a big complicated thing with lots of juicy
60−subcomponents, we want to rip it apart using Generics and feast on
61−its succulent headmeats:
62−
63−``` haskell
64−deriving via (Compound Payload) instance Breakdown Payload
65−deriving via (Compound Payload) instance BuildFrom Payload
19+newtype Foo = Foo Int
20+ deriving (Generic, Eq, Show, Typeable)
21+ deriving newtype (FromHttpApiData, ToHttpApiData)
22+
23+type FooApi =
24+ "item" :> Get '[JSON] Foo
25+ :<|> "itemAdd" :> Capture "one" Foo :> Capture "two" Foo :> Get '[JSON] Foo
26+ :<|> "item" :> Capture "itemId" Foo :> Get '[JSON] ()
6627```
6728
68−### priming the pump
29+From the tests:
6930
70−Sometimes there are values we'd like to smuggle into the API that are
71−not derivable from within the API itself: sometimes this is a warning
72−sign that your API is incomplete, but it can be quite reasonable to
73−require identifying credentials within an API and not provide a way to
74−get them. It might also be reasonable to have some sample values that
75−the user is expected to come up with.
31+```
32+ assert "should find an error in Foo" . not
33+ =<< checkSequential (Group "Foo" [("Foo", RS.prop_sequential @Foo.FooApi Foo.fooServer)])
34+```
7635
77−For those cases, override the `seed` in the `Config` with a
78−list of seed values, suitably hashed:
36+We have a server that blows up if the value of the int in a `Foo` ever gets above 10. Note:
37+there is no generator for `Foo` types: larger `Foo`s can only be made only by combining existing
38+`Foo`s with `itemAdd`. This is an important distinction, because many APIs will return UUIDs or
39+similar as keys, which make it impossible to cover a useful section of the state space without
40+using the values returned by the API
7941
80−``` haskell
81−defaultConfig { seed = [hashedDyn creds, hashedDyn userJwt]}
82−```
8342
84−## why not servant-quickcheck?
43+# why not servant-quickcheck?
8544
8645[servant-quickcheck](https://hackage.haskell.org/package/servant-quickcheck)
8746is a great package and I've learned a lot from it. Unfortunately, as mentioned previously,
@@ -89,26 +48,3 @@ there's a lot of the state space you just can't explore without context: modern
8948full of pointer-like structures, whether they're URLs or database
9049keys/uuids, and servant-quickcheck requires that you be able to generate
9150these without context via Arbitrary.
92−
93−## limitations and future work
94−
95−Currently, the display of failing traces is pretty tragic, both in the
96−formatting and in its non-minimality. This is pretty ticklish:
97−arguably the right way to do this is to return a trace that we can
98−also rerun, and let quickcheck or hedgehog a level up shrink it until
99−it's satisfactorily short. In the interest of being useful earlier
100−rather than later, I'm releasing v1.0 before I crack this particular
101−nut. We do know which calls we made that led to the failing case, so
102−we would want to show that distinction in a visible way: it's possible
103−that other calls that don't have direct data dependencies were
104−important, but we definitely know we need the direct data dependencies.
105−
106−The provenance stuff is a bit underbaked. It should at least pull a
107−representation of the route chosen rather than just an integer index.
108−
109−It would also be nice to have a robust strategy for deriving recursive
110−datatypes, or at least rejecting attempts to generate them that don't
111−end in an infinite loop.
112−
113−Currently the `FlattenServer` instance for `:>` is quadratic. It would
114−be nice to fix this but I lack the art.
benchmarks/README.mddeleted−30
@@ -1,30 +0,0 @@
1−# Benchmarks
2−
3−I've noticed compilation can be a little slow on realistic apps so I set up a test.
4−
5−## components
6−
7−1. codegen
8−
9− given an integer input n, create an api with n endpoints. they should take a reasonably
10− complicated input and return a reasonably complicated output
11−
12−2. runner
13−
14− build dependencies
15− for i from 1 to 100
16− generate api with i endpoints
17− compile it, but timed
18− print (i,time)
19−
20−3. plot to see linearity/quadraticity?
21−
22−## possible gotchas
23−
24−- we usually compile these in different modules rather than together: if we don't get slow
25− compilation in one file, try out multiple files.
26−
27−## notes
28−
29−- flat profile if you don't have the call to `fuzz`
30−- no difference between deriving via Atom vs Compound, so not a Generics thing.
benchmarks/genFile.shdeleted−26
@@ -1,26 +0,0 @@
1−n=$1
2−k=$2
3−
4−cat prelude
5−echo -n "type SubApi = \"route1\" :> ReqBody '[JSON] Input :> Post '[JSON] Output"
6−for i in $(seq 2 $n); do
7− echo -n " :<|> \"route${i}\" :> "
8− echo "ReqBody '[JSON] Input :> Post '[JSON] Output"
9−done
10−
11−echo -n "server = handler "
12−
13−for i in $(seq 2 $n); do
14− echo -n ":<|> handler "
15−done
16−echo
17−echo
18−
19−echo -n "type Api = Flatten ("
20−for i in $(seq 1 $k); do
21− echo -n '"foo" :> '
22−done;
23−
24−echo "SubApi )"
25−
26−
benchmarks/preludedeleted−61
@@ -1,61 +0,0 @@
1−{-# LANGUAGE LambdaCase #-}
2−{-# LANGUAGE TypeOperators #-}
3−{-# LANGUAGE DataKinds #-}
4−{-# LANGUAGE TypeApplications #-}
5−{-# LANGUAGE DerivingVia #-}
6−{-# LANGUAGE StandaloneDeriving #-}
7−{-# LANGUAGE DeriveGeneric #-}
8−
9−
10−import Roboservant
11−import Servant
12−import Data.Hashable
13−import GHC.Generics
14−import Data.Aeson
15−import Servant.API.Flatten
16−
17−data Input = Input1 { foo :: String }
18− | Input2 { bar :: Int }
19− deriving (Generic, Eq)
20−
21−instance FromJSON Input
22−instance ToJSON Input
23−
24−
25−deriving via (Compound Input) instance Breakdown Input
26−deriving via (Compound Input) instance BuildFrom Input
27−
28−
29−-- deriving via (Atom Input) instance Breakdown Input
30−-- deriving via (Atom Input) instance BuildFrom Input
31−
32−instance Hashable Input
33−
34−
35−data Output = Output1 { ofoo :: String }
36− | Output2 { obar :: Int }
37− deriving (Generic, Eq)
38−
39−instance FromJSON Output
40−instance ToJSON Output
41−
42−instance Hashable Output
43−
44−deriving via (Compound Output) instance Breakdown Output
45−deriving via (Compound Output) instance BuildFrom Output
46−
47−-- deriving via (Atom Output) instance Breakdown Output
48−-- deriving via (Atom Output) instance BuildFrom Output
49−
50−
51−handler :: Input -> Handler Output
52−handler = pure . \case
53− Input1 foo -> Output1 foo
54− Input2 bar -> Output2 bar
55−
56−main =
57− -- fuzz @Api server defaultConfig
58− pure ()
59−
60−f = flatten (Proxy @Api)
61−
benchmarks/runTiming.shdeleted−10
@@ -1,10 +0,0 @@
1−lo=$1
2−hi=$2
3−k=$3
4−for i in $(seq $lo 5 $hi); do
5− ./genFile.sh $i $k > main.hs;
6− echo "sievedn: $i,";
7− /usr/bin/time -f "%e" stack ghc main.hs 2>&1;
8−# /usr/bin/time -f "%e" stack ghc -- -fomit-interface-pragmas main.hs 2>&1;
9−
10−done | grep -v Compiling | grep -v Linking | awk '/,$/ { printf("%s", $0); next } 1' | sed 's/sievedn: //'
benchmarks/runTimingNoisy.shdeleted−10
@@ -1,10 +0,0 @@
1−lo=$1
2−hi=$2
3−
4−for i in $(seq $lo 5 $hi); do
5− ./genFile.sh $i > main.hs;
6− echo "sievedn: $i,";
7− /usr/bin/time -f "%e" stack ghc main.hs 2>&1;
8−# /usr/bin/time -f "%e" stack ghc -- -fomit-interface-pragmas main.hs 2>&1;
9−
10−done
package.yaml+3−24
@@ -1,8 +1,9 @@
1−
21name: roboservant
3−version: 0.1.0.3
2+version: 0.1.0.2
43github: "mwotton/roboservant"
54license: BSD3
5+author: "Mark Wotton, Samuel Schlesinger"
6+maintainer: "mwotton@gmail.com"
67copyright: "2020 Mark Wotton, Samuel Schlesinger"
78synopsis: Automatic session-aware servant testing
89category: Web
@@ -18,8 +19,6 @@ dependencies:
1819- bytestring
1920- containers
2021- random
21−- hashable
22−- http-types
2322- lifted-base
2423- monad-control
2524- mtl
@@ -31,9 +30,7 @@ dependencies:
3130- vinyl
3231- dependent-sum
3332- dependent-map
34−- unordered-containers
3533- text
36−- time
3734
3835ghc-options:
3936- -Wall
@@ -55,22 +52,4 @@ tests:
5552 - aeson
5653 - hspec
5754 - hspec-core
58− - http-api-data
59− - http-client
60− - hspec-wai
61− - wai
62− - warp
63− example:
64− main: Example.lhs
65− source-dirs: .
66−
67− dependencies:
68− - roboservant
69− - hspec
70− - warp
71− - http-client
72− - aeson
73− - hspec-core
74− ghc-options: -pgmL markdown-unlit
7555
76− build-tools: markdown-unlit
roboservant.cabal+6−68
@@ -1,18 +1,20 @@
11cabal-version: 1.12
22
3−-- This file has been generated from package.yaml by hpack version 0.35.0.
3+-- This file has been generated from package.yaml by hpack version 0.33.0.
44--
55-- see: https://github.com/sol/hpack
66--
7−-- hash: 0c0c35e1dca1d6c52e5bc61591cbd8c0da9d811b83341bbc676c5f8b93b0b20d
7+-- hash: d9b75d0543bde313d954ce10fe6c45bbb21467e7c2d65320dc3a85e15a6ec345
88
99name: roboservant
10−version: 0.1.0.3
10+version: 0.1.0.2
1111synopsis: Automatic session-aware servant testing
1212description: Please see the README on GitHub at <https://github.com/mwotton/roboservant#readme>
1313category: Web
1414homepage: https://github.com/mwotton/roboservant#readme
1515bug-reports: https://github.com/mwotton/roboservant/issues
16+author: Mark Wotton, Samuel Schlesinger
17+maintainer: mwotton@gmail.com
1618copyright: 2020 Mark Wotton, Samuel Schlesinger
1719license: BSD3
1820license-file: LICENSE
@@ -28,17 +30,11 @@ source-repository head
2830library
2931 exposed-modules:
3032 Roboservant
31− Roboservant.Client
3233 Roboservant.Direct
33− Roboservant.Server
3434 Roboservant.Types
3535 Roboservant.Types.Breakdown
36− Roboservant.Types.BuildFrom
37− Roboservant.Types.Config
38− Roboservant.Types.Internal
39− Roboservant.Types.Orphans
36+ Roboservant.Types.FlattenServer
4037 Roboservant.Types.ReifiedApi
41− Roboservant.Types.ReifiedApi.Server
4238 other-modules:
4339 Paths_roboservant
4440 hs-source-dirs:
@@ -50,8 +46,6 @@ library
5046 , containers
5147 , dependent-map
5248 , dependent-sum
53− , hashable
54− , http-types
5549 , lifted-base
5650 , monad-control
5751 , mtl
@@ -62,62 +56,15 @@ library
6256 , servant-server
6357 , string-conversions
6458 , text
65− , time
66− , unordered-containers
6759 , vinyl
6860 default-language: Haskell2010
6961
70−test-suite example
71− type: exitcode-stdio-1.0
72− main-is: Example.lhs
73− other-modules:
74− Paths_roboservant
75− hs-source-dirs:
76− ./
77− ghc-options: -Wall -fwrite-ide-info -hiedir=.hie -pgmL markdown-unlit
78− build-tool-depends:
79− markdown-unlit:markdown-unlit
80− build-depends:
81− aeson
82− , base >=4.7 && <5
83− , bytestring
84− , containers
85− , dependent-map
86− , dependent-sum
87− , hashable
88− , hspec
89− , hspec-core
90− , http-client
91− , http-types
92− , lifted-base
93− , monad-control
94− , mtl
95− , random
96− , roboservant
97− , servant
98− , servant-client
99− , servant-flatten
100− , servant-server
101− , string-conversions
102− , text
103− , time
104− , unordered-containers
105− , vinyl
106− , warp
107− default-language: Haskell2010
108−
10962test-suite roboservant-test
11063 type: exitcode-stdio-1.0
11164 main-is: Spec.hs
11265 other-modules:
113− Breakdown
11466 Foo
11567 Headers
116− Nested
117− Post
118− Product
119− Put
120− QueryParams
12168 Seeded
12269 UnsafeIO
12370 Valid
@@ -132,13 +79,8 @@ test-suite roboservant-test
13279 , containers
13380 , dependent-map
13481 , dependent-sum
135− , hashable
13682 , hspec
13783 , hspec-core
138− , hspec-wai
139− , http-api-data
140− , http-client
141− , http-types
14284 , lifted-base
14385 , monad-control
14486 , mtl
@@ -150,9 +92,5 @@ test-suite roboservant-test
15092 , servant-server
15193 , string-conversions
15294 , text
153− , time
154− , unordered-containers
15595 , vinyl
156− , wai
157− , warp
15896 default-language: Haskell2010
src/Roboservant.hs+3−6
@@ -1,8 +1,5 @@
1−module Roboservant
2− ( module Roboservant.Direct,
3− module Roboservant.Types,
4− )
5−where
1+module Roboservant (module Roboservant.Direct
2+ ,module Roboservant.Types.Breakdown) where
63
74import Roboservant.Direct
8−import Roboservant.Types
5+import Roboservant.Types.Breakdown
src/Roboservant/Client.hsdeleted−127
@@ -1,127 +0,0 @@
1−{-# LANGUAGE RankNTypes #-}
2−{-# LANGUAGE TypeOperators #-}
3−{-# LANGUAGE NamedFieldPuns #-}
4−{-# LANGUAGE DataKinds #-}
5−{-# LANGUAGE AllowAmbiguousTypes #-}
6−{-# LANGUAGE DataKinds #-}
7−{-# LANGUAGE GADTs #-}
8−{-# LANGUAGE PolyKinds #-}
9−{-# LANGUAGE ScopedTypeVariables #-}
10−{-# LANGUAGE TypeApplications #-}
11−
12−{-# LANGUAGE UndecidableInstances #-}
13−{-# LANGUAGE TypeFamilies #-}
14−{-# LANGUAGE FlexibleContexts #-}
15−-- should all the NormalizeFunction instances be in one place?
16−{-# OPTIONS_GHC -fno-warn-orphans #-}
17−module Roboservant.Client where
18−
19−import Data.Proxy
20−import Servant.Client
21−import Roboservant.Types
22−import Roboservant(Report, fuzz')
23−import Servant
24−import Data.Bifunctor
25−import Data.List.NonEmpty (NonEmpty)
26−import Data.Dynamic (Dynamic,Typeable)
27−import qualified Data.Vinyl.Curry as V
28−import qualified Data.Text as T
29−import Control.Monad.Reader
30−import Data.Hashable
31−import Network.HTTP.Types.Status
32−
33−-- fuzz :: forall api.
34−-- (FlattenServer api, ToReifiedApi (Endpoints api)) =>
35−-- Server api ->
36−-- Config ->
37−-- IO (Maybe Report)
38−-- fuzz s = fuzz' (reifyServer s)
39−-- -- todo: how do we pull reifyServer out?
40−-- where reifyServer :: (FlattenServer api, ToReifiedApi (Endpoints api))
41−-- => Server api -> ReifiedApi
42−-- reifyServer server = toReifiedApi (flattenServer @api server) (Proxy @(Endpoints api))
43−
44−fuzz :: forall api . (ToReifiedClientApi (Endpoints api), FlattenClient api, HasClient ClientM api)
45− => ClientEnv -> Config -> IO (Maybe Report)
46−fuzz clientEnv
47− = fuzz'
48− (toReifiedClientApi
49− (flattenClient @api apiClient) (Proxy @(Endpoints api)) clientEnv)
50− where apiClient = client (Proxy @api)
51−
52−
53−
54−class ToReifiedClientApi api where
55− toReifiedClientApi :: ClientBundled api -> Proxy api -> ClientEnv -> ReifiedApi
56−
57−data ClientBundled endpoints where
58− AClientEndpoint :: Client ClientM endpoint -> ClientBundled endpoints -> ClientBundled (endpoint ': endpoints)
59− NoClientEndpoints :: ClientBundled '[]
60−
61−
62−class FlattenClient api where
63− flattenClient :: Client ClientM api -> ClientBundled (Endpoints api)
64−
65−instance
66− ( NormalizeFunction (Client ClientM endpoint)
67− , Normal (Client ClientM endpoint) ~ V.Curried (EndpointArgs endpoint) (ReaderT ClientEnv IO (Either InteractionError (NonEmpty (Dynamic,Int))))
68− , ToReifiedClientApi endpoints
69− , V.RecordCurry' (EndpointArgs endpoint)
70− , ToReifiedEndpoint endpoint) =>
71− ToReifiedClientApi (endpoint : endpoints) where
72− toReifiedClientApi (endpoint `AClientEndpoint` endpoints) _ clientEnv =
73− (0, ReifiedEndpoint
74− { reArguments = reifiedEndpointArguments @endpoint
75− , reEndpointFunc = foo (normalize endpoint)
76− }
77− )
78− : (map . first) (+1)
79− (toReifiedClientApi endpoints (Proxy @endpoints) clientEnv)
80− where
81−
82− foo :: V.Curried (EndpointArgs endpoint) (ReaderT ClientEnv IO ResultType)
83− -> V.Curried (EndpointArgs endpoint) (IO ResultType)
84− foo = mapCurried @(EndpointArgs endpoint) @(ReaderT ClientEnv IO ResultType) (`runReaderT` clientEnv)
85−
86−mapCurried :: forall ts a b. V.RecordCurry' ts => (a -> b) -> V.Curried ts a -> V.Curried ts b
87−mapCurried f g = V.rcurry' @ts $ f . V.runcurry' g
88−
89−type ResultType = Either InteractionError (NonEmpty (Dynamic,Int))
90−-- runClientM :: ClientM a -> ClientEnv -> IO (Either ServantError a)
91−
92−
93−instance (Typeable x, Hashable x, Breakdown x) => NormalizeFunction (ClientM x) where
94− type Normal (ClientM x) = ReaderT ClientEnv IO (Either InteractionError (NonEmpty (Dynamic,Int)))
95− normalize c = ReaderT $
96− fmap (bimap renderClientError breakdown) . runClientM c
97− where
98− renderClientError :: ClientError -> InteractionError
99− renderClientError err = case err of
100− FailureResponse _ Response{responseStatusCode} -> InteractionError textual (responseStatusCode == status500)
101− _ -> InteractionError textual True
102−
103− where textual = T.pack $ show err
104−instance ToReifiedClientApi '[] where
105− toReifiedClientApi NoClientEndpoints _ _ = []
106−
107−
108−instance
109− ( FlattenClient api,
110− Endpoints endpoint ~ '[endpoint]
111− ) =>
112− FlattenClient (endpoint :<|> api)
113− where
114− flattenClient (endpoint :<|> c) = endpoint `AClientEndpoint` flattenClient @api c
115−
116−instance
117− (
118− Endpoints api ~ '[api]
119− ) =>
120− FlattenClient (x :> api)
121− where
122− flattenClient c = c `AClientEndpoint` NoClientEndpoints
123−
124−
125−instance FlattenClient (Verb method statusCode contentTypes responseType)
126− where
127− flattenClient c = c `AClientEndpoint` NoClientEndpoints
src/Roboservant/Direct.hs+174−248
@@ -1,311 +1,237 @@
11{-# LANGUAGE AllowAmbiguousTypes #-}
2+{-# LANGUAGE DataKinds #-}
3+{-# LANGUAGE DerivingStrategies #-}
24{-# LANGUAGE FlexibleContexts #-}
5+{-# LANGUAGE FlexibleInstances #-}
36{-# LANGUAGE GADTs #-}
7+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
48{-# LANGUAGE LambdaCase #-}
59{-# LANGUAGE PolyKinds #-}
610{-# LANGUAGE RankNTypes #-}
711{-# LANGUAGE RecordWildCards #-}
812{-# LANGUAGE ScopedTypeVariables #-}
13+{-# LANGUAGE StandaloneDeriving #-}
914{-# LANGUAGE TupleSections #-}
10−
15+{-# LANGUAGE TypeApplications #-}
1116{-# LANGUAGE TypeFamilies #-}
17+{-# LANGUAGE TypeOperators #-}
1218{-# LANGUAGE UndecidableInstances #-}
1319
1420module Roboservant.Direct
15− ( fuzz',
16− Config (..),
17− -- TODO come up with something smarter than exporting all this, we should
18− -- have some nice error-display functions
19− RoboservantException (..),
20− FuzzState (..),
21− FuzzOp (..),
22− FailureType (..),
23− Report (..),
21+ ( fuzz, Config(..)
2422 )
2523where
2624
27−import Control.Exception.Lifted
28− ( Exception,
29− Handler (Handler),
30− SomeAsyncException,
31− SomeException,
32− catch,
33− catches,
34− handle,
35− throw,
36− )
37−import Control.Monad.State.Strict
38− ( MonadIO (..),
39− MonadState (get),
40− StateT (runStateT),
41− modify',
42− )
43−import Control.Monad.Trans.Control (MonadBaseControl)
44−import qualified Data.Dependent.Map as DM
45−import Data.Dynamic (Dynamic (..))
46−import qualified Data.IntSet as IntSet
47−import Data.List.NonEmpty (NonEmpty (..))
48−import qualified Data.List.NonEmpty as NEL
25+import Control.Exception.Lifted(throw,Handler(..), Exception,SomeException,SomeAsyncException, catch, catches)
26+import Control.Monad(void,replicateM,forM_)
27+import Control.Monad.State.Strict(MonadState,MonadIO,get,modify',liftIO,execStateT)
28+import Control.Monad.Trans.Control(MonadBaseControl)
29+import Data.Dependent.Map (DMap)
30+import Data.Dynamic (Dynamic(..), dynApply, dynTypeRep, fromDynamic)
31+import Data.List.NonEmpty (NonEmpty(..))
32+import Data.Map.Strict(Map)
4933import Data.Maybe (mapMaybe)
50−import qualified Data.Set as Set
51−import Data.Time.Clock (UTCTime, addUTCTime, getCurrentTime)
52−import qualified Data.Vinyl as V
53−import qualified Data.Vinyl.Curry as V
34+import Data.Typeable (TypeRep)
35+import Servant (Endpoints, Proxy (Proxy), Server, ServerError(..))
36+import System.Random(StdGen,randomR,mkStdGen)
37+import System.Timeout.Lifted(timeout)
38+import GHC.Generics ((:*:)(..))
5439import qualified Data.Vinyl.Functor as V
55−import GHC.Generics ((:*:) (..))
40+import qualified Data.Vinyl.Curry as V
41+import qualified Data.Dependent.Map as DM
42+import qualified Data.Vinyl as V
43+import qualified Data.List.NonEmpty as NEL
44+import qualified Data.Map.Strict as Map
45+import qualified Type.Reflection as R
46+
47+import Roboservant.Types.Breakdown
5648import Roboservant.Types
5749 ( ApiOffset (..),
58− Argument (..),
59− InteractionError(..),
60− Provenance (..),
61− ReifiedApi,
62− ReifiedEndpoint (..),
63− Stash (..),
64− StashValue (..),
65− TypedF,
66− )
67−import Roboservant.Types.Config
50+ FlattenServer (..),
51+-- ReifiedApi,
52+ ToReifiedApi (..),
53+ ReifiedEndpoint(..),
54+ Argument(..),
55+ TypedF
6856
69−import System.Random (Random (randomR), StdGen, mkStdGen)
70−import qualified Type.Reflection as R
57+ )
7158
7259data RoboservantException
73− = RoboservantException
74− { failureReason :: FailureType,
75− serverException :: Maybe SomeException,
76− fuzzState :: FuzzState
77− }
60+ = RoboservantException FailureType (Maybe SomeException) FuzzState
7861 deriving (Show)
79−
62+-- we believe in nussink, lebowski
8063instance Exception RoboservantException
8164
8265data FailureType
8366 = ServerCrashed
8467 | CheckerFailed
8568 | NoPossibleMoves
86− | InsufficientCoverage Double
87− deriving (Show, Eq)
88−
89−data FuzzOp
90− = FuzzOp
91− { apiOffset :: ApiOffset,
92− provenance :: [Provenance]
93− }
94− deriving (Show, Eq)
95−
96−data FuzzState
97− = FuzzState
98− { path :: [FuzzOp],
99− stash :: Stash,
100− currentRng :: StdGen
101− }
102− deriving (Show)
103−
104−data EndpointOption
105− = forall as.
106− (V.RecordToList as, V.RMap as) =>
107− EndpointOption
108− { eoCall :: V.Curried as (IO (Either InteractionError (NonEmpty (Dynamic, Int)))),
109− eoArgs :: V.Rec (TypedF StashValue) as
110− }
111−
112−data StopReason
113− = TimedOut
114− | HitMaxIterations
115− deriving (Show, Eq)
116−
117−data Report
118− = Report
119− { textual :: String,
120− rsException :: RoboservantException
121− }
122− deriving (Show)
123−
124−
125−
126−-- fuzzClient :: Client api -> Config -> IO (Maybe Report)
127−-- fuzzClient = undefined
128−
129−
130−
131−fuzz' ::
132− ReifiedApi ->
133− Config ->
134− IO (Maybe Report)
135−fuzz' reifiedApi Config {..} = handle (pure . Just . formatException) $ do
69+ deriving (Show,Eq)
70+
71+
72+data FuzzOp = FuzzOp ApiOffset [Provenance]
73+ deriving (Show,Eq)
74+
75+data Config
76+ = Config
77+ { seed :: [Dynamic]
78+ , maxRuntime :: Int -- seconds to test for
79+ , maxReps :: Int
80+ , rngSeed :: Int
81+ }
82+
83+data FuzzState = FuzzState
84+ { path :: [FuzzOp]
85+ , stash :: Stash
86+ , currentRng :: StdGen
87+ }
88+ deriving Show
89+
90+data EndpointOption = forall as. (V.RecordToList as, V.RMap as) => EndpointOption
91+ { eoCall :: V.Curried as (IO (Either ServerError (NonEmpty Dynamic)))
92+ , eoArgs :: V.Rec (TypedF StashValue) as
93+ }
94+
95+fuzz :: forall api. (FlattenServer api, ToReifiedApi (Endpoints api))
96+ => Server api
97+ -> Config
98+ -> IO ()
99+ -> IO ()
100+fuzz server Config{..} checker = do
136101 let path = []
137102 stash = addToStash seed mempty
138103 currentRng = mkStdGen rngSeed
139− deadline :: UTCTime <- addUTCTime (realToFrac $ maxRuntime * 1000000) <$> getCurrentTime
140− (stopreason, _fs) <-
141− runStateT
142− (untilDone (maxReps, deadline) go <* (evaluateCoverage =<< get))
143− FuzzState {..}
144− logInfo $ show stopreason
145− pure Nothing
104+ -- either we time out without finding an error, which is fine, or we find an error
105+ -- and throw an exception that propagates through this.
106+
107+ void $ timeout (maxRuntime * 1000000) ( execStateT (replicateM maxReps go) FuzzState{..})
108+ forM_ reifiedApi $ \(offset, ReifiedEndpoint{..}) ->
109+ print (
110+ offset
111+ , recordToList' (\(tr :*: _) -> R.SomeTypeRep tr) reArguments
112+ )
146113 where
147− -- something less terrible later
148− formatException :: RoboservantException -> Report
149− formatException r@(RoboservantException failureType exception _state) =
150− Report
151− (unlines [show failureType, show exception])
152− r
153− displayDiagnostics FuzzState {..} = liftIO $
154− logInfo $ unlines $
155− ["api endpoints covered"]
156− <> (map show . Set.toList . Set.fromList $ map apiOffset path)
157− <> ["", "types in stash"]
158− <> DM.foldrWithKey (\_ v r -> (show . NEL.length . getStashValue $ v) : r) [] (getStash stash)
159− -- <> (map (show . NEL.length . getStashValue ) $ DM.assocs (getStash stash))
160− -- $ \_k v ->
161− -- (show . NEL.length $ getStashValue v))
162114
163− evaluateCoverage f@FuzzState {..}
164− | coverage > coverageThreshold = pure ()
165− | otherwise = do
166− displayDiagnostics f
167− throw $ RoboservantException (InsufficientCoverage coverage) Nothing f
168− where
169− hitRoutes = fromIntegral . Set.size . Set.fromList $ map apiOffset path
170− totalRoutes = fromIntegral routeCount
171− coverage = hitRoutes / totalRoutes
172− untilDone :: MonadIO m => (Integer, UTCTime) -> m a -> m StopReason
173− untilDone (0, _) _ = pure HitMaxIterations
174− untilDone (n, deadline) action = do
175− now <- liftIO getCurrentTime
176− if now > deadline
177− then pure TimedOut
178− else do
179− _ <- action
180− untilDone (n -1, deadline) action
115+ reifiedApi = toReifiedApi (flattenServer @api server) (Proxy @(Endpoints api))
181116
182− routeCount = length reifiedApi
183− elementOrFail ::
184− (MonadState FuzzState m, MonadIO m) =>
185− [a] ->
186− m a
117+ elementOrFail :: (MonadState FuzzState m, MonadIO m)
118+ => [a] -> m a
187119 elementOrFail [] = liftIO . throw . RoboservantException NoPossibleMoves Nothing =<< get
188120 elementOrFail l = do
189121 st <- get
190− let (index, newGen) = randomR (0, length l - 1) (currentRng st)
191− modify' $ \st' -> st' {currentRng = newGen}
122+ let (index,newGen) = randomR (0, length l - 1) (currentRng st)
123+ modify' $ \st' -> st' { currentRng = newGen }
192124 pure (l !! index)
193− withOp ::
194− (MonadState FuzzState m, MonadIO m) =>
195− ( forall as.
196− (V.RecordToList as, V.RMap as) =>
197− FuzzOp ->
198− V.Curried as (IO (Either InteractionError (NonEmpty (Dynamic, Int)))) ->
199− V.Rec (TypedF V.Identity) as ->
200− m r
201− ) ->
202− m r
125+
126+ withOp :: (MonadState FuzzState m, MonadIO m)
127+ => (forall as. (V.RecordToList as, V.RMap as)
128+ => FuzzOp
129+ -> V.Curried as (IO (Either ServerError (NonEmpty Dynamic)))
130+ -> V.Rec (TypedF V.Identity) as -> m r
131+ )
132+ -> m r
203133 withOp callback = do
204134 -- choose a call to make, from the endpoints with fillable arguments.
205− (offset, EndpointOption {..}) <- elementOrFail . options =<< get
206− r <-
207− V.rtraverse
208− ( \(tr :*: StashValue svs _) ->
209− elementOrFail $
210− zipWith
211− (\i xy -> V.Const i :*: tr :*: xy)
212− [0 ..]
213− (NEL.toList svs)
214− )
215− eoArgs
216− let pathSegment =
217− FuzzOp offset $
218− recordToList'
219− (\(V.Const index :*: tr :*: _) -> Provenance (R.SomeTypeRep tr) index)
220− r
221− argValues =
222− V.rmap
223− (\(_ :*: tr :*: (_, x)) -> tr :*: V.Identity x)
135+ (offset, EndpointOption{..}) <- elementOrFail . options =<< get
136+ r <- V.rtraverse
137+ (\(tr :*: StashValue svs) -> elementOrFail $
138+ zipWith (\i xy -> V.Const i :*: tr :*: xy)
139+ [0..]
140+ (NEL.toList svs)
141+ )
142+ eoArgs
143+ let pathSegment = FuzzOp offset $
144+ recordToList'
145+ (\(V.Const index :*: tr :*: _) -> Provenance (R.SomeTypeRep tr) index)
224146 r
225− modify' (\f -> f {path = path f <> [pathSegment]})
147+ argValues = V.rmap
148+ (\(_ :*: tr :*: (_, x)) -> tr :*: V.Identity x)
149+ r
150+ modify' (\f -> f { path = path f <> [pathSegment] })
226151 callback pathSegment eoCall argValues
227152 where
228153 options :: FuzzState -> [(ApiOffset, EndpointOption)]
229− options FuzzState {..} =
154+ options FuzzState{..} =
230155 mapMaybe
231− ( \(offset, ReifiedEndpoint {..}) -> do
232− args <- V.rtraverse (\(tr :*: Argument bf) -> (tr :*:) <$> bf stash) reArguments
233− pure (offset, EndpointOption reEndpointFunc args)
156+ ( \(offset, ReifiedEndpoint{..}) -> do
157+ args <- V.rtraverse (\(tr :*: Argument bf) -> (tr :*:) <$> bf stash) reArguments
158+ pure (offset, EndpointOption reEndpointFunc args)
234159 )
235160 reifiedApi
236− execute ::
237− (MonadState FuzzState m, MonadIO m, V.RecordToList as, V.RMap as) =>
238− FuzzOp ->
239− V.Curried as (IO (Either InteractionError (NonEmpty (Dynamic, Int)))) ->
240− V.Rec (TypedF V.Identity) as ->
241− m ()
161+
162+ execute :: (MonadState FuzzState m, MonadIO m, V.RecordToList as, V.RMap as)
163+ => FuzzOp
164+ -> V.Curried as (IO (Either ServerError (NonEmpty Dynamic)))
165+ -> V.Rec (TypedF V.Identity) as
166+ -> m ()
242167 execute fuzzop func args = do
243− (liftIO . logInfo . show . (fuzzop,) . stash) =<< get
168+ liftIO $ print fuzzop
169+ st <- get
170+ let showable = unlines $ ("args":map show argTypes)
171+ <> ["fuzzop"
172+ , show fuzzop
173+ -- ,"dyncall"
174+ -- ,show (dynTypeRep dyncall)
175+ ,"state"
176+ ,show st]
177+ liftIO $ putStrLn showable
244178 liftIO (V.runcurry' func argVals) >>= \case
245− Left (e::InteractionError) ->
246− if fatalError e
247− then throw e
248− else pure ()
249− Right (dyn :: NEL.NonEmpty (Dynamic, Int)) ->
250− modify'
251− ( \fs@FuzzState {..} ->
252− fs {stash = addToStash (NEL.toList dyn) stash}
253− )
179+ -- parameterise this
180+ Left (serverError :: ServerError) ->
181+ case errHTTPCode serverError of
182+ 500 -> throw serverError
183+ _ -> do
184+ liftIO $ print ("ignoring non-500 error" , serverError)
185+ Right (dyn :: NEL.NonEmpty Dynamic) -> do
186+ liftIO $ print ("storing", fmap dynTypeRep dyn)
187+ modify' (\fs@FuzzState{..} ->
188+ fs { stash = addToStash (NEL.toList dyn) stash } )
254189 where
255190 argVals = V.rmap (\(_ :*: V.Identity x) -> V.Identity x) args
256− -- argTypes = recordToList' (\(tr :*: _) -> R.SomeTypeRep tr) args
257− go ::
258− (MonadState FuzzState m, MonadIO m, MonadBaseControl IO m) =>
259− m ()
191+ argTypes = recordToList' (\(tr :*: _) -> R.SomeTypeRep tr) args
192+
193+ go :: (MonadState FuzzState m, MonadIO m, MonadBaseControl IO m)
194+ => m ()
260195 go = withOp $ \op func args -> do
261− catches
262− (execute op func args)
263− [ Handler (\(e :: SomeAsyncException) -> throw e),
264− Handler
265− ( \(e :: SomeException) ->
266− throw . RoboservantException ServerCrashed (Just e) =<< get
267− )
196+ catches (execute op func args)
197+ [ Handler (\(e :: SomeAsyncException) -> throw e)
198+ , Handler (\(e :: SomeException) -> throw . RoboservantException ServerCrashed (Just e) =<< get)
268199 ]
269− catch
270− (liftIO healthCheck)
271− (\(e :: SomeException) -> throw . RoboservantException CheckerFailed (Just e) =<< get)
272−
273−addToStash ::
274− [(Dynamic, Int)] ->
275− Stash ->
276− Stash
200+ catch (liftIO checker)
201+ (\(e :: SomeException) -> throw . RoboservantException CheckerFailed (Just e) =<< get)
202+
203+ -- actions <-
204+ -- forAll $ do
205+ -- Gen.sequential
206+ -- (Range.linear 1 100)
207+ -- emptyState
208+ -- (fmap preload seed <> [callEndpoint reifiedApi])
209+ -- executeSequential emptyState actions
210+addToStash :: [Dynamic]
211+ -> Stash
212+ -> Stash
277213addToStash result stash =
278− foldr
279− ( \(Dynamic tr x, hashed) (Stash dict) ->
280− Stash $
281− DM.insertWith
282− renumber
283− tr
284− (StashValue (([Provenance (R.SomeTypeRep tr) 0], x) :| []) (IntSet.singleton hashed))
285− dict
214+ foldr (\(Dynamic tr x) (Stash dict) -> Stash $
215+ DM.insertWith renumber tr (StashValue (([Provenance (R.SomeTypeRep tr) 0],x):|[])) dict
216+ -- in DM.insertWith renumber _ (pure ([Provenance tr 0],x)) dict
286217 )
287218 stash
288219 result
220+-- Map.insertWith (flip (<>)) (dynTypeRep result) (_pure result) stash })
289221 where
290− renumber ::
291− StashValue a ->
292− StashValue a ->
293− StashValue a
294− renumber (StashValue singleDyn singleHash) orig@(StashValue l intSet)
295− | not $ IntSet.null (singleHash `IntSet.intersection` intSet) = orig
296− | otherwise =
297− StashValue
298− ( case NEL.toList singleDyn of
299− [([Provenance tr _], dyn)] ->
300− l <> pure ([Provenance tr (length (NEL.last l) + 1)], dyn)
301− _ -> error "should be impossible"
302− )
303− (IntSet.union singleHash intSet)
222+ renumber :: StashValue a
223+ -> StashValue a
224+ -> StashValue a
225+ renumber (StashValue singleDyn) (StashValue l) = StashValue $ case NEL.toList singleDyn of
226+ [([Provenance tr _], dyn)] -> l
227+ <> pure ([Provenance tr (length (NEL.last l) + 1)], dyn)
228+ _ -> error "should be impossible"
229+
304230
305231-- why isn't this in vinyl?
306−recordToList' ::
307− (V.RecordToList as, V.RMap as) =>
308− (forall x. f x -> a) ->
309− V.Rec f as ->
310− [a]
232+recordToList'
233+ :: (V.RecordToList as, V.RMap as)
234+ => (forall x. f x -> a)
235+ -> V.Rec f as
236+ -> [a]
311237recordToList' f = V.recordToList . V.rmap (V.Const . f)
src/Roboservant/Server.hsdeleted−36
@@ -1,36 +0,0 @@
1−{-# LANGUAGE RankNTypes #-}
2−{-# LANGUAGE TypeApplications #-}
3−{-# LANGUAGE PolyKinds #-}
4−{-# LANGUAGE AllowAmbiguousTypes #-}
5−{-# LANGUAGE FlexibleContexts #-}
6−{-# LANGUAGE GADTs #-}
7−{-# LANGUAGE PolyKinds #-}
8−
9−{-# LANGUAGE ScopedTypeVariables #-}
10−{-# LANGUAGE TypeApplications #-}
11−{-# LANGUAGE TypeFamilies #-}
12−{-# LANGUAGE UndecidableInstances #-}
13−
14−module Roboservant.Server (fuzz, module Roboservant.Types ) where
15−
16−import Roboservant.Direct(fuzz',Report)
17−import Roboservant.Types
18− ( FlattenServer (..),
19− ReifiedApi,
20− )
21−import Roboservant.Types.ReifiedApi.Server(ToReifiedApi (..))
22−import Servant (Endpoints, Proxy (Proxy), Server)
23−import Roboservant.Types.Config
24−
25−fuzz :: forall api.
26− (FlattenServer api, ToReifiedApi (Endpoints api)) =>
27− Server api ->
28− Config ->
29− IO (Maybe Report)
30−fuzz s = fuzz' (reifyServer s)
31− -- todo: how do we pull reifyServer out?
32− where reifyServer :: (FlattenServer api, ToReifiedApi (Endpoints api))
33− => Server api -> ReifiedApi
34− reifyServer server = toReifiedApi (flattenServer @api server) (Proxy @(Endpoints api))
35−-- reifyServer server = toReifiedApi server (Proxy @(Endpoints api))
36−
src/Roboservant/Types.hs+10−11
@@ -2,25 +2,24 @@
22{-# LANGUAGE DataKinds #-}
33{-# LANGUAGE DerivingStrategies #-}
44{-# LANGUAGE FlexibleInstances #-}
5+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
6+{-# LANGUAGE LambdaCase #-}
57{-# LANGUAGE PolyKinds #-}
68{-# LANGUAGE ScopedTypeVariables #-}
9+{-# LANGUAGE StandaloneDeriving #-}
10+{-# LANGUAGE TupleSections #-}
11+{-# LANGUAGE TypeApplications #-}
712{-# LANGUAGE TypeFamilies #-}
13+{-# LANGUAGE TypeOperators #-}
814{-# LANGUAGE UndecidableInstances #-}
915
1016module Roboservant.Types
11− ( module Roboservant.Types.Breakdown,
12− module Roboservant.Types.BuildFrom,
13− module Roboservant.Types.ReifiedApi,
14− module Roboservant.Types.ReifiedApi.Server,
15− module Roboservant.Types.Internal,
16− module Roboservant.Types.Config,
17+ ( module Roboservant.Types.Breakdown
18+ , module Roboservant.Types.FlattenServer
19+ , module Roboservant.Types.ReifiedApi
1720 )
1821where
1922
2023import Roboservant.Types.Breakdown
21−import Roboservant.Types.BuildFrom
22−import Roboservant.Types.Config
23−import Roboservant.Types.Internal
24+import Roboservant.Types.FlattenServer
2425import Roboservant.Types.ReifiedApi
25−import Roboservant.Types.ReifiedApi.Server
26−import Roboservant.Types.Orphans()
src/Roboservant/Types/Breakdown.hs+74−48
@@ -1,81 +1,107 @@
11{-# LANGUAGE AllowAmbiguousTypes #-}
22{-# LANGUAGE DataKinds #-}
3−{-# LANGUAGE FlexibleContexts #-}
4−{-# LANGUAGE DerivingVia #-}
3+{-# LANGUAGE DefaultSignatures #-}
4+{-# LANGUAGE DeriveFunctor #-}
5+{-# LANGUAGE DerivingStrategies #-}
56{-# LANGUAGE FlexibleInstances #-}
67{-# LANGUAGE GADTs #-}
8+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
79{-# LANGUAGE LambdaCase #-}
810{-# LANGUAGE MultiParamTypeClasses #-}
911{-# LANGUAGE PolyKinds #-}
1012{-# LANGUAGE ScopedTypeVariables #-}
1113{-# LANGUAGE StandaloneDeriving #-}
14+{-# LANGUAGE TupleSections #-}
15+{-# LANGUAGE TypeApplications #-}
1216{-# LANGUAGE TypeFamilies #-}
1317{-# LANGUAGE TypeOperators #-}
1418{-# LANGUAGE UndecidableInstances #-}
1519
1620module Roboservant.Types.Breakdown where
1721
18−import Data.Dynamic (Dynamic)
19−import Data.Hashable
20−import Data.Kind
21−import Data.List.NonEmpty (NonEmpty (..))
22+import Data.Dynamic (Dynamic, fromDynamic, toDyn)
23+import Data.List.NonEmpty (NonEmpty(..))
2224import qualified Data.List.NonEmpty as NEL
23−import Data.Typeable (Typeable)
24−import GHC.Generics
25−import Roboservant.Types.Internal
26−import Servant
27−import Roboservant.Types.Orphans()
28−
29−breakdown ::
30− (Hashable x, Typeable x, Breakdown x) =>
31− x ->
32− NonEmpty (Dynamic, Int)
33−breakdown x = hashedDyn x :| breakdownExtras x
25+import qualified Data.Map.Strict as Map
26+import Data.Map.Strict (Map)
27+import Data.Maybe (fromMaybe)
28+import Data.Proxy (Proxy (..))
29+import Data.Typeable (TypeRep, Typeable, typeRep)
30+import qualified Data.Dependent.Map as DM
31+import Data.Dependent.Map (DMap)
32+import qualified Type.Reflection as R
33+import Data.Dependent.Sum
34+import Data.Kind
3435
35−class Breakdown x where
36− breakdownExtras :: x -> [(Dynamic, Int)]
36+data Provenance
37+ = Provenance R.SomeTypeRep Int
38+ deriving (Show,Eq)
39+
40+newtype StashValue a = StashValue { getStashValue :: NonEmpty ([Provenance], a) }
41+ deriving (Functor, Show)
42+
43+-- wrap in newtype to give a custom Show instance, since the normal
44+-- instance for DMap is not happy since StashValue needs Show a to show
45+newtype Stash = Stash { getStash :: DMap R.TypeRep StashValue }
46+ deriving (Semigroup, Monoid)
3747
38−instance (Hashable x, Typeable x) => Breakdown (Atom x) where
39− breakdownExtras _ = []
48+instance Show Stash where
49+ showsPrec i (Stash x) = showsPrec i $
50+ Map.fromList . map (\(tr :=> StashValue vs) -> (R.SomeTypeRep tr, fmap fst vs)) $ DM.toList x
4051
41−deriving via (Atom ()) instance Breakdown ()
52+class Typeable x => BuildFrom (x :: Type) where
53+ buildFrom :: Stash -> Maybe (StashValue x)
54+ default buildFrom :: Stash -> Maybe (StashValue x)
55+ buildFrom = DM.lookup R.typeRep . getStash
4256
43−deriving via (Atom Int) instance Breakdown Int
44−deriving via (Atom Char) instance Breakdown Char
57+ -- (fmap promisedDyn . NEL.toList) . Map.lookup (typeRep (Proxy @x))
4558
46−deriving via (Compound (Maybe x)) instance (Typeable x, Hashable x, Breakdown x) => Breakdown (Maybe x)
4759
48−instance (Hashable x, Typeable x, Breakdown x) => Breakdown [x] where
49− breakdownExtras stash = concatMap (NEL.toList . breakdown) stash
60+-- baseLookup :: TypeRep -> Stash -> Maybe (NonEmpty ([Provenance], Dynamic))
61+-- baseLookup tr mmm = -- Map.lookup (typeRep (Proxy @x)) mmm
62+-- Map.lookup tr mmm
5063
64+-- | only use this when we are using the internal typerep map.
65+promisedDyn :: Typeable a => Dynamic -> a
66+promisedDyn = fromMaybe (error "internal error, typerep map misconstructed") . fromDynamic
67+
68+instance BuildFrom Bool
69+
70+instance (Typeable x, BuildFrom x) => BuildFrom (Maybe x) where
71+ buildFrom dict = Just options
72+ where options :: StashValue (Maybe x)
73+ options = StashValue $
74+ ([],Nothing) :|
75+ (maybe [] (NEL.toList . getStashValue . fmap Just) $ buildFrom @x dict
76+ )
77+
78+class Breakdown x where
79+ breakdown :: x -> NonEmpty Dynamic
80+ default breakdown :: Typeable x => x -> NonEmpty Dynamic
81+ breakdown = pure . toDyn
82+
83+-- | Can't break it down any further -- stuck in your teeth, maybe.
84+newtype Chewy x = Chewy { unChew :: x }
5185
52−class GBreakdown (f :: k -> Type) where
53− gBreakdownExtras :: f a -> [(Dynamic, Int)]
86+instance Breakdown () where
87+ breakdown = pure . toDyn
5488
55−instance (Hashable x, Typeable x, Generic x, GBreakdown (Rep x)) => Breakdown (Compound (x :: Type)) where
56− breakdownExtras = gBreakdownExtras . from . unCompound
89+instance Breakdown Int where
90+ breakdown = pure . toDyn
5791
58−instance GBreakdown f => GBreakdown (M1 S c f) where
59− gBreakdownExtras (M1 f) = gBreakdownExtras f
6092
61−instance GBreakdown b => GBreakdown (M1 D a b) where
62− gBreakdownExtras (M1 f) = gBreakdownExtras f
93+instance Typeable x => Breakdown (Chewy x) where
94+ breakdown x = pure (toDyn x)
6395
64−instance GBreakdown b => GBreakdown (M1 C a b) where
65− gBreakdownExtras (M1 f) = gBreakdownExtras f
96+--let d = toDyn x in Map.fromList [(dynTypeRep d, pure d)]
6697
67−instance (GBreakdown a, GBreakdown b) => GBreakdown (a :*: b) where
68− gBreakdownExtras (a :*: b) = gBreakdownExtras a <> gBreakdownExtras b
98+-- instance (Typeable x, Generic x) => Breakdown x where
99+-- breakdown = Map.fromListWith (<>) . fmap ((dynTypeRep &&& (\x -> NEL.fromList [x])) . toDyn . Generics.to) . _ . Generics.from
69100
70−instance (GBreakdown a, GBreakdown b) => GBreakdown (a :+: b) where
71− gBreakdownExtras = \case
72− L1 a -> gBreakdownExtras a
73− R1 a -> gBreakdownExtras a
74101
75−instance (Hashable a, Typeable a, Breakdown a) => GBreakdown (K1 R a) where
76− gBreakdownExtras (K1 c) = NEL.toList $ breakdown c
102+instance (Typeable a, Breakdown a) => Breakdown [a] where
103+ breakdown x = toDyn x :| mconcat (map (NEL.toList . breakdown) x)
77104
78−instance GBreakdown U1 where
79− gBreakdownExtras U1 = []
105+instance (BuildFrom a) => BuildFrom [a] -- where
106+-- breakdown x = toDyn x :| mconcat (map (NEL.toList . breakdown) x)
80107
81−deriving via (Atom NoContent) instance Breakdown NoContent
src/Roboservant/Types/BuildFrom.hsdeleted−107
@@ -1,107 +0,0 @@
1−{-# LANGUAGE AllowAmbiguousTypes #-}
2−{-# LANGUAGE DerivingVia #-}
3−{-# LANGUAGE FlexibleContexts #-}
4−{-# LANGUAGE FlexibleInstances #-}
5−{-# LANGUAGE PolyKinds #-}
6−{-# LANGUAGE ScopedTypeVariables #-}
7−{-# LANGUAGE StandaloneDeriving #-}
8−{-# LANGUAGE TypeApplications #-}
9−{-# LANGUAGE TypeFamilies #-}
10−{-# LANGUAGE TypeOperators #-}
11−{-# LANGUAGE UndecidableInstances #-}
12−
13−module Roboservant.Types.BuildFrom where
14−
15−import Data.List(nub)
16−import qualified Data.Dependent.Map as DM
17−import Data.Hashable
18−import qualified Data.IntSet as IntSet
19−import Data.Kind
20−import Data.List.NonEmpty (NonEmpty (..))
21−import qualified Data.List.NonEmpty as NEL
22−import Data.Typeable (Typeable)
23−import GHC.Generics
24−import Roboservant.Types.Internal
25−import qualified Type.Reflection as R
26−import Servant(NoContent)
27−import Roboservant.Types.Orphans()
28−
29−buildFrom :: forall x. (Hashable x, BuildFrom x, Typeable x) => Stash -> Maybe (StashValue x)
30−buildFrom = buildStash . buildFrom'
31− where
32− buildStash :: [([Provenance], x)] -> Maybe (StashValue x)
33− buildStash = fmap (foldr1 addStash . fmap promoteToStash) . NEL.nonEmpty
34− promoteToStash :: ([Provenance], x) -> StashValue x
35− promoteToStash (p, x) =
36− StashValue
37− (pure (p, x))
38− (IntSet.singleton (hash x))
39− addStash :: StashValue x -> StashValue x -> StashValue x
40− addStash old (StashValue newVal _) =
41− let insertableVals = NEL.filter ((`IntSet.notMember` stashHash old) . hash) newVal
42− in StashValue
43− (addListToNE (getStashValue old) insertableVals)
44− (IntSet.union (IntSet.fromList . map hash . fmap snd . NEL.toList $ newVal) (stashHash old))
45− addListToNE :: NonEmpty a -> [a] -> NonEmpty a
46− addListToNE ne l = NEL.fromList (NEL.toList ne <> l)
47−
48−buildFrom' :: forall x. (Hashable x, BuildFrom x, Typeable x) => Stash -> [([Provenance], x)]
49−buildFrom' stash =
50− maybe [] (NEL.toList . getStashValue) (DM.lookup R.typeRep (getStash stash))
51− <> extras stash
52−
53−class (Hashable x, Typeable x) => BuildFrom (x :: Type) where
54− extras :: Stash -> [([Provenance], x)]
55−
56−instance (Hashable x, Typeable x) => BuildFrom (Atom x) where
57− extras _ = []
58−
59−deriving via (Atom Bool) instance BuildFrom Bool
60−
61−deriving via (Compound (Maybe x)) instance (Typeable x, Hashable x, BuildFrom x) => BuildFrom (Maybe x)
62−
63−-- this isn't wonderful, but we need a hand-rolled instance for recursive datatypes right now.
64−-- with an arbitrary-ish interface, we could use a size parameter, rng access etc.
65−instance (Eq x, BuildFrom x) => BuildFrom [x] where
66− extras stash =
67− nub $ map (\xs -> (concatMap fst xs, map snd xs)) $ notpowerset $ buildFrom' @x stash
68− where
69− -- powerset creates way too much stuff. something better here eventually.
70− notpowerset xs = []:xs:map pure xs
71−
72−
73−instance (Hashable x, Typeable x, Generic x, GBuildFrom (Rep x)) => BuildFrom (Compound (x :: Type)) where
74− extras stash = fmap (Compound . to) <$> gExtras stash
75−
76−deriving via (Atom Int) instance BuildFrom Int
77−
78−deriving via (Atom Char) instance BuildFrom Char
79−
80−class GBuildFrom (f :: k -> Type) where
81− gExtras :: Stash -> [([Provenance], f a)]
82−
83−instance GBuildFrom b => GBuildFrom (M1 D a b) where
84− gExtras = fmap (fmap M1) . gExtras
85−
86−-- not recursion safe!
87−instance (GBuildFrom a, GBuildFrom b) => GBuildFrom (a :+: b) where
88− gExtras stash =
89− (fmap L1 <$> gExtras stash)
90− <> (fmap R1 <$> gExtras stash)
91−
92−instance (GBuildFrom a, GBuildFrom b) => GBuildFrom (a :*: b) where
93− gExtras stash = [(pa <> pb, a' :*: b') | (pa, a') <- gExtras stash, (pb, b') <- gExtras stash]
94−
95−instance GBuildFrom b => GBuildFrom (M1 C a b) where
96− gExtras = fmap (fmap M1) . gExtras
97−
98−instance GBuildFrom b => GBuildFrom (M1 S a b) where
99− gExtras = fmap (fmap M1) . gExtras
100−
101−instance BuildFrom a => GBuildFrom (K1 i a) where
102− gExtras = fmap (fmap K1) . buildFrom'
103−
104−instance GBuildFrom U1 where
105− gExtras _ = [([], U1)]
106−
107−deriving via (Atom NoContent) instance BuildFrom NoContent
src/Roboservant/Types/Config.hsdeleted−29
@@ -1,29 +0,0 @@
1−module Roboservant.Types.Config where
2−
3−import Data.Dynamic
4−
5−data Config
6− = Config
7− { seed :: [(Dynamic, Int)],
8− maxRuntime :: Double, -- seconds to test for
9− maxReps :: Integer,
10− rngSeed :: Int,
11− coverageThreshold :: Double,
12− logInfo :: String -> IO (),
13− healthCheck :: IO ()
14− }
15−
16−defaultConfig :: Config
17−defaultConfig =
18− Config
19− { seed = [],
20− maxRuntime = 0.5,
21− maxReps = 1000,
22− rngSeed = 0,
23− coverageThreshold = 0,
24− logInfo = const (pure ()),
25− healthCheck = pure ()
26− }
27−
28−noisyConfig :: Config
29−noisyConfig = defaultConfig {logInfo = print}
src/Roboservant/Types/FlattenServer.hsnew file+45
@@ -0,0 +1,45 @@
1+{-# LANGUAGE AllowAmbiguousTypes #-}
2+{-# LANGUAGE DataKinds #-}
3+{-# LANGUAGE GADTs #-}
4+{-# LANGUAGE PolyKinds #-}
5+{-# LANGUAGE ScopedTypeVariables #-}
6+{-# LANGUAGE TypeApplications #-}
7+{-# LANGUAGE TypeOperators #-}
8+{-# LANGUAGE UndecidableInstances #-}
9+
10+module Roboservant.Types.FlattenServer where
11+
12+import Servant
13+
14+
15+data Bundled endpoints where
16+ AnEndpoint :: Server endpoint -> Bundled endpoints -> Bundled (endpoint ': endpoints)
17+ NoEndpoints :: Bundled '[]
18+
19+class FlattenServer api where
20+ flattenServer :: Server api -> Bundled (Endpoints api)
21+
22+instance
23+ ( Endpoints (endpoint :<|> api) ~ (endpoint ': Endpoints api),
24+ Server (endpoint :<|> api) ~ (Server endpoint :<|> Server api),
25+ FlattenServer api
26+ ) =>
27+ FlattenServer (endpoint :<|> api)
28+ where
29+ flattenServer (endpoint :<|> server) = endpoint `AnEndpoint` flattenServer @api server
30+
31+instance
32+ ( HasServer (x :> api) '[],
33+ Endpoints (x :> api) ~ '[x :> api]
34+ ) =>
35+ FlattenServer (x :> api)
36+ where
37+ flattenServer server = server `AnEndpoint` NoEndpoints
38+
39+instance
40+ ( HasServer (Verb method statusCode contentTypes responseType) '[],
41+ Endpoints (Verb method statusCode contentTypes responseType) ~ '[Verb method statusCode contentTypes responseType]
42+ ) =>
43+ FlattenServer (Verb method statusCode contentTypes responseType)
44+ where
45+ flattenServer server = server `AnEndpoint` NoEndpoints
src/Roboservant/Types/Internal.hsdeleted−53
@@ -1,53 +0,0 @@
1−{-# LANGUAGE DeriveFunctor #-}
2−{-# LANGUAGE DeriveGeneric #-}
3−{-# LANGUAGE DerivingStrategies #-}
4−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5−
6−module Roboservant.Types.Internal where
7−
8−import qualified Data.Dependent.Map as DM
9−import Data.Dependent.Map (DMap)
10−import Data.Dependent.Sum
11−import Data.Dynamic (Dynamic, toDyn)
12−import Data.Hashable (Hashable, hash)
13−import Data.IntSet (IntSet)
14−import Data.List.NonEmpty (NonEmpty (..))
15−import qualified Data.Map.Strict as Map
16−import Data.Typeable (Typeable)
17−import GHC.Generics (Generic)
18−import qualified Type.Reflection as R
19−
20−data Provenance
21− = Provenance R.SomeTypeRep Int
22− deriving (Show, Eq, Generic)
23−
24−instance Hashable Provenance
25−
26−data StashValue a
27− = StashValue
28− { getStashValue :: NonEmpty ([Provenance], a),
29− stashHash :: IntSet
30− }
31− deriving (Functor, Show)
32−
33−-- wrap in newtype to give a custom Show instance, since the normal
34−-- instance for DMap is not happy since StashValue needs Show a to show
35−newtype Stash = Stash {getStash :: DMap R.TypeRep StashValue}
36− deriving (Semigroup, Monoid)
37−
38−instance Show Stash where
39− showsPrec i (Stash x) =
40− showsPrec i
41− $ Map.fromList . map (\(tr :=> StashValue vs _) -> (R.SomeTypeRep tr, fmap fst vs))
42− $ DM.toList x
43−
44−-- | Can't be built up from parts, can't be broken down further.
45−newtype Atom x = Atom {unAtom :: x}
46− deriving newtype (Hashable, Typeable,Eq)
47−
48−-- | can be broken down and built up from generic pieces
49−newtype Compound x = Compound {unCompound :: x}
50− deriving newtype (Hashable, Typeable, Eq)
51−
52−hashedDyn :: (Hashable a, Typeable a) => a -> (Dynamic, Int)
53−hashedDyn a = (toDyn a, hash a)
src/Roboservant/Types/Orphans.hsdeleted−7
@@ -1,7 +0,0 @@
1−{-# OPTIONS_GHC -fno-warn-orphans #-}
2−module Roboservant.Types.Orphans where
3−
4−import Servant
5−import Data.Hashable
6−
7−instance Hashable NoContent
src/Roboservant/Types/ReifiedApi.hs+60−76
@@ -1,7 +1,3 @@
1−-- | Description: ways to build a reified api from a servant description.
2−--
3−-- arguably this could be more general and be abstracted away from even relying on servant
4−-- but that's future work.
51
62{-# LANGUAGE AllowAmbiguousTypes #-}
73{-# LANGUAGE DataKinds #-}
@@ -12,33 +8,40 @@
128{-# LANGUAGE GeneralizedNewtypeDeriving #-}
139{-# LANGUAGE LambdaCase #-}
1410{-# LANGUAGE PolyKinds #-}
11+{-# LANGUAGE RecordWildCards #-}
1512{-# LANGUAGE ScopedTypeVariables #-}
13+{-# LANGUAGE StandaloneDeriving #-}
14+{-# LANGUAGE TupleSections #-}
1615{-# LANGUAGE TypeApplications #-}
1716{-# LANGUAGE TypeFamilies #-}
1817{-# LANGUAGE TypeOperators #-}
1918{-# LANGUAGE UndecidableInstances #-}
20−{-# LANGUAGE CPP #-}
19+
2120module Roboservant.Types.ReifiedApi where
2221
23−import Data.Dynamic (Dynamic)
24−import Control.Exception(Exception)
22+
23+import Control.Monad.Except (runExceptT)
24+import Data.Bifunctor
25+import Data.Dependent.Sum
26+import Data.Dynamic (Dynamic, toDyn)
27+import Data.Function ((&))
28+import Data.Kind
2529import Data.List.NonEmpty (NonEmpty)
26−import Data.Typeable (Typeable)
30+import Data.Typeable (TypeRep, Typeable, typeRep)
2731import GHC.Generics ((:*:)(..))
28−import Roboservant.Types.Internal
32+import GHC.TypeLits (Symbol)
2933import Roboservant.Types.Breakdown
30−import Roboservant.Types.BuildFrom
31−import Data.Kind(Type)
34+import Roboservant.Types.FlattenServer
3235import Servant
3336import Servant.API.Modifiers(FoldRequired,FoldLenient)
34−import GHC.TypeLits (Symbol)
3537import qualified Data.Text as T
3638import qualified Data.Vinyl as V
3739import qualified Data.Vinyl.Curry as V
3840import qualified Type.Reflection as R
3941
42+
4043newtype ApiOffset = ApiOffset Int
41− deriving (Eq, Show, Ord)
44+ deriving (Eq, Show)
4245 deriving newtype (Enum, Num)
4346
4447type TypedF = (:*:) R.TypeRep
@@ -49,32 +52,60 @@ newtype Argument a = Argument
4952
5053data ReifiedEndpoint = forall as. (V.RecordToList as, V.RMap as) => ReifiedEndpoint
5154 { reArguments :: V.Rec (TypedF Argument) as
52− , reEndpointFunc :: V.Curried as (IO (Either InteractionError (NonEmpty (Dynamic,Int))))
55+ , reEndpointFunc :: V.Curried as (IO (Either ServerError (NonEmpty Dynamic)))
5356 }
5457
55−instance Show ReifiedEndpoint where
56− show _ = "lol"
58+type ReifiedApi = [(ApiOffset, ReifiedEndpoint)]
59+
60+tagType :: Typeable a => f a -> TypedF f a
61+tagType = (R.typeRep :*:)
62+
63+class ToReifiedApi (endpoints :: [*]) where
64+ toReifiedApi :: Bundled endpoints -> Proxy endpoints -> ReifiedApi
5765
58−class ( V.RecordToList (EndpointArgs endpoint)
66+class ( V.Curried (EndpointArgs endpoint) (Handler (EndpointRes endpoint)) ~ Server endpoint
67+ , V.RecordToList (EndpointArgs endpoint)
5968 , V.RMap (EndpointArgs endpoint)
60− ) => ToReifiedEndpoint (endpoint :: Type) where
69+ ) => ToReifiedEndpoint (endpoint :: *) where
6170 type EndpointArgs endpoint :: [Type]
6271 type EndpointRes endpoint :: Type
6372
6473 reifiedEndpointArguments :: V.Rec (TypedF Argument) (EndpointArgs endpoint)
6574
75+instance ToReifiedApi '[] where
76+ toReifiedApi NoEndpoints _ = []
6677
67−tagType :: Typeable a => f a -> TypedF f a
68−tagType = (R.typeRep :*:)
69−
70−data InteractionError = InteractionError
71− { errorMessage :: T.Text
72− , fatalError :: Bool
73− }
74− deriving Show
75−instance Exception InteractionError
76−
77−
78+instance
79+ ( Typeable (EndpointRes endpoint)
80+ , NormalizeFunction (ServerT endpoint Handler)
81+ , Normal (ServerT endpoint Handler) ~ V.Curried (EndpointArgs endpoint) (IO (Either ServerError (NonEmpty Dynamic)))
82+ , ToReifiedEndpoint endpoint
83+ , ToReifiedApi endpoints, Typeable (ServerT endpoint Handler)
84+ ) =>
85+ ToReifiedApi (endpoint : endpoints)
86+ where
87+ toReifiedApi (endpoint `AnEndpoint` endpoints) _ =
88+ (0, ReifiedEndpoint
89+ { reArguments = reifiedEndpointArguments @endpoint
90+ , reEndpointFunc = normalize endpoint
91+ }
92+ )
93+ : (map . first) (+1)
94+ (toReifiedApi endpoints (Proxy @endpoints))
95+
96+class NormalizeFunction m where
97+ type Normal m
98+ normalize :: m -> Normal m
99+
100+instance NormalizeFunction x => NormalizeFunction (r -> x) where
101+ type Normal (r -> x) = r -> Normal x
102+ normalize = fmap normalize
103+
104+instance (Typeable x, Breakdown x) => NormalizeFunction (Handler x) where
105+ type Normal (Handler x) = IO (Either ServerError (NonEmpty Dynamic))
106+ normalize handler = (runExceptT . runHandler') handler >>= \case
107+ Left serverError -> pure (Left serverError)
108+ Right x -> pure $ Right $ breakdown x
78109
79110instance
80111 (Typeable responseType, Breakdown responseType) =>
@@ -84,12 +115,6 @@ instance
84115 type EndpointRes (Verb method statusCode contentTypes responseType) = responseType
85116 reifiedEndpointArguments = V.RNil
86117
87−instance ToReifiedEndpoint (NoContentVerb method)
88− where
89− type EndpointArgs (NoContentVerb method) = '[]
90− type EndpointRes (NoContentVerb method) = NoContent
91− reifiedEndpointArguments = V.RNil
92−
93118instance
94119 (ToReifiedEndpoint endpoint) =>
95120 ToReifiedEndpoint ((x :: Symbol) :> endpoint)
@@ -98,16 +123,6 @@ instance
98123 type EndpointRes ((x :: Symbol) :> endpoint) = EndpointRes endpoint
99124 reifiedEndpointArguments = reifiedEndpointArguments @endpoint
100125
101−instance
102− (ToReifiedEndpoint endpoint) =>
103− ToReifiedEndpoint (RemoteHost :> endpoint)
104− where
105− type EndpointArgs (RemoteHost :> endpoint) = EndpointArgs endpoint
106− type EndpointRes (RemoteHost :> endpoint) = EndpointRes endpoint
107− reifiedEndpointArguments = reifiedEndpointArguments @endpoint
108−
109−
110−
111126instance
112127 (ToReifiedEndpoint endpoint) =>
113128 ToReifiedEndpoint (Description s :> endpoint)
@@ -151,23 +166,6 @@ instance
151166 V.:& reifiedEndpointArguments @endpoint
152167
153168
154−instance
155− ( BuildFrom paramType
156− , ToReifiedEndpoint endpoint
157− , Show paramType
158− , Eq paramType
159− ) =>
160− ToReifiedEndpoint (QueryParams name paramType :> endpoint)
161− where
162− type EndpointArgs (QueryParams name paramType :> endpoint) = [paramType] ': EndpointArgs endpoint
163− type EndpointRes (QueryParams name paramType :> endpoint) = EndpointRes endpoint
164− reifiedEndpointArguments =
165− tagType (Argument (buildFrom @[paramType]))
166− V.:& reifiedEndpointArguments @endpoint
167−
168−
169−
170−
171169instance
172170 ( BuildFrom (IfRequiredLenient T.Text mods headerType)
173171 , ToReifiedEndpoint endpoint
@@ -180,7 +178,6 @@ instance
180178 tagType (Argument (buildFrom @(IfRequiredLenient T.Text mods headerType)))
181179 V.:& reifiedEndpointArguments @endpoint
182180
183−#if MIN_VERSION_servant(0,17,0)
184181instance
185182 ( BuildFrom (IfLenient String mods captureType)
186183 , ToReifiedEndpoint endpoint) =>
@@ -191,19 +188,6 @@ instance
191188 reifiedEndpointArguments =
192189 tagType (Argument (buildFrom @(IfLenient String mods captureType)))
193190 V.:& reifiedEndpointArguments @endpoint
194−#else
195−instance
196− ( BuildFrom captureType
197− , ToReifiedEndpoint endpoint) =>
198− ToReifiedEndpoint (Capture' mods name captureType :> endpoint)
199− where
200− type EndpointArgs (Capture' mods name captureType :> endpoint) = captureType ': EndpointArgs endpoint
201− type EndpointRes (Capture' mods name captureType :> endpoint) = EndpointRes endpoint
202− reifiedEndpointArguments =
203− tagType (Argument (buildFrom @(captureType)))
204− V.:& reifiedEndpointArguments @endpoint
205−
206−#endif
207191
208192instance
209193 ( BuildFrom (IfLenient String mods requestType)
src/Roboservant/Types/ReifiedApi/Server.hsdeleted−114
@@ -1,114 +0,0 @@
1−{-# LANGUAGE RankNTypes #-}
2−{-# LANGUAGE TypeApplications #-}
3−{-# LANGUAGE PolyKinds #-}
4−{-# LANGUAGE AllowAmbiguousTypes #-}
5−{-# LANGUAGE FlexibleContexts #-}
6−{-# LANGUAGE GADTs #-}
7−{-# LANGUAGE LambdaCase #-}
8−{-# LANGUAGE PolyKinds #-}
9−
10−{-# LANGUAGE ScopedTypeVariables #-}
11−{-# LANGUAGE TypeApplications #-}
12−{-# LANGUAGE TypeFamilies #-}
13−{-# LANGUAGE UndecidableInstances #-}
14−{-# LANGUAGE CPP #-}
15−{-# LANGUAGE DataKinds #-}
16−{-# LANGUAGE TypeOperators #-}
17−{-# LANGUAGE FlexibleInstances #-}
18−{-# LANGUAGE MultiParamTypeClasses #-}
19−module Roboservant.Types.ReifiedApi.Server(module Roboservant.Types.ReifiedApi.Server) where
20−
21−import Servant
22−
23−import Control.Monad.Except (runExceptT)
24−import Data.Bifunctor
25−import Data.Dynamic (Dynamic)
26−import Data.List.NonEmpty (NonEmpty)
27−import Data.Typeable (Typeable)
28−import Roboservant.Types.Breakdown
29−import Roboservant.Types.ReifiedApi
30−
31−import qualified Data.Text as T
32−import qualified Data.Vinyl.Curry as V
33−import Data.Hashable(Hashable)
34−
35−type ReifiedApi = [(ApiOffset, ReifiedEndpoint )]
36−
37−
38−class ToReifiedApi endpoints where
39− toReifiedApi :: Bundled endpoints -> Proxy endpoints -> ReifiedApi
40−
41−instance ToReifiedApi '[] where
42− toReifiedApi NoEndpoints _ = []
43−
44−instance
45− ( NormalizeFunction (ServerT endpoint Handler)
46− , Normal (ServerT endpoint Handler) ~ V.Curried (EndpointArgs endpoint) (IO (Either InteractionError (NonEmpty (Dynamic,Int))))
47− , ToReifiedEndpoint endpoint
48− , ToReifiedApi endpoints
49− ) =>
50− ToReifiedApi (endpoint : endpoints)
51− where
52− toReifiedApi (endpoint `AnEndpoint` endpoints) _ =
53− (0, ReifiedEndpoint
54− { reArguments = reifiedEndpointArguments @endpoint
55− , reEndpointFunc = normalize endpoint
56− }
57− )
58− : (map . first) (+1)
59− (toReifiedApi endpoints (Proxy @endpoints))
60−
61−
62−instance (Typeable x, Hashable x, Breakdown x) => NormalizeFunction (Handler x) where
63− type Normal (Handler x) = IO (Either InteractionError (NonEmpty (Dynamic,Int)))
64− normalize handler = (runExceptT . runHandler') handler >>= \case
65− Left serverError -> pure (Left (renderServerError serverError))
66− where
67− -- | TODO improve this
68− renderServerError :: ServerError -> InteractionError
69− renderServerError s = InteractionError (T.pack $ show s) (errHTTPCode serverError == 500)
70−
71− Right x -> pure $ Right $ breakdown x
72−
73−
74−-- case errHTTPCode serverError of
75−-- 500 -> throw serverError
76−-- _ ->
77−-- liftIO . logInfo . show $ ("ignoring non-500 error", serverError)
78−
79−
80−data Bundled endpoints where
81− -- AnEndpoint :: Server endpoint -> Bundled endpoints -> Bundled (endpoint ': endpoints)
82− AnEndpoint :: Server endpoint -> Bundled endpoints -> Bundled (endpoint ': endpoints)
83− NoEndpoints :: Bundled '[]
84−
85−class FlattenServer api where
86− flattenServer :: Server api -> Bundled (Endpoints api)
87−
88−instance
89− ( FlattenServer api,
90− Endpoints endpoint ~ '[endpoint]
91− ) =>
92− FlattenServer (endpoint :<|> api)
93− where
94− flattenServer (endpoint :<|> server) = endpoint `AnEndpoint` flattenServer @api server
95−
96−instance
97− (
98− Endpoints api ~ '[api]
99− ) =>
100− FlattenServer (x :> api)
101− where
102− flattenServer server = server `AnEndpoint` NoEndpoints
103−
104−instance FlattenServer (Verb method statusCode contentTypes responseType)
105− where
106− flattenServer server = server `AnEndpoint` NoEndpoints
107−
108−class NormalizeFunction m where
109− type Normal m
110− normalize :: m -> Normal m
111−
112−instance NormalizeFunction x => NormalizeFunction (r -> x) where
113− type Normal (r -> x) = r -> Normal x
114− normalize = fmap normalize
stack.yaml+22−5
@@ -1,10 +1,27 @@
1−resolver: lts-20.4
1+resolver: lts-16.16
22packages:
33- .
44
55extra-deps:
6−# - dependent-map-0.4.0.0@sha256:ca2b131046f4340a1c35d138c5a003fe4a5be96b14efc26291ed35fd08c62221,1657
7−# - dependent-sum-0.7.1.0@sha256:5599aa89637db434431b1dd3fa7c34bc3d565ee44f0519bfbc877be1927c2531,2068
86- servant-flatten-0.2@sha256:276896f7c5cdec5b8f8493f6205fded0cc602d050b58fdb09a6d7c85c3bb0837,1234
9−# - constraints-extras-0.3.0.2@sha256:013b8d0392582c6ca068e226718a4fe8be8e22321cc0634f6115505bf377ad26,1853
10−- some-1.0.3
7+- markov-chain-usage-model-0.0.0@sha256:1afa95faeb9213c4d960a669190078b41b89169462b8edd910472980671ba8c0,2112
8+- servant-0.18
9+- servant-client-0.18
10+- servant-client-core-0.18
11+- servant-server-0.18
12+# - hspec-hedgehog-0.0.1.2@sha256:aaeeb50a43d2bce8a1850baa3ddf934a964bfe38c64efd59306b0c1421e55b45,1405
13+- QuickCheck-2.14.1@sha256:01e46d7b0a8d3148288ec977625f62d5516ebb5031a50c63f0453863301b4a79,7736
14+- random-1.2.0
15+- splitmix-0.1.0.1@sha256:22f9662e7e8b173421872241edd39350078a9ed4bb9e9f503948c5b483c79276,5253
16+- vinyl-0.13.0
17+- dependent-sum-0.7.1.0
18+- constraints-extras-0.3.0.2
19+- dependent-map-0.4.0.0
20+- git: https://github.com/hedgehogqa/haskell-hedgehog.git
21+ commit: 03714682586e43b5ddf5c00391035471a6e01238
22+ subdirs:
23+ - hedgehog
24+- git: https://github.com/mwotton/hspec
25+ commit: cd9012414a93cf08f86e409544d4d3a8f282b507
26+ subdirs:
27+ - hspec-core
stack.yaml.lock+112−9
@@ -7,20 +7,123 @@ packages:
77- completed:
88 hackage: servant-flatten-0.2@sha256:276896f7c5cdec5b8f8493f6205fded0cc602d050b58fdb09a6d7c85c3bb0837,1234
99 pantry-tree:
10− sha256: 04f12c7bef2c3f9a25d94eb9489752ed498db8e243069fe95838dbb51df1dcb3
1110 size: 325
11+ sha256: 04f12c7bef2c3f9a25d94eb9489752ed498db8e243069fe95838dbb51df1dcb3
1212 original:
1313 hackage: servant-flatten-0.2@sha256:276896f7c5cdec5b8f8493f6205fded0cc602d050b58fdb09a6d7c85c3bb0837,1234
1414- completed:
15− hackage: some-1.0.3@sha256:938cec5fc0d3a08dde34cfc0acd5a0aa2e9fd489c9dbb64417f16588dfb47af0,2399
15+ hackage: markov-chain-usage-model-0.0.0@sha256:1afa95faeb9213c4d960a669190078b41b89169462b8edd910472980671ba8c0,2112
16+ pantry-tree:
17+ size: 432
18+ sha256: 6871bd9281acf589296d0998a3d62892b036040ab10e74e8a0f356f68c194f4f
19+ original:
20+ hackage: markov-chain-usage-model-0.0.0@sha256:1afa95faeb9213c4d960a669190078b41b89169462b8edd910472980671ba8c0,2112
21+- completed:
22+ hackage: servant-0.18@sha256:2b5c81089540c208b1945b5ca0c3551c862138d71b224a39fa275a62852a5c75,5068
23+ pantry-tree:
24+ size: 2458
25+ sha256: 701b6443e486601dd127b92a28ca052278dea8dc528f013781c1304826e117d8
26+ original:
27+ hackage: servant-0.18
28+- completed:
29+ hackage: servant-client-0.18@sha256:3e3b8d145cf811e19849744ad305aa0bd42301d01ff75e1dac169bc9f5d18221,4589
30+ pantry-tree:
31+ size: 1299
32+ sha256: 445c610d83a6e356ed2a7808af8deac84708be3e44d38cbf76e60fd6105f9715
33+ original:
34+ hackage: servant-client-0.18
35+- completed:
36+ hackage: servant-client-core-0.18@sha256:6702f04f3e5ac47abace5b26ab1948480437334747895407f0697502a403b96a,3593
37+ pantry-tree:
38+ size: 1444
39+ sha256: 20f538ae12091696073a2ac5f3301a4a56686f5e2889b2a9e539c0d61a332cd1
40+ original:
41+ hackage: servant-client-core-0.18
42+- completed:
43+ hackage: servant-server-0.18@sha256:ec7f361bc9848968b1fff8091fa2213b721d3f47549421945c6a7ca1702e22e6,5424
44+ pantry-tree:
45+ size: 2546
46+ sha256: c2470cd8665742cee13faae1db7e3d49d70b027d57c4578e3f7ba66ffc8dc7b4
47+ original:
48+ hackage: servant-server-0.18
49+- completed:
50+ hackage: QuickCheck-2.14.1@sha256:01e46d7b0a8d3148288ec977625f62d5516ebb5031a50c63f0453863301b4a79,7736
51+ pantry-tree:
52+ size: 2315
53+ sha256: 21961d0875fc156cdd97110a6fb277f37aa31faa3c7910895f84c82630e7ba46
54+ original:
55+ hackage: QuickCheck-2.14.1@sha256:01e46d7b0a8d3148288ec977625f62d5516ebb5031a50c63f0453863301b4a79,7736
56+- completed:
57+ hackage: random-1.2.0@sha256:4321209c8faedc034810ea8ed0dbc4a36f1a1df97b75af024219f2f533da57de,6094
58+ pantry-tree:
59+ size: 1259
60+ sha256: 29ff0a7ec5bf123e2c40f849b7af163950854a6c08c68b4a1b4364ce7a515af0
61+ original:
62+ hackage: random-1.2.0
63+- completed:
64+ hackage: splitmix-0.1.0.1@sha256:22f9662e7e8b173421872241edd39350078a9ed4bb9e9f503948c5b483c79276,5253
65+ pantry-tree:
66+ size: 1149
67+ sha256: b9a0c60e2d4786bbb276ce6606027a99615bf703fa3e0f01911e8c6a656c5161
68+ original:
69+ hackage: splitmix-0.1.0.1@sha256:22f9662e7e8b173421872241edd39350078a9ed4bb9e9f503948c5b483c79276,5253
70+- completed:
71+ hackage: vinyl-0.13.0@sha256:0f247cd3f8682b30881a07de18e6fec52d540646fbcb328420049cc8d63cd407,3724
72+ pantry-tree:
73+ size: 1857
74+ sha256: 860fb95820b595161cdbdec5f376100ebae2d14e5ef0dbe311546202f7525d01
75+ original:
76+ hackage: vinyl-0.13.0
77+- completed:
78+ hackage: dependent-sum-0.7.1.0@sha256:5599aa89637db434431b1dd3fa7c34bc3d565ee44f0519bfbc877be1927c2531,2068
79+ pantry-tree:
80+ size: 290
81+ sha256: 9cbfb32b5a8a782b7a1c941803fd517633cb699159b851c1d82267a9e9391b50
82+ original:
83+ hackage: dependent-sum-0.7.1.0
84+- completed:
85+ hackage: constraints-extras-0.3.0.2@sha256:013b8d0392582c6ca068e226718a4fe8be8e22321cc0634f6115505bf377ad26,1853
86+ pantry-tree:
87+ size: 594
88+ sha256: 3ce1012bfb02e4d7def9df19ce80b8cd2b472c691b25b181d9960638673fecd1
89+ original:
90+ hackage: constraints-extras-0.3.0.2
91+- completed:
92+ hackage: dependent-map-0.4.0.0@sha256:ca2b131046f4340a1c35d138c5a003fe4a5be96b14efc26291ed35fd08c62221,1657
93+ pantry-tree:
94+ size: 551
95+ sha256: 5defa30010904d2ad05a036f3eaf83793506717c93cbeb599f40db1a3632cfc5
96+ original:
97+ hackage: dependent-map-0.4.0.0
98+- completed:
99+ subdir: hedgehog
100+ name: hedgehog
101+ version: 1.0.3
102+ git: https://github.com/hedgehogqa/haskell-hedgehog.git
103+ pantry-tree:
104+ size: 2575
105+ sha256: 7e69930fef281dfeb47b2f7c745c26234768e56ffb5490065f607c57146ffbab
106+ commit: 03714682586e43b5ddf5c00391035471a6e01238
107+ original:
108+ subdir: hedgehog
109+ git: https://github.com/hedgehogqa/haskell-hedgehog.git
110+ commit: 03714682586e43b5ddf5c00391035471a6e01238
111+- completed:
112+ subdir: hspec-core
113+ name: hspec-core
114+ version: 2.7.4
115+ git: https://github.com/mwotton/hspec
16116 pantry-tree:
17− sha256: b88dddc6b991cd570c6750f8d309a9feb76938b29a690abdabc4ababb8c7b2f8
18− size: 709
117+ size: 3899
118+ sha256: 6ece11ad28a7f122a502e766eb1b3ef950815c2d16babb0ea8744646f1c1bd70
119+ commit: cd9012414a93cf08f86e409544d4d3a8f282b507
19120 original:
20− hackage: some-1.0.3
121+ subdir: hspec-core
122+ git: https://github.com/mwotton/hspec
123+ commit: cd9012414a93cf08f86e409544d4d3a8f282b507
21124snapshots:
22125- completed:
23− sha256: 3770dfd79f5aed67acdcc65c4e7730adddffe6dba79ea723cfb0918356fc0f94
24− size: 648660
25− url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/20/4.yaml
26− original: lts-20.4
126+ size: 532380
127+ url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/16/16.yaml
128+ sha256: d6b004b095fe2a0b8b14fbc30014ee97e58843b9c9362ddb9244273dda62649e
129+ original: lts-16.16
test/Breakdown.hsdeleted−50
@@ -1,50 +0,0 @@
1−{-# LANGUAGE DataKinds #-}
2−{-# LANGUAGE DeriveGeneric #-}
3−{-# LANGUAGE DerivingStrategies #-}
4−
5−{-# LANGUAGE OverloadedStrings #-}
6−{-# LANGUAGE TypeOperators #-}
7−
8−module Breakdown where
9−
10−import Data.Aeson
11−import Data.Hashable
12−import Data.Typeable (Typeable)
13−import GHC.Generics (Generic)
14−import Servant
15−
16−data Foo = Foo Int String
17− deriving (Generic, Eq, Show, Typeable)
18−
19−instance Hashable Foo
20−
21−instance ToJSON Foo
22−
23−instance FromJSON Foo
24−
25−data SomeSum = A Int | B String
26− deriving (Generic, Eq, Show, Typeable)
27−
28−instance Hashable SomeSum
29−
30−instance ToJSON SomeSum
31−
32−instance FromJSON SomeSum
33−
34−type ProductApi =
35− "item" :> ReqBody '[JSON] Int :> Post '[JSON] ()
36− :<|> "getFoo" :> Get '[JSON] Foo
37−
38−eliminate :: Int -> Handler ()
39−eliminate _ = throwError $ err500 {errBody = "eliminate blew up, oh no!"}
40−
41−productServer :: Server ProductApi
42−productServer = eliminate :<|> pure (Foo 12 "abc")
43−
44−type SumApi =
45− "item" :> ReqBody '[JSON] Int :> Post '[JSON] ()
46− :<|> "getFoo1" :> Get '[JSON] SomeSum
47− :<|> "getFoo2" :> Get '[JSON] SomeSum
48−
49−sumServer :: Server SumApi
50−sumServer = eliminate :<|> pure (B "hi") :<|> pure (A 3)
test/Foo.hs−3
@@ -8,7 +8,6 @@
88module Foo where
99
1010import Data.Aeson
11−import Data.Hashable
1211import Data.Typeable (Typeable)
1312import GHC.Generics (Generic)
1413import Servant
@@ -17,8 +16,6 @@ newtype Foo = Foo Int
1716 deriving (Generic, Eq, Show, Typeable)
1817 deriving newtype (FromHttpApiData, ToHttpApiData)
1918
20−instance Hashable Foo
21−
2219instance ToJSON Foo
2320
2421instance FromJSON Foo
test/Headers.hs+2−4
@@ -8,7 +8,6 @@
88module Headers where
99
1010import Data.Aeson
11−import Data.Hashable
1211import Data.Typeable (Typeable)
1312import GHC.Generics (Generic)
1413import Servant
@@ -17,8 +16,6 @@ newtype Foo = Foo Int
1716 deriving (Generic, Eq, Show, Typeable)
1817 deriving newtype (FromHttpApiData, ToHttpApiData)
1918
20−instance Hashable Foo
21−
2219instance ToJSON Foo
2320
2421instance FromJSON Foo
@@ -35,7 +32,8 @@ combine :: Maybe Foo -> Maybe Foo -> Handler Foo
3532combine (Just (Foo a)) (Just (Foo b)) = pure (Foo (a + b))
3633combine (Just a) Nothing = pure a
3734combine Nothing (Just a) = pure a
38−combine Nothing Nothing = pure (Foo 1)
35+combine Nothing Nothing = pure (Foo 1)
36+
3937
4038eliminate :: Foo -> Handler ()
4139eliminate (Foo a)
test/Nested.hsdeleted−23
@@ -1,23 +0,0 @@
1−{-# LANGUAGE DataKinds #-}
2−
3−{-# LANGUAGE DerivingStrategies #-}
4−
5−{-# LANGUAGE OverloadedStrings #-}
6−{-# LANGUAGE TypeOperators #-}
7−
8−module Nested where
9−
10−import Servant
11−import Servant.API.Flatten
12−
13−type Api =
14− ( "one" :> Summary "foo" :> Post '[JSON] Int
15− :<|> "two" :> Post '[JSON] Int
16− )
17− :<|> ( "three" :> Post '[JSON] Int
18− )
19−
20−type FlatApi = Flat Api
21−
22−server :: Server FlatApi
23−server = pure 1 :<|> pure 2 :<|> pure 3
test/Post.hsdeleted−31
@@ -1,31 +0,0 @@
1−{-# LANGUAGE DataKinds #-}
2−{-# LANGUAGE DeriveGeneric #-}
3−{-# LANGUAGE DerivingStrategies #-}
4−
5−
6−{-# LANGUAGE TypeOperators #-}
7−
8−module Post where
9−
10−import Data.Aeson
11−import Data.Hashable
12−import GHC.Generics (Generic)
13−import Servant
14−
15−type Api =
16− Get '[JSON] FooPost
17− :<|> ReqBody '[JSON] FooPost :> Post '[JSON] ()
18−
19−data FooPost = FooPost
20− deriving (Eq, Show, Generic)
21−
22−instance Hashable FooPost
23−
24−instance ToJSON FooPost
25−
26−instance FromJSON FooPost
27−
28−server :: Server Api
29−server =
30− pure FooPost
31− :<|> const (pure ())
test/Product.hsdeleted−31
@@ -1,31 +0,0 @@
1−{-# LANGUAGE DataKinds #-}
2−{-# LANGUAGE DeriveGeneric #-}
3−{-# LANGUAGE DerivingStrategies #-}
4−
5−{-# LANGUAGE OverloadedStrings #-}
6−{-# LANGUAGE TypeOperators #-}
7−
8−module Product where
9−
10−import Data.Aeson
11−import Data.Hashable
12−import Data.Typeable (Typeable)
13−import GHC.Generics (Generic)
14−import Servant
15−
16−data Foo = Foo Int String
17− deriving (Generic, Eq, Show, Typeable)
18−
19−instance Hashable Foo
20−
21−instance ToJSON Foo
22−
23−instance FromJSON Foo
24−
25−type Api = "item" :> ReqBody '[JSON] Foo :> Post '[JSON] ()
26−
27−eliminate :: Foo -> Handler ()
28−eliminate (Foo _a _b) = throwError $ err500 {errBody = "eliminate blew up, oh no!"}
29−
30−server :: Server Api
31−server = eliminate
test/Put.hsdeleted−33
@@ -1,33 +0,0 @@
1−{-# LANGUAGE DataKinds #-}
2−{-# LANGUAGE DeriveGeneric #-}
3−{-# LANGUAGE DerivingStrategies #-}
4−
5−
6−{-# LANGUAGE TypeOperators #-}
7−
8−module Put where
9−
10−import Data.Aeson
11−import Data.Hashable
12−import GHC.Generics (Generic)
13−import Servant
14−
15−type Api =
16− Get '[JSON] Foo
17− :<|> ReqBody '[JSON] Foo :> Put '[JSON] ()
18− :<|> ReqBody '[JSON] Foo :> Put '[JSON] NoContent
19−
20−data Foo = Foo
21− deriving (Eq, Show, Generic)
22−
23−instance Hashable Foo
24−
25−instance ToJSON Foo
26−
27−instance FromJSON Foo
28−
29−server :: Server Api
30−server =
31− pure Foo
32− :<|> const (pure ())
33− :<|> const (pure NoContent)
test/QueryParams.hsdeleted−15
@@ -1,15 +0,0 @@
1−{-# LANGUAGE DataKinds #-}
2−
3−{-# LANGUAGE DerivingStrategies #-}
4−
5−
6−{-# LANGUAGE TypeOperators #-}
7−
8−module QueryParams where
9−
10−import Servant
11−
12−type Api = QueryParams "ints" Int :> Get '[JSON] [Int]
13−
14−server :: Server Api
15−server = pure
test/Seeded.hs+4−10
@@ -8,7 +8,6 @@
88module Seeded where
99
1010import Data.Aeson
11−import Data.Hashable
1211import Data.Typeable (Typeable)
1312import GHC.Generics (Generic)
1413import Servant
@@ -18,16 +17,11 @@ newtype Seed = Seed Int
1817 deriving newtype (FromHttpApiData, ToHttpApiData)
1918
2019instance ToJSON Seed
21−
2220instance FromJSON Seed
2321
24−instance Hashable Seed
25−
26−type Api =
27− Capture "seed" Seed :> Get '[JSON] ()
28− :<|> Get '[JSON] ()
22+type Api = Capture "seed" Seed :> Get '[JSON] ()
23+ :<|> Get '[JSON] ()
2924
3025server :: Server Api
31−server =
32− (\(Seed _) -> error "we blow up if we get here")
33− :<|> pure ()
26+server = (\(Seed _) -> error "we blow up if we get here")
27+ :<|> pure ()
test/Spec.hs+47−150
@@ -1,170 +1,67 @@
1−{-# LANGUAGE DerivingVia #-}
2−{-# LANGUAGE LambdaCase #-}
1+
32{-# LANGUAGE OverloadedStrings #-}
4−{-# LANGUAGE RecordWildCards #-}
5−{-# LANGUAGE StandaloneDeriving #-}
63{-# LANGUAGE TypeApplications #-}
7−{-# LANGUAGE RankNTypes #-}
8−{-# LANGUAGE FlexibleContexts #-}
9−{-# LANGUAGE AllowAmbiguousTypes #-}
10−{-# LANGUAGE ScopedTypeVariables #-}
11−{-# LANGUAGE DataKinds #-}
124{-# OPTIONS_GHC -fno-warn-orphans #-}
13−{-# LANGUAGE TypeFamilies #-}
14−
15−import qualified Breakdown
16−import Data.Dynamic (toDyn)
17−import Data.Hashable (Hashable (hash))
18−import Data.Maybe (isNothing)
19−import Data.Void (Void)
205import qualified Foo
21−import qualified Headers
22−import qualified Nested
23−import qualified Post
24−import qualified Put
25−import qualified Product
26−import qualified QueryParams
27−import qualified Roboservant as R
28−import qualified Roboservant.Server as RS
29−import qualified Roboservant.Client as RC
306import qualified Seeded
31−import Test.Hspec
32−import Test.Hspec.Core.Spec (FailureReason (Reason), ResultStatus (Failure, Success), itemExample, mapSpecItem_, resultStatus)
337import qualified Valid
34−import Servant ( Server, Proxy(..), serve, Endpoints, HasServer )
8+import qualified Headers
9+-- import qualified UnsafeIO
10+
11+import Data.Dynamic(toDyn)
12+import qualified Roboservant as RS
13+
14+import Test.Hspec
15+import Test.Hspec.Core.Spec(shouldFail)
3516
36−import Servant.Client(ClientEnv, mkClientEnv, baseUrlPort, parseBaseUrl,HasClient,ClientM)
37−import Network.Wai(Application)
38−import qualified Network.Wai.Handler.Warp as Warp
39−import Network.HTTP.Client (newManager, defaultManagerSettings)
40−import Control.Monad((>=>))
4117
4218main :: IO ()
4319main = hspec spec
4420
45−fuzzBoth
46− :: forall a .
47− (R.ToReifiedApi (Endpoints a), HasServer a '[], RS.FlattenServer a, RC.ToReifiedClientApi (Endpoints a), RC.FlattenClient a,
48− HasClient ClientM a)
49− => String -> Server a -> R.Config -> (Maybe R.Report -> IO ()) -> Spec
50−fuzzBoth name server config condition = do
51− it (name <> " via server") $
52− RS.fuzz @a server config >>= condition
53−
54− around (withServer (serve (Proxy :: Proxy a) server)) $
55− it (name <> " via client") $ \(clientEnv::ClientEnv) -> do
56− RC.fuzz @a clientEnv config >>= condition
57−
58−withServer :: Application -> ActionWith ClientEnv -> IO ()
59−withServer app action = Warp.testWithApplication (pure app) (genClientEnv >=> action)
60− where genClientEnv port = do
61− baseUrl <- parseBaseUrl "http://localhost"
62− manager <- newManager defaultManagerSettings
63− pure $ mkClientEnv manager (baseUrl { baseUrlPort = port })
21+noCheck :: IO ()
22+noCheck = pure ()
6423
6524spec :: Spec
6625spec = do
6726 describe "Basic usage" $ do
6827 describe "noError" $ do
69− fuzzBoth @Valid.Api "find no error in a basic app" Valid.server R.defaultConfig (`shouldSatisfy` isNothing)
70− fuzzBoth @Valid.RoutedApi "finds no error in a valid generic app" Valid.routedServer R.defaultConfig (`shouldSatisfy` isNothing)
71− fuzzBoth @Valid.Api "fails coverage check" Valid.server R.defaultConfig {R.coverageThreshold = 0.6}
72− (\r ->
73− fmap (R.failureReason . R.rsException) r
74− `shouldSatisfy` ( \case
75− Just (R.InsufficientCoverage _) -> True
76− _ -> False
77− ))
78− describe "posted body" $
79− fuzzBoth @Post.Api "passes a coverage check using a posted body" Post.server R.defaultConfig {R.coverageThreshold = 0.99}
80− (`shouldSatisfy` isNothing)
28+ it "finds no error in a valid app" $ do
29+ RS.fuzz @Valid.Api Valid.server (RS.Config [toDyn $ Seeded.Seed 1] 1 1000 1) noCheck
30+ describe "seeded" $ do
31+ shouldFail $
32+ it "finds an error using information passed in" $
33+ RS.fuzz @Seeded.Api Seeded.server (RS.Config [toDyn $ Seeded.Seed 1] 5 1000 1) noCheck
8134
35+ shouldFail $ it "finds an error in a basic app" $
36+ RS.fuzz @Foo.Api Foo.server (RS.Config [] 5 1000 1) noCheck
8237
83− describe "PUTted body" $
84− fuzzBoth @Put.Api "passes a coverage check using a posted body" Put.server R.defaultConfig {R.coverageThreshold = 0.99}
85− (`shouldSatisfy` isNothing)
38+ shouldFail $ it "should find a failure that's dependent on using header info" $ do
39+ RS.fuzz @Headers.Api Headers.server (RS.Config [] 5 10000 1) noCheck
8640
41+ -- -- -- The UnsafeIO checker does not actually really use the contextually aware stuff, though it
42+ -- -- -- could: it's mostly here to show how to test for concurrency problems.
43+ -- describe "concurrency bugs" $ do
44+ -- before UnsafeIO.makeServer $ do
45+ -- describe "sequential checking" $ do
46+ -- it "safe use" $ \unsafeServer -> do
47+ -- hedgehog $ RS.prop_sequential @UnsafeIO.UnsafeApi unsafeServer []
8748
88− describe "seeded" $ do
89− let res = Seeded.Seed 1
90− shouldFail $ fuzzBoth @Seeded.Api "finds an error using information passed in" Seeded.server
91− (R.defaultConfig {R.seed = [(toDyn res, hash res)]})
92− (`shouldSatisfy` isNothing)
93−
94− describe "Foo" $
95− fuzzBoth @Foo.Api "finds an error in a basic app" Foo.server R.defaultConfig (`shouldSatisfy` serverFailure)
96−
97− describe "QueryParams" $
98− fuzzBoth @QueryParams.Api "can handle query params" QueryParams.server R.defaultConfig { R.seed = [R.hashedDyn (12::Int)] }
99− (`shouldSatisfy` isNothing)
100−
101− describe "BuildFrom" $ do
102− describe "headers (and sum types)" $
103− fuzzBoth @Headers.Api "should find a failure that's dependent on using header info" Headers.server R.defaultConfig
104− (`shouldSatisfy` serverFailure)
105− describe "product types" $
106− fuzzBoth @Product.Api "should find a failure that's dependent on creating a product" Product.server
107− R.defaultConfig {R.seed = [R.hashedDyn 'a', R.hashedDyn (1 :: Int)]}
108− (`shouldSatisfy` serverFailure)
109− describe "Breakdown" $ do
110− fuzzBoth @Breakdown.ProductApi "handles products" Breakdown.productServer R.defaultConfig
111− (`shouldSatisfy` serverFailure)
112− fuzzBoth @Breakdown.SumApi "handles sums" Breakdown.sumServer R.defaultConfig
113− (`shouldSatisfy` serverFailure)
114− describe "flattening" $
115− fuzzBoth @Nested.FlatApi "can handle nested apis" Nested.server R.defaultConfig {R.coverageThreshold = 0.99}
116− (`shouldSatisfy` isNothing)
117−
118−serverFailure :: Maybe R.Report -> Bool
119−serverFailure = \case
120− Just R.Report {..} ->
121− let R.RoboservantException {..} = rsException
122− in failureReason /= R.NoPossibleMoves
123− _ -> False
124−
125−deriving via (R.Atom Foo.Foo) instance R.Breakdown Foo.Foo
126−
127−deriving via (R.Atom Foo.Foo) instance R.BuildFrom Foo.Foo
128−
129−deriving via (R.Atom Headers.Foo) instance R.Breakdown Headers.Foo
130−
131−deriving via (R.Atom Headers.Foo) instance R.BuildFrom Headers.Foo
132−
133−deriving via (R.Atom Seeded.Seed) instance R.Breakdown Seeded.Seed
134−
135−deriving via (R.Atom Seeded.Seed) instance R.BuildFrom Seeded.Seed
136−
137−deriving via (R.Atom Void) instance R.BuildFrom Void
138−
139−deriving via (R.Atom Post.FooPost) instance R.Breakdown Post.FooPost
140−deriving via (R.Atom Post.FooPost) instance R.BuildFrom Post.FooPost
141−
142−deriving via (R.Atom Put.Foo) instance R.Breakdown Put.Foo
143−deriving via (R.Atom Put.Foo) instance R.BuildFrom Put.Foo
144−
145−
146−
147−deriving via (R.Compound Breakdown.Foo) instance R.Breakdown Breakdown.Foo
148−
149−deriving via (R.Compound Product.Foo) instance R.BuildFrom Product.Foo
150−
151−deriving via (R.Compound Breakdown.SomeSum) instance R.Breakdown Breakdown.SomeSum
152−
153−-- | `shouldFail` allows you to assert that a given `Spec` should contain at least one failing test.
154−-- this is often useful when testing tests.
155−shouldFail :: SpecWith a -> SpecWith a
156−shouldFail =
157− mapSpecItem_
158− ( \i ->
159− i
160− { itemExample = \p a cb -> do
161− r <- itemExample i p a cb
162− pure
163− r
164− { resultStatus = case resultStatus r of
165− Success -> Failure Nothing (Reason "Unexpected success")
166− Failure _ _ -> Success
167− x -> x
168− }
169− }
170− )
49+ -- modifyMaxSuccess (const 10000) $
50+ -- shouldFail $
51+ -- describe "concurrent" $ do
52+ -- it "concurrent, dangerous use" $ \unsafeServer -> do
53+ -- RS.prop_concurrent @UnsafeIO.UnsafeApi unsafeServer []
54+
55+
56+instance RS.Breakdown Foo.Foo where
57+ breakdown = pure . toDyn
58+
59+
60+instance RS.Breakdown Headers.Foo where
61+ breakdown = pure . toDyn
62+
63+instance RS.BuildFrom Foo.Foo
64+
65+instance RS.BuildFrom Headers.Foo
66+
67+instance RS.BuildFrom Seeded.Seed
test/UnsafeIO.hs+15−13
@@ -1,39 +1,41 @@
11{-# LANGUAGE DataKinds #-}
2−
2+{-# LANGUAGE DeriveGeneric #-}
33{-# LANGUAGE DerivingStrategies #-}
4−
4+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
55{-# LANGUAGE OverloadedStrings #-}
66{-# LANGUAGE TypeOperators #-}
77
88module UnsafeIO where
99
10−import Control.Monad.Trans (MonadIO (liftIO))
11−import Data.Aeson ()
12−import qualified Data.ByteString.Lazy.Char8 as BL8
13−import Data.IORef (IORef, newIORef, readIORef, writeIORef)
10+import Data.Aeson()
1411import Servant
12+import Data.IORef (writeIORef, IORef, readIORef, newIORef)
13+import Control.Monad.Trans (MonadIO(liftIO))
14+import qualified Data.ByteString.Lazy.Char8 as BL8
15+
1516
1617type UnsafeApi =
1718 "add" :> Get '[JSON] ()
18− :<|> "healthcheck" :> Get '[JSON] ()
19+ :<|> "healthcheck" :> Get '[JSON] ()
1920
2021healthcheck :: IORef Int -> Handler ()
2122healthcheck ref = do
2223 t <- liftIO $ readIORef ref
2324 case t of
2425 0 -> pure ()
25− n -> throwError $ err500 {errBody = "observed inconsistency: " <> BL8.pack (show n)}
26+ n -> throwError $ err500 {errBody = "observed inconsistency: " <> (BL8.pack $ show n)}
27+
28+
2629
2730makeServer :: IO (Server UnsafeApi)
2831makeServer = do
2932 ref <- newIORef 0
30− pure $
31− unsafeMunge ref
32− :<|> healthcheck ref
33+ pure $ unsafeMunge ref
34+ :<|> healthcheck ref
3335
3436unsafeMunge :: IORef Int -> Handler ()
3537unsafeMunge ref = liftIO $ do
3638 t <- readIORef ref
37− writeIORef ref (t + 1)
39+ writeIORef ref (t+1)
3840 t2 <- readIORef ref
39− writeIORef ref (t2 -1)
41+ writeIORef ref (t2-1)
test/Valid.hs+3−32
@@ -1,44 +1,15 @@
11{-# LANGUAGE DataKinds #-}
22{-# LANGUAGE DeriveGeneric #-}
33{-# LANGUAGE DerivingStrategies #-}
4−
4+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
55{-# LANGUAGE OverloadedStrings #-}
66{-# LANGUAGE TypeOperators #-}
77
88module Valid where
99
10−import Data.Void
11−import GHC.Generics
1210import Servant
13−import Servant.Server.Generic
14−
15−type Api =
16− Get '[JSON] Int
17− :<|> Capture "void" Void :> Get '[JSON] ()
18−
19−data Routes route
20− = Routes
21− { getInt ::
22− route
23− :- Summary "i'm a summary!" :> Get '[JSON] Int,
24− captureIt ::
25− route
26− :- Capture "void" Void :> Get '[JSON] ()
27− }
28− deriving (Generic)
29−
30−type RoutedApi = ToServantApi Routes
31−
32−-- routedApi = genericApi (Proxy :: Proxy Routes)
33−routedServer :: Server RoutedApi
34−routedServer = genericServer routes
3511
36−routes :: Routes AsServer
37−routes =
38− Routes
39− { getInt = pure 7,
40− captureIt = const (pure ())
41− }
12+type Api = Get '[JSON] Int
4213
4314server :: Server Api
44−server = pure 7 :<|> const (pure ())
15+server = pure 7