aws-sdk-dynamodbstreams 1.26.0 → 1.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3895d33d1f49db7f0c34332c5439169ed15a93c3cc7a3e4d970ef725ce6f755f
4
- data.tar.gz: 780bb3a618714a91cc2b6d4eaaa9a283c42122e2c65ecbeaf1b95e96a91c381a
3
+ metadata.gz: adaf2de9db3083531212490efc797a07ef15a365a3c6a12eecbe836c3ad53841
4
+ data.tar.gz: f45543e1251b629f2c82733f3ed4e69d97fad3f504b72db261233f93afa7b043
5
5
  SHA512:
6
- metadata.gz: bda879d8b2e114301d7f86c4791ed70fb32e411f0b9f31e0205b01b979b0efb3f6fcd02190b8528d444e54a512ff11e18023ba4ad800686d3c44f11b29c2ae78
7
- data.tar.gz: 29ed9f2f337356ba36869aaa3e05cff4fcfcc2c4718ebe9daddc8758c323495d7f430d7ab4891134e40975ae58b569972fe80aa24b34e452e17d466af7fbf028
6
+ metadata.gz: 2c3ca4b72b802c0a5f026f3fb63f42b09292269b4506fba773d4699effa3cac2131d6f132462af163ff874182c9d350a3dfb9a35b36d947b169e1e74daa588d1
7
+ data.tar.gz: 9f0f6c964291715cd30556e7da7c35a169a1f42c1b9803dfe95f4d0b57857abb01bdfac0f246738695c307bf75f3afe925d6abd3a7cff10d431c9bef18fedfb9
@@ -48,6 +48,6 @@ require_relative 'aws-sdk-dynamodbstreams/customizations'
48
48
  # @!group service
49
49
  module Aws::DynamoDBStreams
50
50
 
51
- GEM_VERSION = '1.26.0'
51
+ GEM_VERSION = '1.27.0'
52
52
 
53
53
  end
@@ -0,0 +1,40 @@
1
+ module Aws
2
+ module DynamoDBStreams
3
+ # A utility class that translates DynamoDBStream events from Lambda
4
+ # functions into their simple attribute equivalents.
5
+ class AttributeTranslator
6
+ # Parse a DynamoDBStream event hash from Lambda that contains 1 or more
7
+ # records. When using the SDK to retrieve DynamoDB stream records, use the
8
+ # `simple_attributes: true` client option instead.
9
+ #
10
+ # @param [Hash] event A DynamoDBStream event.
11
+ #
12
+ # @example Parse a DynamoDB stream event from Lambda
13
+ # def lambda_handler(event:, context:)
14
+ # records = Aws::DynamoDBStreams::AttributeTranslator.from_event(event)
15
+ # records.each do |record|
16
+ # puts record.dynamodb.new_image
17
+ # # => { "size" => 123, "enabled" => true, ... }
18
+ # end
19
+ # end
20
+ def self.from_event(event)
21
+ return unless event.is_a?(Hash)
22
+
23
+ # TODO: This implementation is slow and inefficient. It would be
24
+ # better to write a switch-case that has similar logic to AttributeValue
25
+ # used in the ValueTranslator. This implementation however provides
26
+ # consistency between both DynamoDB and DynamoDBStreams. The translator
27
+ # only works with shapes and structs and not a regular Ruby hash.
28
+ shape_ref = ClientApi::Shapes::ShapeRef.new(
29
+ shape: ClientApi::GetRecordsOutput
30
+ )
31
+ translator = Plugins::SimpleAttributes::ValueTranslator.new(
32
+ shape_ref, :unmarshal
33
+ )
34
+ parser = Aws::Json::Parser.new(shape_ref)
35
+ input = parser.parse(Aws::Json.dump(event))
36
+ translator.apply(input).records
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bigdecimal'
4
+ require 'stringio'
5
+ require 'set'
6
+
7
+ module Aws
8
+ module DynamoDBStreams
9
+ # @api private
10
+ class AttributeValue
11
+
12
+ def initialize
13
+ @unmarshaler = Unmarshaler.new
14
+ end
15
+
16
+ def unmarshal(value)
17
+ @unmarshaler.format(value)
18
+ end
19
+
20
+ class Unmarshaler
21
+
22
+ def format(obj)
23
+ type, value = extract_type_and_value(obj)
24
+ case type
25
+ when :m
26
+ value.each.with_object({}) do |(k, v), map|
27
+ map[k] = format(v)
28
+ end
29
+ when :l then value.map { |v| format(v) }
30
+ when :s then value
31
+ when :n then BigDecimal(value)
32
+ when :b then StringIO.new(value)
33
+ when :null then nil
34
+ when :bool then value
35
+ when :ss then Set.new(value)
36
+ when :ns then Set.new(value.map { |n| BigDecimal(n) })
37
+ when :bs then Set.new(value.map { |b| StringIO.new(b) })
38
+ else
39
+ raise ArgumentError, "unhandled type #{type.inspect}"
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def extract_type_and_value(obj)
46
+ case obj
47
+ when Hash then obj.to_a.first
48
+ when Struct
49
+ obj.members.each do |key|
50
+ value = obj[key]
51
+ return [key, value] unless value.nil?
52
+ end
53
+ else
54
+ raise ArgumentError, "unhandled type #{obj.inspect}"
55
+ end
56
+ end
57
+
58
+ end
59
+ end
60
+ end
61
+ end
@@ -29,6 +29,7 @@ require 'aws-sdk-core/plugins/transfer_encoding.rb'
29
29
  require 'aws-sdk-core/plugins/http_checksum.rb'
30
30
  require 'aws-sdk-core/plugins/signature_v4.rb'
31
31
  require 'aws-sdk-core/plugins/protocols/json_rpc.rb'
32
+ require 'aws-sdk-dynamodbstreams/plugins/simple_attributes.rb'
32
33
 
33
34
  Aws::Plugins::GlobalConfiguration.add_identifier(:dynamodbstreams)
34
35
 
@@ -75,6 +76,7 @@ module Aws::DynamoDBStreams
75
76
  add_plugin(Aws::Plugins::HttpChecksum)
76
77
  add_plugin(Aws::Plugins::SignatureV4)
77
78
  add_plugin(Aws::Plugins::Protocols::JsonRpc)
79
+ add_plugin(Aws::DynamoDBStreams::Plugins::SimpleAttributes)
78
80
 
79
81
  # @overload initialize(options)
80
82
  # @param [Hash] options
@@ -266,6 +268,10 @@ module Aws::DynamoDBStreams
266
268
  #
267
269
  # @option options [String] :session_token
268
270
  #
271
+ # @option options [Boolean] :simple_attributes (false)
272
+ # When enabled, returns DynamoDBStream attribute values using
273
+ # hashes, arrays, sets, integers, floats, booleans, and nil.
274
+ #
269
275
  # @option options [Boolean] :simple_json (false)
270
276
  # Disables request parameter conversion, validation, and formatting.
271
277
  # Also disable response data type conversions. This option is useful
@@ -787,7 +793,7 @@ module Aws::DynamoDBStreams
787
793
  params: params,
788
794
  config: config)
789
795
  context[:gem_name] = 'aws-sdk-dynamodbstreams'
790
- context[:gem_version] = '1.26.0'
796
+ context[:gem_version] = '1.27.0'
791
797
  Seahorse::Client::Request.new(handlers, context)
792
798
  end
793
799
 
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ # utility classes
4
+ require 'aws-sdk-dynamodbstreams/attribute_translator'
5
+ require 'aws-sdk-dynamodbstreams/attribute_value'
@@ -0,0 +1,152 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Aws
4
+ module DynamoDBStreams
5
+ module Plugins
6
+ # Simplifies working with Amazon DynamoDBStreams attribute values.
7
+ # Translates attribute values from responses to sensible Ruby natives.
8
+ #
9
+ # This plugin is disabled by default for all {DynamoDBStreams::Client}
10
+ # objects. You can enable this plugin by passing
11
+ # `simple_attributes: false` to the client constructor:
12
+ #
13
+ # ddb = Aws::DynamoDBStreams::Client.new(simple_attributes: true)
14
+ #
15
+ # ## Output Examples
16
+ #
17
+ # With this plugin **enabled**, `simple_attributes: true`:
18
+ #
19
+ # resp = dynamodbstreams.get_records(shard_iterator: iterator)
20
+ # resp.records.first.dynamodb.new_image
21
+ # {
22
+ # id: 'uuid',
23
+ # enabled: true,
24
+ # tags: #<Set: {"attributes", "simple"}>,
25
+ # data: #<StringIO:0x00007fe4061e6bc0>,
26
+ # scores: [0.1e1, 0.2e1, 0.3e1, nil],
27
+ # name: {
28
+ # first: 'John',
29
+ # last: 'Doe',
30
+ # }
31
+ # }
32
+ #
33
+ # With this plugin **disabled**, `simple_attributes: false`:
34
+ #
35
+ # resp = dynamodbstreams.get_records(shard_iterator: iterator)
36
+ # resp.records.first.dynamodb.new_image
37
+ # {
38
+ # "id"=> <struct s='uuid', n=nil, b=nil, ss=nil, ns=nil, bs=nil, m=nil, l=nil, null=nil, bool=nil>
39
+ # "enabled"=> <struct s=nil, n=nil, b=nil, ss=nil, ns=nil, bs=nil, m=nil, l=nil, null=nil, bool=true>
40
+ # ...
41
+ # }
42
+ #
43
+ class SimpleAttributes < Seahorse::Client::Plugin
44
+
45
+ option(:simple_attributes,
46
+ default: false,
47
+ doc_type: 'Boolean',
48
+ docstring: <<-DOCS
49
+ When enabled, returns DynamoDBStream attribute values using
50
+ hashes, arrays, sets, integers, floats, booleans, and nil.
51
+ DOCS
52
+ )
53
+
54
+ def add_handlers(handlers, config)
55
+ if config.simple_attributes
56
+ handlers.add(Handler, step: :initialize, operations: [:get_records])
57
+ end
58
+ end
59
+
60
+ class Handler < Seahorse::Client::Handler
61
+
62
+ def call(context)
63
+ @handler.call(context).on(200) do |response|
64
+ response.data = translate_output(response)
65
+ end
66
+ end
67
+
68
+ private
69
+
70
+ def translate_output(response)
71
+ if shape = response.context.operation.output
72
+ ValueTranslator.new(shape, :unmarshal).apply(response.data)
73
+ else
74
+ response.data
75
+ end
76
+ end
77
+
78
+ end
79
+
80
+ # @api private
81
+ class ValueTranslator
82
+
83
+ include Seahorse::Model::Shapes
84
+
85
+ def self.apply(rules, mode, data)
86
+ new(rules, mode).apply(data)
87
+ end
88
+
89
+ def initialize(rules, mode)
90
+ @rules = rules
91
+ @mode = mode
92
+ end
93
+
94
+ def apply(values)
95
+ structure(@rules, values) if @rules
96
+ end
97
+
98
+ private
99
+
100
+ def structure(ref, values)
101
+ shape = ref.shape
102
+ if values.is_a?(Struct)
103
+ values.members.each.with_object(values.class.new) do |key, st|
104
+ st[key] = translate(shape.member(key), values[key])
105
+ end
106
+ elsif values.is_a?(Hash)
107
+ values.each.with_object({}) do |(key, value), hash|
108
+ hash[key] = translate(shape.member(key), value)
109
+ end
110
+ else
111
+ values
112
+ end
113
+ end
114
+
115
+ def list(ref, values)
116
+ return values unless values.is_a?(Array)
117
+ member_ref = ref.shape.member
118
+ values.inject([]) do |list, value|
119
+ list << translate(member_ref, value)
120
+ end
121
+ end
122
+
123
+ def map(ref, values)
124
+ return values unless values.is_a?(Hash)
125
+ value_ref = ref.shape.value
126
+ values.each.with_object({}) do |(key, value), hash|
127
+ hash[key] = translate(value_ref, value)
128
+ end
129
+ end
130
+
131
+ def translate(ref, value)
132
+ if ClientApi::AttributeValue === ref.shape
133
+ AttributeValue.new.send(@mode, value)
134
+ else
135
+ translate_complex(ref, value)
136
+ end
137
+ end
138
+
139
+ def translate_complex(ref, value)
140
+ case ref.shape
141
+ when StructureShape then structure(ref, value)
142
+ when ListShape then list(ref, value)
143
+ when MapShape then map(ref, value)
144
+ else value
145
+ end
146
+ end
147
+
148
+ end
149
+ end
150
+ end
151
+ end
152
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws-sdk-dynamodbstreams
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.26.0
4
+ version: 1.27.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Amazon Web Services
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-05 00:00:00.000000000 Z
11
+ date: 2021-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-core
@@ -53,10 +53,13 @@ extensions: []
53
53
  extra_rdoc_files: []
54
54
  files:
55
55
  - lib/aws-sdk-dynamodbstreams.rb
56
+ - lib/aws-sdk-dynamodbstreams/attribute_translator.rb
57
+ - lib/aws-sdk-dynamodbstreams/attribute_value.rb
56
58
  - lib/aws-sdk-dynamodbstreams/client.rb
57
59
  - lib/aws-sdk-dynamodbstreams/client_api.rb
58
60
  - lib/aws-sdk-dynamodbstreams/customizations.rb
59
61
  - lib/aws-sdk-dynamodbstreams/errors.rb
62
+ - lib/aws-sdk-dynamodbstreams/plugins/simple_attributes.rb
60
63
  - lib/aws-sdk-dynamodbstreams/resource.rb
61
64
  - lib/aws-sdk-dynamodbstreams/types.rb
62
65
  homepage: https://github.com/aws/aws-sdk-ruby