Skip to content

Commit

Permalink
Promote smithy model classes to their own gem (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
mullermp authored Feb 17, 2025
1 parent 352ba3b commit d4711fd
Show file tree
Hide file tree
Showing 58 changed files with 354 additions and 160 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:
- name: Test
run: bundle exec rake smithy:spec

smithy-test-rbs:
smithy-rbs:
needs: [smithy-test]
runs-on: ubuntu-latest

Expand Down
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Naming/FileName:
- '**/spec/**/*.rb'
- 'gems/smithy-client/lib/smithy-client.rb'
- 'gems/smithy-server/lib/smithy-server.rb'
- 'gems/smithy-model/lib/smithy-model.rb'

Style/BlockComments:
Exclude:
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ gem 'rake', require: false
gem 'smithy', path: 'gems/smithy'
gem 'smithy-client', path: 'gems/smithy-client'
# gem 'smithy-server', path: 'gems/smithy-server'
gem 'smithy-model', path: 'gems/smithy-model'

group :development do
gem 'byebug', platforms: :ruby
Expand Down
41 changes: 36 additions & 5 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace :smithy do
include_paths = []
plans = []
rbs_targets = %w[Smithy Smithy::* Smithy::Client]
sig_paths = ['gems/smithy-client/sig']
sig_paths = %w[gems/smithy-client/sig gems/smithy-model/sig]
Dir.glob('gems/smithy/spec/fixtures/endpoints/*/model.json') do |model_path|
test_name = model_path.split('/')[-2]
test_module = test_name.gsub('-', '').camelize
Expand Down Expand Up @@ -108,7 +108,7 @@ namespace 'smithy-client' do

desc 'Run RBS validation.'
task 'rbs:validate' do
sh('bundle exec rbs -I gems/smithy-client/sig validate')
sh('bundle exec rbs -I gems/smithy-client/sig -I gems/smithy-model/sig validate')
end

desc 'Run RBS spy tests on all unit tests.'
Expand All @@ -117,8 +117,8 @@ namespace 'smithy-client' do
'RUBYOPT' => '-r bundler/setup -r rbs/test/setup',
'RBS_TEST_RAISE' => 'true',
'RBS_TEST_LOGLEVEL' => 'error',
'RBS_TEST_OPT' => '-I gems/smithy-client/sig',
'RBS_TEST_TARGET' => '"Smithy,Smithy::*,Smithy::Client"',
'RBS_TEST_OPT' => '-I gems/smithy-client/sig -I gems/smithy-model/sig',
'RBS_TEST_TARGET' => '"Smithy::Client,Smithy::Client::*"',
'RBS_TEST_DOUBLE_SUITE' => 'rspec'
}
sh(env,
Expand All @@ -127,5 +127,36 @@ namespace 'smithy-client' do
end

desc 'Run RBS validation and spy tests.'
task 'rbs' => ['rbs:validate', 'rbs:test']
task 'rbs' => %w[rbs:validate rbs:test]
end

namespace 'smithy-model' do
RSpec::Core::RakeTask.new do |t|
t.pattern = 'gems/smithy-model/spec/**/*_spec.rb'
t.ruby_opts = '-I gems/smithy-model/spec'
t.rspec_opts = '--format documentation'
end

desc 'Run RBS validation.'
task 'rbs:validate' do
sh('bundle exec rbs -I gems/smithy-model/sig validate')
end

desc 'Run RBS spy tests on all unit tests.'
task 'rbs:test' do
env = {
'RUBYOPT' => '-r bundler/setup -r rbs/test/setup',
'RBS_TEST_RAISE' => 'true',
'RBS_TEST_LOGLEVEL' => 'error',
'RBS_TEST_OPT' => '-I gems/smithy-client/sig',
'RBS_TEST_TARGET' => '"Smithy::Model,Smithy::Model::*"',
'RBS_TEST_DOUBLE_SUITE' => 'rspec'
}
sh(env,
'bundle exec rspec gems/smithy-model/spec -I gems/smithy-model/lib -I gems/smithy-model/spec ' \
"--require spec_helper --tag '~rbs_test:skip'")
end

desc 'Run RBS validation and spy tests.'
task 'rbs' => %w[rbs:validate rbs:test]
end
17 changes: 6 additions & 11 deletions gems/smithy-client/lib/smithy-client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,28 @@

require 'jmespath'

require 'smithy-model'

# client

require_relative 'smithy-client/block_io'
require_relative 'smithy-client/configuration'
require_relative 'smithy-client/endpoint_rules'
require_relative 'smithy-client/errors'
require_relative 'smithy-client/handler'
require_relative 'smithy-client/handler_builder'
require_relative 'smithy-client/handler_context'
require_relative 'smithy-client/handler_list'
require_relative 'smithy-client/handler_list_entry'
require_relative 'smithy-client/managed_file'
require_relative 'smithy-client/networking_error'
require_relative 'smithy-client/param_converter'
require_relative 'smithy-client/param_validator'
require_relative 'smithy-client/plugin'
require_relative 'smithy-client/plugin_list'
require_relative 'smithy-client/input'
require_relative 'smithy-client/output'
require_relative 'smithy-client/base'

# client http

Expand All @@ -27,17 +33,6 @@
require_relative 'smithy-client/net_http/connection_pool'
require_relative 'smithy-client/net_http/handler'

# model

require_relative 'smithy-client/base'
require_relative 'smithy-client/errors'
require_relative 'smithy-client/schema'
require_relative 'smithy-client/shapes'
require_relative 'smithy-client/structure'
require_relative 'smithy-client/union'
require_relative 'smithy-client/param_converter'
require_relative 'smithy-client/param_validator'

# codecs

require_relative 'smithy-client/cbor'
Expand Down
4 changes: 2 additions & 2 deletions gems/smithy-client/lib/smithy-client/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ def plugins
Array(@plugins).freeze
end

# @return [Schema]
# @return [Model::Schema]
def schema
@schema ||= Schema.new
@schema ||= Model::Schema.new
end

# @param [Schema] schema
Expand Down
2 changes: 1 addition & 1 deletion gems/smithy-client/lib/smithy-client/codecs/cbor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Codecs
# * Update implementation to handle event streams
# * Handle query_compatible trait
class CBOR
include Shapes
include Model::Shapes

def initialize(options = {}); end

Expand Down
6 changes: 3 additions & 3 deletions gems/smithy-client/lib/smithy-client/param_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Smithy
module Client
# @api private
class ParamConverter
include Smithy::Client::Shapes
include Model::Shapes

@mutex = Mutex.new
@converters = Hash.new { |h, k| h[k] = {} }
Expand Down Expand Up @@ -50,7 +50,7 @@ def structure(shape, values)

def union(shape, values)
values = c(shape, values)
if values.is_a?(Union) || values.is_a?(Hash)
if values.is_a?(Model::Union) || values.is_a?(Hash)
values.each_pair do |k, v|
next if v.nil?
next unless shape.member?(k)
Expand Down Expand Up @@ -246,7 +246,7 @@ def each_base_class(shape_class, &)
end

add(UnionShape, Hash) { |h, _| h.dup }
add(UnionShape, Union)
add(UnionShape, Model::Union)
end
end
end
2 changes: 1 addition & 1 deletion gems/smithy-client/lib/smithy-client/param_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Smithy
module Client
# @api private
class ParamValidator
include Smithy::Client::Shapes
include Smithy::Model::Shapes

EXPECTED_GOT = 'expected %s to be %s, got class %s instead.'

Expand Down
5 changes: 2 additions & 3 deletions gems/smithy-client/sig/smithy-client/codec/cbor.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ module Smithy
class CBOR
def initialize: (?::Hash[untyped, untyped] options) -> void

def serialize: (untyped data, Shapes::Shape shape) -> (nil | String)

def deserialize: (String bytes, Shapes::Shape shape, ?Struct[untyped] target) -> (Hash[untyped, untyped] | Object)
def serialize: (untyped data, Model::Shapes::Shape shape) -> (nil | String)

def deserialize: (String bytes, Model::Shapes::Shape shape, ?Struct[untyped] target) -> (Hash[untyped, untyped] | Object)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion gems/smithy-client/sig/smithy-client/handler_context.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Smithy
class HandlerContext
def initialize: (?::Hash[untyped, untyped] options) -> void
attr_accessor operation_name: (Symbol | String)?
attr_accessor operation: Shapes::OperationShape?
attr_accessor operation: Model::Shapes::OperationShape?
attr_accessor client: Base?
attr_accessor params: Hash[Symbol | String, untyped]
attr_accessor config: Struct[untyped]?
Expand Down
2 changes: 2 additions & 0 deletions gems/smithy-client/smithy-client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ Gem::Specification.new do |spec|

spec.add_dependency('jmespath', '~> 1', '>= 1.6.1') # necessary for secure jmespath JSON parsing

spec.add_dependency('smithy-model', '1.0.0.pre0')

spec.required_ruby_version = '>= 3.3'
end
12 changes: 6 additions & 6 deletions gems/smithy-client/spec/smithy-client/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Smithy
module Client
describe Base do
let(:schema) { Schema.new }
let(:schema) { Model::Schema.new }
let(:client_class) { Base.define(schema: schema) }
let(:plugin_a) { Plugin.new }
let(:plugin_b) { Plugin.new }
Expand Down Expand Up @@ -46,7 +46,7 @@ module Client
let(:input) { subject.build_input(:operation) }

before(:each) do
schema.add_operation(:operation, Shapes::OperationShape.new)
schema.add_operation(:operation, Model::Shapes::OperationShape.new)
end

it 'returns an Input' do
Expand Down Expand Up @@ -101,7 +101,7 @@ module Client
let(:input) { Input.new }

before(:each) do
schema.add_operation(:operation, Shapes::OperationShape.new)
schema.add_operation(:operation, Model::Shapes::OperationShape.new)
allow(subject).to receive(:build_input).and_return(input)
allow(input).to receive(:send_request)
end
Expand Down Expand Up @@ -237,13 +237,13 @@ module Client

describe '.schema' do
it 'defaults to a Schema' do
expect(client_class.schema).to be_kind_of(Schema)
expect(client_class.schema).to be_kind_of(Model::Schema)
end
end

describe '.schema=' do
it 'can be set' do
schema = Schema.new
schema = Model::Schema.new
client_class.schema = schema
expect(client_class.schema).to be(schema)
end
Expand All @@ -256,7 +256,7 @@ module Client
end

it 'sets the schema on the client class' do
schema = Schema.new
schema = Model::Schema.new
client_class = Base.define(schema: schema)
expect(client_class.schema).to be(schema)
end
Expand Down
18 changes: 9 additions & 9 deletions gems/smithy-client/spec/smithy-client/codecs/cbor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@ module Smithy
module Client
module Codecs
describe CBOR do
let(:string_shape) { Shapes::StringShape.new(id: 'string') }
let(:string_shape) { Model::Shapes::StringShape.new(id: 'string') }

let(:list_shape) do
shape = Shapes::ListShape.new(id: 'list')
shape.set_member(Shapes::Prelude::String)
shape = Model::Shapes::ListShape.new(id: 'list')
shape.set_member(Model::Shapes::Prelude::String)
shape
end

let(:map_shape) do
shape = Shapes::MapShape.new(id: 'map')
shape.set_key(Shapes::Prelude::String)
shape.set_value(Shapes::Prelude::Blob)
shape = Model::Shapes::MapShape.new(id: 'map')
shape.set_key(Model::Shapes::Prelude::String)
shape.set_value(Model::Shapes::Prelude::Blob)
shape
end

let(:typed_struct) do
Struct.new(:s, :l, :m, keyword_init: true) do
include Smithy::Client::Structure
include Model::Structure
end
end

let(:structure_shape) do
struct = Shapes::StructureShape.new(id: 'structure')
struct = Model::Shapes::StructureShape.new(id: 'structure')
struct.add_member(:s, string_shape)
struct.add_member(:l, list_shape)
struct.add_member(:m, map_shape)
Expand All @@ -35,7 +35,7 @@ module Codecs
end

it 'serializes returns nil when given shape is Prelude::Unit' do
expect(subject.serialize('', Shapes::Prelude::Unit)).to be_nil
expect(subject.serialize('', Model::Shapes::Prelude::Unit)).to be_nil
end

it 'deserializes returns an empty hash when given bytes are empty' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module Client
end

it 'can be set in the constructor' do
operation = Shapes::OperationShape.new
operation = Model::Shapes::OperationShape.new
context = HandlerContext.new(operation: operation)
expect(context.operation).to be(operation)
end
Expand Down
Loading

0 comments on commit d4711fd

Please sign in to comment.