Repository: thrift
Updated Branches:
  refs/heads/master 73b58a246 -> 303eb1b4f


THRIFT-2401 Haskell tutorial compiles

Patch: John Chee & Roger Meier

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


Project: http://git-wip-us.apache.org/repos/asf/thrift/repo
Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/303eb1b4
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/303eb1b4
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/303eb1b4

Branch: refs/heads/master
Commit: 303eb1b4f0ada3b6be45625beb6020a757a2f429
Parents: 73b58a2
Author: Roger Meier <[email protected]>
Authored: Wed May 14 00:49:41 2014 +0200
Committer: Roger Meier <[email protected]>
Committed: Wed May 14 00:49:41 2014 +0200

----------------------------------------------------------------------
 compiler/cpp/src/generate/t_hs_generator.cc |  4 +--
 configure.ac                                |  1 +
 lib/hs/Thrift.cabal                         |  2 +-
 tutorial/Makefile.am                        |  4 +++
 tutorial/hs/HaskellClient.hs                |  9 +++---
 tutorial/hs/HaskellServer.hs                | 12 ++++---
 tutorial/hs/Makefile.am                     | 40 +++++++++++++++++++++++
 tutorial/hs/Setup.lhs                       | 21 ++++++++++++
 tutorial/hs/ThriftTutorial.cabal            | 41 +++++++++++++++++-------
 9 files changed, 111 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/thrift/blob/303eb1b4/compiler/cpp/src/generate/t_hs_generator.cc
----------------------------------------------------------------------
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 @@ 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";

http://git-wip-us.apache.org/repos/asf/thrift/blob/303eb1b4/configure.ac
----------------------------------------------------------------------
diff --git a/configure.ac b/configure.ac
index 995dba5..cc89593 100755
--- a/configure.ac
+++ b/configure.ac
@@ -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

http://git-wip-us.apache.org/repos/asf/thrift/blob/303eb1b4/lib/hs/Thrift.cabal
----------------------------------------------------------------------
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 @@ Synopsis:       Haskell bindings for the Apache Thrift RPC 
system
 Homepage:       http://thrift.apache.org
 Bug-Reports:    https://issues.apache.org/jira/browse/THRIFT
 Maintainer:     [email protected]
-License-File:   LICENSE
+License-File:   ../../LICENSE
 
 Description:
   Haskell bindings for the Apache Thrift RPC system. Requires the use of the 
thrift code generator.

http://git-wip-us.apache.org/repos/asf/thrift/blob/303eb1b4/tutorial/Makefile.am
----------------------------------------------------------------------
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 @@ if WITH_RUBY
 SUBDIRS += rb
 endif
 
+if WITH_HASKELL
+SUBDIRS += hs
+endif
+
 if WITH_GO
 SUBDIRS += go
 endif

http://git-wip-us.apache.org/repos/asf/thrift/blob/303eb1b4/tutorial/hs/HaskellClient.hs
----------------------------------------------------------------------
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
 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

http://git-wip-us.apache.org/repos/asf/thrift/blob/303eb1b4/tutorial/hs/HaskellServer.hs
----------------------------------------------------------------------
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.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

http://git-wip-us.apache.org/repos/asf/thrift/blob/303eb1b4/tutorial/hs/Makefile.am
----------------------------------------------------------------------
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

http://git-wip-us.apache.org/repos/asf/thrift/blob/303eb1b4/tutorial/hs/Setup.lhs
----------------------------------------------------------------------
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

http://git-wip-us.apache.org/repos/asf/thrift/blob/303eb1b4/tutorial/hs/ThriftTutorial.cabal
----------------------------------------------------------------------
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:     [email protected]
+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

Reply via email to