Skip to content

Rename Lambda.Context to LambdaContext #233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Examples/Benchmark/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct MyLambda: EventLoopLambdaHandler {
typealias Event = String
typealias Output = String

func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture<String> {
func handle(_ event: String, context: LambdaContext) -> EventLoopFuture<String> {
context.eventLoop.makeSucceededFuture("hello, world!")
}
}
Expand Down
2 changes: 1 addition & 1 deletion Examples/Deployment/Sources/Benchmark/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct BenchmarkHandler: EventLoopLambdaHandler {
typealias Event = String
typealias Output = String

func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture<String> {
func handle(_ event: String, context: LambdaContext) -> EventLoopFuture<String> {
context.eventLoop.makeSucceededFuture("hello, world!")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct HelloWorldHandler: LambdaHandler {
// setup your resources that you want to reuse here.
}

func handle(_ event: String, context: Lambda.Context) async throws -> String {
func handle(_ event: String, context: LambdaContext) async throws -> String {
"hello, world"
}
}
2 changes: 1 addition & 1 deletion Examples/Echo/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct MyLambda: LambdaHandler {
// setup your resources that you want to reuse for every invocation here.
}

func handle(_ input: String, context: Lambda.Context) async throws -> String {
func handle(_ input: String, context: LambdaContext) async throws -> String {
// as an example, respond with the input's reversed
String(input.reversed())
}
Expand Down
2 changes: 1 addition & 1 deletion Examples/ErrorHandling/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct MyLambda: LambdaHandler {

init(context: Lambda.InitializationContext) async throws {}

func handle(_ request: Request, context: Lambda.Context) async throws -> Response {
func handle(_ request: Request, context: LambdaContext) async throws -> Response {
// switch over the error type "requested" by the request, and trigger such error accordingly
switch request.error {
// no error here!
Expand Down
2 changes: 1 addition & 1 deletion Examples/Foundation/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct MyLambda: LambdaHandler {
self.calculator = ExchangeRatesCalculator()
}

func handle(_ event: Request, context: Lambda.Context) async throws -> [Exchange] {
func handle(_ event: Request, context: LambdaContext) async throws -> [Exchange] {
try await withCheckedThrowingContinuation { continuation in
self.calculator.run(logger: context.logger) { result in
switch result {
Expand Down
2 changes: 1 addition & 1 deletion Examples/JSON/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct MyLambda: LambdaHandler {
// setup your resources that you want to reuse for every invocation here.
}

func handle(_ event: Request, context: Lambda.Context) async throws -> Response {
func handle(_ event: Request, context: LambdaContext) async throws -> Response {
// as an example, respond with the input event's reversed body
Response(body: String(event.body.reversed()))
}
Expand Down
2 changes: 1 addition & 1 deletion Examples/LocalDebugging/MyLambda/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct MyLambda: LambdaHandler {
// setup your resources that you want to reuse for every invocation here.
}

func handle(_ request: Request, context: Lambda.Context) async throws -> Response {
func handle(_ request: Request, context: LambdaContext) async throws -> Response {
// TODO: something useful
Response(message: "Hello, \(request.name)!")
}
Expand Down
2 changes: 1 addition & 1 deletion Examples/Testing/Sources/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct MyLambda: LambdaHandler {
// setup your resources that you want to reuse for every invocation here.
}

func handle(_ event: String, context: Lambda.Context) async throws -> String {
func handle(_ event: String, context: LambdaContext) async throws -> String {
// as an example, respond with the event's reversed body
String(event.reversed())
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/AWSLambdaRuntime/Context+Foundation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import AWSLambdaRuntimeCore
import struct Foundation.Date

extension Lambda.Context {
extension LambdaContext {
var deadlineDate: Date {
let secondsSinceEpoch = Double(Int64(bitPattern: self.deadline.rawValue)) / -1_000_000_000
return Date(timeIntervalSince1970: secondsSinceEpoch)
Expand Down
2 changes: 1 addition & 1 deletion Sources/AWSLambdaRuntimeCore/Lambda+LocalServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import NIOPosix
// For example:
//
// try Lambda.withLocalServer {
// Lambda.run { (context: Lambda.Context, event: String, callback: @escaping (Result<String, Error>) -> Void) in
// Lambda.run { (context: LambdaContext, event: String, callback: @escaping (Result<String, Error>) -> Void) in
// callback(.success("Hello, \(event)!"))
// }
// }
Expand Down
256 changes: 127 additions & 129 deletions Sources/AWSLambdaRuntimeCore/LambdaContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,151 +58,149 @@ extension Lambda {

// MARK: - Context

extension Lambda {
/// Lambda runtime context.
/// The Lambda runtime generates and passes the `Context` to the Lambda handler as an argument.
public struct Context: CustomDebugStringConvertible {
final class _Storage {
var requestID: String
var traceID: String
var invokedFunctionARN: String
var deadline: DispatchWallTime
var cognitoIdentity: String?
var clientContext: String?
var logger: Logger
var eventLoop: EventLoop
var allocator: ByteBufferAllocator

init(
requestID: String,
traceID: String,
invokedFunctionARN: String,
deadline: DispatchWallTime,
cognitoIdentity: String?,
clientContext: String?,
logger: Logger,
eventLoop: EventLoop,
allocator: ByteBufferAllocator
) {
self.requestID = requestID
self.traceID = traceID
self.invokedFunctionARN = invokedFunctionARN
self.deadline = deadline
self.cognitoIdentity = cognitoIdentity
self.clientContext = clientContext
self.logger = logger
self.eventLoop = eventLoop
self.allocator = allocator
}
/// Lambda runtime context.
/// The Lambda runtime generates and passes the `Context` to the Lambda handler as an argument.
public struct LambdaContext: CustomDebugStringConvertible {
final class _Storage {
var requestID: String
var traceID: String
var invokedFunctionARN: String
var deadline: DispatchWallTime
var cognitoIdentity: String?
var clientContext: String?
var logger: Logger
var eventLoop: EventLoop
var allocator: ByteBufferAllocator

init(
requestID: String,
traceID: String,
invokedFunctionARN: String,
deadline: DispatchWallTime,
cognitoIdentity: String?,
clientContext: String?,
logger: Logger,
eventLoop: EventLoop,
allocator: ByteBufferAllocator
) {
self.requestID = requestID
self.traceID = traceID
self.invokedFunctionARN = invokedFunctionARN
self.deadline = deadline
self.cognitoIdentity = cognitoIdentity
self.clientContext = clientContext
self.logger = logger
self.eventLoop = eventLoop
self.allocator = allocator
}
}

private var storage: _Storage
private var storage: _Storage

/// The request ID, which identifies the request that triggered the function invocation.
public var requestID: String {
self.storage.requestID
}
/// The request ID, which identifies the request that triggered the function invocation.
public var requestID: String {
self.storage.requestID
}

/// The AWS X-Ray tracing header.
public var traceID: String {
self.storage.traceID
}
/// The AWS X-Ray tracing header.
public var traceID: String {
self.storage.traceID
}

/// The ARN of the Lambda function, version, or alias that's specified in the invocation.
public var invokedFunctionARN: String {
self.storage.invokedFunctionARN
}
/// The ARN of the Lambda function, version, or alias that's specified in the invocation.
public var invokedFunctionARN: String {
self.storage.invokedFunctionARN
}

/// The timestamp that the function times out
public var deadline: DispatchWallTime {
self.storage.deadline
}
/// The timestamp that the function times out
public var deadline: DispatchWallTime {
self.storage.deadline
}

/// For invocations from the AWS Mobile SDK, data about the Amazon Cognito identity provider.
public var cognitoIdentity: String? {
self.storage.cognitoIdentity
}
/// For invocations from the AWS Mobile SDK, data about the Amazon Cognito identity provider.
public var cognitoIdentity: String? {
self.storage.cognitoIdentity
}

/// For invocations from the AWS Mobile SDK, data about the client application and device.
public var clientContext: String? {
self.storage.clientContext
}
/// For invocations from the AWS Mobile SDK, data about the client application and device.
public var clientContext: String? {
self.storage.clientContext
}

/// `Logger` to log with
///
/// - note: The `LogLevel` can be configured using the `LOG_LEVEL` environment variable.
public var logger: Logger {
self.storage.logger
}
/// `Logger` to log with
///
/// - note: The `LogLevel` can be configured using the `LOG_LEVEL` environment variable.
public var logger: Logger {
self.storage.logger
}

/// The `EventLoop` the Lambda is executed on. Use this to schedule work with.
/// This is useful when implementing the `EventLoopLambdaHandler` protocol.
///
/// - note: The `EventLoop` is shared with the Lambda runtime engine and should be handled with extra care.
/// Most importantly the `EventLoop` must never be blocked.
public var eventLoop: EventLoop {
self.storage.eventLoop
}
/// The `EventLoop` the Lambda is executed on. Use this to schedule work with.
/// This is useful when implementing the `EventLoopLambdaHandler` protocol.
///
/// - note: The `EventLoop` is shared with the Lambda runtime engine and should be handled with extra care.
/// Most importantly the `EventLoop` must never be blocked.
public var eventLoop: EventLoop {
self.storage.eventLoop
}

/// `ByteBufferAllocator` to allocate `ByteBuffer`
/// This is useful when implementing `EventLoopLambdaHandler`
public var allocator: ByteBufferAllocator {
self.storage.allocator
}
/// `ByteBufferAllocator` to allocate `ByteBuffer`
/// This is useful when implementing `EventLoopLambdaHandler`
public var allocator: ByteBufferAllocator {
self.storage.allocator
}

init(requestID: String,
traceID: String,
invokedFunctionARN: String,
deadline: DispatchWallTime,
cognitoIdentity: String? = nil,
clientContext: String? = nil,
logger: Logger,
eventLoop: EventLoop,
allocator: ByteBufferAllocator) {
self.storage = _Storage(
requestID: requestID,
traceID: traceID,
invokedFunctionARN: invokedFunctionARN,
deadline: deadline,
cognitoIdentity: cognitoIdentity,
clientContext: clientContext,
logger: logger,
eventLoop: eventLoop,
allocator: allocator
)
}
init(requestID: String,
traceID: String,
invokedFunctionARN: String,
deadline: DispatchWallTime,
cognitoIdentity: String? = nil,
clientContext: String? = nil,
logger: Logger,
eventLoop: EventLoop,
allocator: ByteBufferAllocator) {
self.storage = _Storage(
requestID: requestID,
traceID: traceID,
invokedFunctionARN: invokedFunctionARN,
deadline: deadline,
cognitoIdentity: cognitoIdentity,
clientContext: clientContext,
logger: logger,
eventLoop: eventLoop,
allocator: allocator
)
}

public func getRemainingTime() -> TimeAmount {
let deadline = self.deadline.millisSinceEpoch
let now = DispatchWallTime.now().millisSinceEpoch
public func getRemainingTime() -> TimeAmount {
let deadline = self.deadline.millisSinceEpoch
let now = DispatchWallTime.now().millisSinceEpoch

let remaining = deadline - now
return .milliseconds(remaining)
}
let remaining = deadline - now
return .milliseconds(remaining)
}

public var debugDescription: String {
"\(Self.self)(requestID: \(self.requestID), traceID: \(self.traceID), invokedFunctionARN: \(self.invokedFunctionARN), cognitoIdentity: \(self.cognitoIdentity ?? "nil"), clientContext: \(self.clientContext ?? "nil"), deadline: \(self.deadline))"
}
public var debugDescription: String {
"\(Self.self)(requestID: \(self.requestID), traceID: \(self.traceID), invokedFunctionARN: \(self.invokedFunctionARN), cognitoIdentity: \(self.cognitoIdentity ?? "nil"), clientContext: \(self.clientContext ?? "nil"), deadline: \(self.deadline))"
}

/// This interface is not part of the public API and must not be used by adopters. This API is not part of semver versioning.
public static func __forTestsOnly(
requestID: String,
traceID: String,
invokedFunctionARN: String,
timeout: DispatchTimeInterval,
logger: Logger,
eventLoop: EventLoop
) -> Context {
Context(
requestID: requestID,
traceID: traceID,
invokedFunctionARN: invokedFunctionARN,
deadline: .now() + timeout,
logger: logger,
eventLoop: eventLoop,
allocator: ByteBufferAllocator()
)
}
/// This interface is not part of the public API and must not be used by adopters. This API is not part of semver versioning.
public static func __forTestsOnly(
requestID: String,
traceID: String,
invokedFunctionARN: String,
timeout: DispatchTimeInterval,
logger: Logger,
eventLoop: EventLoop
) -> LambdaContext {
LambdaContext(
requestID: requestID,
traceID: traceID,
invokedFunctionARN: invokedFunctionARN,
deadline: .now() + timeout,
logger: logger,
eventLoop: eventLoop,
allocator: ByteBufferAllocator()
)
}
}

Expand Down
Loading