turbopuffer 0.1.0 → 1.2.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.
Files changed (207) hide show
  1. checksums.yaml +4 -4
  2. data/.ignore +2 -0
  3. data/CHANGELOG.md +205 -0
  4. data/README.md +270 -40
  5. data/SECURITY.md +27 -0
  6. data/lib/turbopuffer/client.rb +122 -16
  7. data/lib/turbopuffer/errors.rb +228 -0
  8. data/lib/turbopuffer/file_part.rb +55 -0
  9. data/lib/turbopuffer/internal/namespace_page.rb +86 -0
  10. data/lib/turbopuffer/internal/transport/base_client.rb +567 -0
  11. data/lib/turbopuffer/internal/transport/pooled_net_requester.rb +201 -0
  12. data/lib/turbopuffer/internal/type/array_of.rb +168 -0
  13. data/lib/turbopuffer/internal/type/base_model.rb +529 -0
  14. data/lib/turbopuffer/internal/type/base_page.rb +55 -0
  15. data/lib/turbopuffer/internal/type/boolean.rb +77 -0
  16. data/lib/turbopuffer/internal/type/converter.rb +327 -0
  17. data/lib/turbopuffer/internal/type/enum.rb +152 -0
  18. data/lib/turbopuffer/internal/type/file_input.rb +108 -0
  19. data/lib/turbopuffer/internal/type/hash_of.rb +188 -0
  20. data/lib/turbopuffer/internal/type/request_parameters.rb +42 -0
  21. data/lib/turbopuffer/internal/type/union.rb +254 -0
  22. data/lib/turbopuffer/internal/type/unknown.rb +81 -0
  23. data/lib/turbopuffer/internal/util.rb +914 -0
  24. data/lib/turbopuffer/internal.rb +20 -0
  25. data/lib/turbopuffer/models/aggregation_group.rb +8 -0
  26. data/lib/turbopuffer/models/attribute_schema.rb +19 -0
  27. data/lib/turbopuffer/models/attribute_schema_config.rb +56 -0
  28. data/lib/turbopuffer/models/attribute_type.rb +7 -0
  29. data/lib/turbopuffer/models/client_namespaces_params.rb +38 -0
  30. data/lib/turbopuffer/models/columns.rb +64 -0
  31. data/lib/turbopuffer/models/distance_metric.rb +19 -0
  32. data/lib/turbopuffer/models/full_text_search.rb +20 -0
  33. data/lib/turbopuffer/models/full_text_search_config.rb +83 -0
  34. data/lib/turbopuffer/models/id.rb +19 -0
  35. data/lib/turbopuffer/models/include_attributes.rb +22 -0
  36. data/lib/turbopuffer/models/language.rb +32 -0
  37. data/lib/turbopuffer/models/namespace_delete_all_params.rb +20 -0
  38. data/lib/turbopuffer/models/namespace_delete_all_response.rb +19 -0
  39. data/lib/turbopuffer/models/namespace_explain_query_params.rb +137 -0
  40. data/lib/turbopuffer/models/namespace_explain_query_response.rb +19 -0
  41. data/lib/turbopuffer/models/namespace_hint_cache_warm_params.rb +20 -0
  42. data/lib/turbopuffer/models/namespace_hint_cache_warm_response.rb +26 -0
  43. data/lib/turbopuffer/models/namespace_metadata.rb +43 -0
  44. data/lib/turbopuffer/models/namespace_metadata_params.rb +20 -0
  45. data/lib/turbopuffer/models/namespace_multi_query_params.rb +73 -0
  46. data/lib/turbopuffer/models/namespace_multi_query_response.rb +58 -0
  47. data/lib/turbopuffer/models/namespace_query_params.rb +137 -0
  48. data/lib/turbopuffer/models/namespace_query_response.rb +49 -0
  49. data/lib/turbopuffer/models/namespace_recall_params.rb +66 -0
  50. data/lib/turbopuffer/models/namespace_recall_response.rb +68 -0
  51. data/lib/turbopuffer/models/namespace_schema_params.rb +20 -0
  52. data/lib/turbopuffer/models/namespace_schema_response.rb +8 -0
  53. data/lib/turbopuffer/models/namespace_summary.rb +19 -0
  54. data/lib/turbopuffer/models/namespace_update_schema_params.rb +29 -0
  55. data/lib/turbopuffer/models/namespace_update_schema_response.rb +9 -0
  56. data/lib/turbopuffer/models/namespace_write_params.rb +158 -0
  57. data/lib/turbopuffer/models/namespace_write_response.rb +67 -0
  58. data/lib/turbopuffer/models/query.rb +81 -0
  59. data/lib/turbopuffer/models/query_billing.rb +26 -0
  60. data/lib/turbopuffer/models/query_performance.rb +63 -0
  61. data/lib/turbopuffer/models/row.rb +46 -0
  62. data/lib/turbopuffer/models/tokenizer.rb +19 -0
  63. data/lib/turbopuffer/models/vector.rb +22 -0
  64. data/lib/turbopuffer/models/vector_encoding.rb +16 -0
  65. data/lib/turbopuffer/models/write_billing.rb +26 -0
  66. data/lib/turbopuffer/models.rb +106 -0
  67. data/lib/turbopuffer/namespace.rb +14 -50
  68. data/lib/turbopuffer/request_options.rb +77 -0
  69. data/lib/turbopuffer/resources/namespaces.rb +366 -0
  70. data/lib/turbopuffer/version.rb +1 -1
  71. data/lib/turbopuffer.rb +92 -4
  72. data/manifest.yaml +15 -0
  73. data/rbi/turbopuffer/client.rbi +91 -0
  74. data/rbi/turbopuffer/errors.rbi +205 -0
  75. data/rbi/turbopuffer/file_part.rbi +37 -0
  76. data/rbi/turbopuffer/internal/namespace_page.rbi +22 -0
  77. data/rbi/turbopuffer/internal/transport/base_client.rbi +297 -0
  78. data/rbi/turbopuffer/internal/transport/pooled_net_requester.rbi +80 -0
  79. data/rbi/turbopuffer/internal/type/array_of.rbi +104 -0
  80. data/rbi/turbopuffer/internal/type/base_model.rbi +304 -0
  81. data/rbi/turbopuffer/internal/type/base_page.rbi +43 -0
  82. data/rbi/turbopuffer/internal/type/boolean.rbi +58 -0
  83. data/rbi/turbopuffer/internal/type/converter.rbi +216 -0
  84. data/rbi/turbopuffer/internal/type/enum.rbi +82 -0
  85. data/rbi/turbopuffer/internal/type/file_input.rbi +59 -0
  86. data/rbi/turbopuffer/internal/type/hash_of.rbi +104 -0
  87. data/rbi/turbopuffer/internal/type/request_parameters.rbi +31 -0
  88. data/rbi/turbopuffer/internal/type/union.rbi +128 -0
  89. data/rbi/turbopuffer/internal/type/unknown.rbi +58 -0
  90. data/rbi/turbopuffer/internal/util.rbi +487 -0
  91. data/rbi/turbopuffer/internal.rbi +18 -0
  92. data/rbi/turbopuffer/models/aggregation_group.rbi +13 -0
  93. data/rbi/turbopuffer/models/attribute_schema.rbi +17 -0
  94. data/rbi/turbopuffer/models/attribute_schema_config.rbi +103 -0
  95. data/rbi/turbopuffer/models/attribute_type.rbi +7 -0
  96. data/rbi/turbopuffer/models/client_namespaces_params.rbi +71 -0
  97. data/rbi/turbopuffer/models/columns.rbi +82 -0
  98. data/rbi/turbopuffer/models/distance_metric.rbi +27 -0
  99. data/rbi/turbopuffer/models/full_text_search.rbi +19 -0
  100. data/rbi/turbopuffer/models/full_text_search_config.rbi +133 -0
  101. data/rbi/turbopuffer/models/id.rbi +16 -0
  102. data/rbi/turbopuffer/models/include_attributes.rbi +24 -0
  103. data/rbi/turbopuffer/models/language.rbi +36 -0
  104. data/rbi/turbopuffer/models/namespace_delete_all_params.rbi +41 -0
  105. data/rbi/turbopuffer/models/namespace_delete_all_response.rbi +31 -0
  106. data/rbi/turbopuffer/models/namespace_explain_query_params.rbi +273 -0
  107. data/rbi/turbopuffer/models/namespace_explain_query_response.rbi +34 -0
  108. data/rbi/turbopuffer/models/namespace_hint_cache_warm_params.rbi +41 -0
  109. data/rbi/turbopuffer/models/namespace_hint_cache_warm_response.rbi +38 -0
  110. data/rbi/turbopuffer/models/namespace_metadata.rbi +62 -0
  111. data/rbi/turbopuffer/models/namespace_metadata_params.rbi +41 -0
  112. data/rbi/turbopuffer/models/namespace_multi_query_params.rbi +175 -0
  113. data/rbi/turbopuffer/models/namespace_multi_query_response.rbi +121 -0
  114. data/rbi/turbopuffer/models/namespace_query_params.rbi +267 -0
  115. data/rbi/turbopuffer/models/namespace_query_response.rbi +84 -0
  116. data/rbi/turbopuffer/models/namespace_recall_params.rbi +106 -0
  117. data/rbi/turbopuffer/models/namespace_recall_response.rbi +134 -0
  118. data/rbi/turbopuffer/models/namespace_schema_params.rbi +41 -0
  119. data/rbi/turbopuffer/models/namespace_schema_response.rbi +11 -0
  120. data/rbi/turbopuffer/models/namespace_summary.rbi +28 -0
  121. data/rbi/turbopuffer/models/namespace_update_schema_params.rbi +80 -0
  122. data/rbi/turbopuffer/models/namespace_update_schema_response.rbi +11 -0
  123. data/rbi/turbopuffer/models/namespace_write_params.rbi +290 -0
  124. data/rbi/turbopuffer/models/namespace_write_response.rbi +101 -0
  125. data/rbi/turbopuffer/models/query.rbi +132 -0
  126. data/rbi/turbopuffer/models/query_billing.rbi +46 -0
  127. data/rbi/turbopuffer/models/query_performance.rbi +82 -0
  128. data/rbi/turbopuffer/models/row.rbi +47 -0
  129. data/rbi/turbopuffer/models/tokenizer.rbi +24 -0
  130. data/rbi/turbopuffer/models/vector.rbi +22 -0
  131. data/rbi/turbopuffer/models/vector_encoding.rbi +22 -0
  132. data/rbi/turbopuffer/models/write_billing.rbi +49 -0
  133. data/rbi/turbopuffer/models.rbi +72 -0
  134. data/rbi/turbopuffer/namespace.rbi +8 -0
  135. data/rbi/turbopuffer/request_options.rbi +59 -0
  136. data/rbi/turbopuffer/resources/namespaces.rbi +304 -0
  137. data/rbi/turbopuffer/version.rbi +5 -0
  138. data/sig/turbopuffer/client.rbs +41 -0
  139. data/sig/turbopuffer/errors.rbs +117 -0
  140. data/sig/turbopuffer/file_part.rbs +21 -0
  141. data/sig/turbopuffer/internal/namespace_page.rbs +13 -0
  142. data/sig/turbopuffer/internal/transport/base_client.rbs +131 -0
  143. data/sig/turbopuffer/internal/transport/pooled_net_requester.rbs +45 -0
  144. data/sig/turbopuffer/internal/type/array_of.rbs +48 -0
  145. data/sig/turbopuffer/internal/type/base_model.rbs +102 -0
  146. data/sig/turbopuffer/internal/type/base_page.rbs +24 -0
  147. data/sig/turbopuffer/internal/type/boolean.rbs +26 -0
  148. data/sig/turbopuffer/internal/type/converter.rbs +79 -0
  149. data/sig/turbopuffer/internal/type/enum.rbs +32 -0
  150. data/sig/turbopuffer/internal/type/file_input.rbs +25 -0
  151. data/sig/turbopuffer/internal/type/hash_of.rbs +48 -0
  152. data/sig/turbopuffer/internal/type/request_parameters.rbs +19 -0
  153. data/sig/turbopuffer/internal/type/union.rbs +52 -0
  154. data/sig/turbopuffer/internal/type/unknown.rbs +26 -0
  155. data/sig/turbopuffer/internal/util.rbs +185 -0
  156. data/sig/turbopuffer/internal.rbs +9 -0
  157. data/sig/turbopuffer/models/aggregation_group.rbs +7 -0
  158. data/sig/turbopuffer/models/attribute_schema.rbs +11 -0
  159. data/sig/turbopuffer/models/attribute_schema_config.rbs +52 -0
  160. data/sig/turbopuffer/models/attribute_type.rbs +5 -0
  161. data/sig/turbopuffer/models/client_namespaces_params.rbs +38 -0
  162. data/sig/turbopuffer/models/columns.rbs +42 -0
  163. data/sig/turbopuffer/models/distance_metric.rbs +17 -0
  164. data/sig/turbopuffer/models/full_text_search.rbs +11 -0
  165. data/sig/turbopuffer/models/full_text_search_config.rbs +75 -0
  166. data/sig/turbopuffer/models/id.rbs +11 -0
  167. data/sig/turbopuffer/models/include_attributes.rbs +13 -0
  168. data/sig/turbopuffer/models/language.rbs +48 -0
  169. data/sig/turbopuffer/models/namespace_delete_all_params.rbs +25 -0
  170. data/sig/turbopuffer/models/namespace_delete_all_response.rbs +13 -0
  171. data/sig/turbopuffer/models/namespace_explain_query_params.rbs +141 -0
  172. data/sig/turbopuffer/models/namespace_explain_query_response.rbs +15 -0
  173. data/sig/turbopuffer/models/namespace_hint_cache_warm_params.rbs +25 -0
  174. data/sig/turbopuffer/models/namespace_hint_cache_warm_response.rbs +18 -0
  175. data/sig/turbopuffer/models/namespace_metadata.rbs +35 -0
  176. data/sig/turbopuffer/models/namespace_metadata_params.rbs +25 -0
  177. data/sig/turbopuffer/models/namespace_multi_query_params.rbs +86 -0
  178. data/sig/turbopuffer/models/namespace_multi_query_response.rbs +65 -0
  179. data/sig/turbopuffer/models/namespace_query_params.rbs +139 -0
  180. data/sig/turbopuffer/models/namespace_query_response.rbs +48 -0
  181. data/sig/turbopuffer/models/namespace_recall_params.rbs +63 -0
  182. data/sig/turbopuffer/models/namespace_recall_response.rbs +61 -0
  183. data/sig/turbopuffer/models/namespace_schema_params.rbs +25 -0
  184. data/sig/turbopuffer/models/namespace_schema_response.rbs +8 -0
  185. data/sig/turbopuffer/models/namespace_summary.rbs +13 -0
  186. data/sig/turbopuffer/models/namespace_update_schema_params.rbs +37 -0
  187. data/sig/turbopuffer/models/namespace_update_schema_response.rbs +8 -0
  188. data/sig/turbopuffer/models/namespace_write_params.rbs +156 -0
  189. data/sig/turbopuffer/models/namespace_write_response.rbs +56 -0
  190. data/sig/turbopuffer/models/query.rbs +75 -0
  191. data/sig/turbopuffer/models/query_billing.rbs +25 -0
  192. data/sig/turbopuffer/models/query_performance.rbs +45 -0
  193. data/sig/turbopuffer/models/row.rbs +24 -0
  194. data/sig/turbopuffer/models/tokenizer.rbs +16 -0
  195. data/sig/turbopuffer/models/vector.rbs +13 -0
  196. data/sig/turbopuffer/models/vector_encoding.rbs +14 -0
  197. data/sig/turbopuffer/models/write_billing.rbs +27 -0
  198. data/sig/turbopuffer/models.rbs +65 -0
  199. data/sig/turbopuffer/namespace.rbs +5 -0
  200. data/sig/turbopuffer/request_options.rbs +36 -0
  201. data/sig/turbopuffer/resources/namespaces.rbs +99 -0
  202. data/sig/turbopuffer/version.rbs +3 -0
  203. metadata +218 -29
  204. data/.standard.yml +0 -3
  205. data/LICENSE.txt +0 -21
  206. data/Rakefile +0 -10
  207. data/sig/turbopuffer.rbs +0 -4
@@ -0,0 +1,327 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Turbopuffer
4
+ module Internal
5
+ module Type
6
+ # @api private
7
+ module Converter
8
+ extend Turbopuffer::Internal::Util::SorbetRuntimeSupport
9
+
10
+ # rubocop:disable Lint/UnusedMethodArgument
11
+
12
+ # @api private
13
+ #
14
+ # @param value [Object]
15
+ #
16
+ # @param state [Hash{Symbol=>Object}] .
17
+ #
18
+ # @option state [Boolean] :translate_names
19
+ #
20
+ # @option state [Boolean] :strictness
21
+ #
22
+ # @option state [Hash{Symbol=>Object}] :exactness
23
+ #
24
+ # @option state [Class<StandardError>] :error
25
+ #
26
+ # @option state [Integer] :branched
27
+ #
28
+ # @return [Object]
29
+ def coerce(value, state:) = (raise NotImplementedError)
30
+
31
+ # @api private
32
+ #
33
+ # @param value [Object]
34
+ #
35
+ # @param state [Hash{Symbol=>Object}] .
36
+ #
37
+ # @option state [Boolean] :can_retry
38
+ #
39
+ # @return [Object]
40
+ def dump(value, state:)
41
+ case value
42
+ in Array
43
+ value.map { Turbopuffer::Internal::Type::Unknown.dump(_1, state: state) }
44
+ in Hash
45
+ value.transform_values { Turbopuffer::Internal::Type::Unknown.dump(_1, state: state) }
46
+ in Turbopuffer::Internal::Type::BaseModel
47
+ value.class.dump(value, state: state)
48
+ in StringIO
49
+ value.string
50
+ in Pathname | IO
51
+ state[:can_retry] = false if value.is_a?(IO)
52
+ Turbopuffer::FilePart.new(value)
53
+ in Turbopuffer::FilePart
54
+ state[:can_retry] = false if value.content.is_a?(IO)
55
+ value
56
+ else
57
+ value
58
+ end
59
+ end
60
+
61
+ # @api private
62
+ #
63
+ # @param depth [Integer]
64
+ #
65
+ # @return [String]
66
+ def inspect(depth: 0)
67
+ super()
68
+ end
69
+
70
+ # rubocop:enable Lint/UnusedMethodArgument
71
+
72
+ class << self
73
+ # @api private
74
+ #
75
+ # @param spec [Hash{Symbol=>Object}, Proc, Turbopuffer::Internal::Type::Converter, Class] .
76
+ #
77
+ # @option spec [NilClass, TrueClass, FalseClass, Integer, Float, Symbol] :const
78
+ #
79
+ # @option spec [Proc] :enum
80
+ #
81
+ # @option spec [Proc] :union
82
+ #
83
+ # @option spec [Boolean] :"nil?"
84
+ #
85
+ # @return [Proc]
86
+ def type_info(spec)
87
+ case spec
88
+ in Proc
89
+ spec
90
+ in Hash
91
+ type_info(spec.slice(:const, :enum, :union).first&.last)
92
+ in true | false
93
+ -> { Turbopuffer::Internal::Type::Boolean }
94
+ in Turbopuffer::Internal::Type::Converter | Class | Symbol
95
+ -> { spec }
96
+ in NilClass | Integer | Float
97
+ -> { spec.class }
98
+ end
99
+ end
100
+
101
+ # @api private
102
+ #
103
+ # @param type_info [Hash{Symbol=>Object}, Proc, Turbopuffer::Internal::Type::Converter, Class] .
104
+ #
105
+ # @option type_info [NilClass, TrueClass, FalseClass, Integer, Float, Symbol] :const
106
+ #
107
+ # @option type_info [Proc] :enum
108
+ #
109
+ # @option type_info [Proc] :union
110
+ #
111
+ # @option type_info [Boolean] :"nil?"
112
+ #
113
+ # @param spec [Hash{Symbol=>Object}, Proc, Turbopuffer::Internal::Type::Converter, Class] .
114
+ #
115
+ # @option spec [NilClass, TrueClass, FalseClass, Integer, Float, Symbol] :const
116
+ #
117
+ # @option spec [Proc] :enum
118
+ #
119
+ # @option spec [Proc] :union
120
+ #
121
+ # @option spec [Boolean] :"nil?"
122
+ #
123
+ # @return [Hash{Symbol=>Object}]
124
+ def meta_info(type_info, spec)
125
+ [spec, type_info].grep(Hash).first.to_h.except(:const, :enum, :union, :nil?)
126
+ end
127
+
128
+ # @api private
129
+ #
130
+ # @param translate_names [Boolean]
131
+ #
132
+ # @return [Hash{Symbol=>Object}]
133
+ def new_coerce_state(translate_names: true)
134
+ {
135
+ translate_names: translate_names,
136
+ strictness: true,
137
+ exactness: {yes: 0, no: 0, maybe: 0},
138
+ error: nil,
139
+ branched: 0
140
+ }
141
+ end
142
+
143
+ # @api private
144
+ #
145
+ # Based on `target`, transform `value` into `target`, to the extent possible:
146
+ #
147
+ # 1. if the given `value` conforms to `target` already, return the given `value`
148
+ # 2. if it's possible and safe to convert the given `value` to `target`, then the
149
+ # converted value
150
+ # 3. otherwise, the given `value` unaltered
151
+ #
152
+ # The coercion process is subject to improvement between minor release versions.
153
+ # See https://docs.pydantic.dev/latest/concepts/unions/#smart-mode
154
+ #
155
+ # @param target [Turbopuffer::Internal::Type::Converter, Class]
156
+ #
157
+ # @param value [Object]
158
+ #
159
+ # @param state [Hash{Symbol=>Object}] The `strictness` is one of `true`, `false`. This informs the coercion strategy
160
+ # when we have to decide between multiple possible conversion targets:
161
+ #
162
+ # - `true`: the conversion must be exact, with minimum coercion.
163
+ # - `false`: the conversion can be approximate, with some coercion.
164
+ #
165
+ # The `exactness` is `Hash` with keys being one of `yes`, `no`, or `maybe`. For
166
+ # any given conversion attempt, the exactness will be updated based on how closely
167
+ # the value recursively matches the target type:
168
+ #
169
+ # - `yes`: the value can be converted to the target type with minimum coercion.
170
+ # - `maybe`: the value can be converted to the target type with some reasonable
171
+ # coercion.
172
+ # - `no`: the value cannot be converted to the target type.
173
+ #
174
+ # See implementation below for more details.
175
+ #
176
+ # @option state [Boolean] :translate_names
177
+ #
178
+ # @option state [Boolean] :strictness
179
+ #
180
+ # @option state [Hash{Symbol=>Object}] :exactness
181
+ #
182
+ # @option state [Class<StandardError>] :error
183
+ #
184
+ # @option state [Integer] :branched
185
+ #
186
+ # @return [Object]
187
+ def coerce(target, value, state: Turbopuffer::Internal::Type::Converter.new_coerce_state)
188
+ # rubocop:disable Metrics/BlockNesting
189
+ exactness = state.fetch(:exactness)
190
+
191
+ case target
192
+ in Turbopuffer::Internal::Type::Converter
193
+ return target.coerce(value, state: state)
194
+ in Class
195
+ if value.is_a?(target)
196
+ exactness[:yes] += 1
197
+ return value
198
+ end
199
+
200
+ case target
201
+ in -> { _1 <= NilClass }
202
+ exactness[value.nil? ? :yes : :maybe] += 1
203
+ return nil
204
+ in -> { _1 <= Integer }
205
+ case value
206
+ in Integer
207
+ exactness[:yes] += 1
208
+ return value
209
+ else
210
+ Kernel.then do
211
+ return Integer(value).tap { exactness[:maybe] += 1 }
212
+ rescue ArgumentError, TypeError => e
213
+ state[:error] = e
214
+ end
215
+ end
216
+ in -> { _1 <= Float }
217
+ if value.is_a?(Numeric)
218
+ exactness[:yes] += 1
219
+ return Float(value)
220
+ else
221
+ Kernel.then do
222
+ return Float(value).tap { exactness[:maybe] += 1 }
223
+ rescue ArgumentError, TypeError => e
224
+ state[:error] = e
225
+ end
226
+ end
227
+ in -> { _1 <= String }
228
+ case value
229
+ in String | Symbol | Numeric
230
+ exactness[value.is_a?(Numeric) ? :maybe : :yes] += 1
231
+ return value.to_s
232
+ in StringIO
233
+ exactness[:yes] += 1
234
+ return value.string
235
+ else
236
+ state[:error] = TypeError.new("#{value.class} can't be coerced into #{String}")
237
+ end
238
+ in -> { _1 <= Date || _1 <= Time }
239
+ Kernel.then do
240
+ return target.parse(value).tap { exactness[:yes] += 1 }
241
+ rescue ArgumentError, TypeError => e
242
+ state[:error] = e
243
+ end
244
+ in -> { _1 <= StringIO } if value.is_a?(String)
245
+ exactness[:yes] += 1
246
+ return StringIO.new(value.b)
247
+ else
248
+ end
249
+ in Symbol
250
+ case value
251
+ in Symbol | String
252
+ if value.to_sym == target
253
+ exactness[:yes] += 1
254
+ return target
255
+ else
256
+ exactness[:maybe] += 1
257
+ return value
258
+ end
259
+ else
260
+ message = "cannot convert non-matching #{value.class} into #{target.inspect}"
261
+ state[:error] = ArgumentError.new(message)
262
+ end
263
+ else
264
+ end
265
+
266
+ exactness[:no] += 1
267
+ value
268
+ # rubocop:enable Metrics/BlockNesting
269
+ end
270
+
271
+ # @api private
272
+ #
273
+ # @param target [Turbopuffer::Internal::Type::Converter, Class]
274
+ #
275
+ # @param value [Object]
276
+ #
277
+ # @param state [Hash{Symbol=>Object}] .
278
+ #
279
+ # @option state [Boolean] :can_retry
280
+ #
281
+ # @return [Object]
282
+ def dump(target, value, state: {can_retry: true})
283
+ case target
284
+ in Turbopuffer::Internal::Type::Converter
285
+ target.dump(value, state: state)
286
+ else
287
+ Turbopuffer::Internal::Type::Unknown.dump(value, state: state)
288
+ end
289
+ end
290
+
291
+ # @api private
292
+ #
293
+ # @param target [Object]
294
+ # @param depth [Integer]
295
+ #
296
+ # @return [String]
297
+ def inspect(target, depth:)
298
+ case target
299
+ in Turbopuffer::Internal::Type::Converter
300
+ target.inspect(depth: depth.succ)
301
+ else
302
+ target.inspect
303
+ end
304
+ end
305
+ end
306
+
307
+ define_sorbet_constant!(:Input) do
308
+ T.type_alias { T.any(Turbopuffer::Internal::Type::Converter, T::Class[T.anything]) }
309
+ end
310
+ define_sorbet_constant!(:CoerceState) do
311
+ T.type_alias do
312
+ {
313
+ translate_names: T::Boolean,
314
+ strictness: T::Boolean,
315
+ exactness: {yes: Integer, no: Integer, maybe: Integer},
316
+ error: T::Class[StandardError],
317
+ branched: Integer
318
+ }
319
+ end
320
+ end
321
+ define_sorbet_constant!(:DumpState) do
322
+ T.type_alias { {can_retry: T::Boolean} }
323
+ end
324
+ end
325
+ end
326
+ end
327
+ end
@@ -0,0 +1,152 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Turbopuffer
4
+ module Internal
5
+ module Type
6
+ # @api private
7
+ #
8
+ # A value from among a specified list of options. OpenAPI enum values map to Ruby
9
+ # values in the SDK as follows:
10
+ #
11
+ # 1. boolean => true | false
12
+ # 2. integer => Integer
13
+ # 3. float => Float
14
+ # 4. string => Symbol
15
+ #
16
+ # We can therefore convert string values to Symbols, but can't convert other
17
+ # values safely.
18
+ #
19
+ # @example
20
+ # # `distance_metric` is a `Turbopuffer::DistanceMetric`
21
+ # case distance_metric
22
+ # when Turbopuffer::DistanceMetric::COSINE_DISTANCE
23
+ # # ...
24
+ # when Turbopuffer::DistanceMetric::EUCLIDEAN_SQUARED
25
+ # # ...
26
+ # else
27
+ # puts(distance_metric)
28
+ # end
29
+ #
30
+ # @example
31
+ # case distance_metric
32
+ # in :cosine_distance
33
+ # # ...
34
+ # in :euclidean_squared
35
+ # # ...
36
+ # else
37
+ # puts(distance_metric)
38
+ # end
39
+ module Enum
40
+ include Turbopuffer::Internal::Type::Converter
41
+ include Turbopuffer::Internal::Util::SorbetRuntimeSupport
42
+
43
+ # All of the valid Symbol values for this enum.
44
+ #
45
+ # @return [Array<NilClass, Boolean, Integer, Float, Symbol>]
46
+ def values = constants.map { const_get(_1) }
47
+
48
+ # @api public
49
+ #
50
+ # @param other [Object]
51
+ #
52
+ # @return [Boolean]
53
+ def ===(other) = values.include?(other)
54
+
55
+ # @api public
56
+ #
57
+ # @param other [Object]
58
+ #
59
+ # @return [Boolean]
60
+ def ==(other)
61
+ # rubocop:disable Style/CaseEquality
62
+ Turbopuffer::Internal::Type::Enum === other && other.values.to_set == values.to_set
63
+ # rubocop:enable Style/CaseEquality
64
+ end
65
+
66
+ # @api public
67
+ #
68
+ # @return [Integer]
69
+ def hash = values.to_set.hash
70
+
71
+ # @api private
72
+ #
73
+ # Unlike with primitives, `Enum` additionally validates that the value is a member
74
+ # of the enum.
75
+ #
76
+ # @param value [String, Symbol, Object]
77
+ #
78
+ # @param state [Hash{Symbol=>Object}] .
79
+ #
80
+ # @option state [Boolean] :translate_names
81
+ #
82
+ # @option state [Boolean] :strictness
83
+ #
84
+ # @option state [Hash{Symbol=>Object}] :exactness
85
+ #
86
+ # @option state [Class<StandardError>] :error
87
+ #
88
+ # @option state [Integer] :branched
89
+ #
90
+ # @return [Symbol, Object]
91
+ def coerce(value, state:)
92
+ exactness = state.fetch(:exactness)
93
+ val = value.is_a?(String) ? value.to_sym : value
94
+
95
+ if values.include?(val)
96
+ exactness[:yes] += 1
97
+ val
98
+ elsif values.first&.class == val.class
99
+ exactness[:maybe] += 1
100
+ value
101
+ else
102
+ exactness[:no] += 1
103
+ state[:error] = TypeError.new("#{value.class} can't be coerced into #{self}")
104
+ value
105
+ end
106
+ end
107
+
108
+ # @!method dump(value, state:)
109
+ # @api private
110
+ #
111
+ # @param value [Symbol, Object]
112
+ #
113
+ # @param state [Hash{Symbol=>Object}] .
114
+ #
115
+ # @option state [Boolean] :can_retry
116
+ #
117
+ # @return [Symbol, Object]
118
+
119
+ # @api private
120
+ #
121
+ # @return [Object]
122
+ def to_sorbet_type
123
+ types = values.map { Turbopuffer::Internal::Util::SorbetRuntimeSupport.to_sorbet_type(_1) }.uniq
124
+ case types
125
+ in []
126
+ T.noreturn
127
+ in [type]
128
+ type
129
+ else
130
+ T.any(*types)
131
+ end
132
+ end
133
+
134
+ # @api private
135
+ #
136
+ # @param depth [Integer]
137
+ #
138
+ # @return [String]
139
+ def inspect(depth: 0)
140
+ if depth.positive?
141
+ return is_a?(Module) ? super() : self.class.name
142
+ end
143
+
144
+ members = values.map { Turbopuffer::Internal::Type::Converter.inspect(_1, depth: depth.succ) }
145
+ prefix = is_a?(Module) ? name : self.class.name
146
+
147
+ "#{prefix}[#{members.join(' | ')}]"
148
+ end
149
+ end
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,108 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Turbopuffer
4
+ module Internal
5
+ module Type
6
+ # @api private
7
+ #
8
+ # @abstract
9
+ #
10
+ # Either `Pathname` or `StringIO`, or `IO`, or
11
+ # `Turbopuffer::Internal::Type::FileInput`.
12
+ #
13
+ # Note: when `IO` is used, all retries are disabled, since many IO` streams are
14
+ # not rewindable.
15
+ class FileInput
16
+ extend Turbopuffer::Internal::Type::Converter
17
+
18
+ private_class_method :new
19
+
20
+ # @api public
21
+ #
22
+ # @param other [Object]
23
+ #
24
+ # @return [Boolean]
25
+ def self.===(other)
26
+ case other
27
+ in Pathname | StringIO | IO | String | Turbopuffer::FilePart
28
+ true
29
+ else
30
+ false
31
+ end
32
+ end
33
+
34
+ # @api public
35
+ #
36
+ # @param other [Object]
37
+ #
38
+ # @return [Boolean]
39
+ def self.==(other) = other.is_a?(Class) && other <= Turbopuffer::Internal::Type::FileInput
40
+
41
+ class << self
42
+ # @api private
43
+ #
44
+ # @param value [StringIO, String, Object]
45
+ #
46
+ # @param state [Hash{Symbol=>Object}] .
47
+ #
48
+ # @option state [Boolean] :translate_names
49
+ #
50
+ # @option state [Boolean] :strictness
51
+ #
52
+ # @option state [Hash{Symbol=>Object}] :exactness
53
+ #
54
+ # @option state [Class<StandardError>] :error
55
+ #
56
+ # @option state [Integer] :branched
57
+ #
58
+ # @return [StringIO, Object]
59
+ def coerce(value, state:)
60
+ exactness = state.fetch(:exactness)
61
+ case value
62
+ in String
63
+ exactness[:yes] += 1
64
+ StringIO.new(value)
65
+ in StringIO
66
+ exactness[:yes] += 1
67
+ value
68
+ else
69
+ state[:error] = TypeError.new("#{value.class} can't be coerced into #{StringIO}")
70
+ exactness[:no] += 1
71
+ value
72
+ end
73
+ end
74
+
75
+ # @api private
76
+ #
77
+ # @param value [Pathname, StringIO, IO, String, Object]
78
+ #
79
+ # @param state [Hash{Symbol=>Object}] .
80
+ #
81
+ # @option state [Boolean] :can_retry
82
+ #
83
+ # @return [Pathname, StringIO, IO, String, Object]
84
+ def dump(value, state:)
85
+ # rubocop:disable Lint/DuplicateBranch
86
+ case value
87
+ in IO
88
+ state[:can_retry] = false
89
+ in Turbopuffer::FilePart if value.content.is_a?(IO)
90
+ state[:can_retry] = false
91
+ else
92
+ end
93
+ # rubocop:enable Lint/DuplicateBranch
94
+
95
+ value
96
+ end
97
+
98
+ # @api private
99
+ #
100
+ # @return [Object]
101
+ def to_sorbet_type
102
+ T.any(Pathname, StringIO, IO, String, Turbopuffer::FilePart)
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end