Sha256: e0b175ebdb9c8b1bdcb70451963fb079cabbf9b95dc777b2541a8c76f5fa7542

Contents?: true

Size: 1.72 KB

Versions: 6

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

require_relative 'plugins/simple_attributes'

module Aws
  module DynamoDBStreams
    # A utility class that translates DynamoDBStream events from Lambda
    # functions into their simple attribute equivalents.
    class AttributeTranslator
      # Parse a DynamoDBStream event hash from Lambda that contains 1 or more
      # records. When using the SDK to retrieve DynamoDB stream records, use the
      # `simple_attributes: true` client option instead.
      #
      # @param [Hash] event A DynamoDBStream event.
      #
      # @example Parse a DynamoDB stream event from Lambda
      #   def lambda_handler(event:, context:)
      #     records = Aws::DynamoDBStreams::AttributeTranslator.from_event(event)
      #     records.each do |record|
      #       puts record.dynamodb.new_image
      #       # => { "size" => 123, "enabled" => true, ... }
      #     end
      #   end
      def self.from_event(event)
        return unless event.is_a?(Hash)

        # TODO: This implementation is slow and inefficient. It would be
        # better to write a switch-case that has similar logic to AttributeValue
        # used in the ValueTranslator. This implementation however provides
        # consistency between both DynamoDB and DynamoDBStreams. The translator
        # only works with shapes and structs and not a regular Ruby hash.
        shape_ref = ClientApi::Shapes::ShapeRef.new(
          shape: ClientApi::GetRecordsOutput
        )
        translator = Plugins::SimpleAttributes::ValueTranslator.new(
          shape_ref, :unmarshal
        )
        parser = Aws::Json::Parser.new(shape_ref)
        input = parser.parse(Aws::Json.dump(event))
        translator.apply(input).records
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
aws-sdk-dynamodbstreams-1.73.0 lib/aws-sdk-dynamodbstreams/attribute_translator.rb
aws-sdk-dynamodbstreams-1.72.0 lib/aws-sdk-dynamodbstreams/attribute_translator.rb
aws-sdk-dynamodbstreams-1.71.0 lib/aws-sdk-dynamodbstreams/attribute_translator.rb
aws-sdk-dynamodbstreams-1.70.0 lib/aws-sdk-dynamodbstreams/attribute_translator.rb
aws-sdk-dynamodbstreams-1.69.0 lib/aws-sdk-dynamodbstreams/attribute_translator.rb
aws-sdk-dynamodbstreams-1.68.1 lib/aws-sdk-dynamodbstreams/attribute_translator.rb