Skip to content

include? -> key? #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gems/smithy-cbor/lib/smithy-cbor/deserializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def union(ref, values, target = nil) # rubocop:disable Metrics/AbcSize
end

def sparse?(shape)
shape.traits.include?('smithy.api#sparse')
shape.traits.key?('smithy.api#sparse')
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions gems/smithy-client/lib/smithy-client/param_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def valid_union?(ref, values, errors, context)
def validate_required_members(ref, values, errors, context)
ref.shape.members.each do |name, member_ref|
traits = member_ref.traits
next unless traits.include?('smithy.api#required') && !traits.include?('smithy.api#clientOptional')
next unless traits.key?('smithy.api#required') && !traits.key?('smithy.api#clientOptional')

if values[name].nil?
param = "#{context}[#{name.inspect}]"
Expand All @@ -187,7 +187,7 @@ def validate_required_members(ref, values, errors, context)
end

def streaming_input?(ref)
ref.shape.traits.include?('smithy.api#streaming')
ref.shape.traits.key?('smithy.api#streaming')
end

def io_like?(value, require_size: false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def call(context)
private

def checksum_required_operation?(context)
context.operation.traits.include?('smithy.api#httpChecksumRequired')
context.operation.traits.key?('smithy.api#httpChecksumRequired')
end

def md5(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def apply_host_prefix(context, host_prefix)
def label_value(input, label, params)
name = nil
input.shape.members.each do |member_name, member_ref|
next unless member_ref.traits.include?('smithy.api#hostLabel')
next unless member_ref.traits.key?('smithy.api#hostLabel')
next unless member_ref.member_name == label

name = member_name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def call(context)

def apply_idempotency_token(input, params)
input.shape.members.each do |member_name, member_ref|
next unless member_ref.traits.include?('smithy.api#idempotencyToken')
next unless member_ref.traits.key?('smithy.api#idempotencyToken')

params[member_name] ||= SecureRandom.uuid
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def call(context)
private

def request_compression_trait?(context)
context.operation.traits.include?('smithy.api#requestCompression')
context.operation.traits.key?('smithy.api#requestCompression')
end

def request_encoding_selection(context)
Expand All @@ -93,8 +93,8 @@ def request_encoding_selection(context)

def streaming?(input)
input.shape.members.any? do |_, member_ref|
member_ref.shape.traits.include?('smithy.api#streaming') &&
!member_ref.shape.traits.include?('smithy.api#requiresLength')
member_ref.shape.traits.key?('smithy.api#streaming') &&
!member_ref.shape.traits.key?('smithy.api#requiresLength')
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def apply_url_path(context)
def event_stream?(ref)
ref.shape.members.each_value do |member_ref|
shape = member_ref.shape
return true if shape.traits.include?('smithy.api#streaming') && shape.is_a?(UnionShape)
return true if shape.traits.key?('smithy.api#streaming') && shape.is_a?(UnionShape)
end
false
end
Expand Down
2 changes: 1 addition & 1 deletion gems/smithy-json/lib/smithy-json/deserializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def location_name(ref)
end

def sparse?(shape)
shape.traits.include?('smithy.api#sparse')
shape.traits.key?('smithy.api#sparse')
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions gems/smithy/lib/smithy/views/client/auth_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def auth_schemes(welds)
end

def service_has_auth_trait?
@service_traits.include?('smithy.api#auth')
@service_traits.key?('smithy.api#auth')
end

def service_auth_schemes
Expand All @@ -82,11 +82,11 @@ def operations_with_auth_traits
end

def operation_auth?(operation)
operation.fetch('traits', {}).include?('smithy.api#auth')
operation.fetch('traits', {}).key?('smithy.api#auth')
end

def optional_operation_auth?(operation)
operation.fetch('traits', {}).include?('smithy.api#optionalAuth')
operation.fetch('traits', {}).key?('smithy.api#optionalAuth')
end

def operation_auth_schemes(operation)
Expand All @@ -98,7 +98,7 @@ def operation_auth_schemes(operation)
else
add_registered_auth_schemes(auth_schemes, operation_traits)
end
auth_schemes << 'smithy.api#optionalAuth' if operation_traits.include?('smithy.api#optionalAuth')
auth_schemes << 'smithy.api#optionalAuth' if operation_traits.key?('smithy.api#optionalAuth')
auth_schemes << 'smithy.api#noAuth' if auth_schemes.empty?
auth_schemes
end
Expand All @@ -111,7 +111,7 @@ def add_auth_schemes_from_auth_trait(auth_schemes, auth_trait)

def add_registered_auth_schemes(auth_schemes, traits)
@auth_schemes.each_key do |auth_scheme|
auth_schemes << auth_scheme if traits.include?(auth_scheme)
auth_schemes << auth_scheme if traits.key?(auth_scheme)
end
end

Expand Down
8 changes: 4 additions & 4 deletions gems/smithy/lib/smithy/views/client/client_rbs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ def option_types
end

def operations
@operations = Model::ServiceIndex
.new(@model)
.operations_for(@plan.service)
.map { |id, operation| Operation.new(@model, id, operation) }
Model::ServiceIndex
.new(@model)
.operations_for(@plan.service)
.map { |id, operation| Operation.new(@model, id, operation) }
end

private
Expand Down
2 changes: 1 addition & 1 deletion gems/smithy/lib/smithy/views/client/protocol_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def structure(shape, values)
end

def required?(traits)
traits.include?('smithy.api#required') && !traits.include?('smithy.api#clientOptional')
traits.key?('smithy.api#required') && !traits.key?('smithy.api#clientOptional')
end

def stub_body
Expand Down
27 changes: 17 additions & 10 deletions gems/smithy/lib/smithy/views/client/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ class OperationShape
smithy.api#documentation
smithy.api#examples
smithy.api#paginated
smithy.ruby#skipTests
smithy.test#httpRequestTests
smithy.test#httpResponseTests
smithy.ruby#skipTests
smithy.waiters#waitable
].freeze

def initialize(service, id, shape)
Expand All @@ -107,7 +108,7 @@ def traits
end

def paginated?
@traits.include?('smithy.api#paginated')
@traits.key?('smithy.api#paginated')
end

def paginator
Expand Down Expand Up @@ -278,6 +279,7 @@ def build_shape_refs(members)
class ShapeRef
OMITTED_TRAITS = %w[
smithy.api#documentation
smithy.api#httpPayload
].freeze

PRELUDE_SHAPES_MAP = {
Expand Down Expand Up @@ -308,26 +310,31 @@ def initialize(service, member_name, shape_ref)
@service = service
@name = member_name.underscore if member_name
@member_name = member_name
@target = target(shape_ref['target'])
@traits = shape_ref.fetch('traits', {}).except(*OMITTED_TRAITS)
@shape = shape(shape_ref['target'])
@traits = shape_ref.fetch('traits', {})
end

attr_reader :name

def target(id)
def initializer
options_str = "shape: #{@shape}"
options_str += ", member_name: '#{@member_name}'" if @member_name
options_str += ", traits: #{@traits}" unless @traits.empty?
"Smithy::Schema::Shapes::ShapeRef.new(#{options_str})"
end

def shape(id)
return "Smithy::Schema::Shapes::#{PRELUDE_SHAPES_MAP[id]}" if PRELUDE_SHAPES_MAP.key?(id)

(@service.dig('rename', id) || Model::Shape.name(id)).camelize
end

def initializer
traits_str = ", traits: #{@traits}" unless @traits.empty?
member_name_str = ", member_name: '#{@member_name}'" if @member_name
"Smithy::Schema::Shapes::ShapeRef.new(shape: #{@target}#{member_name_str}#{traits_str})"
def traits
@traits.except(*OMITTED_TRAITS)
end

def http_payload?
@traits.include?('smithy.api#httpPayload')
@traits.key?('smithy.api#httpPayload')
end

def http_payload
Expand Down
10 changes: 5 additions & 5 deletions gems/smithy/lib/smithy/views/client/types_rbs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ def module_name
end

def types
@types ||= Model::ServiceIndex
.new(@model)
.shapes_for(@plan.service)
.select { |_key, shape| %w[structure union].include?(shape['type']) }
.map { |id, structure| Type.new(@model, id, structure) }
Model::ServiceIndex
.new(@model)
.shapes_for(@plan.service)
.select { |_key, shape| %w[structure union].include?(shape['type']) }
.map { |id, structure| Type.new(@model, id, structure) }
end

# @api private
Expand Down
2 changes: 1 addition & 1 deletion gems/smithy/lib/smithy/welds/auth/http_api_key_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Welds
class HttpApiKeyAuth < Weld
def for?(service)
_id, service = service.first
return false unless service.fetch('traits', {}).include?('smithy.api#httpApiKeyAuth')
return false unless service.fetch('traits', {}).key?('smithy.api#httpApiKeyAuth')

say_status :insert, 'Adding the HttpApiKeyAuth plugin', :yellow unless @plan.quiet
true
Expand Down
2 changes: 1 addition & 1 deletion gems/smithy/lib/smithy/welds/auth/http_basic_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Welds
class HttpBasicAuth < Weld
def for?(service)
_id, service = service.first
return false unless service.fetch('traits', {}).include?('smithy.api#httpBasicAuth')
return false unless service.fetch('traits', {}).key?('smithy.api#httpBasicAuth')

say_status :insert, 'Adding the HttpBasicAuth plugin', :yellow unless @plan.quiet
true
Expand Down
2 changes: 1 addition & 1 deletion gems/smithy/lib/smithy/welds/auth/http_bearer_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Welds
class HttpBearerAuth < Weld
def for?(service)
_id, service = service.first
return false unless service.fetch('traits', {}).include?('smithy.api#httpBearerAuth')
return false unless service.fetch('traits', {}).key?('smithy.api#httpBearerAuth')

say_status :insert, 'Adding the HttpBearerAuth plugin', :yellow unless @plan.quiet
true
Expand Down
2 changes: 1 addition & 1 deletion gems/smithy/lib/smithy/welds/auth/http_digest_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Welds
class HttpDigestAuth < Weld
def for?(service)
_id, service = service.first
return false unless service.fetch('traits', {}).include?('smithy.api#httpDigestAuth')
return false unless service.fetch('traits', {}).key?('smithy.api#httpDigestAuth')

say_status :insert, 'Adding the HttpDigestAuth plugin', :yellow unless @plan.quiet
true
Expand Down
2 changes: 1 addition & 1 deletion gems/smithy/lib/smithy/welds/rpc_v2_cbor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Welds
class RPCv2CBOR < Weld
def for?(service)
_, service = service.first
return false unless service.fetch('traits', {}).include?('smithy.protocols#rpcv2Cbor')
return false unless service.fetch('traits', {}).key?('smithy.protocols#rpcv2Cbor')

say_status :insert, 'Adding the RPCv2 CBOR protocol', :yellow unless @plan.quiet
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def create_synthetic_input_output_shapes(model)
def create_synthetic_input_shape(model, operation_id, operation)
input_target = operation['input']['target']
target = Model.shape(model, input_target)
return if target.fetch('traits', {}).include?('smithy.api#input') || input_target == 'smithy.api#Unit'
return if target.fetch('traits', {}).key?('smithy.api#input') || input_target == 'smithy.api#Unit'

input_target = new_shape_id(model, operation_id, 'Input')
operation['input']['target'] = input_target
Expand All @@ -35,7 +35,7 @@ def create_synthetic_input_shape(model, operation_id, operation)
def create_synthetic_output_shape(model, operation_id, operation)
output_target = operation['output']['target']
target = Model.shape(model, output_target)
return if target.fetch('traits', {}).include?('smithy.api#output') || output_target == 'smithy.api#Unit'
return if target.fetch('traits', {}).key?('smithy.api#output') || output_target == 'smithy.api#Unit'

output_target = new_shape_id(model, operation_id, 'Output')
operation['output']['target'] = output_target
Expand Down
2 changes: 1 addition & 1 deletion projections/weather/lib/weather/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ module Schema
operation.name = "GetForecast"
operation.input = Smithy::Schema::Shapes::ShapeRef.new(shape: GetForecastInput)
operation.output = Smithy::Schema::Shapes::ShapeRef.new(shape: GetForecastOutput)
operation.traits = {"smithy.api#readonly" => {}, "smithy.waiters#waitable" => {"ForecastExists" => {"documentation" => "Waits for a forecast to be available.", "acceptors" => [{"state" => "success", "matcher" => {"success" => true}}]}}}
operation.traits = {"smithy.api#readonly" => {}}
end)
service.add_operation(:list_cities, Smithy::Schema::Shapes::OperationShape.new do |operation|
operation.id = "example.weather#ListCities"
Expand Down