Skip to content

Commit eb115b0

Browse files
committed
Refactor Configuration
1 parent edf2d49 commit eb115b0

File tree

6 files changed

+45
-50
lines changed

6 files changed

+45
-50
lines changed

package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,4 @@ ghc-options:
7979
- -Wpartial-fields
8080
- -fhide-source-paths
8181
- -freverse-errors
82+
- -O2

src/Aws/Lambda/Configuration.hs

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,21 @@ module Aws.Lambda.Configuration
44
, Main.generate
55
, Main.getRecord
66
, configureLambda
7-
, returnAndFail
8-
, returnAndSucceed
9-
, decodeObj
7+
, IPC.returnAndFail
8+
, IPC.returnAndSucceed
9+
, Dispatch.decodeObj
1010
)
1111
where
1212

13-
import Data.Aeson
14-
15-
import qualified Data.ByteString.Lazy as LazyByteString
16-
import Data.Text (Text)
17-
import qualified Data.Text as Text
18-
import qualified Data.Text.Encoding as Encoding
19-
import Language.Haskell.TH
20-
import System.Exit (exitFailure, exitSuccess)
21-
import System.IO (hFlush, stderr, stdout)
13+
import qualified Language.Haskell.TH as Meta
2214

2315
import qualified Aws.Lambda.Meta.Main as Main
2416
import qualified Aws.Lambda.Meta.Run as Run
17+
import qualified Aws.Lambda.Meta.Dispatch as Dispatch
18+
import qualified Aws.Lambda.Runtime.IPC as IPC
2519

26-
putTextLn :: Text -> IO ()
27-
putTextLn = putStrLn . Text.unpack
28-
29-
configureLambda :: Q [Dec]
20+
configureLambda :: Meta.DecsQ
3021
configureLambda = do
3122
main <- Main.generate
3223
run <- Run.generate
3324
return $ main <> [run]
34-
35-
returnAndFail :: ToJSON a => Text -> a -> IO ()
36-
returnAndFail uuid v = do
37-
hFlush stdout
38-
putTextLn uuid
39-
hFlush stdout
40-
putTextLn (Encoding.decodeUtf8 $ LazyByteString.toStrict $ encode v)
41-
hFlush stdout
42-
hFlush stderr
43-
exitFailure
44-
45-
returnAndSucceed :: ToJSON a => Text -> a -> IO ()
46-
returnAndSucceed uuid v = do
47-
hFlush stdout
48-
putTextLn uuid
49-
hFlush stdout
50-
putTextLn (Encoding.decodeUtf8 $ LazyByteString.toStrict $ encode v)
51-
hFlush stdout
52-
exitSuccess
53-
54-
decodeObj :: FromJSON a => Text -> a
55-
decodeObj x =
56-
case (eitherDecode $ LazyByteString.fromStrict $ Encoding.encodeUtf8 x) of
57-
Left e -> error e
58-
Right v -> v

src/Aws/Lambda/Meta/Discover.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ handlerNames modules =
3131
where
3232
changeExtensionToHandler file =
3333
setFileExtension ".handler" file
34-
& Maybe.fromJust
34+
& Maybe.fromJust -- The path will be always parsable, as we just replace the extension
3535

3636
containsHandler :: Path Rel File -> IO Bool
3737
containsHandler file = do

src/Aws/Lambda/Meta/Dispatch.hs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
module Aws.Lambda.Meta.Dispatch
2-
(generate) where
2+
( generate
3+
, decodeObj
4+
) where
35

46
import Data.Function ((&))
57
import Data.Text (Text)
68
import qualified Data.Text as Text
79

10+
import Data.Aeson
11+
import qualified Data.ByteString.Lazy.Char8 as LazyByteString
812
import qualified Language.Haskell.TH as Meta
913

1014
import Aws.Lambda.Meta.Common
1115

16+
decodeObj :: FromJSON a => String -> a
17+
decodeObj x =
18+
case (eitherDecode $ LazyByteString.pack x) of
19+
Left e -> error e
20+
Right v -> v
21+
1222
generate :: [Text] -> Meta.ExpQ
1323
generate fileNames = do
1424
caseExp <- expressionName "functionHandler"
1525
matches <- traverse handlerCase fileNames
1626
unmatched <- unmatchedCase
1727
pure $ Meta.CaseE caseExp (matches <> [unmatched])
1828

19-
2029
handlerCase :: Text -> Meta.MatchQ
2130
handlerCase lambdaHandler = do
2231
let pat = Meta.LitP (Meta.StringL $ Text.unpack lambdaHandler)
@@ -31,7 +40,6 @@ handlerCase lambdaHandler = do
3140
& Text.drop 1
3241
& Text.replace "/" "."
3342

34-
3543
unmatchedCase :: Meta.MatchQ
3644
unmatchedCase = do
3745
let pattern = Meta.WildP

src/Aws/Lambda/Meta/Main.hs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ module Aws.Lambda.Meta.Main
44
, Options.getRecord
55
) where
66

7-
import Data.Text (Text)
87
import GHC.Generics (Generic)
98

109
import qualified Options.Generic as Options
@@ -13,10 +12,10 @@ import qualified Language.Haskell.TH as Meta
1312
import Aws.Lambda.Meta.Common
1413

1514
data LambdaOptions = LambdaOptions
16-
{ eventObject :: !Text
17-
, contextObject :: !Text
18-
, functionHandler :: !Text
19-
, executionUuid :: !Text
15+
{ eventObject :: !String
16+
, contextObject :: !String
17+
, functionHandler :: !String
18+
, executionUuid :: !String
2019
} deriving (Generic, Options.ParseRecord)
2120

2221
generate :: Meta.DecsQ

src/Aws/Lambda/Runtime/IPC.hs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module Aws.Lambda.Runtime.IPC
22
( invoke
3+
, returnAndFail
4+
, returnAndSucceed
35
) where
46

57

@@ -21,6 +23,25 @@ import qualified Aws.Lambda.Runtime.Environment as Environment
2123
import qualified Aws.Lambda.Runtime.Error as Error
2224
import Aws.Lambda.Runtime.Result (LambdaResult (..))
2325

26+
returnAndFail :: ToJSON a => String -> a -> IO ()
27+
returnAndFail uuid v = do
28+
IO.hFlush IO.stdout
29+
putStrLn uuid
30+
IO.hFlush IO.stdout
31+
putStrLn (ByteString.unpack $ encode v)
32+
IO.hFlush IO.stdout
33+
IO.hFlush IO.stderr
34+
Exit.exitFailure
35+
36+
returnAndSucceed :: ToJSON a => String -> a -> IO ()
37+
returnAndSucceed uuid v = do
38+
IO.hFlush IO.stdout
39+
putStrLn uuid
40+
IO.hFlush IO.stdout
41+
putStrLn (ByteString.unpack $ encode v)
42+
IO.hFlush IO.stdout
43+
Exit.exitSuccess
44+
2445
invoke
2546
:: Throws Error.Invocation
2647
=> Throws Error.Parsing

0 commit comments

Comments
 (0)