Skip to content

Commit

Permalink
Move cbor engine to rpc v2 namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
mullermp committed Oct 18, 2024
1 parent aab4c5f commit 0d41ad0
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
59 changes: 3 additions & 56 deletions gems/aws-sdk-core/lib/aws-sdk-core/cbor.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# frozen_string_literal: true

require_relative 'cbor/encoder'
require_relative 'cbor/decoder'

module Aws
# @api private
module Cbor
Expand Down Expand Up @@ -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
65 changes: 63 additions & 2 deletions gems/aws-sdk-core/lib/aws-sdk-core/rpc_v2.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion gems/aws-sdk-core/lib/aws-sdk-core/rpc_v2/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion gems/aws-sdk-core/lib/aws-sdk-core/rpc_v2/error_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion gems/aws-sdk-core/lib/aws-sdk-core/rpc_v2/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
# 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

describe '.decode' do
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
Expand Down

0 comments on commit 0d41ad0

Please sign in to comment.