From 303eb1b4f0ada3b6be45625beb6020a757a2f429 Mon Sep 17 00:00:00 2001 From: Roger Meier Date: Wed, 14 May 2014 00:49:41 +0200 Subject: [PATCH] THRIFT-2401 Haskell tutorial compiles 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 | 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(-) mode change 100644 => 100755 lib/hs/Thrift.cabal create mode 100755 tutorial/hs/Makefile.am create mode 100644 tutorial/hs/Setup.lhs mode change 100644 => 100755 tutorial/hs/ThriftTutorial.cabal diff --git a/compiler/cpp/src/generate/t_hs_generator.cc b/compiler/cpp/src/generate/t_hs_generator.cc index bbfaba56..d52088b4 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"; diff --git a/configure.ac b/configure.ac index 995dba53..cc89593e 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 diff --git a/lib/hs/Thrift.cabal b/lib/hs/Thrift.cabal old mode 100644 new mode 100755 index 6dd9146e..b6592929 --- 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: 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 6fc92092..ccc9c462 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 diff --git a/tutorial/hs/HaskellClient.hs b/tutorial/hs/HaskellClient.hs index a56187b6..18d72ad5 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 diff --git a/tutorial/hs/HaskellServer.hs b/tutorial/hs/HaskellServer.hs index 4f9ab7c9..212e722e 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 diff --git a/tutorial/hs/Makefile.am b/tutorial/hs/Makefile.am new file mode 100755 index 00000000..2b027328 --- /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 00000000..c7df182d --- /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 1655ce7c..6cc29e8a --- 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 -- 2.17.1