Skip to content

Commit 8f319bd

Browse files
committed
Document the code
1 parent 628e02c commit 8f319bd

File tree

17 files changed

+78
-29
lines changed

17 files changed

+78
-29
lines changed

app/Main.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
-- | Main entry point for the layer
12
module Main
23
( main
34
) where

default.nix

Lines changed: 0 additions & 24 deletions
This file was deleted.

shell.nix

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/Aws/Lambda/Configuration.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ import qualified Aws.Lambda.Meta.Main as Main
1717
import qualified Aws.Lambda.Meta.Run as Run
1818
import qualified Aws.Lambda.Runtime.IPC as IPC
1919

20+
{-| Generates a @main@ function to be used with the
21+
AWS Lambda layer.
22+
-}
2023
configureLambda :: Meta.DecsQ
2124
configureLambda = do
2225
main <- Main.generate
2326
run <- Run.generate
24-
return $ main <> [run]
27+
return (main <> [run])

src/Aws/Lambda/Meta/Common.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-| Helper functions to make code generation easier -}
12
module Aws.Lambda.Meta.Common
23
( declarationName
34
, expressionName

src/Aws/Lambda/Meta/Discover.hs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
{-| Discovery of AWS Lambda handlers
2+
A handler is basically a function that has a type definition that
3+
starts with "handler :: ".
4+
-}
15
module Aws.Lambda.Meta.Discover
2-
(handlers) where
6+
( handlers
7+
) where
38

49
import qualified Control.Monad as Monad
510
import Data.Function ((&))
@@ -10,6 +15,13 @@ import qualified Data.Text as Text
1015
import Path
1116
import qualified Path.IO as PathIO
1217

18+
{-| Returns a list of handler paths that look like
19+
20+
@src/Foo/Bar/Quux.handler@
21+
22+
It is the path to the source file, but changing the
23+
extension for ".handler"
24+
-}
1325
handlers :: IO [Text]
1426
handlers = do
1527
(_, files) <- PathIO.listDirRecurRel [reldir|.|]

src/Aws/Lambda/Meta/Dispatch.hs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-| Dispatcher generation -}
12
module Aws.Lambda.Meta.Dispatch
23
( generate
34
, decodeObj
@@ -13,16 +14,27 @@ import qualified Language.Haskell.TH as Meta
1314

1415
import Aws.Lambda.Meta.Common
1516

17+
{-| Helper function that the dispatcher will use to
18+
decode the JSON that comes as an AWS Lambda event into the
19+
appropriate type expected by the handler.
20+
-}
1621
decodeObj :: FromJSON a => String -> a
1722
decodeObj x =
1823
case (eitherDecode $ LazyByteString.pack x) of
1924
Left e -> error e
2025
Right v -> v
2126

27+
{-| Generates the dispatcher out of a list of
28+
handler names in the form @src/Foo/Bar.handler@
29+
30+
This dispatcher has a case for each of the handlers that calls
31+
the appropriate qualified function. In the case of the example above,
32+
the dispatcher will call @Foo.Bar.handler@.
33+
-}
2234
generate :: [Text] -> Meta.ExpQ
23-
generate fileNames = do
35+
generate handlerNames = do
2436
caseExp <- expressionName "functionHandler"
25-
matches <- traverse handlerCase fileNames
37+
matches <- traverse handlerCase handlerNames
2638
unmatched <- unmatchedCase
2739
pure $ Meta.CaseE caseExp (matches <> [unmatched])
2840

src/Aws/Lambda/Meta/Main.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-| main function generation for interoperation with the layer -}
12
module Aws.Lambda.Meta.Main
23
( LambdaOptions(..)
34
, generate
@@ -11,13 +12,15 @@ import qualified Options.Generic as Options
1112

1213
import Aws.Lambda.Meta.Common
1314

15+
-- | Options that the generated main expects
1416
data LambdaOptions = LambdaOptions
1517
{ eventObject :: !String
1618
, contextObject :: !String
1719
, functionHandler :: !String
1820
, executionUuid :: !String
1921
} deriving (Generic, Options.ParseRecord)
2022

23+
-- | Generate the main function that the layer will call
2124
generate :: Meta.DecsQ
2225
generate = [d|
2326
$(declarationName "main") = getRecord "" >>= run

src/Aws/Lambda/Meta/Run.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import Aws.Lambda.Meta.Common
88
import qualified Aws.Lambda.Meta.Discover as Discover
99
import qualified Aws.Lambda.Meta.Dispatch as Dispatch
1010

11+
{-| Generate the run function
12+
13+
It will create a dispatcher that is a huge @case@ expression that
14+
expects the name of the handler provided by AWS Lambda, and will
15+
execute the appropriate user function
16+
-}
1117
generate :: Meta.DecQ
1218
generate = do
1319
handlers <- Meta.runIO Discover.handlers

src/Aws/Lambda/Runtime.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import qualified Aws.Lambda.Runtime.Error as Error
1212
import qualified Aws.Lambda.Runtime.IPC as IPC
1313
import qualified Aws.Lambda.Runtime.Publish as Publish
1414

15+
-- | Runs the user @haskell_lambda@ executable and posts back the
16+
-- results
1517
runLambda
1618
:: Http.Manager
1719
-> IO ()

0 commit comments

Comments
 (0)