turbopuffer-ruby 0.2.0.pre.alpha.1
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/.ignore +2 -0
- data/CHANGELOG.md +60 -0
- data/README.md +291 -0
- data/SECURITY.md +27 -0
- data/lib/turbopuffer/client.rb +130 -0
- data/lib/turbopuffer/errors.rb +214 -0
- data/lib/turbopuffer/file_part.rb +55 -0
- data/lib/turbopuffer/internal/namespace_page.rb +86 -0
- data/lib/turbopuffer/internal/transport/base_client.rb +563 -0
- data/lib/turbopuffer/internal/transport/pooled_net_requester.rb +209 -0
- data/lib/turbopuffer/internal/type/array_of.rb +167 -0
- data/lib/turbopuffer/internal/type/base_model.rb +534 -0
- data/lib/turbopuffer/internal/type/base_page.rb +55 -0
- data/lib/turbopuffer/internal/type/boolean.rb +77 -0
- data/lib/turbopuffer/internal/type/converter.rb +300 -0
- data/lib/turbopuffer/internal/type/enum.rb +152 -0
- data/lib/turbopuffer/internal/type/file_input.rb +108 -0
- data/lib/turbopuffer/internal/type/hash_of.rb +187 -0
- data/lib/turbopuffer/internal/type/request_parameters.rb +42 -0
- data/lib/turbopuffer/internal/type/union.rb +252 -0
- data/lib/turbopuffer/internal/type/unknown.rb +81 -0
- data/lib/turbopuffer/internal/util.rb +914 -0
- data/lib/turbopuffer/internal.rb +20 -0
- data/lib/turbopuffer/models/attribute_schema.rb +19 -0
- data/lib/turbopuffer/models/attribute_schema_config.rb +48 -0
- data/lib/turbopuffer/models/attribute_type.rb +7 -0
- data/lib/turbopuffer/models/client_namespaces_params.rb +38 -0
- data/lib/turbopuffer/models/columns.rb +64 -0
- data/lib/turbopuffer/models/distance_metric.rb +19 -0
- data/lib/turbopuffer/models/full_text_search.rb +20 -0
- data/lib/turbopuffer/models/full_text_search_config.rb +72 -0
- data/lib/turbopuffer/models/id.rb +19 -0
- data/lib/turbopuffer/models/include_attributes.rb +22 -0
- data/lib/turbopuffer/models/language.rb +32 -0
- data/lib/turbopuffer/models/namespace_delete_all_params.rb +20 -0
- data/lib/turbopuffer/models/namespace_delete_all_response.rb +19 -0
- data/lib/turbopuffer/models/namespace_hint_cache_warm_params.rb +20 -0
- data/lib/turbopuffer/models/namespace_hint_cache_warm_response.rb +26 -0
- data/lib/turbopuffer/models/namespace_metadata.rb +35 -0
- data/lib/turbopuffer/models/namespace_metadata_params.rb +20 -0
- data/lib/turbopuffer/models/namespace_multi_query_params.rb +73 -0
- data/lib/turbopuffer/models/namespace_multi_query_response.rb +51 -0
- data/lib/turbopuffer/models/namespace_query_params.rb +119 -0
- data/lib/turbopuffer/models/namespace_query_response.rb +41 -0
- data/lib/turbopuffer/models/namespace_recall_params.rb +57 -0
- data/lib/turbopuffer/models/namespace_recall_response.rb +39 -0
- data/lib/turbopuffer/models/namespace_schema_params.rb +20 -0
- data/lib/turbopuffer/models/namespace_schema_response.rb +8 -0
- data/lib/turbopuffer/models/namespace_summary.rb +19 -0
- data/lib/turbopuffer/models/namespace_update_schema_params.rb +29 -0
- data/lib/turbopuffer/models/namespace_update_schema_response.rb +9 -0
- data/lib/turbopuffer/models/namespace_write_params.rb +158 -0
- data/lib/turbopuffer/models/namespace_write_response.rb +67 -0
- data/lib/turbopuffer/models/query.rb +63 -0
- data/lib/turbopuffer/models/query_billing.rb +26 -0
- data/lib/turbopuffer/models/query_performance.rb +63 -0
- data/lib/turbopuffer/models/row.rb +38 -0
- data/lib/turbopuffer/models/tokenizer.rb +17 -0
- data/lib/turbopuffer/models/vector.rb +22 -0
- data/lib/turbopuffer/models/vector_encoding.rb +16 -0
- data/lib/turbopuffer/models/write_billing.rb +26 -0
- data/lib/turbopuffer/models.rb +101 -0
- data/lib/turbopuffer/namespace.rb +22 -0
- data/lib/turbopuffer/request_options.rb +77 -0
- data/lib/turbopuffer/resources/namespaces.rb +311 -0
- data/lib/turbopuffer/version.rb +5 -0
- data/lib/turbopuffer.rb +94 -0
- data/manifest.yaml +15 -0
- data/rbi/turbopuffer/client.rbi +91 -0
- data/rbi/turbopuffer/errors.rbi +178 -0
- data/rbi/turbopuffer/file_part.rbi +37 -0
- data/rbi/turbopuffer/internal/namespace_page.rbi +22 -0
- data/rbi/turbopuffer/internal/transport/base_client.rbi +298 -0
- data/rbi/turbopuffer/internal/transport/pooled_net_requester.rbi +80 -0
- data/rbi/turbopuffer/internal/type/array_of.rbi +104 -0
- data/rbi/turbopuffer/internal/type/base_model.rbi +304 -0
- data/rbi/turbopuffer/internal/type/base_page.rbi +43 -0
- data/rbi/turbopuffer/internal/type/boolean.rbi +58 -0
- data/rbi/turbopuffer/internal/type/converter.rbi +162 -0
- data/rbi/turbopuffer/internal/type/enum.rbi +82 -0
- data/rbi/turbopuffer/internal/type/file_input.rbi +59 -0
- data/rbi/turbopuffer/internal/type/hash_of.rbi +104 -0
- data/rbi/turbopuffer/internal/type/request_parameters.rbi +31 -0
- data/rbi/turbopuffer/internal/type/union.rbi +121 -0
- data/rbi/turbopuffer/internal/type/unknown.rbi +58 -0
- data/rbi/turbopuffer/internal/util.rbi +487 -0
- data/rbi/turbopuffer/internal.rbi +18 -0
- data/rbi/turbopuffer/models/attribute_schema.rbi +17 -0
- data/rbi/turbopuffer/models/attribute_schema_config.rbi +92 -0
- data/rbi/turbopuffer/models/attribute_type.rbi +7 -0
- data/rbi/turbopuffer/models/client_namespaces_params.rbi +71 -0
- data/rbi/turbopuffer/models/columns.rbi +82 -0
- data/rbi/turbopuffer/models/distance_metric.rbi +27 -0
- data/rbi/turbopuffer/models/full_text_search.rbi +19 -0
- data/rbi/turbopuffer/models/full_text_search_config.rbi +116 -0
- data/rbi/turbopuffer/models/id.rbi +16 -0
- data/rbi/turbopuffer/models/include_attributes.rbi +24 -0
- data/rbi/turbopuffer/models/language.rbi +36 -0
- data/rbi/turbopuffer/models/namespace_delete_all_params.rbi +41 -0
- data/rbi/turbopuffer/models/namespace_delete_all_response.rbi +31 -0
- data/rbi/turbopuffer/models/namespace_hint_cache_warm_params.rbi +41 -0
- data/rbi/turbopuffer/models/namespace_hint_cache_warm_response.rbi +38 -0
- data/rbi/turbopuffer/models/namespace_metadata.rbi +54 -0
- data/rbi/turbopuffer/models/namespace_metadata_params.rbi +41 -0
- data/rbi/turbopuffer/models/namespace_multi_query_params.rbi +175 -0
- data/rbi/turbopuffer/models/namespace_multi_query_response.rbi +111 -0
- data/rbi/turbopuffer/models/namespace_query_params.rbi +241 -0
- data/rbi/turbopuffer/models/namespace_query_response.rbi +73 -0
- data/rbi/turbopuffer/models/namespace_recall_params.rbi +93 -0
- data/rbi/turbopuffer/models/namespace_recall_response.rbi +59 -0
- data/rbi/turbopuffer/models/namespace_schema_params.rbi +41 -0
- data/rbi/turbopuffer/models/namespace_schema_response.rbi +11 -0
- data/rbi/turbopuffer/models/namespace_summary.rbi +28 -0
- data/rbi/turbopuffer/models/namespace_update_schema_params.rbi +80 -0
- data/rbi/turbopuffer/models/namespace_update_schema_response.rbi +11 -0
- data/rbi/turbopuffer/models/namespace_write_params.rbi +290 -0
- data/rbi/turbopuffer/models/namespace_write_response.rbi +101 -0
- data/rbi/turbopuffer/models/query.rbi +106 -0
- data/rbi/turbopuffer/models/query_billing.rbi +46 -0
- data/rbi/turbopuffer/models/query_performance.rbi +82 -0
- data/rbi/turbopuffer/models/row.rbi +47 -0
- data/rbi/turbopuffer/models/tokenizer.rbi +22 -0
- data/rbi/turbopuffer/models/vector.rbi +22 -0
- data/rbi/turbopuffer/models/vector_encoding.rbi +22 -0
- data/rbi/turbopuffer/models/write_billing.rbi +49 -0
- data/rbi/turbopuffer/models.rbi +64 -0
- data/rbi/turbopuffer/namespace.rbi +8 -0
- data/rbi/turbopuffer/request_options.rbi +59 -0
- data/rbi/turbopuffer/resources/namespaces.rbi +243 -0
- data/rbi/turbopuffer/version.rbi +5 -0
- data/sig/turbopuffer/client.rbs +41 -0
- data/sig/turbopuffer/errors.rbs +110 -0
- data/sig/turbopuffer/file_part.rbs +21 -0
- data/sig/turbopuffer/internal/namespace_page.rbs +13 -0
- data/sig/turbopuffer/internal/transport/base_client.rbs +131 -0
- data/sig/turbopuffer/internal/transport/pooled_net_requester.rbs +45 -0
- data/sig/turbopuffer/internal/type/array_of.rbs +48 -0
- data/sig/turbopuffer/internal/type/base_model.rbs +102 -0
- data/sig/turbopuffer/internal/type/base_page.rbs +24 -0
- data/sig/turbopuffer/internal/type/boolean.rbs +26 -0
- data/sig/turbopuffer/internal/type/converter.rbs +62 -0
- data/sig/turbopuffer/internal/type/enum.rbs +32 -0
- data/sig/turbopuffer/internal/type/file_input.rbs +25 -0
- data/sig/turbopuffer/internal/type/hash_of.rbs +48 -0
- data/sig/turbopuffer/internal/type/request_parameters.rbs +19 -0
- data/sig/turbopuffer/internal/type/union.rbs +52 -0
- data/sig/turbopuffer/internal/type/unknown.rbs +26 -0
- data/sig/turbopuffer/internal/util.rbs +185 -0
- data/sig/turbopuffer/internal.rbs +9 -0
- data/sig/turbopuffer/models/attribute_schema.rbs +11 -0
- data/sig/turbopuffer/models/attribute_schema_config.rbs +45 -0
- data/sig/turbopuffer/models/attribute_type.rbs +5 -0
- data/sig/turbopuffer/models/client_namespaces_params.rbs +38 -0
- data/sig/turbopuffer/models/columns.rbs +42 -0
- data/sig/turbopuffer/models/distance_metric.rbs +17 -0
- data/sig/turbopuffer/models/full_text_search.rbs +11 -0
- data/sig/turbopuffer/models/full_text_search_config.rbs +68 -0
- data/sig/turbopuffer/models/id.rbs +11 -0
- data/sig/turbopuffer/models/include_attributes.rbs +13 -0
- data/sig/turbopuffer/models/language.rbs +48 -0
- data/sig/turbopuffer/models/namespace_delete_all_params.rbs +25 -0
- data/sig/turbopuffer/models/namespace_delete_all_response.rbs +13 -0
- data/sig/turbopuffer/models/namespace_hint_cache_warm_params.rbs +25 -0
- data/sig/turbopuffer/models/namespace_hint_cache_warm_response.rbs +17 -0
- data/sig/turbopuffer/models/namespace_metadata.rbs +30 -0
- data/sig/turbopuffer/models/namespace_metadata_params.rbs +25 -0
- data/sig/turbopuffer/models/namespace_multi_query_params.rbs +86 -0
- data/sig/turbopuffer/models/namespace_multi_query_response.rbs +53 -0
- data/sig/turbopuffer/models/namespace_query_params.rbs +125 -0
- data/sig/turbopuffer/models/namespace_query_response.rbs +39 -0
- data/sig/turbopuffer/models/namespace_recall_params.rbs +56 -0
- data/sig/turbopuffer/models/namespace_recall_response.rbs +26 -0
- data/sig/turbopuffer/models/namespace_schema_params.rbs +25 -0
- data/sig/turbopuffer/models/namespace_schema_response.rbs +8 -0
- data/sig/turbopuffer/models/namespace_summary.rbs +13 -0
- data/sig/turbopuffer/models/namespace_update_schema_params.rbs +37 -0
- data/sig/turbopuffer/models/namespace_update_schema_response.rbs +8 -0
- data/sig/turbopuffer/models/namespace_write_params.rbs +156 -0
- data/sig/turbopuffer/models/namespace_write_response.rbs +56 -0
- data/sig/turbopuffer/models/query.rbs +61 -0
- data/sig/turbopuffer/models/query_billing.rbs +25 -0
- data/sig/turbopuffer/models/query_performance.rbs +45 -0
- data/sig/turbopuffer/models/row.rbs +24 -0
- data/sig/turbopuffer/models/tokenizer.rbs +15 -0
- data/sig/turbopuffer/models/vector.rbs +13 -0
- data/sig/turbopuffer/models/vector_encoding.rbs +14 -0
- data/sig/turbopuffer/models/write_billing.rbs +27 -0
- data/sig/turbopuffer/models.rbs +61 -0
- data/sig/turbopuffer/namespace.rbs +5 -0
- data/sig/turbopuffer/request_options.rbs +36 -0
- data/sig/turbopuffer/resources/namespaces.rbs +81 -0
- data/sig/turbopuffer/version.rbs +3 -0
- metadata +251 -0
@@ -0,0 +1,209 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Turbopuffer
|
4
|
+
module Internal
|
5
|
+
module Transport
|
6
|
+
# @api private
|
7
|
+
class PooledNetRequester
|
8
|
+
extend Turbopuffer::Internal::Util::SorbetRuntimeSupport
|
9
|
+
|
10
|
+
# from the golang stdlib
|
11
|
+
# https://github.com/golang/go/blob/c8eced8580028328fde7c03cbfcb720ce15b2358/src/net/http/transport.go#L49
|
12
|
+
KEEP_ALIVE_TIMEOUT = 30
|
13
|
+
|
14
|
+
DEFAULT_MAX_CONNECTIONS = [Etc.nprocessors, 99].max
|
15
|
+
|
16
|
+
class << self
|
17
|
+
# @api private
|
18
|
+
#
|
19
|
+
# @param url [URI::Generic]
|
20
|
+
#
|
21
|
+
# @return [Net::HTTP]
|
22
|
+
def connect(url)
|
23
|
+
port =
|
24
|
+
case [url.port, url.scheme]
|
25
|
+
in [Integer, _]
|
26
|
+
url.port
|
27
|
+
in [nil, "http" | "ws"]
|
28
|
+
Net::HTTP.http_default_port
|
29
|
+
in [nil, "https" | "wss"]
|
30
|
+
Net::HTTP.https_default_port
|
31
|
+
end
|
32
|
+
|
33
|
+
Net::HTTP.new(url.host, port).tap do
|
34
|
+
_1.use_ssl = %w[https wss].include?(url.scheme)
|
35
|
+
_1.max_retries = 0
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# @api private
|
40
|
+
#
|
41
|
+
# @param conn [Net::HTTP]
|
42
|
+
# @param deadline [Float]
|
43
|
+
def calibrate_socket_timeout(conn, deadline)
|
44
|
+
timeout = deadline - Turbopuffer::Internal::Util.monotonic_secs
|
45
|
+
conn.open_timeout = conn.read_timeout = conn.write_timeout = conn.continue_timeout = timeout
|
46
|
+
end
|
47
|
+
|
48
|
+
# @api private
|
49
|
+
#
|
50
|
+
# @param request [Hash{Symbol=>Object}] .
|
51
|
+
#
|
52
|
+
# @option request [Symbol] :method
|
53
|
+
#
|
54
|
+
# @option request [URI::Generic] :url
|
55
|
+
#
|
56
|
+
# @option request [Hash{String=>String}] :headers
|
57
|
+
#
|
58
|
+
# @param blk [Proc]
|
59
|
+
#
|
60
|
+
# @yieldparam [String]
|
61
|
+
# @return [Array(Net::HTTPGenericRequest, Proc)]
|
62
|
+
def build_request(request, &blk)
|
63
|
+
method, url, headers, body = request.fetch_values(:method, :url, :headers, :body)
|
64
|
+
req = Net::HTTPGenericRequest.new(
|
65
|
+
method.to_s.upcase,
|
66
|
+
!body.nil?,
|
67
|
+
method != :head,
|
68
|
+
URI(url.to_s) # ensure we construct a URI class of the right scheme
|
69
|
+
)
|
70
|
+
|
71
|
+
headers.each { req[_1] = _2 }
|
72
|
+
|
73
|
+
case body
|
74
|
+
in nil
|
75
|
+
nil
|
76
|
+
in String
|
77
|
+
req["content-length"] ||= body.bytesize.to_s unless req["transfer-encoding"]
|
78
|
+
req.body_stream = Turbopuffer::Internal::Util::ReadIOAdapter.new(body, &blk)
|
79
|
+
in StringIO
|
80
|
+
req["content-length"] ||= body.size.to_s unless req["transfer-encoding"]
|
81
|
+
req.body_stream = Turbopuffer::Internal::Util::ReadIOAdapter.new(body, &blk)
|
82
|
+
in Pathname | IO | Enumerator
|
83
|
+
req["transfer-encoding"] ||= "chunked" unless req["content-length"]
|
84
|
+
req.body_stream = Turbopuffer::Internal::Util::ReadIOAdapter.new(body, &blk)
|
85
|
+
end
|
86
|
+
|
87
|
+
[req, req.body_stream&.method(:close)]
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
# @api private
|
92
|
+
#
|
93
|
+
# @param url [URI::Generic]
|
94
|
+
# @param deadline [Float]
|
95
|
+
# @param blk [Proc]
|
96
|
+
#
|
97
|
+
# @raise [Timeout::Error]
|
98
|
+
# @yieldparam [Net::HTTP]
|
99
|
+
private def with_pool(url, deadline:, &blk)
|
100
|
+
origin = Turbopuffer::Internal::Util.uri_origin(url)
|
101
|
+
timeout = deadline - Turbopuffer::Internal::Util.monotonic_secs
|
102
|
+
pool =
|
103
|
+
@mutex.synchronize do
|
104
|
+
@pools[origin] ||= ConnectionPool.new(size: @size) do
|
105
|
+
self.class.connect(url)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
pool.with(timeout: timeout, &blk)
|
110
|
+
end
|
111
|
+
|
112
|
+
# @api private
|
113
|
+
#
|
114
|
+
# @param request [Hash{Symbol=>Object}] .
|
115
|
+
#
|
116
|
+
# @option request [Symbol] :method
|
117
|
+
#
|
118
|
+
# @option request [URI::Generic] :url
|
119
|
+
#
|
120
|
+
# @option request [Hash{String=>String}] :headers
|
121
|
+
#
|
122
|
+
# @option request [Object] :body
|
123
|
+
#
|
124
|
+
# @option request [Float] :deadline
|
125
|
+
#
|
126
|
+
# @return [Array(Integer, Net::HTTPResponse, Enumerable<String>)]
|
127
|
+
def execute(request)
|
128
|
+
url, deadline = request.fetch_values(:url, :deadline)
|
129
|
+
|
130
|
+
req = nil
|
131
|
+
eof = false
|
132
|
+
finished = false
|
133
|
+
closing = nil
|
134
|
+
|
135
|
+
# rubocop:disable Metrics/BlockLength
|
136
|
+
enum = Enumerator.new do |y|
|
137
|
+
with_pool(url, deadline: deadline) do |conn|
|
138
|
+
next if finished
|
139
|
+
|
140
|
+
req, closing = self.class.build_request(request) do
|
141
|
+
self.class.calibrate_socket_timeout(conn, deadline)
|
142
|
+
end
|
143
|
+
|
144
|
+
self.class.calibrate_socket_timeout(conn, deadline)
|
145
|
+
unless conn.started?
|
146
|
+
conn.keep_alive_timeout = self.class::KEEP_ALIVE_TIMEOUT
|
147
|
+
conn.start
|
148
|
+
end
|
149
|
+
|
150
|
+
self.class.calibrate_socket_timeout(conn, deadline)
|
151
|
+
conn.request(req) do |rsp|
|
152
|
+
y << [conn, req, rsp]
|
153
|
+
break if finished
|
154
|
+
|
155
|
+
rsp.read_body do |bytes|
|
156
|
+
y << bytes.force_encoding(Encoding::BINARY)
|
157
|
+
break if finished
|
158
|
+
|
159
|
+
self.class.calibrate_socket_timeout(conn, deadline)
|
160
|
+
end
|
161
|
+
eof = true
|
162
|
+
end
|
163
|
+
end
|
164
|
+
rescue Timeout::Error
|
165
|
+
raise Turbopuffer::Errors::APITimeoutError.new(url: url, request: req)
|
166
|
+
rescue StandardError
|
167
|
+
raise Turbopuffer::Errors::APIConnectionError.new(url: url, request: req)
|
168
|
+
end
|
169
|
+
# rubocop:enable Metrics/BlockLength
|
170
|
+
|
171
|
+
conn, _, response = enum.next
|
172
|
+
body = Turbopuffer::Internal::Util.fused_enum(enum, external: true) do
|
173
|
+
finished = true
|
174
|
+
tap do
|
175
|
+
enum.next
|
176
|
+
rescue StopIteration
|
177
|
+
nil
|
178
|
+
end
|
179
|
+
ensure
|
180
|
+
conn.finish if !eof && conn&.started?
|
181
|
+
closing&.call
|
182
|
+
end
|
183
|
+
[Integer(response.code), response, body]
|
184
|
+
end
|
185
|
+
|
186
|
+
# @api private
|
187
|
+
#
|
188
|
+
# @param size [Integer]
|
189
|
+
def initialize(size: self.class::DEFAULT_MAX_CONNECTIONS)
|
190
|
+
@mutex = Mutex.new
|
191
|
+
@size = size
|
192
|
+
@pools = {}
|
193
|
+
end
|
194
|
+
|
195
|
+
define_sorbet_constant!(:Request) do
|
196
|
+
T.type_alias do
|
197
|
+
{
|
198
|
+
method: Symbol,
|
199
|
+
url: URI::Generic,
|
200
|
+
headers: T::Hash[String, String],
|
201
|
+
body: T.anything,
|
202
|
+
deadline: Float
|
203
|
+
}
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|
209
|
+
end
|
@@ -0,0 +1,167 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Turbopuffer
|
4
|
+
module Internal
|
5
|
+
module Type
|
6
|
+
# @api private
|
7
|
+
#
|
8
|
+
# @abstract
|
9
|
+
#
|
10
|
+
# @generic Elem
|
11
|
+
#
|
12
|
+
# Array of items of a given type.
|
13
|
+
class ArrayOf
|
14
|
+
include Turbopuffer::Internal::Type::Converter
|
15
|
+
include Turbopuffer::Internal::Util::SorbetRuntimeSupport
|
16
|
+
|
17
|
+
private_class_method :new
|
18
|
+
|
19
|
+
# @overload [](type_info, spec = {})
|
20
|
+
#
|
21
|
+
# @param type_info [Hash{Symbol=>Object}, Proc, Turbopuffer::Internal::Type::Converter, Class]
|
22
|
+
#
|
23
|
+
# @param spec [Hash{Symbol=>Object}] .
|
24
|
+
#
|
25
|
+
# @option spec [NilClass, TrueClass, FalseClass, Integer, Float, Symbol] :const
|
26
|
+
#
|
27
|
+
# @option spec [Proc] :enum
|
28
|
+
#
|
29
|
+
# @option spec [Proc] :union
|
30
|
+
#
|
31
|
+
# @option spec [Boolean] :"nil?"
|
32
|
+
#
|
33
|
+
# @return [self]
|
34
|
+
def self.[](...) = new(...)
|
35
|
+
|
36
|
+
# @api public
|
37
|
+
#
|
38
|
+
# @param other [Object]
|
39
|
+
#
|
40
|
+
# @return [Boolean]
|
41
|
+
def ===(other) = other.is_a?(Array) && other.all?(item_type)
|
42
|
+
|
43
|
+
# @api public
|
44
|
+
#
|
45
|
+
# @param other [Object]
|
46
|
+
#
|
47
|
+
# @return [Boolean]
|
48
|
+
def ==(other)
|
49
|
+
# rubocop:disable Layout/LineLength
|
50
|
+
other.is_a?(Turbopuffer::Internal::Type::ArrayOf) && other.nilable? == nilable? && other.item_type == item_type
|
51
|
+
# rubocop:enable Layout/LineLength
|
52
|
+
end
|
53
|
+
|
54
|
+
# @api public
|
55
|
+
#
|
56
|
+
# @return [Integer]
|
57
|
+
def hash = [self.class, item_type].hash
|
58
|
+
|
59
|
+
# @api private
|
60
|
+
#
|
61
|
+
# @param value [Array<Object>, Object]
|
62
|
+
#
|
63
|
+
# @param state [Hash{Symbol=>Object}] .
|
64
|
+
#
|
65
|
+
# @option state [Boolean] :translate_names
|
66
|
+
#
|
67
|
+
# @option state [Boolean] :strictness
|
68
|
+
#
|
69
|
+
# @option state [Hash{Symbol=>Object}] :exactness
|
70
|
+
#
|
71
|
+
# @option state [Class<StandardError>] :error
|
72
|
+
#
|
73
|
+
# @option state [Integer] :branched
|
74
|
+
#
|
75
|
+
# @return [Array<Object>, Object]
|
76
|
+
def coerce(value, state:)
|
77
|
+
exactness = state.fetch(:exactness)
|
78
|
+
|
79
|
+
unless value.is_a?(Array)
|
80
|
+
exactness[:no] += 1
|
81
|
+
state[:error] = TypeError.new("#{value.class} can't be coerced into #{Array}")
|
82
|
+
return value
|
83
|
+
end
|
84
|
+
|
85
|
+
target = item_type
|
86
|
+
exactness[:yes] += 1
|
87
|
+
value
|
88
|
+
.map do |item|
|
89
|
+
case [nilable?, item]
|
90
|
+
in [true, nil]
|
91
|
+
exactness[:yes] += 1
|
92
|
+
nil
|
93
|
+
else
|
94
|
+
Turbopuffer::Internal::Type::Converter.coerce(target, item, state: state)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
# @api private
|
100
|
+
#
|
101
|
+
# @param value [Array<Object>, Object]
|
102
|
+
#
|
103
|
+
# @param state [Hash{Symbol=>Object}] .
|
104
|
+
#
|
105
|
+
# @option state [Boolean] :can_retry
|
106
|
+
#
|
107
|
+
# @return [Array<Object>, Object]
|
108
|
+
def dump(value, state:)
|
109
|
+
target = item_type
|
110
|
+
if value.is_a?(Array)
|
111
|
+
value.map do
|
112
|
+
Turbopuffer::Internal::Type::Converter.dump(target, _1, state: state)
|
113
|
+
end
|
114
|
+
else
|
115
|
+
super
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
# @api private
|
120
|
+
#
|
121
|
+
# @return [Object]
|
122
|
+
def to_sorbet_type
|
123
|
+
T::Array[Turbopuffer::Internal::Util::SorbetRuntimeSupport.to_sorbet_type(item_type)]
|
124
|
+
end
|
125
|
+
|
126
|
+
# @api private
|
127
|
+
#
|
128
|
+
# @return [generic<Elem>]
|
129
|
+
protected def item_type = @item_type_fn.call
|
130
|
+
|
131
|
+
# @api private
|
132
|
+
#
|
133
|
+
# @return [Boolean]
|
134
|
+
protected def nilable? = @nilable
|
135
|
+
|
136
|
+
# @api private
|
137
|
+
#
|
138
|
+
# @param type_info [Hash{Symbol=>Object}, Proc, Turbopuffer::Internal::Type::Converter, Class]
|
139
|
+
#
|
140
|
+
# @param spec [Hash{Symbol=>Object}] .
|
141
|
+
#
|
142
|
+
# @option spec [NilClass, TrueClass, FalseClass, Integer, Float, Symbol] :const
|
143
|
+
#
|
144
|
+
# @option spec [Proc] :enum
|
145
|
+
#
|
146
|
+
# @option spec [Proc] :union
|
147
|
+
#
|
148
|
+
# @option spec [Boolean] :"nil?"
|
149
|
+
def initialize(type_info, spec = {})
|
150
|
+
@item_type_fn = Turbopuffer::Internal::Type::Converter.type_info(type_info || spec)
|
151
|
+
@nilable = spec.fetch(:nil?, false)
|
152
|
+
end
|
153
|
+
|
154
|
+
# @api private
|
155
|
+
#
|
156
|
+
# @param depth [Integer]
|
157
|
+
#
|
158
|
+
# @return [String]
|
159
|
+
def inspect(depth: 0)
|
160
|
+
items = Turbopuffer::Internal::Type::Converter.inspect(item_type, depth: depth.succ)
|
161
|
+
|
162
|
+
"#{self.class}[#{[items, nilable? ? 'nil' : nil].compact.join(' | ')}]"
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|