THRIFT-2401 Haskell tutorial compiles
authorRoger Meier <roger@apache.org>
Tue, 13 May 2014 22:49:41 +0000 (00:49 +0200)
committerRoger Meier <roger@apache.org>
Tue, 13 May 2014 22:49:41 +0000 (00:49 +0200)
Patch: John Chee & Roger Meier

includes minifix from:
THRIFT-2453 haskell tutorial: fix up division by 0 example

compiler/cpp/src/generate/t_hs_generator.cc
configure.ac
lib/hs/Thrift.cabal [changed mode: 0644->0755]
tutorial/Makefile.am
tutorial/hs/HaskellClient.hs
tutorial/hs/HaskellServer.hs
tutorial/hs/Makefile.am [new file with mode: 0755]
tutorial/hs/Setup.lhs [new file with mode: 0644]
tutorial/hs/ThriftTutorial.cabal [changed mode: 0644->0755]

index bbfaba5..d52088b 100644 (file)
@@ -249,7 +249,7 @@ string t_hs_generator::hs_imports() {
       "                 (.), (&&), (||), (==), (++), ($), (-) )\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 @@ string t_hs_generator::render_hs_type(t_type* type, bool needs_parens) {
     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";
index 995dba5..cc89593 100755 (executable)
@@ -660,6 +660,7 @@ AC_CONFIG_FILES([
   tutorial/Makefile
   tutorial/cpp/Makefile
   tutorial/go/Makefile
+  tutorial/hs/Makefile
   tutorial/java/Makefile
   tutorial/js/Makefile
   tutorial/nodejs/Makefile
old mode 100644 (file)
new mode 100755 (executable)
index 6dd9146..b659292
@@ -27,7 +27,7 @@ Synopsis:       Haskell bindings for the Apache Thrift RPC system
 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.
index 6fc9209..ccc9c46 100755 (executable)
@@ -42,6 +42,10 @@ if WITH_RUBY
 SUBDIRS += rb
 endif
 
+if WITH_HASKELL
+SUBDIRS += hs
+endif
+
 if WITH_GO
 SUBDIRS += go
 endif
index a56187b..18d72ad 100644 (file)
@@ -30,7 +30,9 @@ import Thrift.Transport
 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 @@ main = do
                     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 @@ main = do
   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
index 4f9ab7c..212e722 100644 (file)
@@ -17,6 +17,8 @@
 -- under the License.
 --
 
+{-# LANGUAGE OverloadedStrings #-}
+
 import qualified Calculator
 import Calculator_Iface
 import Tutorial_Types
@@ -28,6 +30,8 @@ import Thrift.Protocol.Binary
 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 qualified Data.Map as M
 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 @@ instance Calculator_Iface CalculatorHandler where
                     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 (executable)
index 0000000..2b02732
--- /dev/null
@@ -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 (file)
index 0000000..c7df182
--- /dev/null
@@ -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
old mode 100644 (file)
new mode 100755 (executable)
index 1655ce7..6cc29e8
 
 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