Skip to content
This repository was archived by the owner on Jul 14, 2020. It is now read-only.

Commit 5ecf46b

Browse files
committed
work in progress.
1 parent c133d65 commit 5ecf46b

File tree

15 files changed

+655
-0
lines changed

15 files changed

+655
-0
lines changed
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"object": {
3+
"pins": [
4+
{
5+
"package": "async-http-client",
6+
"repositoryURL": "https://github.com/swift-server/async-http-client.git",
7+
"state": {
8+
"branch": null,
9+
"revision": "51dc885a30ca704b02fa803099b0a9b5b38067b6",
10+
"version": "1.0.0"
11+
}
12+
},
13+
{
14+
"package": "swift-log",
15+
"repositoryURL": "https://github.com/apple/swift-log.git",
16+
"state": {
17+
"branch": null,
18+
"revision": "74d7b91ceebc85daf387ebb206003f78813f71aa",
19+
"version": "1.2.0"
20+
}
21+
},
22+
{
23+
"package": "swift-nio",
24+
"repositoryURL": "https://github.com/apple/swift-nio.git",
25+
"state": {
26+
"branch": null,
27+
"revision": "ff01888051cd7efceb1bf8319c1dd3986c4bf6fc",
28+
"version": "2.10.1"
29+
}
30+
},
31+
{
32+
"package": "swift-nio-extras",
33+
"repositoryURL": "https://github.com/apple/swift-nio-extras.git",
34+
"state": {
35+
"branch": null,
36+
"revision": "53808818c2015c45247cad74dc05c7a032c96a2f",
37+
"version": "1.3.2"
38+
}
39+
},
40+
{
41+
"package": "swift-nio-ssl",
42+
"repositoryURL": "https://github.com/apple/swift-nio-ssl.git",
43+
"state": {
44+
"branch": null,
45+
"revision": "ccf96bbe65ecc7c1558ab0dba7ffabdea5c1d31f",
46+
"version": "2.4.4"
47+
}
48+
}
49+
]
50+
},
51+
"version": 1
52+
}

Examples/EventSources/Package.swift

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// swift-tools-version:5.1
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "EventSources",
8+
platforms: [
9+
.macOS(.v10_12)
10+
],
11+
dependencies: [
12+
// Dependencies declare other packages that this package depends on.
13+
// .package(url: /* package url */, from: "1.0.0"),
14+
.package(path: "../.."),
15+
.package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.9.0")),
16+
.package(url: "https://github.com/apple/swift-log.git", .upToNextMajor(from: "1.1.1")),
17+
],
18+
targets: [
19+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
20+
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
21+
.target(
22+
name: "EventSources",
23+
dependencies: ["AWSLambda", "Logging", "NIO", "NIOFoundationCompat", "NIOHTTP1"]),
24+
.testTarget(
25+
name: "EventSourcesTests",
26+
dependencies: ["EventSources"]),
27+
]
28+
)

Examples/EventSources/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# EventSources
2+
3+
A description of this package.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Foundation
2+
import AWSLambda
3+
import NIOFoundationCompat
4+
import NIO
5+
6+
class SNSController {
7+
8+
func handleSNS(data: NIO.ByteBuffer, ctx: Context) -> EventLoopFuture<NIO.ByteBuffer?> {
9+
let string = data.getString(at: 0, length: data.readableBytes)
10+
ctx.logger.info("Payload: \(String(describing: string))")
11+
return ctx.eventLoop.makeSucceededFuture(nil)
12+
}
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import AWSLambda
2+
import NIO
3+
import Logging
4+
import Foundation
5+
6+
LoggingSystem.bootstrap(StreamLogHandler.standardError)
7+
8+
9+
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
10+
defer { try! group.syncShutdownGracefully() }
11+
let logger = Logger(label: "AWSLambda.EventSources")
12+
13+
do {
14+
logger.info("start runtime")
15+
let runtime = try Runtime.createRuntime(eventLoopGroup: group)
16+
17+
defer { try! runtime.syncShutdown() }
18+
19+
logger.info("register functions")
20+
21+
let snsController = SNSController()
22+
23+
runtime.register(for: "handleSNS", handler: snsController.handleSNS)
24+
25+
logger.info("starting runloop")
26+
27+
try runtime.start().wait()
28+
}
29+
catch {
30+
logger.error("error: \(String(describing: error))")
31+
}
32+
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import XCTest
2+
import class Foundation.Bundle
3+
4+
final class EventSourcesTests: XCTestCase {
5+
func testExample() throws {
6+
// This is an example of a functional test case.
7+
// Use XCTAssert and related functions to verify your tests produce the correct
8+
// results.
9+
10+
// Some of the APIs that we use below are available in macOS 10.13 and above.
11+
guard #available(macOS 10.13, *) else {
12+
return
13+
}
14+
15+
let fooBinary = productsDirectory.appendingPathComponent("EventSources")
16+
17+
let process = Process()
18+
process.executableURL = fooBinary
19+
20+
let pipe = Pipe()
21+
process.standardOutput = pipe
22+
23+
try process.run()
24+
process.waitUntilExit()
25+
26+
let data = pipe.fileHandleForReading.readDataToEndOfFile()
27+
let output = String(data: data, encoding: .utf8)
28+
29+
XCTAssertEqual(output, "Hello, world!\n")
30+
}
31+
32+
/// Returns path to the built products directory.
33+
var productsDirectory: URL {
34+
#if os(macOS)
35+
for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") {
36+
return bundle.bundleURL.deletingLastPathComponent()
37+
}
38+
fatalError("couldn't find the products directory")
39+
#else
40+
return Bundle.main.bundleURL
41+
#endif
42+
}
43+
44+
static var allTests = [
45+
("testExample", testExample),
46+
]
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import XCTest
2+
3+
#if !canImport(ObjectiveC)
4+
public func allTests() -> [XCTestCaseEntry] {
5+
return [
6+
testCase(EventSourcesTests.allTests),
7+
]
8+
}
9+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import XCTest
2+
3+
import EventSourcesTests
4+
5+
var tests = [XCTestCaseEntry]()
6+
tests += EventSourcesTests.allTests()
7+
XCTMain(tests)

Examples/EventSources/template.yaml

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: >
4+
sam-app
5+
6+
Sample SAM Template for sam-app
7+
8+
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
9+
Globals:
10+
Function:
11+
Timeout: 3
12+
13+
Resources:
14+
15+
SwiftLayer:
16+
Type: AWS::Serverless::LayerVersion
17+
Properties:
18+
ContentUri: ../../Layer/swift-lambda-runtime/
19+
# ContentUri:
20+
# Bucket: de.fabianfett.swift-lambda-runtimes
21+
# Key: swift-lambda-runtime.zip
22+
23+
# DynamoDbTestTable:
24+
# Type: "AWS::DynamoDB::Table"
25+
# Properties:
26+
# BillingMode: PAY_PER_REQUEST
27+
# AttributeDefinitions:
28+
# - AttributeName: ListId
29+
# AttributeType: S
30+
# - AttributeName: TodoId
31+
# AttributeType: S
32+
# KeySchema:
33+
# - AttributeName: ListId
34+
# KeyType: HASH
35+
# - AttributeName: TodoId
36+
# KeyType: RANGE
37+
# TableName: "SwiftTestTable"
38+
39+
SNSTopic:
40+
Type: AWS::SNS::Topic
41+
42+
ConsumeSNSTopicLambda:
43+
Type: AWS::Serverless::Function
44+
Properties:
45+
CodeUri: lambda.zip
46+
Handler: "EventSources.handleSNS"
47+
Runtime: provided
48+
Layers:
49+
- !Ref SwiftLayer
50+
Policies:
51+
- SNSCrudPolicy:
52+
TopicName: !GetAtt SNSTopic.TopicName
53+
Events:
54+
sns:
55+
Type: SNS
56+
Properties:
57+
Topic: !Ref SNSTopic
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Foundation
2+
import NIO
3+
4+
public extension DynamoDB {
5+
6+
enum AttributeValue {
7+
case boolean(Bool)
8+
case binary(NIO.ByteBuffer)
9+
case binarySet([NIO.ByteBuffer])
10+
case date(Date)
11+
case string(String)
12+
case stringSet(Set<String>)
13+
case null
14+
case numeric
15+
case numericSet(Set<String>)
16+
17+
case list([AttributeValue])
18+
case map([String: AttributeValue])
19+
}
20+
21+
}
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import Foundation
2+
import NIO
3+
4+
/// https://github.com/aws/aws-lambda-go/blob/master/events/dynamodb.go
5+
public struct DynamoDB {
6+
7+
public enum KeyType: String {
8+
case hash = "HASH"
9+
case range = "RANGE"
10+
}
11+
12+
public enum OperationType: String {
13+
case insert = "INSERT"
14+
case modify = "MODIFY"
15+
case remove = "REMOVE"
16+
}
17+
18+
public enum SharedIteratorType: String {
19+
case trimHorizon = "TRIM_HORIZON"
20+
case latest = "LATEST"
21+
case atSequenceNumber = "AT_SEQUENCE_NUMBER"
22+
case afterSequenceNumber = "AFTER_SEQUENCE_NUMBER"
23+
}
24+
25+
public enum StreamStatus: String {
26+
case enabling = "ENABLING"
27+
case enabled = "ENABLED"
28+
case disabling = "DISABLING"
29+
case disabled = "DISABLED"
30+
}
31+
32+
public enum StreamViewType: String, Codable {
33+
/// the entire item, as it appeared after it was modified.
34+
case newImage = "NEW_IMAGE"
35+
/// the entire item, as it appeared before it was modified.
36+
case oldImage = "OLD_IMAGE"
37+
/// both the new and the old item images of the item.
38+
case newAndOldImages = "NEW_AND_OLD_IMAGES"
39+
/// only the key attributes of the modified item.
40+
case keysOnly = "KEYS_ONLY"
41+
}
42+
43+
public struct Event {
44+
let awsRegion: String
45+
// let
46+
}
47+
48+
public struct StreamRecord {
49+
/// The approximate date and time when the stream record was created, in UNIX
50+
/// epoch time (http://www.epochconverter.com/) format.
51+
let approximateCreationDateTime: Date
52+
53+
/// The primary key attribute(s) for the DynamoDB item that was modified.
54+
let keys: [String: AttributeValue]
55+
56+
/// The item in the DynamoDB table as it appeared after it was modified.
57+
let newImage: [String: AttributeValue]
58+
59+
/// The item in the DynamoDB table as it appeared before it was modified.
60+
let oldImage: [String: AttributeValue]
61+
62+
/// The sequence number of the stream record.
63+
let sequenceNumber: String
64+
65+
/// The size of the stream record, in bytes.
66+
let sizeBytes: Int64
67+
68+
/// The type of data from the modified DynamoDB item that was captured in this
69+
/// stream record.
70+
let streamViewType: StreamViewType
71+
}
72+
73+
}

0 commit comments

Comments
 (0)