diff --git a/.github/workflows/haskell-ci.yml b/.github/workflows/haskell-ci.yml new file mode 100644 index 0000000..e61f72d --- /dev/null +++ b/.github/workflows/haskell-ci.yml @@ -0,0 +1,229 @@ +# This GitHub workflow config has been generated by a script via +# +# haskell-ci 'github' 'servant-activeresource.cabal' +# +# To regenerate the script (for example after adjusting tested-with) run +# +# haskell-ci regenerate +# +# For more information, see https://github.com/haskell-CI/haskell-ci +# +# version: 0.16.6 +# +# REGENDATA ("0.16.6",["github","servant-activeresource.cabal"]) +# +name: Haskell-CI +on: + - push + - pull_request +jobs: + linux: + name: Haskell-CI - Linux - ${{ matrix.compiler }} + runs-on: ubuntu-20.04 + timeout-minutes: + 60 + container: + image: buildpack-deps:bionic + continue-on-error: ${{ matrix.allow-failure }} + strategy: + matrix: + include: + - compiler: ghc-9.6.2 + compilerKind: ghc + compilerVersion: 9.6.2 + setup-method: ghcup + allow-failure: false + - compiler: ghc-9.4.5 + compilerKind: ghc + compilerVersion: 9.4.5 + setup-method: ghcup + allow-failure: false + - compiler: ghc-9.2.7 + compilerKind: ghc + compilerVersion: 9.2.7 + setup-method: ghcup + allow-failure: false + - compiler: ghc-9.0.2 + compilerKind: ghc + compilerVersion: 9.0.2 + setup-method: ghcup + allow-failure: false + - compiler: ghc-8.10.7 + compilerKind: ghc + compilerVersion: 8.10.7 + setup-method: ghcup + allow-failure: false + fail-fast: false + steps: + - name: apt + run: | + apt-get update + apt-get install -y --no-install-recommends gnupg ca-certificates dirmngr curl git software-properties-common libtinfo5 + mkdir -p "$HOME/.ghcup/bin" + curl -sL https://downloads.haskell.org/ghcup/0.1.19.2/x86_64-linux-ghcup-0.1.19.2 > "$HOME/.ghcup/bin/ghcup" + chmod a+x "$HOME/.ghcup/bin/ghcup" + "$HOME/.ghcup/bin/ghcup" install ghc "$HCVER" || (cat "$HOME"/.ghcup/logs/*.* && false) + "$HOME/.ghcup/bin/ghcup" install cabal 3.10.1.0 || (cat "$HOME"/.ghcup/logs/*.* && false) + env: + HCKIND: ${{ matrix.compilerKind }} + HCNAME: ${{ matrix.compiler }} + HCVER: ${{ matrix.compilerVersion }} + - name: Set PATH and environment variables + run: | + echo "$HOME/.cabal/bin" >> $GITHUB_PATH + echo "LANG=C.UTF-8" >> "$GITHUB_ENV" + echo "CABAL_DIR=$HOME/.cabal" >> "$GITHUB_ENV" + echo "CABAL_CONFIG=$HOME/.cabal/config" >> "$GITHUB_ENV" + HCDIR=/opt/$HCKIND/$HCVER + HC=$HOME/.ghcup/bin/$HCKIND-$HCVER + echo "HC=$HC" >> "$GITHUB_ENV" + echo "HCPKG=$HOME/.ghcup/bin/$HCKIND-pkg-$HCVER" >> "$GITHUB_ENV" + echo "HADDOCK=$HOME/.ghcup/bin/haddock-$HCVER" >> "$GITHUB_ENV" + echo "CABAL=$HOME/.ghcup/bin/cabal-3.10.1.0 -vnormal+nowrap" >> "$GITHUB_ENV" + HCNUMVER=$(${HC} --numeric-version|perl -ne '/^(\d+)\.(\d+)\.(\d+)(\.(\d+))?$/; print(10000 * $1 + 100 * $2 + ($3 == 0 ? $5 != 1 : $3))') + echo "HCNUMVER=$HCNUMVER" >> "$GITHUB_ENV" + echo "ARG_TESTS=--enable-tests" >> "$GITHUB_ENV" + echo "ARG_BENCH=--enable-benchmarks" >> "$GITHUB_ENV" + echo "HEADHACKAGE=false" >> "$GITHUB_ENV" + echo "ARG_COMPILER=--$HCKIND --with-compiler=$HC" >> "$GITHUB_ENV" + echo "GHCJSARITH=0" >> "$GITHUB_ENV" + env: + HCKIND: ${{ matrix.compilerKind }} + HCNAME: ${{ matrix.compiler }} + HCVER: ${{ matrix.compilerVersion }} + - name: env + run: | + env + - name: write cabal config + run: | + mkdir -p $CABAL_DIR + cat >> $CABAL_CONFIG <> $CABAL_CONFIG < cabal-plan.xz + echo 'f62ccb2971567a5f638f2005ad3173dba14693a45154c1508645c52289714cb2 cabal-plan.xz' | sha256sum -c - + xz -d < cabal-plan.xz > $HOME/.cabal/bin/cabal-plan + rm -f cabal-plan.xz + chmod a+x $HOME/.cabal/bin/cabal-plan + cabal-plan --version + - name: install hlint + run: | + if [ $((HCNUMVER >= 90600)) -ne 0 ] ; then HLINTVER=$(cd /tmp && (${CABAL} v2-install -v $ARG_COMPILER --dry-run hlint --constraint='hlint >=3.5 && <3.6' | perl -ne 'if (/\bhlint-(\d+(\.\d+)*)\b/) { print "$1"; last; }')); echo "HLint version $HLINTVER" ; fi + if [ $((HCNUMVER >= 90600)) -ne 0 ] ; then if [ ! -e $HOME/.haskell-ci-tools/hlint-$HLINTVER/hlint ]; then echo "Downloading HLint version $HLINTVER"; mkdir -p $HOME/.haskell-ci-tools; curl --write-out 'Status Code: %{http_code} Redirects: %{num_redirects} Total time: %{time_total} Total Dsize: %{size_download}\n' --silent --location --output $HOME/.haskell-ci-tools/hlint-$HLINTVER.tar.gz "https://github.com/ndmitchell/hlint/releases/download/v$HLINTVER/hlint-$HLINTVER-x86_64-linux.tar.gz"; tar -xzv -f $HOME/.haskell-ci-tools/hlint-$HLINTVER.tar.gz -C $HOME/.haskell-ci-tools; fi ; fi + if [ $((HCNUMVER >= 90600)) -ne 0 ] ; then mkdir -p $CABAL_DIR/bin && ln -sf "$HOME/.haskell-ci-tools/hlint-$HLINTVER/hlint" $CABAL_DIR/bin/hlint ; fi + if [ $((HCNUMVER >= 90600)) -ne 0 ] ; then hlint --version ; fi + - name: save cache (tools) + uses: actions/cache/save@v3 + if: always() + with: + key: ${{ runner.os }}-${{ matrix.compiler }}-tools-7f419eaa + path: ~/.haskell-ci-tools + - name: checkout + uses: actions/checkout@v3 + with: + path: source + - name: initial cabal.project for sdist + run: | + touch cabal.project + echo "packages: $GITHUB_WORKSPACE/source/." >> cabal.project + cat cabal.project + - name: sdist + run: | + mkdir -p sdist + $CABAL sdist all --output-dir $GITHUB_WORKSPACE/sdist + - name: unpack + run: | + mkdir -p unpacked + find sdist -maxdepth 1 -type f -name '*.tar.gz' -exec tar -C $GITHUB_WORKSPACE/unpacked -xzvf {} \; + - name: generate cabal.project + run: | + PKGDIR_servant_activeresource="$(find "$GITHUB_WORKSPACE/unpacked" -maxdepth 1 -type d -regex '.*/servant-activeresource-[0-9.]*')" + echo "PKGDIR_servant_activeresource=${PKGDIR_servant_activeresource}" >> "$GITHUB_ENV" + rm -f cabal.project cabal.project.local + touch cabal.project + touch cabal.project.local + echo "packages: ${PKGDIR_servant_activeresource}" >> cabal.project + echo "package servant-activeresource" >> cabal.project + echo " ghc-options: -Werror=missing-methods" >> cabal.project + cat >> cabal.project <> cabal.project.local + cat cabal.project + cat cabal.project.local + - name: dump install plan + run: | + $CABAL v2-build $ARG_COMPILER $ARG_TESTS $ARG_BENCH --dry-run all + cabal-plan + - name: restore cache + uses: actions/cache/restore@v3 + with: + key: ${{ runner.os }}-${{ matrix.compiler }}-${{ github.sha }} + path: ~/.cabal/store + restore-keys: ${{ runner.os }}-${{ matrix.compiler }}- + - name: install dependencies + run: | + $CABAL v2-build $ARG_COMPILER --disable-tests --disable-benchmarks --dependencies-only -j2 all + $CABAL v2-build $ARG_COMPILER $ARG_TESTS $ARG_BENCH --dependencies-only -j2 all + - name: build w/o tests + run: | + $CABAL v2-build $ARG_COMPILER --disable-tests --disable-benchmarks all + - name: build + run: | + $CABAL v2-build $ARG_COMPILER $ARG_TESTS $ARG_BENCH all --write-ghc-environment-files=always + - name: tests + run: | + $CABAL v2-test $ARG_COMPILER $ARG_TESTS $ARG_BENCH all --test-show-details=direct + - name: hlint + run: | + if [ $((HCNUMVER >= 90600)) -ne 0 ] ; then (cd ${PKGDIR_servant_activeresource} && hlint -XHaskell2010 src) ; fi + - name: cabal check + run: | + cd ${PKGDIR_servant_activeresource} || false + ${CABAL} -vnormal check + - name: haddock + run: | + $CABAL v2-haddock --disable-documentation --haddock-all $ARG_COMPILER --with-haddock $HADDOCK $ARG_TESTS $ARG_BENCH all + - name: unconstrained build + run: | + rm -f cabal.project.local + $CABAL v2-build $ARG_COMPILER --disable-tests --disable-benchmarks all + - name: save cache + uses: actions/cache/save@v3 + if: always() + with: + key: ${{ runner.os }}-${{ matrix.compiler }}-${{ github.sha }} + path: ~/.cabal/store diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8184986 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.dir-locals.el +dist-newstyle/ +dist/ +result diff --git a/LICENSE b/LICENSE index abab91d..23a76bc 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (C) 2021 Bellroy Pty Ltd +Copyright (C) 2024 Bellroy Pty Ltd Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..985c419 --- /dev/null +++ b/flake.lock @@ -0,0 +1,95 @@ +{ + "nodes": { + "bellroy-nix-foss": { + "inputs": { + "flake-compat": "flake-compat", + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + }, + "locked": { + "lastModified": 1709697643, + "narHash": "sha256-w3yK1C6/JsBVvZRKnZw4HPBajYWpcA2BR3t13mfALzE=", + "owner": "bellroy", + "repo": "bellroy-nix-foss", + "rev": "1e130e6a80cfc7afc5246bb14c6b090aa06a7af0", + "type": "github" + }, + "original": { + "owner": "bellroy", + "repo": "bellroy-nix-foss", + "type": "github" + } + }, + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1673956053, + "narHash": "sha256-4gtG9iQuiKITOjNQQeQIpoIB6b16fm+504Ch3sNKLd8=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "35bb57c0c8d8b62bbfd284272c928ceb64ddbde9", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1687709756, + "narHash": "sha256-Y5wKlQSkgEK2weWdOu4J3riRd+kV/VCgHsqLNTTWQ/0=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "dbabf0ca0c0c4bce6ea5eaf65af5cb694d2082c7", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1710408871, + "narHash": "sha256-YpSGYZR96I8g5OK/Fdm0O4+mHLen6YPA1cPanqqNqT0=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "bd5ddf2c6bfafff031edf80221e1ee94e86ca10a", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "ref": "nixpkgs-unstable", + "type": "indirect" + } + }, + "root": { + "inputs": { + "bellroy-nix-foss": "bellroy-nix-foss" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix index 96d3bbb..d05f5cf 100644 --- a/flake.nix +++ b/flake.nix @@ -1,10 +1,6 @@ { inputs = { - bellroy-nix-foss = { - url = "github:bellroy/bellroy-nix-foss"; - inputs.nixpkgs.follows = "nixpkgs"; - }; - nixpkgs.url = "github:nixos/nixpkgs"; + bellroy-nix-foss.url = "github:bellroy/bellroy-nix-foss"; }; outputs = inputs: @@ -15,14 +11,11 @@ path = ./servant-activeresource.nix; } ]; - supportedCompilers = [ "ghc8107" "ghc92" "ghc94" ]; + supportedCompilers = [ "ghc810" "ghc90" "ghc92" "ghc94" "ghc96" ]; defaultCompiler = "ghc92"; -# haskellPackagesOverride = { compilerName, haskellLib, final, prev }: -# if compilerName == "ghc94" -# then { -# # hal doesn't support newer hedgehog -# hedgehog = haskellLib.compose.dontCheck (prev.callHackage "hedgehog" "1.1.2" { }); -# } -# else { }; + haskellPackagesOverride = { haskellLib, prev, ... }: { + servant = haskellLib.doJailbreak prev.servant; + servant-server = haskellLib.doJailbreak prev.servant-server; + }; }; } diff --git a/servant-activeresource.cabal b/servant-activeresource.cabal index 0f3475e..93c4b9e 100644 --- a/servant-activeresource.cabal +++ b/servant-activeresource.cabal @@ -1,21 +1,29 @@ cabal-version: 2.2 name: servant-activeresource version: 0.1.0.0 -synopsis: TODO -description: TODO -bug-reports: TODO +synopsis: Servant endpoints compatible with Rails's ActiveResources +description: + [ActiveResource](https://github.com/rails/activeresource) is a Rails + library for representing resources from a RESTful API as Ruby + objects, with a similar interface to the Rails ActiveRecord ORM. + . + This library provides types and TH helpers for describing such APIs, + and for implementing Servant-style servers to provide them. + +homepage: https://github.com/bellroy/servant-activeresource +bug-reports: https://github.com/bellroy/servant-activeresource/issues license: BSD-3-Clause license-file: LICENSE author: Bellroy Tech Team maintainer: Bellroy Tech Team -copyright: Copyright (C) 2020-2021 Bellroy Pty Ltd +copyright: Copyright (C) 2024 Bellroy Pty Ltd category: Servant, Web build-type: Simple -extra-source-files: +extra-doc-files: CHANGELOG.md README.md -tested-with: GHC ==8.6.5 || ==8.8.4 || ==8.10.4 +tested-with: GHC ==8.10.7 || ==9.0.2 || ==9.2.7 || ==9.4.5 || ==9.6.2 common opts default-language: Haskell2010 @@ -28,14 +36,14 @@ common opts common deps build-depends: - , aeson ^>=2.1.1.0 - , base ^>=4.17 - , bytestring ^>=0.11.3 - , containers ^>=0.6 - , servant ^>=0.20 - , servant-server ^>=0.20 - , template-haskell ^>=2.19 - , text ^>=2.0 + , aeson ^>=2.1.1.0 || ^>=2.2 + , base >=4.14 && <4.19 + , bytestring >=0.10.12 && <0.13 + , containers ^>=0.6 || ^>=0.7 + , servant >=0.19 && <0.21 + , servant-server >=0.19 && <0.21 + , template-haskell >=2.16 && <2.21 + , text ^>=1.2 || >=2.0 && <=2.2 library import: deps, opts diff --git a/servant-activeresource.nix b/servant-activeresource.nix new file mode 100644 index 0000000..3720b63 --- /dev/null +++ b/servant-activeresource.nix @@ -0,0 +1,19 @@ +{ mkDerivation, aeson, base, bytestring, containers, lib, servant +, servant-server, template-haskell, text +}: +mkDerivation { + pname = "servant-activeresource"; + version = "0.1.0.0"; + src = ./.; + libraryHaskellDepends = [ + aeson base bytestring containers servant servant-server + template-haskell text + ]; + testHaskellDepends = [ + aeson base bytestring containers servant servant-server + template-haskell text + ]; + homepage = "https://github.com/bellroy/servant-activeresource"; + description = "Servant endpoints compatible with Rails's ActiveResources"; + license = lib.licenses.bsd3; +} diff --git a/src/Servant/ActiveResource.hs b/src/Servant/ActiveResource.hs index 93f9539..280ce78 100644 --- a/src/Servant/ActiveResource.hs +++ b/src/Servant/ActiveResource.hs @@ -10,6 +10,48 @@ {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeOperators #-} + +-- | +-- +-- Module : Servant.ActiveResource +-- Copyright : (C) 2024 Bellroy Pty Ltd +-- License : BSD-3-Clause +-- Maintainer : Bellroy Tech Team +-- Stability : experimental +-- +-- Types and helpers for defining Servant routes compatible with +-- [Rails' ActiveResource](https://github.com/rails/activeresource). +-- +-- @ +-- {-# LANGUAGE TemplateHaskell #-} +-- +-- import qualified Servant.ActiveResource as AR +-- +-- newtype MyResourceId = MyResourceId Int +-- -- Type for new values or updates to existing values. Usually +-- -- missing an @id@ field. +-- data MyResource = MyResource {...} +-- -- Like MyResource, but returned from the database. +-- data MyStoredResource = MyStoredResource {...} +-- +-- -- The exact monad used will depend on your program. Here, we just assume +-- -- 'Handler' from package servant-server. +-- instance AR.'Resource' MyResourceId Handler where +-- type 'ResourceData' MyResourceId = MyResource +-- type 'StoredResourceData' MyResourceId = MyStoredResource +-- +-- -- These form the implementation of your API. +-- 'listResources' = ... +-- 'createResource' = ... +-- 'readResource' = ... +-- 'upsertResource' = ... +-- 'deleteResource' = ... +-- +-- -- Record of routes, which can be spliced into a top-level handler +-- -- via 'Servant.API.NamedRoutes'. +-- routes :: AR.'ResourceRoutes' MyResourceId (AsServerT Handler) +-- routes = $(AR.'makeResourceServerT' [t|MyResourceId|]) +-- @ module Servant.ActiveResource ( Resource (..), NotFoundError (..), @@ -93,7 +135,7 @@ instance ToJSON NotFoundError where -- the payload, and the values are lists of errors at that field. -- -- The 'ToJSON' instance wraps the whole thing in an @{ "errors": ... }@ --- object, to match . +-- object, to match [ActiveResource's expectations](https://github.com/rails/activeresource/blob/3cf44f731f655dccc13ba23f78603f7e214e3352/lib/active_resource/validations.rb#L35-L71). newtype ValidationError = ValidationError (Map Text [Text]) deriving stock (Eq, Show, Generic)