File: jsonvalue_converter.rb

package info (click to toggle)
ruby-aws-sdk-core 3.168.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,924 kB
  • sloc: ruby: 15,292; makefile: 4
file content (59 lines) | stat: -rw-r--r-- 1,804 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

module Aws
  module Plugins

    # Converts input value to JSON Syntax for members with jsonvalue trait
    class JsonvalueConverter < Seahorse::Client::Plugin

      # @api private
      class Handler < Seahorse::Client::Handler

        def call(context)
          context.operation.input.shape.members.each do |m, ref|
            convert_jsonvalue(m, ref, context.params, 'params')
          end
          @handler.call(context)
        end

        def convert_jsonvalue(m, ref, params, context)
          return if params.nil? || !params.key?(m)

          if ref['jsonvalue']
            params[m] = serialize_jsonvalue(params[m], "#{context}[#{m}]")
          else
            case ref.shape
            when Seahorse::Model::Shapes::StructureShape
              ref.shape.members.each do |member_m, ref|
                convert_jsonvalue(member_m, ref, params[m], "#{context}[#{m}]")
              end
            when Seahorse::Model::Shapes::ListShape
              if ref.shape.member['jsonvalue']
                params[m] = params[m].each_with_index.map do |v, i|
                  serialize_jsonvalue(v, "#{context}[#{m}][#{i}]")
                end
              end
            when Seahorse::Model::Shapes::MapShape
              if ref.shape.value['jsonvalue']
                params[m].each do |k, v|
                  params[m][k] = serialize_jsonvalue(v, "#{context}[#{m}][#{k}]")
                end
              end
            end
          end
        end

        def serialize_jsonvalue(v, context)
          unless v.respond_to?(:to_json)
            raise ArgumentError, "The value of #{context} is not JSON serializable."
          end
          v.to_json
        end

      end

      handler(Handler, step: :initialize)
    end

  end
end