diff options
author | Roger Meier <roger@apache.org> | 2014-05-14 00:49:41 +0200 |
---|---|---|
committer | Roger Meier <roger@apache.org> | 2014-05-14 00:49:41 +0200 |
commit | 303eb1b4f0ada3b6be45625beb6020a757a2f429 (patch) | |
tree | 0ef6b88a11aac6d65c27c5fdd6eef2f2b85f7ab2 /tutorial | |
parent | 73b58a24699eb6b9d43ad05f8a19cd084979c31a (diff) | |
download | thrift-303eb1b4f0ada3b6be45625beb6020a757a2f429.tar.gz |
THRIFT-2401 Haskell tutorial compiles
Patch: John Chee & Roger Meier
includes minifix from:
THRIFT-2453 haskell tutorial: fix up division by 0 example
Diffstat (limited to 'tutorial')
-rwxr-xr-x | tutorial/Makefile.am | 4 | ||||
-rw-r--r-- | tutorial/hs/HaskellClient.hs | 9 | ||||
-rw-r--r-- | tutorial/hs/HaskellServer.hs | 12 | ||||
-rwxr-xr-x | tutorial/hs/Makefile.am | 40 | ||||
-rw-r--r-- | tutorial/hs/Setup.lhs | 21 | ||||
-rwxr-xr-x[-rw-r--r--] | tutorial/hs/ThriftTutorial.cabal | 41 |
6 files changed, 107 insertions, 20 deletions
diff --git a/tutorial/Makefile.am b/tutorial/Makefile.am index 6fc920925..ccc9c4620 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 a56187b6a..18d72ad5b 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 4f9ab7c99..212e722e9 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 000000000..2b0273280 --- /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 000000000..c7df182d3 --- /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 index 1655ce7cd..6cc29e8ac 100644..100755 --- 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 |