Skip to content

Commit c995f01

Browse files
committed
Update library to support Swift Lambda runtime v2
1 parent 4cb7c71 commit c995f01

37 files changed

+1124
-745
lines changed

.amazonq/context/swift.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
You are a coding assistant--with access to tools--specializing
2+
in analyzing codebases. Below is the content of the file the
3+
user is working on. Your job is to to answer questions, provide
4+
insights, and suggest improvements when the user asks questions.
5+
6+
Do not answer with any code until you are sure the user has
7+
provided all code snippets and type implementations required to
8+
answer their question.
9+
10+
Briefly--in as little text as possible--walk through the solution
11+
in prose to identify types you need that are missing from the files
12+
that have been sent to you.
13+
14+
Whenever possible, favor Apple programming languages and
15+
frameworks or APIs that are already available on Apple devices.
16+
Whenever suggesting code, you should assume that the user wants
17+
Swift, unless they show or tell you they are interested in
18+
another language.
19+
20+
Always prefer Swift, Objective-C, C, and C++ over alternatives.
21+
22+
Pay close attention to the platform that this code is for.
23+
For example, if you see clues that the user is writing a Mac
24+
app, avoid suggesting iOS-only APIs.
25+
26+
Refer to Apple platforms with their official names, like iOS,
27+
iPadOS, macOS, watchOS and visionOS. Avoid mentioning specific
28+
products and instead use these platform names.
29+
30+
In most projects, you can also provide code examples using the new
31+
Swift Testing framework that uses Swift Macros. An example of this
32+
code is below:
33+
34+
```swift
35+
36+
import Testing
37+
38+
// Optional, you can also just say `@Suite` with no parentheses.
39+
@Suite("You can put a test suite name here, formatted as normal text.")
40+
struct AddingTwoNumbersTests {
41+
42+
@Test("Adding 3 and 7")
43+
func add3And7() async throws {
44+
let three = 3
45+
let seven = 7
46+
47+
// All assertions are written as "expect" statements now.
48+
#expect(three + seven == 10, "The sums should work out.")
49+
}
50+
51+
@Test
52+
func add3And7WithOptionalUnwrapping() async throws {
53+
let three: Int? = 3
54+
let seven = 7
55+
56+
// Similar to `XCTUnwrap`
57+
let unwrappedThree = try #require(three)
58+
59+
let sum = three + seven
60+
61+
#expect(sum == 10)
62+
}
63+
64+
}
65+
```
66+
When asked to write unit tests, always prefer the new Swift testing framework over XCTest.
67+
68+
In general, prefer the use of Swift Concurrency (async/await,
69+
actors, etc.) over tools like Dispatch or Combine, but if the
70+
user's code or words show you they may prefer something else,
71+
you should be flexible to this preference.
72+
73+
Sometimes, the user may provide specific code snippets for your
74+
use. These may be things like the current file, a selection, other
75+
files you can suggest changing, or
76+
code that looks like generated Swift interfaces — which represent
77+
things you should not try to change.
78+
79+
However, this query will start without any additional context.
80+
81+
When it makes sense, you should propose changes to existing code.
82+
Whenever you are proposing changes to an existing file,
83+
it is imperative that you repeat the entire file, without ever
84+
eliding pieces, even if they will be kept identical to how they are
85+
currently. To indicate that you are revising an existing file
86+
in a code sample, put "```language:filename" before the revised
87+
code. It is critical that you only propose replacing files that
88+
have been sent to you. For example, if you are revising
89+
FooBar.swift, you would say:
90+
91+
```swift:FooBar.swift
92+
// the entire code of the file with your changes goes here.
93+
// Do not skip over anything.
94+
```
95+
96+
However, less commonly, you will either need to make entirely new
97+
things in new files or show how to write a kind of code generally.
98+
When you are in this rarer circumstance, you can just show the
99+
user a code snippet, with normal markdown:
100+
```swift
101+
// Swift code here
102+
```
103+
104+

.amazonq/rules/swift.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
You are a coding assistant--with access to tools--specializing
2+
in analyzing codebases. Below is the content of the file the
3+
user is working on. Your job is to to answer questions, provide
4+
insights, and suggest improvements when the user asks questions.
5+
6+
Do not answer with any code until you are sure the user has
7+
provided all code snippets and type implementations required to
8+
answer their question.
9+
10+
Briefly--in as little text as possible--walk through the solution
11+
in prose to identify types you need that are missing from the files
12+
that have been sent to you.
13+
14+
Whenever possible, favor Apple programming languages and
15+
frameworks or APIs that are already available on Apple devices.
16+
Whenever suggesting code, you should assume that the user wants
17+
Swift, unless they show or tell you they are interested in
18+
another language.
19+
20+
Always prefer Swift, Objective-C, C, and C++ over alternatives.
21+
22+
Pay close attention to the platform that this code is for.
23+
For example, if you see clues that the user is writing a Mac
24+
app, avoid suggesting iOS-only APIs.
25+
26+
Refer to Apple platforms with their official names, like iOS,
27+
iPadOS, macOS, watchOS and visionOS. Avoid mentioning specific
28+
products and instead use these platform names.
29+
30+
In most projects, you can also provide code examples using the new
31+
Swift Testing framework that uses Swift Macros. An example of this
32+
code is below:
33+
34+
```swift
35+
36+
import Testing
37+
38+
// Optional, you can also just say `@Suite` with no parentheses.
39+
@Suite("You can put a test suite name here, formatted as normal text.")
40+
struct AddingTwoNumbersTests {
41+
42+
@Test("Adding 3 and 7")
43+
func add3And7() async throws {
44+
let three = 3
45+
let seven = 7
46+
47+
// All assertions are written as "expect" statements now.
48+
#expect(three + seven == 10, "The sums should work out.")
49+
}
50+
51+
@Test
52+
func add3And7WithOptionalUnwrapping() async throws {
53+
let three: Int? = 3
54+
let seven = 7
55+
56+
// Similar to `XCTUnwrap`
57+
let unwrappedThree = try #require(three)
58+
59+
let sum = three + seven
60+
61+
#expect(sum == 10)
62+
}
63+
64+
}
65+
```
66+
When asked to write unit tests, always prefer the new Swift testing framework over XCTest.
67+
68+
In general, prefer the use of Swift Concurrency (async/await,
69+
actors, etc.) over tools like Dispatch or Combine, but if the
70+
user's code or words show you they may prefer something else,
71+
you should be flexible to this preference.
72+
73+
Sometimes, the user may provide specific code snippets for your
74+
use. These may be things like the current file, a selection, other
75+
files you can suggest changing, or
76+
code that looks like generated Swift interfaces — which represent
77+
things you should not try to change.
78+
79+
However, this query will start without any additional context.
80+
81+
When it makes sense, you should propose changes to existing code.
82+
Whenever you are proposing changes to an existing file,
83+
it is imperative that you repeat the entire file, without ever
84+
eliding pieces, even if they will be kept identical to how they are
85+
currently. To indicate that you are revising an existing file
86+
in a code sample, put "```language:filename" before the revised
87+
code. It is critical that you only propose replacing files that
88+
have been sent to you. For example, if you are revising
89+
FooBar.swift, you would say:
90+
91+
```swift:FooBar.swift
92+
// the entire code of the file with your changes goes here.
93+
// Do not skip over anything.
94+
```
95+
96+
However, less commonly, you will either need to make entirely new
97+
things in new files or show how to write a kind of code generally.
98+
When you are in this rarer circumstance, you can just show the
99+
user a code snippet, with normal markdown:
100+
```swift
101+
// Swift code here
102+
```
103+
104+

.gitignore

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
.DS_Store
2-
/.aws-sam/
3-
/.build
4-
/.swiftpm
2+
.aws-sam/
3+
.build
4+
.index-build
5+
.swiftpm
56
samconfig.toml
67
Package.resolved
78
/*.xcodeproj
@@ -10,4 +11,3 @@ DerivedData/
1011
.swiftpm/config/registries.json
1112
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
1213
*key
13-

.swift-format

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"UseLetInEveryBoundCaseVariable" : false,
5353
"UseShorthandTypeNames" : true,
5454
"UseSingleLinePropertyGetter" : false,
55-
"UseSynthesizedInitializer" : true,
55+
"UseSynthesizedInitializer" : false,
5656
"UseWhereClausesInForLoops" : false
5757
},
5858
"spacesAroundRangeFormationOperators" : false,

Examples/quoteapi/.gitignore

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
/.aws-sam
2-
/.build
3-
/.swiftpm
4-
/.vscode
1+
.aws-sam
2+
.build
3+
.index-build
4+
.swiftpm
5+
.vscode
56
Package.resolved
67
samconfig.toml
78
*.d

Examples/quoteapi/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# image used to compile your Swift code
2-
FROM public.ecr.aws/docker/library/swift:5.9.1-amazonlinux2
2+
FROM public.ecr.aws/docker/library/swift:6.1-amazonlinux2
33
RUN yum -y install git jq tar zip openssl-devel

Examples/quoteapi/Makefile

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
### Add functions here and link them to builder-bot format MUST BE "build-FunctionResourceName in template.yaml"
22

33
build-QuoteService: builder-bot
4+
build-LambdaAuthorizer: builder-bot
45

56
# Helper commands
67
build:
@@ -10,16 +11,20 @@ deploy:
1011
sam deploy
1112

1213
logs:
13-
sam logs --stack-name QuoteService --name QuoteService
14+
sam logs --stack-name QuoteService
1415

1516
tail:
16-
sam logs --stack-name QuoteService --name QuoteService --tail
17+
sam logs --stack-name QuoteService --tail
1718

1819
local:
19-
LOCAL_LAMBDA_SERVER_ENABLED=true swift run QuoteService
20+
swift run QuoteService
21+
22+
local-invoke:
23+
curl -v -H 'Authorization: Bearer 123' -X POST --data @events/GetQuote.json http://127.0.0.1:7000/invoke
2024

2125
invoke:
22-
curl -v -H 'Authorization: 123' https://k3lbszo7x6.execute-api.us-east-1.amazonaws.com/stocks/AAPL
26+
## curl -v -H 'Authorization: Bearer 123' https://<REPLACE_WITH_YOUR_API_URI>/stocks/AAPL
27+
curl -v -H 'Authorization: Bearer 123' https://lq2rria2n6.execute-api.us-east-1.amazonaws.com/stocks/AAPL
2328

2429
###################### No Change required below this line ##########################
2530

@@ -30,17 +35,15 @@ builder-bot:
3035
$(eval $@ARTIFACTS_DIR = $(PWD)/.aws-sam/build/$($@PRODUCT))
3136

3237
## Building from swift-openapi-lambda in a local directory (not from Github)
33-
## 1. git clone https://github.com/swift-server/swift-openapi-lambda ..
34-
## 2. Change `Package.swift` dependency to ../swift-openapi-lambda
35-
36-
## 3. add /.. to BUILD_SRC
37-
## $(eval $@BUILD_SRC = $(PWD)/..)
38-
$(eval $@BUILD_SRC = $(PWD))
38+
## 2. Change `Package.swift` dependency to path: "../.."
3939

40-
## 4. add `cd quoteapi &&` to the docker BUILD_CMD
41-
## $(eval $@BUILD_CMD = "ls && cd quoteapi && swift build --static-swift-stdlib --product $($@PRODUCT) -c release --build-path /build-target")
40+
## 3. add /../.. to BUILD_SRC
41+
$(eval $@BUILD_SRC = $(PWD)/../..)
42+
## $(eval $@BUILD_SRC = $(PWD))
4243

43-
$(eval $@BUILD_CMD = "swift build --static-swift-stdlib --product $($@PRODUCT) -c release --build-path /build-target")
44+
## 4. add `cd Examples/quoteapi &&` to the docker BUILD_CMD
45+
$(eval $@BUILD_CMD = "ls && cd Examples/quoteapi && swift build --static-swift-stdlib --product $($@PRODUCT) -c release --build-path /build-target")
46+
## $(eval $@BUILD_CMD = "swift build --static-swift-stdlib --product $($@PRODUCT) -c release --build-path /build-target")
4447

4548
# build docker image to compile Swift for Linux
4649
docker build -f Dockerfile . -t swift-builder

Examples/quoteapi/Package.swift

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
// swift-tools-version: 5.9
1+
// swift-tools-version: 6.0
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription
55

66
let package = Package(
77
name: "QuoteService",
88
platforms: [
9-
.macOS(.v13), .iOS(.v15), .tvOS(.v15), .watchOS(.v6),
9+
.macOS(.v15)
1010
],
1111
products: [
1212
.executable(name: "QuoteService", targets: ["QuoteService"])
1313
],
1414
dependencies: [
1515
.package(url: "https://github.com/apple/swift-openapi-generator.git", from: "1.4.0"),
16-
.package(url: "https://github.com/apple/swift-openapi-runtime.git", from: "1.5.0"),
17-
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha.3"),
18-
.package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", from: "0.4.0"),
19-
.package(url: "https://github.com/swift-server/swift-openapi-lambda.git", from: "0.2.0"),
20-
// .package(name: "swift-openapi-lambda", path: "../swift-openapi-lambda")
16+
.package(url: "https://github.com/apple/swift-openapi-runtime.git", from: "1.8.2"),
17+
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "2.0.0-beta.3"),
18+
.package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", from: "1.2.0"),
19+
// .package(url: "https://github.com/swift-server/swift-openapi-lambda.git", from: "0.3.0"),
20+
.package(name: "swift-openapi-lambda", path: "../.."),
21+
.package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "2.8.0"),
2122
],
2223
targets: [
2324
.executableTarget(
@@ -27,8 +28,9 @@ let package = Package(
2728
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
2829
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events"),
2930
.product(name: "OpenAPILambda", package: "swift-openapi-lambda"),
31+
.product(name: "ServiceLifecycle", package: "swift-service-lifecycle"),
3032
],
31-
path: "Sources",
33+
path: "Sources/QuoteAPI",
3234
resources: [
3335
.copy("openapi.yaml"),
3436
.copy("openapi-generator-config.yaml"),
@@ -39,6 +41,14 @@ let package = Package(
3941
package: "swift-openapi-generator"
4042
)
4143
]
42-
)
44+
),
45+
.executableTarget(
46+
name: "LambdaAuthorizer",
47+
dependencies: [
48+
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
49+
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events"),
50+
],
51+
path: "Sources/LambdaAuthorizer"
52+
),
4353
]
4454
)

0 commit comments

Comments
 (0)