Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
blair55 committed Oct 4, 2023
0 parents commit 44ac586
Show file tree
Hide file tree
Showing 16 changed files with 4,288 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"version": 1,
"isRoot": true,
"tools": {
"amazon.lambda.tools": {
"version": "5.8.0",
"commands": [
"dotnet-lambda"
]
},
"paket": {
"version": "7.2.1",
"commands": [
"paket"
]
},
"fantomas": {
"version": "6.2.1",
"commands": [
"fantomas"
]
}
}
}
17 changes: 17 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
root = true

[*]
indent_style = space
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.yml]
indent_size = 2

[*.json]
indent_size = 2

[*.fs]
max_line_length = 80
indent_size = 4
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
bin/
obj/
build/
deploy/
publish/
packages/
paket-files/
node_modules/
.paket/
.serverless/
.ionide/
.dynamodb/
package.zip
deploy.zip
yarn-error.log
.idea/
.DS_Store
9 changes: 9 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations.
// Extension identifier format: ${publisher}.${name}. Example: vscode.csharp

// List of extensions which should be recommended for users of this workspace.
"recommendations": ["ionide.ionide-fsharp", "editorconfig.editorconfig"],
// List of extensions recommended by VS Code that should not be recommended for users of this workspace.
"unwantedRecommendations": []
}
23 changes: 23 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "build",
"group": {
"kind": "build",
"isDefault": true
}
},
{
"type": "npm",
"script": "test",
"group": {
"kind": "test",
"isDefault": true
}
}
]
}
13 changes: 13 additions & 0 deletions example-two-input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"Records": [
{
"Body": "{ \"Name\": \"Bob\", \"Size\": 2 }"
},
{
"Body": "{ \"Name\": \"Sue\", \"Size\": 9 }"
},
{
"Body": "{ \"Name\": \"Ian\", \"Size\": 5 }"
}
]
}
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"private": true,
"devDependencies": {
"@totallymoney/github-serverless-dotnet-artifacts": "^3.2.0",
"serverless": "^3.35.2",
"serverless-pseudo-parameters": "^2.6.1"
},
"scripts": {
"preinstall": "dotnet tool restore",
"prebuild": "dotnet paket restore",
"build": "dotnet build src",
"test": "dotnet run --project tests",
"publish": "gsda-publish mediaingenuity/HiSerialize src",
"deploy": "gsda-deploy mediaingenuity/HiSerialize",
"pick": "gsda-pick -e stage -e prod"
}
}
5 changes: 5 additions & 0 deletions paket.dependencies
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
framework auto-detect
source https://api.nuget.org/v3/index.json
nuget Amazon.Lambda.Core
nuget Amazon.Lambda.SQSEvents
nuget FSharp.Core
6 changes: 6 additions & 0 deletions paket.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
RESTRICTION: == net6.0
NUGET
remote: https://api.nuget.org/v3/index.json
Amazon.Lambda.Core (2.1)
Amazon.Lambda.SQSEvents (2.1)
FSharp.Core (7.0.400)
22 changes: 22 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# HiSerialize

## Deploying

Review the [release artifacts](https://github.com/mediaingenuity/HiSerialize/releases).

Use `pick` for interactive deployments:

```bash
$ yarn pick
```

`pick` simply uses the underlying `deploy` command:

```bash
$ yarn deploy <version> <env> # e.g yarn deploy 0.1.2 prod
```

## Tooling

* Project created by [fsharp-lambda-scaffold](https://github.com/mediaingenuity/fsharp-lambda-scaffold)
* CD provided by [github-serverless-dotnet-artifacts](https://github.com/totallymoney/github-serverless-dotnet-artifacts)
34 changes: 34 additions & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
service: sls-HiSerialize

frameworkVersion: "3"

plugins:
- serverless-pseudo-parameters

custom:
gsda:
version: ${env:VERSION, '0.0.0'}
deployedDate: ${env:DEPLOYED_DATE, ''}

provider:
name: aws
runtime: dotnet6
architecture: arm64
deploymentMethod: direct
stage: ${opt:stage, 'stage'}
region: eu-west-1
profile: sandbox
timeout: 30
memorySize: 512
logRetentionInDays: 14
versionFunctions: false

package:
artifact: package.zip

functions:
ExampleOne:
handler: HiSerialize::ExampleOne::ExampleOne

ExampleTwo:
handler: HiSerialize::ExampleTwo::ExampleTwo
60 changes: 60 additions & 0 deletions src/ExampleOne.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module ExampleOne



// Lambda dotnet serialization docs
// https://docs.aws.amazon.com/lambda/latest/dg/csharp-handler.html#csharp-handler-serializer-reflection


open System.IO
open System.Text.Json
open Amazon.Lambda.Core


type MySerializer() =

// member this.Serialize(res, stream) =
// (this :> ILambdaSerializer).Serialize(res, stream)

// member this.Deserialize(stream) =
// (this :> ILambdaSerializer).Deserialize(stream)

interface ILambdaSerializer with

member this.Serialize(res, stream: Stream) : unit =
use writer = new Utf8JsonWriter(stream)
JsonSerializer.Serialize(writer, res)

member this.Deserialize<'T>(stream: Stream) : 'T =
JsonSerializer.Deserialize<'T> stream




type MyDto = { Name: string; Size: int }

[<LambdaSerializer(typeof<MySerializer>)>]
let ExampleOne (event: MyDto) =
printfn "the event is:\n\n%A" event
event


(*
yarn gsda-dev-deploy nick
*)

(*
yarn sls invoke -s nick -f ExampleOne -d '{ "Name": "Bob", "Size": 2 }'
*)


// C# interface example
// https://sharplab.io/#v2:CYLg1APgAgzABASwHYBcCmAnAZgQwMZpwCSAsAFADe5cNcUALHABoAUUAjAAxwDOAlAG5yAX3LlYdAExwAwnBDE4VMrTrwGzNl158lossKA=


// F# interface example
// https://sharplab.io/#v2:DYLgZgzgNAJiDUAfALgTwA4FMAEBJbAvALABQ252AhgEYTIBOlAxstgBojYSd30CWAOwDm2ALQA+bAFUBfZKQUk0WbAGEAFAEpCpCtkHJM9MMxz4A7nIAWuvRQC2me9SPZkVvhAB0bddy4MgkLaBNhaukA==


// F# calling interface methods docs
// https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/interfaces#calling-interface-methods
82 changes: 82 additions & 0 deletions src/ExampleTwo.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
module ExampleTwo


open System.IO
open System.Text.Json
open Amazon.Lambda.Core
open Amazon.Lambda.SQSEvents


type TypicalDto = { Name: string; Size: int }

[<LambdaSerializer(typeof<ExampleOne.MySerializer>)>]
let ``A typical SQS event handler`` (event: SQSEvent) =

let msgs: TypicalDto list =
event.Records
|> List.ofSeq
|> List.map (fun r -> JsonSerializer.Deserialize<TypicalDto> r.Body)

printfn "the messages are:\n\n%A" msgs

()




open type Amazon.Lambda.SQSEvents.SQSEvent



type SQSEventSerializer() =

member this.Serialize(res, stream) =
System.Runtime.CompilerServices.Unsafe
.As<ILambdaSerializer>(this)
.Serialize(res, stream)

(this :> ILambdaSerializer).Serialize(res, stream)

member this.Deserialize(stream) =
(this :> ILambdaSerializer).Deserialize(stream)

interface ILambdaSerializer with

member this.Serialize(res, stream: Stream) : unit =
use writer = new Utf8JsonWriter(stream)
JsonSerializer.Serialize(writer, res)

member this.Deserialize<'T>(stream: Stream) : 'T =
let sqsEvent: SQSEvent = JsonSerializer.Deserialize<SQSEvent> stream

use stream = new MemoryStream()
use writer = new Utf8JsonWriter(stream)
let parse (m: SQSMessage) = JsonDocument.Parse(m.Body).RootElement
let write (e: JsonElement) = e.WriteTo writer

writer.WriteStartArray()
sqsEvent.Records |> Seq.iter (parse >> write)
writer.WriteEndArray()

writer.Flush()
stream.Position <- 0
JsonSerializer.Deserialize<'T> stream





type MySqsMsg = { Name: string; Size: int }


[<LambdaSerializer(typeof<SQSEventSerializer>)>]
let ExampleTwo (msgs: MySqsMsg list) =
printfn "the messages are:\n\n%A" msgs
msgs




(*
yarn sls invoke -s nick -f ExampleTwo -p ./example-two-input.json
*)
14 changes: 14 additions & 0 deletions src/HiSerialize.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<WarningsAsErrors>FS0025</WarningsAsErrors>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<NoWarn>FS0988</NoWarn>
</PropertyGroup>
<ItemGroup>
<Compile Include="ExampleOne.fs" />
<Compile Include="ExampleTwo.fs" />
</ItemGroup>
<Import Project="..\.paket\Paket.Restore.targets" />
</Project>
3 changes: 3 additions & 0 deletions src/paket.references
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Amazon.Lambda.Core
FSharp.Core
Amazon.Lambda.SQSEvents
Loading

0 comments on commit 44ac586

Please sign in to comment.