boto-rb 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/lib/boto/configuration.rb +113 -0
- data/lib/boto/endpoint/boolean.rb +16 -0
- data/lib/boto/endpoint/endpoint.rb +227 -0
- data/lib/boto/endpoint/params.rb +100 -0
- data/lib/boto/http/app.rb +283 -0
- data/lib/boto/http/error.rb +121 -0
- data/lib/boto/http/problem.rb +27 -0
- data/lib/boto/http/request_body.rb +59 -0
- data/lib/boto/http/router.rb +168 -0
- data/lib/boto/openapi/operation.rb +116 -0
- data/lib/boto/openapi/schema_catalog.rb +32 -0
- data/lib/boto/openapi.rb +107 -0
- data/lib/boto/plugin/stack.rb +17 -0
- data/lib/boto/plugin.rb +53 -0
- data/lib/boto/schema/compiler.rb +103 -0
- data/lib/boto/schema.rb +172 -0
- data/lib/boto/solution/boot.rb +83 -0
- data/lib/boto/solution/botofile.rb +95 -0
- data/lib/boto/solution/isolation/scanner.rb +151 -0
- data/lib/boto/solution/isolation.rb +79 -0
- data/lib/boto/solution/isolation_error.rb +56 -0
- data/lib/boto/solution/project.rb +24 -0
- data/lib/boto/solution/project_loader.rb +55 -0
- data/lib/boto/solution/solution.rb +84 -0
- data/lib/boto/types/coercion.rb +61 -0
- data/lib/boto/types/constraints.rb +121 -0
- data/lib/boto/types/enum.rb +28 -0
- data/lib/boto/types/invalid.rb +27 -0
- data/lib/boto/types/list.rb +56 -0
- data/lib/boto/types/scalar.rb +73 -0
- data/lib/boto/types/schema_ref.rb +46 -0
- data/lib/boto/types.rb +31 -0
- data/lib/boto/version.rb +5 -0
- data/lib/boto.rb +17 -0
- metadata +103 -0
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module OpenAPI
|
|
5
|
+
# The operation object of one endpoint: its parameters, body and responses.
|
|
6
|
+
class Operation
|
|
7
|
+
# auth: the :auth plugin guarding the endpoint, or nil. secured: whether the document declares any security.
|
|
8
|
+
def initialize(endpoint, auth: nil, secured: false)
|
|
9
|
+
@endpoint = endpoint
|
|
10
|
+
@auth = auth
|
|
11
|
+
@secured = secured
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def to_h
|
|
15
|
+
operation = { "operationId" => operation_id }
|
|
16
|
+
operation["summary"] = @endpoint.summary_text if @endpoint.summary_text
|
|
17
|
+
operation["parameters"] = parameters unless definitions.empty?
|
|
18
|
+
operation["requestBody"] = request_body if @endpoint.body_schema
|
|
19
|
+
operation["security"] = security if @secured
|
|
20
|
+
operation["responses"] = responses
|
|
21
|
+
operation
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
# Empty list = public: the document has no global security, so each operation states its own.
|
|
27
|
+
def security
|
|
28
|
+
return [] unless @auth
|
|
29
|
+
|
|
30
|
+
[{ OpenAPI.scheme_name(@auth.strategy) => [] }]
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def operation_id
|
|
34
|
+
@endpoint.name.split(NAMESPACE_SEPARATOR).drop(1).join
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def definitions
|
|
38
|
+
@endpoint.params.definitions
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def parameters
|
|
42
|
+
definitions.map { |definition| parameter(definition) }
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def parameter(definition)
|
|
46
|
+
path = path_param_names.include?(definition.name)
|
|
47
|
+
{
|
|
48
|
+
"name" => definition.key,
|
|
49
|
+
"in" => path ? PATH_LOCATION : QUERY_LOCATION,
|
|
50
|
+
"required" => path || definition.required,
|
|
51
|
+
"schema" => parameter_schema(definition)
|
|
52
|
+
}
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def parameter_schema(definition)
|
|
56
|
+
schema = definition.type.json_schema
|
|
57
|
+
return schema if definition.default.equal?(Params::MISSING)
|
|
58
|
+
|
|
59
|
+
schema.merge("default" => definition.default)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def request_body
|
|
63
|
+
{ "required" => true, "content" => { JSON_CONTENT => { "schema" => @endpoint.body_schema.json_schema_ref } } }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def responses
|
|
67
|
+
result = { status.to_s => success_response }
|
|
68
|
+
error_codes.each { |code| result[code.to_s] = problem_response(code) }
|
|
69
|
+
result
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def status
|
|
73
|
+
@endpoint.default_status
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def success_response
|
|
77
|
+
response = { "description" => title_for(status) }
|
|
78
|
+
return response if status == NO_CONTENT
|
|
79
|
+
|
|
80
|
+
response.merge("content" => { JSON_CONTENT => { "schema" => response_schema } })
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def response_schema
|
|
84
|
+
schema = @endpoint.return_schema
|
|
85
|
+
return {} unless schema
|
|
86
|
+
|
|
87
|
+
ref = schema.json_schema_ref
|
|
88
|
+
@endpoint.returns_list? ? { "type" => ARRAY_TYPE, "items" => ref } : ref
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def error_codes
|
|
92
|
+
(@endpoint.error_statuses + implicit_error_codes).uniq.sort
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def implicit_error_codes
|
|
96
|
+
[(UNPROCESSABLE if validated?), (NOT_FOUND unless path_param_names.empty?), (UNAUTHORIZED if @auth)].compact
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def validated?
|
|
100
|
+
!definitions.empty? || !@endpoint.body_schema.nil?
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def path_param_names
|
|
104
|
+
@endpoint.path_param_names
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def problem_response(code)
|
|
108
|
+
{ "description" => title_for(code), "content" => { PROBLEM_CONTENT => { "schema" => PROBLEM_REF } } }
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def title_for(code)
|
|
112
|
+
Rack::Utils::HTTP_STATUS_CODES.fetch(code, UNKNOWN_STATUS_TITLE)
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module OpenAPI
|
|
5
|
+
# Every schema reachable from the roots, once each, keyed by schema_name.
|
|
6
|
+
class SchemaCatalog
|
|
7
|
+
def self.collect(roots)
|
|
8
|
+
found = {}
|
|
9
|
+
pending = roots.dup
|
|
10
|
+
until pending.empty?
|
|
11
|
+
schema = pending.shift
|
|
12
|
+
existing = found[schema.schema_name]
|
|
13
|
+
next if existing.equal?(schema)
|
|
14
|
+
raise ArgumentError, "two different schemas are named #{schema.schema_name}" if existing
|
|
15
|
+
|
|
16
|
+
found[schema.schema_name] = schema
|
|
17
|
+
pending.concat(nested_schemas(schema))
|
|
18
|
+
end
|
|
19
|
+
found
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Schema#nested_schemas is private, so the fields are read here through the public API.
|
|
23
|
+
def self.nested_schemas(schema)
|
|
24
|
+
schema.fields.flat_map { |field| [field.declared_type].flatten }.select do |type|
|
|
25
|
+
Types::SchemaRef.schema?(type)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private_class_method :nested_schemas
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
data/lib/boto/openapi.rb
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rack/utils"
|
|
4
|
+
|
|
5
|
+
module Boto
|
|
6
|
+
# Builds an OpenAPI 3.1 document from an app's endpoints and schemas. Pure: it reads the
|
|
7
|
+
# declarations and returns a Hash, without IO, so it runs at boot or from a CLI command.
|
|
8
|
+
module OpenAPI
|
|
9
|
+
SPEC_VERSION = "3.1.0"
|
|
10
|
+
# OpenAPI 3.1 has no QUERY operation; 3.2.0 adds the Path Item `query` field.
|
|
11
|
+
QUERY_SPEC_VERSION = "3.2.0"
|
|
12
|
+
QUERY_VERB = "QUERY"
|
|
13
|
+
JSON_CONTENT = "application/json"
|
|
14
|
+
PROBLEM_CONTENT = "application/problem+json"
|
|
15
|
+
PROBLEM_SCHEMA_NAME = "Problem"
|
|
16
|
+
PROBLEM_REF = { "$ref" => "#/components/schemas/#{PROBLEM_SCHEMA_NAME}" }.freeze
|
|
17
|
+
PROBLEM_SCHEMA = {
|
|
18
|
+
"type" => "object",
|
|
19
|
+
"properties" => {
|
|
20
|
+
"type" => { "type" => "string" },
|
|
21
|
+
"title" => { "type" => "string" },
|
|
22
|
+
"status" => { "type" => "integer" },
|
|
23
|
+
"detail" => { "type" => "string" },
|
|
24
|
+
"errors" => {
|
|
25
|
+
"type" => "array",
|
|
26
|
+
"items" => {
|
|
27
|
+
"type" => "object",
|
|
28
|
+
"properties" => { "param" => { "type" => "string" }, "message" => { "type" => "string" } }
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"required" => %w[type title status]
|
|
33
|
+
}.freeze
|
|
34
|
+
|
|
35
|
+
PATH_PARAM = /:(\w+)/
|
|
36
|
+
PATH_PARAM_TEMPLATE = '{\1}'
|
|
37
|
+
NAMESPACE_SEPARATOR = "::"
|
|
38
|
+
PATH_LOCATION = "path"
|
|
39
|
+
QUERY_LOCATION = "query"
|
|
40
|
+
ARRAY_TYPE = "array"
|
|
41
|
+
NO_CONTENT = 204
|
|
42
|
+
NOT_FOUND = 404
|
|
43
|
+
UNAUTHORIZED = 401
|
|
44
|
+
UNPROCESSABLE = 422
|
|
45
|
+
UNKNOWN_STATUS_TITLE = "Unknown Status"
|
|
46
|
+
CAMEL_WORD_BOUNDARY = /([a-z\d])([A-Z])/
|
|
47
|
+
CAMEL_ACRONYM_BOUNDARY = /([A-Z]+)([A-Z][a-z])/
|
|
48
|
+
SNAKE_JOIN = '\1_\2'
|
|
49
|
+
|
|
50
|
+
def self.generate(app, title:, version:)
|
|
51
|
+
endpoints = app.endpoints
|
|
52
|
+
guards = endpoints.to_h { |endpoint| [endpoint, app.auth_for(endpoint)] }
|
|
53
|
+
{
|
|
54
|
+
"openapi" => spec_version(endpoints),
|
|
55
|
+
"info" => { "title" => title, "version" => version },
|
|
56
|
+
"paths" => paths(endpoints, guards),
|
|
57
|
+
"components" => components(endpoints, guards)
|
|
58
|
+
}
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def self.paths(endpoints, guards)
|
|
62
|
+
sorted = endpoints.sort_by { |endpoint| [endpoint.route.last, endpoint.route.first] }
|
|
63
|
+
secured = guards.each_value.any?
|
|
64
|
+
sorted.each_with_object({}) do |endpoint, result|
|
|
65
|
+
verb, path = endpoint.route
|
|
66
|
+
operation = Operation.new(endpoint, auth: guards[endpoint], secured: secured).to_h
|
|
67
|
+
(result[openapi_path(path)] ||= {})[verb.downcase] = operation
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# The name of a strategy's security scheme: its class name in snake_case (Auth::ApiKey -> "api_key").
|
|
72
|
+
def self.scheme_name(strategy)
|
|
73
|
+
class_name = strategy.class.name.split(NAMESPACE_SEPARATOR).last
|
|
74
|
+
class_name.gsub(CAMEL_ACRONYM_BOUNDARY, SNAKE_JOIN).gsub(CAMEL_WORD_BOUNDARY, SNAKE_JOIN).downcase
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def self.components(endpoints, guards)
|
|
78
|
+
components = { "schemas" => schemas(endpoints) }
|
|
79
|
+
schemes = security_schemes(guards.values.compact)
|
|
80
|
+
components["securitySchemes"] = schemes unless schemes.empty?
|
|
81
|
+
components
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def self.security_schemes(auths)
|
|
85
|
+
auths.map { |auth| [scheme_name(auth.strategy), auth.strategy.security_scheme] }
|
|
86
|
+
.uniq.sort_by(&:first).to_h
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def self.openapi_path(path)
|
|
90
|
+
path.gsub(PATH_PARAM, PATH_PARAM_TEMPLATE)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def self.schemas(endpoints)
|
|
94
|
+
roots = endpoints.flat_map { |endpoint| [endpoint.body_schema, endpoint.return_schema] }.compact
|
|
95
|
+
named = SchemaCatalog.collect(roots).transform_values(&:json_schema)
|
|
96
|
+
raise ArgumentError, "schema name #{PROBLEM_SCHEMA_NAME} is reserved for errors" if named.key?(PROBLEM_SCHEMA_NAME)
|
|
97
|
+
|
|
98
|
+
named.merge(PROBLEM_SCHEMA_NAME => PROBLEM_SCHEMA).sort_by(&:first).to_h
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def self.spec_version(endpoints)
|
|
102
|
+
endpoints.any? { |endpoint| endpoint.route.first == QUERY_VERB } ? QUERY_SPEC_VERSION : SPEC_VERSION
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
private_class_method :spec_version
|
|
106
|
+
end
|
|
107
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
class Plugin
|
|
5
|
+
# Chains plugins around an inner app. The first plugin is the outermost one.
|
|
6
|
+
module Stack
|
|
7
|
+
def self.build(plugins, inner)
|
|
8
|
+
plugins.reverse.reduce(inner) { |app, plugin| wrap(plugin, app) }
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def self.wrap(plugin, app)
|
|
12
|
+
->(env) { plugin.call(env, app) }
|
|
13
|
+
end
|
|
14
|
+
private_class_method :wrap
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
data/lib/boto/plugin.rb
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
# Base class of project plugins. A plugin wraps the app like Rack middleware: it either
|
|
5
|
+
# calls app.call(env) to continue, or returns a Rack response itself.
|
|
6
|
+
class Plugin
|
|
7
|
+
ORDER = %i[hosts cors secure_headers sessions csrf auth rate_limit].freeze
|
|
8
|
+
SCOPES = %i[global endpoint].freeze
|
|
9
|
+
DEFAULT_SCOPE = :endpoint
|
|
10
|
+
|
|
11
|
+
class << self
|
|
12
|
+
def scope(value = nil)
|
|
13
|
+
return @scope || DEFAULT_SCOPE if value.nil?
|
|
14
|
+
raise ArgumentError, "unknown scope #{value.inspect}" unless SCOPES.include?(value)
|
|
15
|
+
|
|
16
|
+
@scope = value
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def plugin_name
|
|
20
|
+
underscore(name.split("::").last).to_sym
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def order_index
|
|
24
|
+
ORDER.index(plugin_name) || ORDER.size
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
def underscore(camel_case)
|
|
30
|
+
camel_case.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
attr_reader :env_name
|
|
35
|
+
|
|
36
|
+
def initialize(env_name:, **options)
|
|
37
|
+
@env_name = env_name
|
|
38
|
+
configure(**options)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def call(_env, _app)
|
|
42
|
+
raise NotImplementedError, "#{self.class} must implement #call"
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def development?
|
|
46
|
+
env_name == "development"
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
private
|
|
50
|
+
|
|
51
|
+
def configure = nil
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
class Schema
|
|
5
|
+
# Writes the per-class methods as source. Each field gets its own direct accessor,
|
|
6
|
+
# so a request runs no dynamic dispatch. Generated names are field names, validated by FIELD_NAME.
|
|
7
|
+
class Compiler
|
|
8
|
+
def initialize(schema)
|
|
9
|
+
@schema = schema
|
|
10
|
+
@fields = schema.fields
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def compile
|
|
14
|
+
@schema.class_eval(instance_source, __FILE__, __LINE__)
|
|
15
|
+
@schema.singleton_class.class_eval(class_source, __FILE__, __LINE__)
|
|
16
|
+
self
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
def instance_source
|
|
22
|
+
<<~RUBY
|
|
23
|
+
def initialize(#{value_names.join(", ")})
|
|
24
|
+
#{assignments}
|
|
25
|
+
freeze
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def to_h
|
|
29
|
+
#{hash_literal { |field, _index| "__boto_plain(@#{field.name})" }}
|
|
30
|
+
end
|
|
31
|
+
RUBY
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def class_source
|
|
35
|
+
<<~RUBY
|
|
36
|
+
def coerce(input)
|
|
37
|
+
__boto_require_object(input)
|
|
38
|
+
errors = nil
|
|
39
|
+
#{coerce_blocks}
|
|
40
|
+
raise ::Boto::Types::Invalid.new(errors) if errors
|
|
41
|
+
|
|
42
|
+
new(#{value_names.join(", ")})
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def serialize(object)
|
|
46
|
+
object.is_a?(Hash) ? __boto_serialize_hash(object) : __boto_serialize_object(object)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
#{serializer_methods}
|
|
50
|
+
RUBY
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Parameters are positional (v0, v1...) so a field named like a Ruby keyword cannot break the source.
|
|
54
|
+
def value_names
|
|
55
|
+
@fields.each_index.map { |index| value_name(index) }
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def value_name(index)
|
|
59
|
+
"v#{index}"
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def assignments
|
|
63
|
+
@fields.each_with_index.map { |field, index| "@#{field.name} = #{value_name(index)}" }.join("\n")
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def coerce_blocks
|
|
67
|
+
@fields.each_index.map { |index| coerce_block(index) }.join
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def coerce_block(index)
|
|
71
|
+
<<~RUBY
|
|
72
|
+
begin
|
|
73
|
+
#{value_name(index)} = __boto_coerce_field(@fields[#{index}], input)
|
|
74
|
+
rescue ::Boto::Types::Invalid => e
|
|
75
|
+
errors = __boto_add_errors(errors, @fields[#{index}], e)
|
|
76
|
+
end
|
|
77
|
+
RUBY
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def serializer_methods
|
|
81
|
+
<<~RUBY
|
|
82
|
+
def __boto_serialize_object(object)
|
|
83
|
+
#{hash_literal { |field, index| "__boto_dump(@fields[#{index}], object.#{field.name})" }}
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def __boto_serialize_hash(hash)
|
|
87
|
+
#{hash_literal { |field, index| "__boto_dump(@fields[#{index}], __boto_read_output(hash, @fields[#{index}]))" }}
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
private :__boto_serialize_object, :__boto_serialize_hash
|
|
91
|
+
RUBY
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def hash_literal(&entry)
|
|
95
|
+
"{ #{pairs(&entry)} }"
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def pairs
|
|
99
|
+
@fields.each_with_index.map { |field, index| "#{field.name}: #{yield(field, index)}" }.join(", ")
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
data/lib/boto/schema.rb
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
# A typed shape for input (coerce) and output (serialize). Subclasses declare fields;
|
|
5
|
+
# compile! generates the per-request methods once, so a request runs direct accessors only.
|
|
6
|
+
class Schema
|
|
7
|
+
class OutputError < StandardError; end
|
|
8
|
+
|
|
9
|
+
FIELD_NAME = /\A[a-z_][a-z0-9_]*\z/
|
|
10
|
+
# Methods the schema calls on its instances; a field with one of these names would replace them.
|
|
11
|
+
RESERVED_NAMES = %i[class freeze hash inspect to_h].freeze
|
|
12
|
+
MUST_BE_OBJECT = "must be an object"
|
|
13
|
+
REQUIRED_MESSAGE = "is required"
|
|
14
|
+
REF_PREFIX = "#/components/schemas/"
|
|
15
|
+
NAMESPACE_SEPARATOR = "::"
|
|
16
|
+
|
|
17
|
+
Field = Data.define(:name, :key, :type, :required, :declared_type)
|
|
18
|
+
|
|
19
|
+
def self.field(name, type, required: true, **constraints)
|
|
20
|
+
raise FrozenError, "#{self} is already compiled" if compiled?
|
|
21
|
+
|
|
22
|
+
name = validate_field_name!(name)
|
|
23
|
+
definition = Field.new(
|
|
24
|
+
name:, key: name.to_s.freeze, type: Types.build(type, **constraints),
|
|
25
|
+
required:, declared_type: type
|
|
26
|
+
)
|
|
27
|
+
fields << definition
|
|
28
|
+
attr_reader name
|
|
29
|
+
name
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def self.fields
|
|
33
|
+
@fields ||= []
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def self.compiled?
|
|
37
|
+
@compiled == true
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def self.compile!
|
|
41
|
+
# @compiling guards schemas that reference each other (A -> B -> A).
|
|
42
|
+
return self if compiled? || @compiling
|
|
43
|
+
|
|
44
|
+
@compiling = true
|
|
45
|
+
nested_schemas.each(&:compile!)
|
|
46
|
+
fields.freeze
|
|
47
|
+
Compiler.new(self).compile
|
|
48
|
+
@compiled = true
|
|
49
|
+
self
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Before compile! these call compile!, which defines the generated versions that replace them.
|
|
53
|
+
def self.coerce(input) = compile!.coerce(input)
|
|
54
|
+
|
|
55
|
+
def self.serialize(object) = compile!.serialize(object)
|
|
56
|
+
|
|
57
|
+
# Memoized only once compiled, when the fields can no longer change.
|
|
58
|
+
def self.json_schema
|
|
59
|
+
return build_json_schema.freeze unless compiled?
|
|
60
|
+
|
|
61
|
+
@json_schema ||= build_json_schema.freeze
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def self.json_schema_ref
|
|
65
|
+
@json_schema_ref ||= { "$ref" => "#{REF_PREFIX}#{schema_name}" }.freeze
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Drops the first namespace segment: Orders::Admin::UserSchema -> AdminUserSchema.
|
|
69
|
+
def self.schema_name
|
|
70
|
+
segments = name.to_s.split(NAMESPACE_SEPARATOR)
|
|
71
|
+
segments.size > 1 ? segments.drop(1).join : name.to_s
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def self.build_json_schema
|
|
75
|
+
schema = { "type" => "object", "properties" => json_properties }
|
|
76
|
+
required = fields.select(&:required).map(&:key)
|
|
77
|
+
schema["required"] = required.freeze unless required.empty?
|
|
78
|
+
schema
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def self.json_properties
|
|
82
|
+
fields.to_h { |field| [field.key, field.type.json_schema] }.freeze
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def self.nested_schemas
|
|
86
|
+
fields.flat_map { |field| schemas_in(field.declared_type) }.uniq
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def self.schemas_in(type)
|
|
90
|
+
case type
|
|
91
|
+
when Array then type.flat_map { |item| schemas_in(item) }
|
|
92
|
+
when Class then Types::SchemaRef.schema?(type) ? [type] : []
|
|
93
|
+
else []
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def self.validate_field_name!(name)
|
|
98
|
+
raise ArgumentError, "invalid field name #{name.inspect}" unless FIELD_NAME.match?(name.to_s)
|
|
99
|
+
|
|
100
|
+
symbol = name.to_sym
|
|
101
|
+
raise ArgumentError, "reserved field name #{name}" if RESERVED_NAMES.include?(symbol)
|
|
102
|
+
raise ArgumentError, "duplicate field #{name}" if fields.any? { |field| field.name == symbol }
|
|
103
|
+
|
|
104
|
+
symbol
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Helpers below are private class methods; the generated code calls them with an implicit receiver.
|
|
108
|
+
def self.__boto_require_object(input)
|
|
109
|
+
raise Types::Invalid.single(MUST_BE_OBJECT) unless input.is_a?(Hash)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def self.__boto_coerce_field(field, input)
|
|
113
|
+
raw = __boto_read_input(input, field)
|
|
114
|
+
return field.type.coerce(raw) unless raw.nil?
|
|
115
|
+
raise Types::Invalid.single(REQUIRED_MESSAGE) if field.required
|
|
116
|
+
|
|
117
|
+
nil
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def self.__boto_add_errors(errors, field, invalid)
|
|
121
|
+
errors ||= []
|
|
122
|
+
errors.concat(invalid.errors.map { |error| error.merge(path: Types.join_path(field.key, error[:path])) })
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def self.__boto_dump(field, value)
|
|
126
|
+
return field.type.dump(value) unless value.nil?
|
|
127
|
+
raise OutputError, "#{schema_name}.#{field.name} is required" if field.required
|
|
128
|
+
|
|
129
|
+
nil
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def self.__boto_read_input(hash, field)
|
|
133
|
+
hash.key?(field.key) ? hash[field.key] : hash[field.name]
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def self.__boto_read_output(hash, field)
|
|
137
|
+
hash.key?(field.name) ? hash[field.name] : hash[field.key]
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
private_class_method :validate_field_name!, :build_json_schema, :json_properties,
|
|
141
|
+
:nested_schemas, :schemas_in,
|
|
142
|
+
:__boto_require_object, :__boto_coerce_field, :__boto_add_errors,
|
|
143
|
+
:__boto_dump, :__boto_read_input, :__boto_read_output
|
|
144
|
+
|
|
145
|
+
def ==(other)
|
|
146
|
+
other.class == self.class && other.to_h == to_h
|
|
147
|
+
end
|
|
148
|
+
alias eql? ==
|
|
149
|
+
|
|
150
|
+
def hash
|
|
151
|
+
[self.class, to_h].hash
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def inspect
|
|
155
|
+
"#<#{self.class.schema_name} #{inspect_pairs}>"
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
private
|
|
159
|
+
|
|
160
|
+
def inspect_pairs
|
|
161
|
+
to_h.map { |key, value| "#{key}=#{value.inspect}" }.join(" ")
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def __boto_plain(value)
|
|
165
|
+
case value
|
|
166
|
+
when Schema then value.to_h
|
|
167
|
+
when Array then value.map { |item| __boto_plain(item) }
|
|
168
|
+
else value
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
end
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "set"
|
|
4
|
+
|
|
5
|
+
module Boto
|
|
6
|
+
# Loads a solution's projects in dependency order and builds the Rack app of one project.
|
|
7
|
+
class Boot
|
|
8
|
+
CONFIG_FILE = File.join("config", "app.rb")
|
|
9
|
+
SKIP_ENV = "BOTO_SKIP_ISOLATION"
|
|
10
|
+
SKIP_VALUE = "1"
|
|
11
|
+
SKIP_WARNING = "Boto: project isolation check skipped (BOTO_SKIP_ISOLATION=1)"
|
|
12
|
+
|
|
13
|
+
def initialize(solution, root:, env: ENV, warn_output: $stderr)
|
|
14
|
+
@solution = solution
|
|
15
|
+
@root = root
|
|
16
|
+
@env = env
|
|
17
|
+
@warn_output = warn_output
|
|
18
|
+
@loaders = {}
|
|
19
|
+
@checked = Set.new
|
|
20
|
+
@skip_warned = false
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def checked_projects
|
|
24
|
+
@checked.dup.freeze
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def load(name)
|
|
28
|
+
verify_isolation(name)
|
|
29
|
+
loaders = load_order(name).map { |item| loader_for(item) }
|
|
30
|
+
loaders.each(&:setup)
|
|
31
|
+
loaders.each(&:eager_load)
|
|
32
|
+
loader_for(name).namespace
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def app(name, **app_options)
|
|
36
|
+
env_name = app_options.fetch(:env_name) { ENV.fetch("BOTO_ENV", "production") }
|
|
37
|
+
namespace = load(name)
|
|
38
|
+
config = Configuration.load(config_path(name), env_name: env_name)
|
|
39
|
+
App.new(**app_options, env_name: env_name, namespace: namespace, plugins: config.plugins)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def shutdown
|
|
43
|
+
@loaders.each_value(&:unload)
|
|
44
|
+
@loaders.clear
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
private
|
|
48
|
+
|
|
49
|
+
def verify_isolation(name)
|
|
50
|
+
pending = load_order(name).reject { @checked.include?(_1) }
|
|
51
|
+
return if pending.empty?
|
|
52
|
+
return warn_skipped if skip_isolation?
|
|
53
|
+
|
|
54
|
+
violations = Isolation.check(@solution, root: @root, projects: pending)
|
|
55
|
+
raise IsolationError.new(violations) unless violations.empty?
|
|
56
|
+
|
|
57
|
+
@checked.merge(pending)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def skip_isolation?
|
|
61
|
+
@env[SKIP_ENV] == SKIP_VALUE
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def warn_skipped
|
|
65
|
+
return if @skip_warned
|
|
66
|
+
|
|
67
|
+
@skip_warned = true
|
|
68
|
+
@warn_output.puts(SKIP_WARNING)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def load_order(name)
|
|
72
|
+
@solution.dependencies_of(name) + [name]
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def loader_for(name)
|
|
76
|
+
@loaders[name] ||= ProjectLoader.new(@solution.project(name), root: @root)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def config_path(name)
|
|
80
|
+
File.join(@root, @solution.project(name).path, CONFIG_FILE)
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|