Skip to content
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

Add new handler protocols + Codable support #351

Merged
merged 8 commits into from
Sep 4, 2024

Conversation

aryan-25
Copy link
Contributor

@aryan-25 aryan-25 commented Sep 2, 2024

Motivation:

As part of the implementation of the v2 API (#339), we want to add the handler protocols LambdaHandler and LambdaWithBackgroundProcessingHandler.

Modifications:

  • Added the two remaining handler protocols: LambdaHandler and LambdaWithBackgroundProcessingHandler.

    • Note: LambdaHandler is currently temporarily named as NewLambdaHandler as there is already an existing LambdaHandler. This will be sorted out closer to release.
  • Added LambdaHandlerAdapter and LambdaCodableAdapter to allow the two new handler protocols to work with the LambdaRuntime (as LambdaRuntime requires a StreamingLambdaHandler conforming handler).

    • LambdaHandlerAdapter:
      • Adapts a LambdaHandler conforming handler to conform to LambdaWithBackgroundProcessingHandler by passing the output returned from LambdaHandler to LambdaWithBackgroundProcessingHandler's output writer.
    • LambdaCodableAdapter:
      • Adapts a LambdaWithBackgroundProcessingHandler conforming handler to conform to StreamingLambdaHandler.
      • First decodes the received ByteBuffer event into LambdaWithBackgroundProcessingHandlers generic Event object, and passes this to its handle(...) function.
      • Encodes the output written to LambdaResponseWriter into a ByteBuffer and send this to StreamingLambdaHandler's LambdaResponseStreamWriter.
  • Added StreamingClosureHandler and ClosureHandler structs conforming to StreamingLambdaHandler and LambdaHandler protocols respectively.

    • This allows a Lambda handler to be created by simply writing the handler in the form of a closure, as compared to first creating an object conforming to the handler protocol and then writing the handle(...) function.

Result:

  • All three handler protocols are introduced.
  • Handlers conforming to the LambdaHandler and LambdaWithBackgroundProcessingHandler can be used with the new LambdaRuntime through the adapters.
  • Handlers can be defined in a clean manner through the closure handlers.

…s. Add closure handlers for StreamingLambdaHandler + NewLambdaHandler. Add Codable adapters.
Copy link
Member

@fabianfett fabianfett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great progress! We need docs, docs and docs. Take them from the proposal!

Sources/AWSLambdaRuntimeCore/NewLambda+JSON.swift Outdated Show resolved Hide resolved
Sources/AWSLambdaRuntimeCore/NewLambda+JSON.swift Outdated Show resolved Hide resolved
Sources/AWSLambdaRuntimeCore/NewLambda+JSON.swift Outdated Show resolved Hide resolved
Sources/AWSLambdaRuntimeCore/NewLambdaHandlers.swift Outdated Show resolved Hide resolved
Sources/AWSLambdaRuntimeCore/NewLambdaHandlers.swift Outdated Show resolved Hide resolved
Sources/AWSLambdaRuntimeCore/NewLambdaHandlers.swift Outdated Show resolved Hide resolved
…mWriter`, move Foundation import from AWSLambdaRuntimeCore
@fabianfett
Copy link
Member

API breakage check also reports on package access modifiers:

2 breaking changes detected in AWSLambdaRuntimeCore:
  💔 API breakage: protocol LambdaRuntimeClientProtocol has generic signature change from <Self.Writer : AWSLambdaRuntimeCore.LambdaResponseStreamWriter> to <Self.Writer : AWSLambdaRuntimeCore.LambdaRuntimeClientResponseStreamWriter>
  💔 API breakage: func LambdaResponseStreamWriter.reportError(_:) has been removed

Sources/AWSLambdaRuntimeCore/NewLambda+JSON.swift Outdated Show resolved Hide resolved
Sources/AWSLambdaRuntimeCore/NewLambda+JSON.swift Outdated Show resolved Hide resolved
Sources/AWSLambdaRuntimeCore/NewLambda+JSON.swift Outdated Show resolved Hide resolved
Sources/AWSLambdaRuntimeCore/NewLambda+JSON.swift Outdated Show resolved Hide resolved
Sources/AWSLambdaRuntimeCore/NewLambdaHandlers.swift Outdated Show resolved Hide resolved
Copy link
Member

@fabianfett fabianfett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thank you!

@fabianfett fabianfett merged commit 4b45451 into swift-server:main Sep 4, 2024
10 of 13 checks passed
@fabianfett fabianfett added this to the 2.0 milestone Sep 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants