File: stub_data.rb

package info (click to toggle)
ruby-aws-sdk-core 3.212.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,232 kB
  • sloc: ruby: 17,533; makefile: 4
file content (60 lines) | stat: -rw-r--r-- 1,832 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# frozen_string_literal: true

module Aws
  # @api private
  module Stubbing
    class StubData

      def initialize(operation)
        @rules = operation.output
        @pager = operation[:pager]
      end

      def stub(data = {})
        stub = EmptyStub.new(@rules).stub
        remove_paging_tokens(stub)
        remove_checksums(stub)
        apply_data(data, stub)
        stub
      end

      private

      def remove_checksums(stub)
        if @rules && @rules.shape.is_a?(Seahorse::Model::Shapes::StructureShape)
          @rules.shape.members.each do |key, member|
            if member.location == 'header' && member.location_name.start_with?('x-amz-checksum-')
              stub[key] = nil
            end
          end
        end
      end

      def remove_paging_tokens(stub)
        if @pager
          @pager.instance_variable_get("@tokens").keys.each do |path|
            if divide = (path[' || '] || path[' or '])
              path = path.split(divide)[0]
            end
            parts = path.split(/\b/)
            # if nested struct/expression, EmptyStub auto-pop "string"
            # currently not support remove "string" for nested/expression
            # as it requires reverse JMESPATH search
            stub[parts[0]] = nil if parts.size == 1
          end
          if more_results = @pager.instance_variable_get('@more_results')
            parts = more_results.split(/\b/)
            # if nested struct/expression, EmptyStub auto-pop false value
            # no further work needed
            stub[parts[0]] = false if parts.size == 1
          end
        end
      end

      def apply_data(data, stub)
        ParamValidator.new(@rules, validate_required: false, input: false).validate!(data)
        DataApplicator.new(@rules).apply_data(data, stub)
      end
    end
  end
end