File: data_applicator.rb

package info (click to toggle)
ruby-aws-sdk-core 3.104.3-3%2Bdeb11u2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,444 kB
  • sloc: ruby: 11,201; makefile: 4
file content (48 lines) | stat: -rw-r--r-- 1,112 bytes parent folder | download | duplicates (3)
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
# frozen_string_literal: true

module Aws
  module Stubbing
    class DataApplicator

      include Seahorse::Model::Shapes

      # @param [Seahorse::Models::Shapes::ShapeRef] rules
      def initialize(rules)
        @rules = rules
      end

      # @param [Hash] data
      # @param [Structure] stub
      def apply_data(data, stub)
        apply_data_to_struct(@rules, data, stub)
      end

      private

      def apply_data_to_struct(ref, data, struct)
        data.each do |key, value|
          struct[key] = member_value(ref.shape.member(key), value)
        end
        struct
      end

      def member_value(ref, value)
        case ref.shape
        when StructureShape
          apply_data_to_struct(ref, value, ref.shape.struct_class.new)
        when ListShape
          value.inject([]) do |list, v|
            list << member_value(ref.shape.member, v)
          end
        when MapShape
          value.inject({}) do |map, (k,v)|
            map[k.to_s] = member_value(ref.shape.value, v)
            map
          end
        else
          value
        end
      end
    end
  end
end