Skip to content

Commit

Permalink
Slim down vise and add new tests (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
mullermp authored Dec 20, 2024
1 parent 82bf668 commit 03d1659
Show file tree
Hide file tree
Showing 29 changed files with 208 additions and 473 deletions.
1 change: 1 addition & 0 deletions gems/smithy/lib/smithy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class << self
# Generates a Ruby artifact from a Smithy model.
# @param [Plan] plan The plan to generate the artifact from.
def smith(plan)
plan.welds.each { |weld| weld.preprocess(plan.model) }
artifact = Smithy::Forge.forge(plan)
plan.polishes.each { |polish| polish.polish(artifact) }
artifact
Expand Down
5 changes: 3 additions & 2 deletions gems/smithy/lib/smithy/anvil/client/views/client_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def gem_version
end

def operations
Vise::Model.operations(@model).map { |id, shape| Operation.new(id, shape) }
service = Vise::ServiceIndex.new(@model).service
Vise::OperationIndex.new(@model).for(service).map { |id, shape| Operation.new(id, shape) }
end

# @api private
Expand All @@ -47,7 +48,7 @@ def documentation
end

def name
@operation.name.underscore
Vise::Shape.name(@id).underscore
end
end
end
Expand Down
9 changes: 4 additions & 5 deletions gems/smithy/lib/smithy/anvil/client/views/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ def namespace
end

def errors
@model
.shapes
.select { |_key, shape| shape.traits&.any? { |_id, trait| trait.id == 'smithy.api#error' } }
@model['shapes']
.select { |_key, shape| shape['traits']&.any? { |id, _trait| id == 'smithy.api#error' } }
.map { |id, structure| Error.new(id, structure) }
end

Expand All @@ -35,11 +34,11 @@ def documentation
end

def name
@structure.name
Vise::Shape.name(@id)
end

def member_names
@structure.members.keys.map(&:underscore)
@structure['members'].keys.map(&:underscore)
end
end
end
Expand Down
9 changes: 4 additions & 5 deletions gems/smithy/lib/smithy/anvil/client/views/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ def namespace
end

def types
@model
.shapes
.select { |_key, shape| shape.is_a?(Vise::StructureShape) }
@model['shapes']
.select { |_key, shape| shape['type'] == 'structure' }
.map { |id, structure| Type.new(id, structure) }
end

Expand All @@ -35,11 +34,11 @@ def documentation
end

def name
@structure.name
Vise::Shape.name(@id)
end

def member_names
@structure.members.keys.map(&:underscore)
@structure['members'].keys.map(&:underscore)
end
end
end
Expand Down
8 changes: 3 additions & 5 deletions gems/smithy/lib/smithy/plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@ class Plan
# @param [Symbol] type The type of code to generate, either :client, :server, or :types.
# @param [Hash] options The options passed to the generator.
def initialize(model, type, options = {})
@model = model
@type = type
@options = options

Welds.load!(self)
@welds = Welds.for(model)
Polishes.load!(self)
@welds = Welds.for(model)
@polishes = Polishes.for(model)

@welds.each { |weld| weld.preprocess(model) }
@model = Vise::Model.new(model)
end

# @return [Vise::Model] The API model wrapped by Vise.
# @return [Hash] The API model as a JSON hash.
attr_reader :model

# @return [Symbol] The type of code to generate.
Expand Down
17 changes: 1 addition & 16 deletions gems/smithy/lib/smithy/vise.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
# frozen_string_literal: true

require_relative 'vise/shape'
require_relative 'vise/operation_index'
require_relative 'vise/service_index'

require_relative 'vise/shape'
require_relative 'vise/trait'

require_relative 'vise/enum_shape'
require_relative 'vise/int_enum_shape'
require_relative 'vise/list_shape'
require_relative 'vise/map_shape'
require_relative 'vise/member_shape'
require_relative 'vise/operation_shape'
require_relative 'vise/resource_shape'
require_relative 'vise/service_shape'
require_relative 'vise/structure_shape'
require_relative 'vise/union_shape'

require_relative 'vise/model'

module Smithy
# A module that parses the Smithy JSON model.
module Vise; end
Expand Down
25 changes: 0 additions & 25 deletions gems/smithy/lib/smithy/vise/enum_shape.rb

This file was deleted.

25 changes: 0 additions & 25 deletions gems/smithy/lib/smithy/vise/int_enum_shape.rb

This file was deleted.

17 changes: 0 additions & 17 deletions gems/smithy/lib/smithy/vise/list_shape.rb

This file was deleted.

21 changes: 0 additions & 21 deletions gems/smithy/lib/smithy/vise/map_shape.rb

This file was deleted.

25 changes: 0 additions & 25 deletions gems/smithy/lib/smithy/vise/member_shape.rb

This file was deleted.

53 changes: 0 additions & 53 deletions gems/smithy/lib/smithy/vise/model.rb

This file was deleted.

28 changes: 17 additions & 11 deletions gems/smithy/lib/smithy/vise/operation_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@

module Smithy
module Vise
# Finds all operations in a service.
# Finds all operation shapes in a service.
class OperationIndex
def initialize(shapes)
@shapes = shapes
RESOURCE_LIFECYCLE_KEYS = %w[create put read update delete list].freeze

# @param [Hash] model Model
def initialize(model)
@shapes = model['shapes']
end

# @param [Hash] service Service shape
# @return [Hash<String, Hash>] The operations for the service.
def for(service)
operations = {}
_id, service = service.first
parse_service_operations(service, operations)
parse_service_resources(service, operations)
operations.sort_by { |k, _v| k }.to_h
Expand All @@ -18,14 +24,14 @@ def for(service)
private

def parse_service_operations(service, operations)
service.operations&.collect do |shape|
service['operations']&.collect do |shape|
id = shape['target']
operations[id] = @shapes[id]
end
end

def parse_service_resources(service, operations)
service.resources&.collect do |shape|
service['resources']&.collect do |shape|
id = shape['target']
parse_resource(@shapes[id], operations)
end
Expand All @@ -36,30 +42,30 @@ def parse_resource(resource, operations)
parse_resource_operations(resource, operations)
parse_resource_collection_operations(resource, operations)

resource.resources&.collect do |shape|
resource['resources']&.collect do |shape|
id = shape['target']
parse_resource(@shapes[id], operations)
end
end

def parse_lifecycles(resource, operations)
resource.lifecycle_operations.each_value do |data|
next unless data
RESOURCE_LIFECYCLE_KEYS.each do |key|
next unless resource[key]

id = data['target']
id = resource[key]['target']
operations[id] = @shapes[id]
end
end

def parse_resource_operations(resource, operations)
resource.operations&.collect do |shape|
resource['operations']&.collect do |shape|
id = shape['target']
operations[id] = @shapes[id]
end
end

def parse_resource_collection_operations(resource, operations)
resource.collection_operations&.collect do |shape|
resource['collectionOperations']&.collect do |shape|
id = shape['target']
operations[id] = @shapes[id]
end
Expand Down
Loading

0 comments on commit 03d1659

Please sign in to comment.