From 0d41ad0a6db16a036f3eb2330fa007362c830fb9 Mon Sep 17 00:00:00 2001 From: Matt Muller Date: Fri, 18 Oct 2024 10:23:45 -0400 Subject: [PATCH] Move cbor engine to rpc v2 namespace --- .../spec/protocol_tests_spec_helper.rb | 7 +- gems/aws-sdk-core/lib/aws-sdk-core/cbor.rb | 59 +---------------- gems/aws-sdk-core/lib/aws-sdk-core/rpc_v2.rb | 65 ++++++++++++++++++- .../lib/aws-sdk-core/rpc_v2/builder.rb | 2 +- .../{cbor => rpc_v2}/cbor_engine.rb | 9 ++- .../lib/aws-sdk-core/rpc_v2/error_handler.rb | 2 +- .../lib/aws-sdk-core/rpc_v2/parser.rb | 2 +- .../spec/aws/{cbor_spec.rb => rpc_v2_spec.rb} | 9 ++- 8 files changed, 80 insertions(+), 75 deletions(-) rename gems/aws-sdk-core/lib/aws-sdk-core/{cbor => rpc_v2}/cbor_engine.rb (56%) rename gems/aws-sdk-core/spec/aws/{cbor_spec.rb => rpc_v2_spec.rb} (73%) diff --git a/build_tools/aws-sdk-code-generator/spec/protocol_tests_spec_helper.rb b/build_tools/aws-sdk-code-generator/spec/protocol_tests_spec_helper.rb index be6a07a23cf..f54ed522936 100644 --- a/build_tools/aws-sdk-code-generator/spec/protocol_tests_spec_helper.rb +++ b/build_tools/aws-sdk-code-generator/spec/protocol_tests_spec_helper.rb @@ -114,8 +114,7 @@ def set_engine(context, protocol, engine) when /xml/, /ec2/, /query/ Aws::Xml::Parser when /smithy-rpc-v2-cbor/ - Aws::RpcV2 # autoload - Aws::Cbor + Aws::RpcV2 else raise "unsupported protocol: #{protocol}" end @@ -302,8 +301,8 @@ def match_req_body(suite, test_case, http_req, it) expected_body = Aws::Json.load(expected_body) end when 'smithy-rpc-v2-cbor' - request_body = Aws::Cbor.decode(request_body) - expected_body = Aws::Cbor.decode(Base64.decode64(expected_body)) + request_body = Aws::RpcV2.decode(request_body) + expected_body = Aws::RpcV2.decode(Base64.decode64(expected_body)) else raise "unsupported protocol: `#{protocol}`" end assert(it, request_body, expected_body) diff --git a/gems/aws-sdk-core/lib/aws-sdk-core/cbor.rb b/gems/aws-sdk-core/lib/aws-sdk-core/cbor.rb index bba6d660704..e65c5743738 100644 --- a/gems/aws-sdk-core/lib/aws-sdk-core/cbor.rb +++ b/gems/aws-sdk-core/lib/aws-sdk-core/cbor.rb @@ -1,5 +1,8 @@ # frozen_string_literal: true +require_relative 'cbor/encoder' +require_relative 'cbor/decoder' + module Aws # @api private module Cbor @@ -46,61 +49,5 @@ def initialize(add_info) super("Unexpected additional information: #{add_info}") end end - - class << self - # @param [Symbol,Class] engine - # Must be one of the following values: - # - # * :cbor - # - def engine=(engine) - @engine = Class === engine ? engine : load_engine(engine) - end - - # @return [Class] Returns the default engine. - # One of: - # - # * {CborEngine} - # - def engine - set_default_engine unless @engine - @engine - end - - def encode(data) - @engine.encode(data) - end - - def decode(bytes) - bytes.force_encoding(Encoding::BINARY) - @engine.decode(bytes) - end - - def set_default_engine - [:cbor].each do |name| - @engine ||= try_load_engine(name) - end - - unless @engine - raise 'Unable to find a compatible cbor library.' - end - end - - private - - def load_engine(name) - require "aws-sdk-core/cbor/#{name}_engine" - const_name = name[0].upcase + name[1..-1] + 'Engine' - const_get(const_name) - end - - def try_load_engine(name) - load_engine(name) - rescue LoadError - false - end - end - - set_default_engine end end diff --git a/gems/aws-sdk-core/lib/aws-sdk-core/rpc_v2.rb b/gems/aws-sdk-core/lib/aws-sdk-core/rpc_v2.rb index db3a16a6f59..f112c61d908 100644 --- a/gems/aws-sdk-core/lib/aws-sdk-core/rpc_v2.rb +++ b/gems/aws-sdk-core/lib/aws-sdk-core/rpc_v2.rb @@ -1,8 +1,69 @@ # frozen_string_literal: true require_relative 'cbor' -require_relative 'rpc_v2/handler' +require_relative 'rpc_v2/builder' require_relative 'rpc_v2/content_type_handler' require_relative 'rpc_v2/error_handler' -require_relative 'rpc_v2/builder' +require_relative 'rpc_v2/handler' require_relative 'rpc_v2/parser' + +module Aws + # @api private + module RpcV2 + class << self + # @param [Symbol,Class] engine + # Must be one of the following values: + # + # * :cbor + # + def engine=(engine) + @engine = Class === engine ? engine : load_engine(engine) + end + + # @return [Class] Returns the default engine. + # One of: + # + # * {CborEngine} + # + def engine + set_default_engine unless @engine + @engine + end + + def encode(data) + @engine.encode(data) + end + + def decode(bytes) + bytes.force_encoding(Encoding::BINARY) + @engine.decode(bytes) + end + + def set_default_engine + [:cbor].each do |name| + @engine ||= try_load_engine(name) + end + + unless @engine + raise 'Unable to find a compatible cbor library.' + end + end + + private + + def load_engine(name) + require "aws-sdk-core/rpc_v2/#{name}_engine" + const_name = name[0].upcase + name[1..-1] + 'Engine' + const_get(const_name) + end + + def try_load_engine(name) + load_engine(name) + rescue LoadError + false + end + end + + set_default_engine + end +end diff --git a/gems/aws-sdk-core/lib/aws-sdk-core/rpc_v2/builder.rb b/gems/aws-sdk-core/lib/aws-sdk-core/rpc_v2/builder.rb index 8a92270c866..b8656445012 100644 --- a/gems/aws-sdk-core/lib/aws-sdk-core/rpc_v2/builder.rb +++ b/gems/aws-sdk-core/lib/aws-sdk-core/rpc_v2/builder.rb @@ -16,7 +16,7 @@ def serialize(params) # different than if the input shape is a structure with no members. return nil if @rules.shape.struct_class == EmptyStructure - Cbor.encode(format(@rules, params)) + RpcV2.encode(format(@rules, params)) end private diff --git a/gems/aws-sdk-core/lib/aws-sdk-core/cbor/cbor_engine.rb b/gems/aws-sdk-core/lib/aws-sdk-core/rpc_v2/cbor_engine.rb similarity index 56% rename from gems/aws-sdk-core/lib/aws-sdk-core/cbor/cbor_engine.rb rename to gems/aws-sdk-core/lib/aws-sdk-core/rpc_v2/cbor_engine.rb index 5f06bd3fdd8..116d16676f1 100644 --- a/gems/aws-sdk-core/lib/aws-sdk-core/cbor/cbor_engine.rb +++ b/gems/aws-sdk-core/lib/aws-sdk-core/rpc_v2/cbor_engine.rb @@ -1,18 +1,17 @@ # frozen_string_literal: true -require_relative 'encoder' -require_relative 'decoder' +require_relative '../cbor' module Aws - module Cbor + module RpcV2 # Pure Ruby implementation of CBOR encode and decode module CborEngine def self.encode(data) - Encoder.new.add(data).bytes + Cbor::Encoder.new.add(data).bytes end def self.decode(bytes) - Decoder.new(bytes.force_encoding(Encoding::BINARY)).decode + Cbor::Decoder.new(bytes.force_encoding(Encoding::BINARY)).decode end end end diff --git a/gems/aws-sdk-core/lib/aws-sdk-core/rpc_v2/error_handler.rb b/gems/aws-sdk-core/lib/aws-sdk-core/rpc_v2/error_handler.rb index 3e13b05f0ab..1c0bae56d70 100644 --- a/gems/aws-sdk-core/lib/aws-sdk-core/rpc_v2/error_handler.rb +++ b/gems/aws-sdk-core/lib/aws-sdk-core/rpc_v2/error_handler.rb @@ -27,7 +27,7 @@ def valid_response?(context) end def extract_error(body, context) - data = Cbor.decode(body) + data = RpcV2.decode(body) code = error_code(data, context) message = data['message'] data = parse_error_data(context, body, code) diff --git a/gems/aws-sdk-core/lib/aws-sdk-core/rpc_v2/parser.rb b/gems/aws-sdk-core/lib/aws-sdk-core/rpc_v2/parser.rb index a4a47312b75..d219e391e2d 100644 --- a/gems/aws-sdk-core/lib/aws-sdk-core/rpc_v2/parser.rb +++ b/gems/aws-sdk-core/lib/aws-sdk-core/rpc_v2/parser.rb @@ -16,7 +16,7 @@ def initialize(rules, query_compatible: false) def parse(cbor, target = nil) return {} if cbor.empty? - parse_ref(@rules, Cbor.decode(cbor), target) + parse_ref(@rules, RpcV2.decode(cbor), target) end private diff --git a/gems/aws-sdk-core/spec/aws/cbor_spec.rb b/gems/aws-sdk-core/spec/aws/rpc_v2_spec.rb similarity index 73% rename from gems/aws-sdk-core/spec/aws/cbor_spec.rb rename to gems/aws-sdk-core/spec/aws/rpc_v2_spec.rb index 6e3a3c445c8..c7716698f74 100644 --- a/gems/aws-sdk-core/spec/aws/cbor_spec.rb +++ b/gems/aws-sdk-core/spec/aws/rpc_v2_spec.rb @@ -1,22 +1,21 @@ # frozen_string_literal: true require_relative '../spec_helper' -require 'aws-sdk-core/cbor' module Aws - describe Cbor do + describe RpcV2 do [:cbor].each do |engine| describe("ENGINE: #{engine}") do begin - Cbor.engine = engine + RpcV2.engine = engine rescue LoadError next end describe '.encode' do it 'encodes an object into bytes' do - expect(Cbor.encode('abc')).to eq("\x63abc") + expect(RpcV2.encode('abc')).to eq("\x63abc") end end @@ -24,7 +23,7 @@ module Aws it 'decodes bytes into an object' do # frozen string bytes = StringIO.new("\x63abc").read - expect(Cbor.decode(bytes)).to eq('abc') + expect(RpcV2.decode(bytes)).to eq('abc') end end end