Skip to content

Commit ec15a06

Browse files
feat: implement #hash for data containers
1 parent fd1a4e1 commit ec15a06

File tree

17 files changed

+60
-19
lines changed

17 files changed

+60
-19
lines changed

lib/openai/internal/transport/pooled_net_requester.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,11 @@ def calibrate_socket_timeout(conn, deadline)
5757
# @return [Array(Net::HTTPGenericRequest, Proc)]
5858
def build_request(request, &blk)
5959
method, url, headers, body = request.fetch_values(:method, :url, :headers, :body)
60-
61-
# ensure we construct a URI class of the right scheme
62-
url = URI(url.to_s)
63-
6460
req = Net::HTTPGenericRequest.new(
6561
method.to_s.upcase,
6662
!body.nil?,
6763
method != :head,
68-
url
64+
URI(url.to_s) # ensure we construct a URI class of the right scheme
6965
)
7066

7167
headers.each { req[_1] = _2 }

lib/openai/internal/type/array_of.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ def ==(other)
4444
# rubocop:enable Layout/LineLength
4545
end
4646

47+
# @return [Integer]
48+
def hash = [self.class, item_type].hash
49+
4750
# @api private
4851
#
4952
# @param value [Array<Object>, Object]

lib/openai/internal/type/base_model.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@ module OpenAI
44
module Internal
55
module Type
66
# @abstract
7-
#
8-
# @example
9-
# # `comparison_filter` is a `OpenAI::Models::ComparisonFilter`
10-
# comparison_filter => {
11-
# key: key,
12-
# type: type,
13-
# value: value
14-
# }
157
class BaseModel
168
extend OpenAI::Internal::Type::Converter
179

@@ -93,11 +85,13 @@ def fields
9385
state: state
9486
)
9587
end
96-
rescue StandardError
88+
rescue StandardError => e
9789
cls = self.class.name.split("::").last
98-
# rubocop:disable Layout/LineLength
99-
message = "Failed to parse #{cls}.#{__method__} from #{value.class} to #{target.inspect}. To get the unparsed API response, use #{cls}[:#{__method__}]."
100-
# rubocop:enable Layout/LineLength
90+
message = [
91+
"Failed to parse #{cls}.#{__method__} from #{value.class} to #{target.inspect}.",
92+
"To get the unparsed API response, use #{cls}[#{__method__.inspect}].",
93+
"Cause: #{e.message}"
94+
].join(" ")
10195
raise OpenAI::Errors::ConversionError.new(message)
10296
end
10397
end
@@ -171,13 +165,19 @@ def optional(name_sym, type_info, spec = {})
171165
def ==(other)
172166
other.is_a?(Class) && other <= OpenAI::Internal::Type::BaseModel && other.fields == fields
173167
end
168+
169+
# @return [Integer]
170+
def hash = fields.hash
174171
end
175172

176173
# @param other [Object]
177174
#
178175
# @return [Boolean]
179176
def ==(other) = self.class == other.class && @data == other.to_h
180177

178+
# @return [Integer]
179+
def hash = [self.class, @data].hash
180+
181181
class << self
182182
# @api private
183183
#

lib/openai/internal/type/enum.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ def ==(other)
6262
# rubocop:enable Style/CaseEquality
6363
end
6464

65+
# @return [Integer]
66+
def hash = values.to_set.hash
67+
6568
# @api private
6669
#
6770
# Unlike with primitives, `Enum` additionally validates that the value is a member

lib/openai/internal/type/hash_of.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ def ==(other)
5959
# rubocop:enable Layout/LineLength
6060
end
6161

62+
# @return [Integer]
63+
def hash = [self.class, item_type].hash
64+
6265
# @api private
6366
#
6467
# @param value [Hash{Object=>Object}, Object]

lib/openai/internal/type/union.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module Union
4343
#
4444
# @return [Array<Array(Symbol, Object)>]
4545
protected def derefed_variants
46-
@known_variants.map { |key, variant_fn| [key, variant_fn.call] }
46+
known_variants.map { |key, variant_fn| [key, variant_fn.call] }
4747
end
4848

4949
# All of the specified variants for this union.
@@ -128,6 +128,9 @@ def ==(other)
128128
OpenAI::Internal::Type::Union === other && other.derefed_variants == derefed_variants
129129
end
130130

131+
# @return [Integer]
132+
def hash = variants.hash
133+
131134
# @api private
132135
#
133136
# @param value [Object]

rbi/lib/openai/internal/type/array_of.rbi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ module OpenAI
3232
sig { params(other: T.anything).returns(T::Boolean) }
3333
def ==(other); end
3434

35+
sig { returns(Integer) }
36+
def hash; end
37+
3538
# @api private
3639
sig do
3740
override

rbi/lib/openai/internal/type/base_model.rbi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,17 @@ module OpenAI
111111

112112
sig { params(other: T.anything).returns(T::Boolean) }
113113
def ==(other); end
114+
115+
sig { returns(Integer) }
116+
def hash; end
114117
end
115118

116119
sig { params(other: T.anything).returns(T::Boolean) }
117120
def ==(other); end
118121

122+
sig { returns(Integer) }
123+
def hash; end
124+
119125
class << self
120126
# @api private
121127
sig do

rbi/lib/openai/internal/type/enum.rbi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ module OpenAI
2828
sig { params(other: T.anything).returns(T::Boolean) }
2929
def ==(other); end
3030

31+
sig { returns(Integer) }
32+
def hash; end
33+
3134
# @api private
3235
#
3336
# Unlike with primitives, `Enum` additionally validates that the value is a member

rbi/lib/openai/internal/type/hash_of.rbi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ module OpenAI
3232
sig { params(other: T.anything).returns(T::Boolean) }
3333
def ==(other); end
3434

35+
sig { returns(Integer) }
36+
def hash; end
37+
3538
# @api private
3639
sig do
3740
override

0 commit comments

Comments
 (0)