Skip to content

Commit daf4f25

Browse files
committed
Version 0.4.0.0
Support for aeson >= 2
1 parent 1e0c96e commit daf4f25

File tree

4 files changed

+74
-83
lines changed

4 files changed

+74
-83
lines changed

.travis.yml

+54-72
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
# This Travis job script has been generated by a script via
22
#
3-
# haskell-ci 'hruby.cabal'
3+
# haskell-ci 'hruby.cabal' '--output' '.travis.yml'
4+
#
5+
# To regenerate the script (for example after adjusting tested-with) run
6+
#
7+
# haskell-ci regenerate
48
#
59
# For more information, see https://github.com/haskell-CI/haskell-ci
610
#
7-
# version: 0.5.20190916
11+
# version: 0.13.20211116
812
#
13+
version: ~> 1.0
914
language: c
10-
dist: xenial
15+
os: linux
16+
dist: bionic
1117
git:
1218
# whether to recursively clone submodules
1319
submodules: false
1420
cache:
1521
directories:
1622
- $HOME/.cabal/packages
1723
- $HOME/.cabal/store
24+
- $HOME/.hlint
1825
before_cache:
1926
- rm -fv $CABALHOME/packages/hackage.haskell.org/build-reports.log
2027
# remove files that are regenerated by 'cabal update'
@@ -24,18 +31,17 @@ before_cache:
2431
- rm -fv $CABALHOME/packages/hackage.haskell.org/01-index.tar
2532
- rm -fv $CABALHOME/packages/hackage.haskell.org/01-index.tar.idx
2633
- rm -rfv $CABALHOME/packages/head.hackage
27-
matrix:
34+
jobs:
2835
include:
36+
- compiler: ghc-9.0.1
37+
addons: {"apt":{"packages":["ghc-9.0.1","cabal-install-3.6"],"sources":[{"key_url":"https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x063dab2bdc0b3f9fcebc378bff3aeacef6f88286","sourceline":"deb http://ppa.launchpad.net/hvr/ghc/ubuntu bionic main"}]}}
38+
os: linux
39+
- compiler: ghc-8.10.2
40+
addons: {"apt":{"packages":["ghc-8.10.2","cabal-install-3.6"],"sources":[{"key_url":"https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x063dab2bdc0b3f9fcebc378bff3aeacef6f88286","sourceline":"deb http://ppa.launchpad.net/hvr/ghc/ubuntu bionic main"}]}}
41+
os: linux
2942
- compiler: ghc-8.8.1
30-
addons: {"apt":{"sources":["hvr-ghc"],"packages":["ghc-8.8.1","cabal-install-3.0", "ruby2.3-dev"]}}
31-
- compiler: ghc-8.6.5
32-
addons: {"apt":{"sources":["hvr-ghc"],"packages":["ghc-8.6.5","cabal-install-3.0", "ruby2.3-dev"]}}
33-
- compiler: ghc-8.4.4
34-
addons: {"apt":{"sources":["hvr-ghc"],"packages":["ghc-8.4.4","cabal-install-3.0", "ruby2.3-dev"]}}
35-
- compiler: ghc-8.2.2
36-
addons: {"apt":{"sources":["hvr-ghc"],"packages":["ghc-8.2.2","cabal-install-3.0", "ruby2.3-dev"]}}
37-
- compiler: ghc-8.0.2
38-
addons: {"apt":{"sources":["hvr-ghc"],"packages":["ghc-8.0.2","cabal-install-3.0", "ruby2.3-dev"]}}
43+
addons: {"apt":{"packages":["ghc-8.8.1","cabal-install-3.6"],"sources":[{"key_url":"https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x063dab2bdc0b3f9fcebc378bff3aeacef6f88286","sourceline":"deb http://ppa.launchpad.net/hvr/ghc/ubuntu bionic main"}]}}
44+
os: linux
3945
before_install:
4046
- HC=$(echo "/opt/$CC/bin/ghc" | sed 's/-/\//')
4147
- WITHCOMPILER="-w $HC"
@@ -48,59 +54,31 @@ before_install:
4854
- TOP=$(pwd)
4955
- "HCNUMVER=$(${HC} --numeric-version|perl -ne '/^(\\d+)\\.(\\d+)\\.(\\d+)(\\.(\\d+))?$/; print(10000 * $1 + 100 * $2 + ($3 == 0 ? $5 != 1 : $3))')"
5056
- echo $HCNUMVER
51-
- CABAL="$CABAL -vnormal+nowrap+markoutput"
57+
- CABAL="$CABAL -vnormal+nowrap"
5258
- set -o pipefail
53-
- |
54-
echo 'function blue(s) { printf "\033[0;34m" s "\033[0m " }' >> .colorful.awk
55-
echo 'BEGIN { state = "output"; }' >> .colorful.awk
56-
echo '/^-----BEGIN CABAL OUTPUT-----$/ { state = "cabal" }' >> .colorful.awk
57-
echo '/^-----END CABAL OUTPUT-----$/ { state = "output" }' >> .colorful.awk
58-
echo '!/^(-----BEGIN CABAL OUTPUT-----|-----END CABAL OUTPUT-----)/ {' >> .colorful.awk
59-
echo ' if (state == "cabal") {' >> .colorful.awk
60-
echo ' print blue($0)' >> .colorful.awk
61-
echo ' } else {' >> .colorful.awk
62-
echo ' print $0' >> .colorful.awk
63-
echo ' }' >> .colorful.awk
64-
echo '}' >> .colorful.awk
65-
- cat .colorful.awk
66-
- |
67-
color_cabal_output () {
68-
awk -f $TOP/.colorful.awk
69-
}
70-
- echo text | color_cabal_output
71-
- rm -rf /home/travis/.rvm
72-
install:
73-
- dpkg -l
74-
- ${CABAL} --version
75-
- echo "$(${HC} --version) [$(${HC} --print-project-git-commit-id 2> /dev/null || echo '?')]"
7659
- TEST=--enable-tests
7760
- BENCH=--enable-benchmarks
7861
- HEADHACKAGE=false
7962
- rm -f $CABALHOME/config
8063
- |
81-
echo "verbose: normal +nowrap +markoutput" >> $CABALHOME/config
82-
echo "remote-build-reporting: anonymous" >> $CABALHOME/config
83-
echo "write-ghc-environment-files: always" >> $CABALHOME/config
84-
echo "remote-repo-cache: $CABALHOME/packages" >> $CABALHOME/config
85-
echo "logs-dir: $CABALHOME/logs" >> $CABALHOME/config
86-
echo "world-file: $CABALHOME/world" >> $CABALHOME/config
87-
echo "extra-prog-path: $CABALHOME/bin" >> $CABALHOME/config
88-
echo "symlink-bindir: $CABALHOME/bin" >> $CABALHOME/config
89-
echo "installdir: $CABALHOME/bin" >> $CABALHOME/config
90-
echo "build-summary: $CABALHOME/logs/build.log" >> $CABALHOME/config
91-
echo "store-dir: $CABALHOME/store" >> $CABALHOME/config
92-
echo "install-dirs user" >> $CABALHOME/config
93-
echo " prefix: $CABALHOME" >> $CABALHOME/config
94-
echo "repository hackage.haskell.org" >> $CABALHOME/config
95-
echo " url: http://hackage.haskell.org/" >> $CABALHOME/config
96-
echo " secure: True" >> $CABALHOME/config
97-
echo " key-threshold: 3" >> $CABALHOME/config
98-
echo " root-keys:" >> $CABALHOME/config
99-
echo " fe331502606802feac15e514d9b9ea83fee8b6ffef71335479a2e68d84adc6b0" >> $CABALHOME/config
100-
echo " 1ea9ba32c526d1cc91ab5e5bd364ec5e9e8cb67179a471872f6e26f0ae773d42" >> $CABALHOME/config
101-
echo " 2c6c3627bd6c982990239487f1abd02e08a02e6cf16edb105a8012d444d870c3" >> $CABALHOME/config
102-
echo " 0a5c7ea47cd1b15f01f5f51a33adda7e655bc0f0b0615baa8e271f4c3351e21d" >> $CABALHOME/config
103-
echo " 51f0161b906011b52c6613376b1ae937670da69322113a246a09f807c62f6921" >> $CABALHOME/config
64+
echo "verbose: normal +nowrap +markoutput" >> $CABALHOME/config
65+
echo "remote-build-reporting: anonymous" >> $CABALHOME/config
66+
echo "write-ghc-environment-files: never" >> $CABALHOME/config
67+
echo "remote-repo-cache: $CABALHOME/packages" >> $CABALHOME/config
68+
echo "logs-dir: $CABALHOME/logs" >> $CABALHOME/config
69+
echo "world-file: $CABALHOME/world" >> $CABALHOME/config
70+
echo "extra-prog-path: $CABALHOME/bin" >> $CABALHOME/config
71+
echo "symlink-bindir: $CABALHOME/bin" >> $CABALHOME/config
72+
echo "installdir: $CABALHOME/bin" >> $CABALHOME/config
73+
echo "build-summary: $CABALHOME/logs/build.log" >> $CABALHOME/config
74+
echo "store-dir: $CABALHOME/store" >> $CABALHOME/config
75+
echo "install-dirs user" >> $CABALHOME/config
76+
echo " prefix: $CABALHOME" >> $CABALHOME/config
77+
echo "repository hackage.haskell.org" >> $CABALHOME/config
78+
echo " url: http://hackage.haskell.org/" >> $CABALHOME/config
79+
install:
80+
- ${CABAL} --version
81+
- echo "$(${HC} --version) [$(${HC} --print-project-git-commit-id 2> /dev/null || echo '?')]"
10482
- |
10583
echo "program-default-options" >> $CABALHOME/config
10684
echo " ghc-options: $GHCJOBS +RTS -M6G -RTS" >> $CABALHOME/config
@@ -112,20 +90,22 @@ install:
11290
- touch cabal.project
11391
- |
11492
echo "packages: ." >> cabal.project
115-
- |
93+
- echo 'package hruby' >> cabal.project
94+
- "echo ' ghc-options: -Werror=missing-methods' >> cabal.project"
95+
- ""
11696
- "for pkg in $($HCPKG list --simple-output); do echo $pkg | sed 's/-[^-]*$//' | (grep -vE -- '^(hruby)$' || true) | sed 's/^/constraints: /' | sed 's/$/ installed/' >> cabal.project.local; done"
11797
- cat cabal.project || true
11898
- cat cabal.project.local || true
11999
- if [ -f "./configure.ac" ]; then (cd "." && autoreconf -i); fi
120-
- ${CABAL} v2-freeze $WITHCOMPILER ${TEST} ${BENCH} | color_cabal_output
100+
- ${CABAL} v2-freeze $WITHCOMPILER ${TEST} ${BENCH}
121101
- "cat cabal.project.freeze | sed -E 's/^(constraints: *| *)//' | sed 's/any.//'"
122102
- rm cabal.project.freeze
123-
- ${CABAL} v2-build $WITHCOMPILER ${TEST} ${BENCH} --dep -j2 all | color_cabal_output
124-
- ${CABAL} v2-build $WITHCOMPILER --disable-tests --disable-benchmarks --dep -j2 all | color_cabal_output
103+
- travis_wait 40 ${CABAL} v2-build $WITHCOMPILER ${TEST} ${BENCH} --dep -j2 all
104+
- travis_wait 40 ${CABAL} v2-build $WITHCOMPILER --disable-tests --disable-benchmarks --dep -j2 all
125105
script:
126106
- DISTDIR=$(mktemp -d /tmp/dist-test.XXXX)
127107
# Packaging...
128-
- ${CABAL} v2-sdist all | color_cabal_output
108+
- ${CABAL} v2-sdist all
129109
# Unpacking...
130110
- mv dist-newstyle/sdist/*.tar.gz ${DISTDIR}/
131111
- cd ${DISTDIR} || false
@@ -137,25 +117,27 @@ script:
137117
- touch cabal.project
138118
- |
139119
echo "packages: ${PKGDIR_hruby}" >> cabal.project
140-
- |
120+
- echo 'package hruby' >> cabal.project
121+
- "echo ' ghc-options: -Werror=missing-methods' >> cabal.project"
122+
- ""
141123
- "for pkg in $($HCPKG list --simple-output); do echo $pkg | sed 's/-[^-]*$//' | (grep -vE -- '^(hruby)$' || true) | sed 's/^/constraints: /' | sed 's/$/ installed/' >> cabal.project.local; done"
142124
- cat cabal.project || true
143125
- cat cabal.project.local || true
144126
# Building...
145127
# this builds all libraries and executables (without tests/benchmarks)
146-
- ${CABAL} v2-build $WITHCOMPILER --disable-tests --disable-benchmarks all | color_cabal_output
128+
- ${CABAL} v2-build $WITHCOMPILER --disable-tests --disable-benchmarks all
147129
# Building with tests and benchmarks...
148130
# build & run tests, build benchmarks
149-
- ${CABAL} v2-build $WITHCOMPILER ${TEST} ${BENCH} all | color_cabal_output
131+
- ${CABAL} v2-build $WITHCOMPILER ${TEST} ${BENCH} all --write-ghc-environment-files=always
150132
# Testing...
151-
- ${CABAL} v2-test $WITHCOMPILER ${TEST} ${BENCH} all | color_cabal_output
133+
- ${CABAL} v2-test $WITHCOMPILER ${TEST} ${BENCH} all --test-show-details=direct
152134
# cabal check...
153135
- (cd ${PKGDIR_hruby} && ${CABAL} -vnormal check)
154136
# haddock...
155-
- ${CABAL} v2-haddock $WITHCOMPILER --with-haddock $HADDOCK ${TEST} ${BENCH} all | color_cabal_output
137+
- ${CABAL} v2-haddock $WITHCOMPILER --with-haddock $HADDOCK ${TEST} ${BENCH} all
156138
# Building without installed constraints for packages in global-db...
157139
- rm -f cabal.project.local
158-
- ${CABAL} v2-build $WITHCOMPILER --disable-tests --disable-benchmarks all | color_cabal_output
140+
- ${CABAL} v2-build $WITHCOMPILER --disable-tests --disable-benchmarks all
159141

160-
# REGENDATA ["hruby.cabal"]
142+
# REGENDATA ("0.13.20211116",["hruby.cabal","--output",".travis.yml"])
161143
# EOF

Foreign/Ruby/Helpers.hs

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
{-# LANGUAGE OverloadedStrings #-}
2+
{-# LANGUAGE TypeApplications #-}
23

34
module Foreign.Ruby.Helpers where
45

56
import Control.Monad
67
import Data.Aeson
8+
import Data.Aeson.Key (coercionToText, fromText, toText)
9+
import qualified Data.Aeson.KeyMap as KM
710
import qualified Data.ByteString.Char8 as BS
8-
import qualified Data.HashMap.Strict as HM
911
import Data.IORef
1012
import Data.Scientific
1113
import qualified Data.Text as T
1214
import qualified Data.Text.Encoding as T
15+
import Data.Type.Coercion (coerceWith)
1316
import qualified Data.Vector as V
1417
import Foreign
1518
import Foreign.C (withCString)
@@ -90,6 +93,9 @@ instance FromRuby Int where
9093
instance ToRuby Int where
9194
toRuby = toRubyIntegral
9295

96+
instance FromRuby Key where
97+
fromRuby = fmap (fmap fromText) . fromRuby @T.Text
98+
9399
-- | This is the most complete instance that is provided in this module.
94100
-- Please note that it is far from being sufficient for even basic
95101
-- requirements. For example, the `Value` type can only encode
@@ -127,7 +133,7 @@ instance FromRuby Value where
127133
case (vk, vv) of
128134
(Right jk, Right jv) -> writeIORef var ((jk, jv) : vvar) >> return 0
129135
_ -> return 1
130-
toHash = Object . HM.fromList
136+
toHash = Object . KM.fromList
131137
wappender <- mkRegisteredCB3 appender
132138
rb_hash_foreach v wappender rbNil
133139
freeHaskellFunPtr wappender
@@ -150,12 +156,17 @@ instance ToRuby Value where
150156
toRuby (Array ar) = toRuby (V.toList ar)
151157
toRuby (Object m) = do
152158
hash <- rb_hash_new
153-
forM_ (HM.toList m) $ \(k, v) -> do
159+
forM_ (KM.toList m) $ \(k, v) -> do
154160
rk <- toRuby k
155161
rv <- toRuby v
156162
rb_hash_aset hash rk rv
157163
return hash
158164

165+
instance ToRuby Key where
166+
toRuby k = toRuby $ case coercionToText of
167+
Nothing -> toText k
168+
Just co -> coerceWith co k
169+
159170
-- | An unsafe version of the corresponding "Foreign.Ruby.Safe" function.
160171
embedHaskellValue :: a -> IO RValue
161172
embedHaskellValue v = do

hruby.cabal

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: >= 1.10
22
name: hruby
3-
version: 0.3.8.1
3+
version: 0.4.0.0
44
synopsis: Embed a Ruby intepreter in your Haskell program !
55
description: This doesn't work with Ruby 1.9. Everything you need should be in Foreign.Ruby.Safe.
66
license: BSD3
@@ -11,7 +11,7 @@ maintainer: [email protected]
1111
category: Language
1212
build-type: Custom
1313
extra-source-files: test/*.rb
14-
Tested-With: GHC == 8.0.2, GHC == 8.2.2, GHC == 8.4.4, GHC == 8.6.5, GHC == 8.8.1
14+
Tested-With: GHC == 8.8.1, GHC == 8.10.2, GHC == 9.0.1
1515

1616
custom-setup
1717
setup-depends: Cabal, base, process
@@ -25,7 +25,7 @@ library
2525
exposed-modules: Foreign.Ruby, Foreign.Ruby.Bindings, Foreign.Ruby.Helpers, Foreign.Ruby.Safe
2626
ghc-options: -Wall
2727
build-depends: base >= 4.6 && < 5
28-
, aeson >= 0.7
28+
, aeson >= 2
2929
, bytestring >= 0.10.0.2
3030
, text >= 0.11
3131
, attoparsec >= 0.11 && < 0.15

test/roundtrip.hs

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Main where
22

33
import Control.Monad
44
import Data.Aeson
5+
import Data.Aeson.Key (fromText)
56
import qualified Data.Text as T
67
import qualified Data.Vector as V
78
import Foreign.Ruby
@@ -29,7 +30,7 @@ n = fmap (Number . fromIntegral) (arbitrary :: Gen Integer)
2930

3031
h :: Gen Value
3132
h = fmap object $ do
32-
k <- listOf str
33+
k <- map fromText <$> listOf str
3334
v <- listOf subvalue
3435
return (zip k v)
3536

@@ -45,10 +46,7 @@ roundTrip i = monadicIO $ do
4546
nxt <- safeMethodCall i "TestClass" "testfunc" [rub]
4647
case nxt of
4748
Right x -> do
48-
out <-
49-
fromRuby i x >>= \r -> case r of
50-
Right r' -> return r'
51-
Left rr -> error (show rr)
49+
out <- fromRuby i x >>= either (error . show) pure
5250
when (out /= v) (print out)
5351
return (v == out)
5452
Left rr -> print rr >> return False

0 commit comments

Comments
 (0)