Skip to content

Commit e25b11c

Browse files
chore(internal): codegen related update (#223)
1 parent f4b7010 commit e25b11c

File tree

4 files changed

+26
-43
lines changed

4 files changed

+26
-43
lines changed

lib/orb/internal/util.rb

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,11 @@ def primitive?(input)
7070

7171
# @api private
7272
#
73-
# @param input [Object]
73+
# @param input [String, Boolean]
7474
#
7575
# @return [Boolean, Object]
7676
def coerce_boolean(input)
7777
case input.is_a?(String) ? input.downcase : input
78-
in Numeric
79-
input.nonzero?
8078
in "true"
8179
true
8280
in "false"
@@ -88,7 +86,7 @@ def coerce_boolean(input)
8886

8987
# @api private
9088
#
91-
# @param input [Object]
89+
# @param input [String, Boolean]
9290
#
9391
# @raise [ArgumentError]
9492
# @return [Boolean, nil]
@@ -103,34 +101,20 @@ def coerce_boolean!(input)
103101

104102
# @api private
105103
#
106-
# @param input [Object]
104+
# @param input [String, Integer]
107105
#
108106
# @return [Integer, Object]
109107
def coerce_integer(input)
110-
case input
111-
in true
112-
1
113-
in false
114-
0
115-
else
116-
Integer(input, exception: false) || input
117-
end
108+
Integer(input, exception: false) || input
118109
end
119110

120111
# @api private
121112
#
122-
# @param input [Object]
113+
# @param input [String, Integer, Float]
123114
#
124115
# @return [Float, Object]
125116
def coerce_float(input)
126-
case input
127-
in true
128-
1.0
129-
in false
130-
0.0
131-
else
132-
Float(input, exception: false) || input
133-
end
117+
Float(input, exception: false) || input
134118
end
135119

136120
# @api private
@@ -159,12 +143,7 @@ class << self
159143
private def deep_merge_lr(lhs, rhs, concat: false)
160144
case [lhs, rhs, concat]
161145
in [Hash, Hash, _]
162-
rhs_cleaned = rhs.reject { _2 == Orb::Internal::OMIT }
163-
lhs
164-
.reject { |key, _| rhs[key] == Orb::Internal::OMIT }
165-
.merge(rhs_cleaned) do |_, old_val, new_val|
166-
deep_merge_lr(old_val, new_val, concat: concat)
167-
end
146+
lhs.merge(rhs) { deep_merge_lr(_2, _3, concat: concat) }
168147
in [Array, Array, true]
169148
lhs.concat(rhs)
170149
else
@@ -362,7 +341,7 @@ def normalized_headers(*headers)
362341
value =
363342
case val
364343
in Array
365-
val.map { _1.to_s.strip }.join(", ")
344+
val.filter_map { _1&.to_s&.strip }.join(", ")
366345
else
367346
val&.to_s&.strip
368347
end
@@ -469,7 +448,7 @@ class << self
469448
case val
470449
in IO
471450
y << "Content-Type: application/octet-stream\r\n\r\n"
472-
IO.copy_stream(val, y)
451+
IO.copy_stream(val.tap(&:rewind), y)
473452
in StringIO
474453
y << "Content-Type: application/octet-stream\r\n\r\n"
475454
y << val.string
@@ -533,6 +512,8 @@ def encode_content(headers, body)
533512
boundary, strio = encode_multipart_streaming(body)
534513
headers = {**headers, "content-type" => "#{content_type}; boundary=#{boundary}"}
535514
[headers, strio]
515+
in [_, IO]
516+
[headers, body.tap(&:rewind)]
536517
in [_, StringIO]
537518
[headers, body.string]
538519
else
@@ -673,7 +654,7 @@ def decode_lines(enum)
673654
#
674655
# @param lines [Enumerable<String>]
675656
#
676-
# @return [Hash{Symbol=>Object}]
657+
# @return [Enumerable<Hash{Symbol=>Object}>]
677658
def decode_sse(lines)
678659
# rubocop:disable Metrics/BlockLength
679660
chain_fused(lines) do |y|

rbi/lib/orb/internal/page.rbi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
module Orb
44
module Internal
55
class Page
6-
Elem = type_member
7-
86
include Orb::Internal::Type::BasePage
97

8+
Elem = type_member
9+
1010
sig { returns(T.nilable(T::Array[Elem])) }
1111
attr_accessor :data
1212

rbi/lib/orb/internal/util.rbi

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,22 @@ module Orb
2828
end
2929

3030
# @api private
31-
sig { params(input: T.anything).returns(T.any(T::Boolean, T.anything)) }
31+
sig { params(input: T.any(String, T::Boolean)).returns(T.any(T::Boolean, T.anything)) }
3232
def coerce_boolean(input)
3333
end
3434

3535
# @api private
36-
sig { params(input: T.anything).returns(T.nilable(T::Boolean)) }
36+
sig { params(input: T.any(String, T::Boolean)).returns(T.nilable(T::Boolean)) }
3737
def coerce_boolean!(input)
3838
end
3939

4040
# @api private
41-
sig { params(input: T.anything).returns(T.any(Integer, T.anything)) }
41+
sig { params(input: T.any(String, Integer)).returns(T.any(Integer, T.anything)) }
4242
def coerce_integer(input)
4343
end
4444

4545
# @api private
46-
sig { params(input: T.anything).returns(T.any(Float, T.anything)) }
46+
sig { params(input: T.any(String, Integer, Float)).returns(T.any(Float, T.anything)) }
4747
def coerce_float(input)
4848
end
4949

@@ -271,7 +271,9 @@ module Orb
271271
# @api private
272272
#
273273
# https://html.spec.whatwg.org/multipage/server-sent-events.html#parsing-an-event-stream
274-
sig { params(lines: T::Enumerable[String]).returns(Orb::Internal::Util::ServerSentEvent) }
274+
sig do
275+
params(lines: T::Enumerable[String]).returns(T::Enumerable[Orb::Internal::Util::ServerSentEvent])
276+
end
275277
def decode_sse(lines)
276278
end
277279
end

sig/orb/internal/util.rbs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ module Orb
99

1010
def self?.primitive?: (top input) -> bool
1111

12-
def self?.coerce_boolean: (top input) -> (bool | top)
12+
def self?.coerce_boolean: (String | bool input) -> (bool | top)
1313

14-
def self?.coerce_boolean!: (top input) -> bool?
14+
def self?.coerce_boolean!: (String | bool input) -> bool?
1515

16-
def self?.coerce_integer: (top input) -> (Integer | top)
16+
def self?.coerce_integer: (String | Integer input) -> (Integer | top)
1717

18-
def self?.coerce_float: (top input) -> (Float | top)
18+
def self?.coerce_float: (String | Integer | Float input) -> (Float | top)
1919

2020
def self?.coerce_hash: (top input) -> (::Hash[top, top] | top)
2121

@@ -133,7 +133,7 @@ module Orb
133133

134134
def self?.decode_sse: (
135135
Enumerable[String] lines
136-
) -> Orb::Internal::Util::server_sent_event
136+
) -> Enumerable[Orb::Internal::Util::server_sent_event]
137137
end
138138
end
139139
end

0 commit comments

Comments
 (0)