-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start derived types, benchmark, export
- Loading branch information
Showing
43 changed files
with
691 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module Taro::Export | ||
Dir[File.join(__dir__, "export", "*.rb")].each { |f| require f } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
class Taro::Export::OpenAPIv3 | ||
attr_reader :components | ||
|
||
# TODO: | ||
# - accept Taro::Rails.definitions as an argument | ||
# - get routes, params, status codes, responses etc. from each Definition | ||
# - use methods below to render their details | ||
# - support list/array type | ||
# - use json-schema gem to validate overall result against OpenAPIv3 schema | ||
def initialize | ||
@components = {} | ||
end | ||
|
||
def export_field(field) | ||
if field.type < Taro::Types::ScalarType | ||
export_scalar_field(field) | ||
else | ||
export_complex_field(field) | ||
end | ||
end | ||
|
||
def export_scalar_field(field) | ||
base = { type: field.openapi_type } | ||
base[:description] = field.description if field.description | ||
base[:default] = field.default if field.default_specified? | ||
base[:enum] = field.enum if field.enum | ||
base | ||
end | ||
|
||
def export_complex_field(field) | ||
ref = extract_component_ref(field.type) | ||
if field.description | ||
# https://github.com/OAI/OpenAPI-Specification/issues/2033 | ||
{ description: field.description, allOf: [ref] } | ||
else | ||
ref | ||
end | ||
end | ||
|
||
def extract_component_ref(type) | ||
components[:schemas] ||= {} | ||
components[:schemas][type.nesting.to_sym] ||= build_type_ref(type) | ||
{ :'$ref' => "#/components/schemas/#{type.nesting}" } | ||
end | ||
|
||
def build_type_ref(type) | ||
if type.respond_to?(:fields) # InputType or ObjectType | ||
build_object_type_ref(type) | ||
elsif type < Taro::Types::EnumType | ||
build_enum_type_ref(type) | ||
else | ||
raise NotImplementedError, "Unsupported type: #{type}" | ||
end | ||
end | ||
|
||
def build_object_type_ref(type) | ||
{ | ||
type: type.openapi_type, | ||
description: type.description, | ||
properties: type.fields.to_h { |name, f| [name, export_field(f)] }, | ||
}.compact | ||
end | ||
|
||
def build_enum_type_ref(enum) | ||
{ | ||
type: enum.item_type.openapi_type, | ||
description: enum.description, | ||
enum: enum.values, | ||
}.compact | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class NoContentType < Taro::Types::ObjectTypes::NoContentType | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,42 @@ | ||
class Taro::Types::CoerceToType | ||
def self.call(arg) | ||
return arg if arg < Taro::Types::BaseType | ||
module Taro::Types::CoerceToType | ||
class << self | ||
def call(arg) | ||
if arg.instance_of?(Array) | ||
type(arg.first).list | ||
else | ||
type(arg) | ||
end | ||
end | ||
|
||
shortcuts[arg] || raise_cast_error(arg) | ||
end | ||
private | ||
|
||
# Map some Ruby classes to built-in types to support e.g. | ||
# `returns String`, or `field { [Integer, ...] }`, etc. | ||
require 'date' | ||
def self.shortcuts | ||
@shortcuts ||= { | ||
# rubocop:disable Layout/HashAlignment - buggy cop | ||
::Date => Taro::Types::Scalar::TimestampType, | ||
::DateTime => Taro::Types::Scalar::TimestampType, | ||
::Float => Taro::Types::Scalar::FloatType, | ||
::Integer => Taro::Types::Scalar::IntegerType, | ||
::String => Taro::Types::Scalar::StringType, | ||
::Time => Taro::Types::Scalar::TimestampType, | ||
# rubocop:enable Layout/HashAlignment - buggy cop | ||
}.freeze | ||
end | ||
def type(arg) | ||
return arg if arg < Taro::Types::BaseType | ||
|
||
shortcuts[arg] || raise_cast_error(arg) | ||
end | ||
|
||
# Map some Ruby classes to built-in types to support e.g. | ||
# `returns String`, or `field { [Integer, ...] }`, etc. | ||
require 'date' | ||
def shortcuts | ||
@shortcuts ||= { | ||
# rubocop:disable Layout/HashAlignment - buggy cop | ||
::Date => Taro::Types::Scalar::TimestampType, | ||
::DateTime => Taro::Types::Scalar::TimestampType, | ||
::Float => Taro::Types::Scalar::FloatType, | ||
::Integer => Taro::Types::Scalar::IntegerType, | ||
::String => Taro::Types::Scalar::StringType, | ||
::Time => Taro::Types::Scalar::TimestampType, | ||
# rubocop:enable Layout/HashAlignment - buggy cop | ||
}.freeze | ||
end | ||
|
||
def self.raise_cast_error(arg) | ||
raise Taro::ArgumentError, <<~MSG | ||
Unsupported type: #{arg.inspect}. Must inherit from a type class | ||
or be one of #{shortcuts.keys.join(', ')}. | ||
MSG | ||
def raise_cast_error(arg) | ||
raise Taro::ArgumentError, <<~MSG | ||
Unsupported type: #{arg.inspect}. Must inherit from a type class | ||
or be one of #{shortcuts.keys.join(', ')}. | ||
MSG | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.