THRIFT-2401 Haskell tutorial compiles
Patch: John Chee & Roger Meier
includes minifix from:
THRIFT-2453 haskell tutorial: fix up division by 0 example
diff --git a/compiler/cpp/src/generate/t_hs_generator.cc b/compiler/cpp/src/generate/t_hs_generator.cc
index bbfaba5..d52088b 100644
--- a/compiler/cpp/src/generate/t_hs_generator.cc
+++ b/compiler/cpp/src/generate/t_hs_generator.cc
@@ -249,7 +249,7 @@
" (.), (&&), (||), (==), (++), ($), (-) )\n"
"\n"
"import Control.Exception\n"
- "import Data.ByteString.Lazy\n"
+ "import qualified Data.ByteString.Lazy as BL\n"
"import Data.Hashable\n"
"import Data.Int\n"
"import Data.Text.Lazy ( Text )\n"
@@ -1472,7 +1472,7 @@
t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
switch (tbase) {
case t_base_type::TYPE_VOID: return "()";
- case t_base_type::TYPE_STRING: return (((t_base_type*)type)->is_binary() ? "ByteString" : "Text");
+ case t_base_type::TYPE_STRING: return (((t_base_type*)type)->is_binary() ? "BL.ByteString" : "Text");
case t_base_type::TYPE_BOOL: return "Bool";
case t_base_type::TYPE_BYTE: return "Int8";
case t_base_type::TYPE_I16: return "Int16";
diff --git a/configure.ac b/configure.ac
index 995dba5..cc89593 100755
--- a/configure.ac
+++ b/configure.ac
@@ -660,6 +660,7 @@
tutorial/Makefile
tutorial/cpp/Makefile
tutorial/go/Makefile
+ tutorial/hs/Makefile
tutorial/java/Makefile
tutorial/js/Makefile
tutorial/nodejs/Makefile
diff --git a/lib/hs/Thrift.cabal b/lib/hs/Thrift.cabal
old mode 100644
new mode 100755
index 6dd9146..b659292
--- a/lib/hs/Thrift.cabal
+++ b/lib/hs/Thrift.cabal
@@ -27,7 +27,7 @@
Homepage: http://thrift.apache.org
Bug-Reports: https://issues.apache.org/jira/browse/THRIFT
Maintainer: dev@thrift.apache.org
-License-File: LICENSE
+License-File: ../../LICENSE
Description:
Haskell bindings for the Apache Thrift RPC system. Requires the use of the thrift code generator.
diff --git a/tutorial/Makefile.am b/tutorial/Makefile.am
index 6fc9209..ccc9c46 100755
--- a/tutorial/Makefile.am
+++ b/tutorial/Makefile.am
@@ -42,6 +42,10 @@
SUBDIRS += rb
endif
+if WITH_HASKELL
+SUBDIRS += hs
+endif
+
if WITH_GO
SUBDIRS += go
endif
diff --git a/tutorial/hs/HaskellClient.hs b/tutorial/hs/HaskellClient.hs
index a56187b..18d72ad 100644
--- a/tutorial/hs/HaskellClient.hs
+++ b/tutorial/hs/HaskellClient.hs
@@ -30,7 +30,9 @@
import Thrift.Transport.Handle
import Thrift.Server
+import Control.Exception
import Data.Maybe
+import Data.Text.Lazy
import Text.Printf
import Network
@@ -52,9 +54,8 @@
f_Work_comment = Nothing
}
- -- TODO - get this one working
- --catch (Client.calculate client 1 work) (\except ->
- -- printf "InvalidOp %s" (show except))
+ Control.Exception.catch (printf "1/0=%d\n" =<< Client.calculate client 1 work)
+ (\e -> printf "InvalidOperation %s\n" (show (e :: InvalidOperation)))
let work = Work { f_Work_op = Just SUBTRACT,
@@ -67,7 +68,7 @@
printf "15-10=%d\n" diff
log <- SClient.getStruct client 1
- printf "Check log: %s\n" $ fromJust $ f_SharedStruct_value log
+ printf "Check log: %s\n" $ fromJust $ unpack `fmap` f_SharedStruct_value log
-- Close!
tClose transport
diff --git a/tutorial/hs/HaskellServer.hs b/tutorial/hs/HaskellServer.hs
index 4f9ab7c..212e722 100644
--- a/tutorial/hs/HaskellServer.hs
+++ b/tutorial/hs/HaskellServer.hs
@@ -17,6 +17,8 @@
-- under the License.
--
+{-# LANGUAGE OverloadedStrings #-}
+
import qualified Calculator
import Calculator_Iface
import Tutorial_Types
@@ -28,6 +30,8 @@
import Thrift.Transport
import Thrift.Server
+import Data.Int
+import Data.String
import Data.Maybe
import Text.Printf
import Control.Exception (throw)
@@ -36,7 +40,7 @@
import Data.Map ((!))
import Data.Monoid
-data CalculatorHandler = CalculatorHandler {mathLog :: MVar (M.Map Int SharedStruct)}
+data CalculatorHandler = CalculatorHandler {mathLog :: MVar (M.Map Int32 SharedStruct)}
newCalculatorHandler = do
log <- newMVar mempty
@@ -70,16 +74,16 @@
if num2 work == 0 then
throw $
InvalidOperation {
- f_InvalidOperation_what = Just $ fromEnum $ op work,
+ f_InvalidOperation_what = Just $ fromIntegral $ fromEnum $ op work,
f_InvalidOperation_why = Just "Cannot divide by 0"
}
else
num1 work `div` num2 work
- let logEntry = SharedStruct (Just logid) (Just (show val))
+ let logEntry = SharedStruct (Just logid) (Just (fromString $ show $ val))
modifyMVar_ (mathLog self) $ return .(M.insert logid logEntry)
- return val
+ return $! val
where
-- stupid dynamic languages f'ing it up
diff --git a/tutorial/hs/Makefile.am b/tutorial/hs/Makefile.am
new file mode 100755
index 0000000..2b02732
--- /dev/null
+++ b/tutorial/hs/Makefile.am
@@ -0,0 +1,40 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+all-local:
+ $(CABAL) configure $(CABAL_CONFIGURE_FLAGS)
+ $(top_builddir)/compiler/cpp/thrift --gen hs -r $(top_srcdir)/tutorial/tutorial.thrift
+ $(CABAL) build
+
+install-exec-hook:
+ $(CABAL) install
+
+# Make sure this doesn't fail if Haskell is not configured.
+clean-local:
+ $(CABAL) clean
+ $(RM) -r gen-*
+
+check-local:
+ $(CABAL) check
+
+tutorialserver: all
+ dist/build/HaskellServer/HaskellServer
+
+tutorialclient: all
+ dist/build/HaskellClient/HaskellClient
\ No newline at end of file
diff --git a/tutorial/hs/Setup.lhs b/tutorial/hs/Setup.lhs
new file mode 100644
index 0000000..c7df182
--- /dev/null
+++ b/tutorial/hs/Setup.lhs
@@ -0,0 +1,21 @@
+#!/usr/bin/env runhaskell
+
+> -- Licensed to the Apache Software Foundation (ASF) under one
+> -- or more contributor license agreements. See the NOTICE file
+> -- distributed with this work for additional information
+> -- regarding copyright ownership. The ASF licenses this file
+> -- to you under the Apache License, Version 2.0 (the
+> -- "License"); you may not use this file except in compliance
+> -- with the License. You may obtain a copy of the License at
+> --
+> -- http://www.apache.org/licenses/LICENSE-2.0
+> --
+> -- Unless required by applicable law or agreed to in writing,
+> -- software distributed under the License is distributed on an
+> -- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+> -- KIND, either express or implied. See the License for the
+> -- specific language governing permissions and limitations
+> -- under the License.
+
+> import Distribution.Simple
+> main = defaultMain
diff --git a/tutorial/hs/ThriftTutorial.cabal b/tutorial/hs/ThriftTutorial.cabal
old mode 100644
new mode 100755
index 1655ce7..6cc29e8
--- a/tutorial/hs/ThriftTutorial.cabal
+++ b/tutorial/hs/ThriftTutorial.cabal
@@ -19,30 +19,47 @@
Name: ThriftTutorial
Version: 0.1.0
-Cabal-Version: >= 1.2
-License: Apache2
+Cabal-Version: >= 1.4
+License: OtherLicense
Category: Foreign
Build-Type: Simple
Synopsis: Thrift Tutorial library package
+Homepage: http://thrift.apache.org
+Bug-Reports: https://issues.apache.org/jira/browse/THRIFT
+Maintainer: dev@thrift.apache.org
+License-File: ../../LICENSE
+
+Description:
+ Haskell tutorial for the Apache Thrift RPC system. Requires the use of the thrift code generator.
Executable HaskellServer
Main-is: HaskellServer.hs
Hs-Source-Dirs:
- ., ../gen-hs/
+ ., gen-hs/
Build-Depends:
- base >=4, network, ghc-prim, containers, Thrift
- ghc-options:
- -fglasgow-exts
+ base >= 4, base < 5, network, ghc-prim, containers, thrift, vector, unordered-containers, text, hashable, bytestring
Extensions:
- DeriveDataTypeable
+ DeriveDataTypeable,
+ ExistentialQuantification,
+ FlexibleInstances,
+ KindSignatures,
+ MagicHash,
+ RankNTypes,
+ ScopedTypeVariables,
+ TypeSynonymInstances
Executable HaskellClient
Main-is: HaskellClient.hs
Hs-Source-Dirs:
- ., ../gen-hs/
+ ., gen-hs/
Build-Depends:
- base >=4, network, ghc-prim, containers, Thrift
- ghc-options:
- -fglasgow-exts
+ base >= 4, base < 5, network, ghc-prim, containers, thrift, vector
Extensions:
- DeriveDataTypeable
+ DeriveDataTypeable,
+ ExistentialQuantification,
+ FlexibleInstances,
+ KindSignatures,
+ MagicHash,
+ RankNTypes,
+ ScopedTypeVariables,
+ TypeSynonymInstances