- 
                Notifications
    
You must be signed in to change notification settings  - Fork 118
 
Add new handler protocols + Codable support #351
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
      
      
    
  
     Merged
                    Changes from 1 commit
      Commits
    
    
            Show all changes
          
          
            8 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      804bed7
              
                Add NewLambdaHandler + LambdaWithBackgroundProcessingHandler protocol…
              
              
                aryan-25 b4a6a4f
              
                Add documentation, add new internal `LambdaRuntimeClientResponseStrea…
              
              
                aryan-25 6bf879c
              
                Refactor
              
              
                aryan-25 3bcc17a
              
                Refactor
              
              
                aryan-25 5eebc52
              
                Remove mutating keyword from write(_:)
              
              
                aryan-25 bf7497a
              
                Fix formatting
              
              
                aryan-25 6f384ae
              
                Add LambdaJSONOutputEncoder
              
              
                aryan-25 f386e76
              
                Apply formatter
              
              
                aryan-25 File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the SwiftAWSLambdaRuntime open source project | ||
| // | ||
| // Copyright (c) 2024 Apple Inc. and the SwiftAWSLambdaRuntime project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| 
     | 
||
| import NIOCore | ||
| import NIOFoundationCompat | ||
| 
     | 
||
| import class Foundation.JSONDecoder | ||
| import class Foundation.JSONEncoder | ||
| 
     | 
||
| package protocol LambdaEventDecoder { | ||
| func decode<Event: Decodable>(_ type: Event.Type, from buffer: ByteBuffer) throws -> Event | ||
| } | ||
| 
     | 
||
| package protocol LambdaOutputEncoder { | ||
| func encode<Output: Encodable>(_ value: Output, into buffer: inout ByteBuffer) throws | ||
| } | ||
                
      
                  fabianfett marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| extension JSONEncoder: LambdaOutputEncoder {} | ||
| 
     | 
||
| extension JSONDecoder: LambdaEventDecoder {} | ||
                
      
                  aryan-25 marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| package struct VoidEncoder: LambdaOutputEncoder { | ||
| package func encode<Output>(_ value: Output, into buffer: inout NIOCore.ByteBuffer) throws where Output: Encodable { | ||
| fatalError("LambdaOutputEncoder must never be called on a void output") | ||
                
      
                  aryan-25 marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| } | ||
| } | ||
| 
     | 
||
| package struct LambdaHandlerAdapter< | ||
| Event: Decodable, | ||
| Output, | ||
| Handler: NewLambdaHandler | ||
| >: LambdaWithBackgroundProcessingHandler where Handler.Event == Event, Handler.Output == Output { | ||
| let handler: Handler | ||
| 
     | 
||
| package init(handler: Handler) { | ||
| self.handler = handler | ||
| } | ||
| 
     | 
||
| package func handle( | ||
| _ event: Event, | ||
| outputWriter: consuming some LambdaResponseWriter<Output>, | ||
| context: NewLambdaContext | ||
| ) async throws { | ||
| let response = try await self.handler.handle(event, context: context) | ||
| try await outputWriter.write(response: response) | ||
| } | ||
| } | ||
| 
     | 
||
| package struct LambdaCodableAdapter< | ||
| Handler: LambdaWithBackgroundProcessingHandler, | ||
| Event: Decodable, | ||
| Output, | ||
| Decoder: LambdaEventDecoder, | ||
| Encoder: LambdaOutputEncoder | ||
| >: StreamingLambdaHandler where Handler.Event == Event, Handler.Output == Output { | ||
| let handler: Handler | ||
| let encoder: Encoder | ||
| let decoder: Decoder | ||
| private var byteBuffer: ByteBuffer = .init() | ||
| 
     | 
||
| package init(encoder: Encoder, decoder: Decoder, handler: Handler) where Output: Encodable { | ||
| self.encoder = encoder | ||
| self.decoder = decoder | ||
| self.handler = handler | ||
| } | ||
| 
     | 
||
| package init(decoder: Decoder, handler: Handler) where Output == Void, Encoder == VoidEncoder { | ||
| self.encoder = VoidEncoder() | ||
| self.decoder = decoder | ||
| self.handler = handler | ||
| } | ||
| 
     | 
||
| package mutating func handle( | ||
| _ request: ByteBuffer, | ||
| responseWriter: some LambdaResponseStreamWriter, | ||
| context: NewLambdaContext | ||
| ) async throws { | ||
| let event = try self.decoder.decode(Event.self, from: request) | ||
| 
     | 
||
| let writer = ResponseWriter<Output>(encoder: self.encoder, streamWriter: responseWriter) | ||
| try await self.handler.handle(event, outputWriter: writer, context: context) | ||
| } | ||
| } | ||
| 
     | 
||
| package struct ResponseWriter<Output>: LambdaResponseWriter { | ||
                
      
                  aryan-25 marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| let underlyingStreamWriter: LambdaResponseStreamWriter | ||
| let encoder: LambdaOutputEncoder | ||
| var byteBuffer = ByteBuffer() | ||
                
      
                  fabianfett marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| package init(encoder: LambdaOutputEncoder, streamWriter: LambdaResponseStreamWriter) { | ||
                
      
                  fabianfett marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| self.encoder = encoder | ||
| self.underlyingStreamWriter = streamWriter | ||
| } | ||
| 
     | 
||
| package mutating func write(response: Output) async throws { | ||
                
      
                  fabianfett marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| if Output.self == Void.self { | ||
| try await self.underlyingStreamWriter.finish() | ||
| } else if let response = response as? Encodable { | ||
| try self.encoder.encode(response, into: &self.byteBuffer) | ||
| try await self.underlyingStreamWriter.writeAndFinish(self.byteBuffer) | ||
| } | ||
                
      
                  fabianfett marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| } | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the SwiftAWSLambdaRuntime open source project | ||
| // | ||
| // Copyright (c) 2024 Apple Inc. and the SwiftAWSLambdaRuntime project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| 
     | 
||
| import NIOCore | ||
| 
     | 
||
| package protocol StreamingLambdaHandler { | ||
                
      
                  fabianfett marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| mutating func handle( | ||
| _ event: ByteBuffer, | ||
| responseWriter: some LambdaResponseStreamWriter, | ||
                
      
                  fabianfett marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| context: NewLambdaContext | ||
| ) async throws | ||
| } | ||
| 
     | 
||
| package protocol LambdaResponseStreamWriter { | ||
| mutating func write(_ buffer: ByteBuffer) async throws | ||
                
      
                  fabianfett marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| func finish() async throws | ||
| func writeAndFinish(_ buffer: ByteBuffer) async throws | ||
| func reportError(_ error: any Error) async throws | ||
| } | ||
                
      
                  aryan-25 marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| package protocol NewLambdaHandler { | ||
| associatedtype Event: Decodable | ||
| associatedtype Output | ||
| 
     | 
||
| func handle(_ event: Event, context: NewLambdaContext) async throws -> Output | ||
| } | ||
                
      
                  fabianfett marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| package protocol LambdaWithBackgroundProcessingHandler { | ||
| associatedtype Event: Decodable | ||
| associatedtype Output | ||
| 
     | 
||
| /// The business logic of the Lambda function. Receives a generic input type and returns a generic output type. | ||
| /// Agnostic to JSON encoding/decoding | ||
| func handle( | ||
| _ event: Event, | ||
| outputWriter: some LambdaResponseWriter<Output>, | ||
| context: NewLambdaContext | ||
| ) async throws | ||
| } | ||
| 
     | 
||
| package protocol LambdaResponseWriter<Output>: ~Copyable { | ||
                
      
                  fabianfett marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| associatedtype Output | ||
| /// Sends the generic Output object (representing the computed result of the handler) | ||
| /// to the AWS Lambda response endpoint. | ||
| /// This function simply serves as a mechanism to return the computed result from a handler function | ||
| /// without an explicit `return`. | ||
| mutating func write(response: Output) async throws | ||
| } | ||
                
      
                  fabianfett marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| package struct StreamingClosureHandler: StreamingLambdaHandler { | ||
                
      
                  fabianfett marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| let body: @Sendable (ByteBuffer, LambdaResponseStreamWriter, NewLambdaContext) async throws -> Void | ||
| 
     | 
||
| package init( | ||
| body: @Sendable @escaping (ByteBuffer, LambdaResponseStreamWriter, NewLambdaContext) async throws -> Void | ||
| ) { | ||
| self.body = body | ||
| } | ||
| 
     | 
||
| package func handle( | ||
| _ request: ByteBuffer, | ||
| responseWriter: some LambdaResponseStreamWriter, | ||
| context: NewLambdaContext | ||
| ) async throws { | ||
| try await self.body(request, responseWriter, context) | ||
| } | ||
| } | ||
| 
     | 
||
| package struct ClosureHandler<Event: Decodable, Output>: NewLambdaHandler { | ||
                
      
                  fabianfett marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| let body: (Event, NewLambdaContext) async throws -> Output | ||
| 
     | 
||
| package init(body: @escaping (Event, NewLambdaContext) async throws -> Output) where Output: Encodable { | ||
| self.body = body | ||
| } | ||
| 
     | 
||
| package init(body: @escaping (Event, NewLambdaContext) async throws -> Void) where Output == Void { | ||
| self.body = body | ||
| } | ||
| 
     | 
||
| package func handle(_ event: Event, context: NewLambdaContext) async throws -> Output { | ||
| try await self.body(event, context) | ||
| } | ||
| } | ||
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.