Skip to content

Commit 2b99ae2

Browse files
chore(internal): codegen related update
1 parent c664f2f commit 2b99ae2

29 files changed

+183
-14
lines changed

lib/orb/internal.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
module Orb
44
# @api private
55
module Internal
6-
OMIT = Object.new.freeze
6+
OMIT =
7+
Object.new.tap do
8+
_1.define_singleton_method(:inspect) { "#<#{Orb::Internal}::OMIT>" }
9+
end
10+
.freeze
711
end
812
end

lib/orb/internal/page.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ class Page
3030
# @param page_data [Hash{Symbol=>Object}]
3131
def initialize(client:, req:, headers:, page_data:)
3232
super
33-
model = req.fetch(:model)
3433

3534
case page_data
3635
in {data: Array | nil => data}
37-
@data = data&.map { Orb::Internal::Type::Converter.coerce(model, _1) }
36+
@data = data&.map { Orb::Internal::Type::Converter.coerce(@model, _1) }
3837
else
3938
end
4039

@@ -73,18 +72,24 @@ def auto_paging_each(&blk)
7372
unless block_given?
7473
raise ArgumentError.new("A block must be given to ##{__method__}")
7574
end
75+
7676
page = self
7777
loop do
78-
page.data&.each { blk.call(_1) }
78+
page.data&.each(&blk)
79+
7980
break unless page.next_page?
8081
page = page.next_page
8182
end
8283
end
8384

85+
# @api private
86+
#
8487
# @return [String]
8588
def inspect
8689
# rubocop:disable Layout/LineLength
87-
"#<#{self.class}:0x#{object_id.to_s(16)} data=#{data.inspect} pagination_metadata=#{pagination_metadata.inspect}>"
90+
model = Orb::Internal::Type::Converter.inspect(@model, depth: 1)
91+
92+
"#<#{self.class}[#{model}]:0x#{object_id.to_s(16)} pagination_metadata=#{pagination_metadata.inspect}>"
8893
# rubocop:enable Layout/LineLength
8994
end
9095

lib/orb/internal/transport/base_client.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,8 @@ def request(req)
460460
end
461461
end
462462

463+
# @api private
464+
#
463465
# @return [String]
464466
def inspect
465467
# rubocop:disable Layout/LineLength

lib/orb/internal/type/array_of.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ module Type
1313
class ArrayOf
1414
include Orb::Internal::Type::Converter
1515

16+
private_class_method :new
17+
1618
# @param type_info [Hash{Symbol=>Object}, Proc, Orb::Internal::Type::Converter, Class]
1719
#
1820
# @param spec [Hash{Symbol=>Object}] .
@@ -120,7 +122,18 @@ def dump(value, state:)
120122
# @option spec [Boolean] :"nil?"
121123
def initialize(type_info, spec = {})
122124
@item_type_fn = Orb::Internal::Type::Converter.type_info(type_info || spec)
123-
@nilable = spec[:nil?]
125+
@nilable = spec.fetch(:nil?, false)
126+
end
127+
128+
# @api private
129+
#
130+
# @param depth [Integer]
131+
#
132+
# @return [String]
133+
def inspect(depth: 0)
134+
# rubocop:disable Layout/LineLength
135+
"#{self.class}[#{[Orb::Internal::Type::Converter.inspect(item_type, depth: depth.succ), nilable? ? 'nil' : nil].compact.join(' | ')}]"
136+
# rubocop:enable Layout/LineLength
124137
end
125138
end
126139
end

lib/orb/internal/type/base_model.rb

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def fields
6363

6464
setter = "#{name_sym}="
6565
api_name = info.fetch(:api_name, name_sym)
66-
nilable = info[:nil?]
66+
nilable = info.fetch(:nil?, false)
6767
const = required && !nilable ? info.fetch(:const, Orb::Internal::OMIT) : Orb::Internal::OMIT
6868

6969
[name_sym, setter].each { undef_method(_1) } if known_fields.key?(name_sym)
@@ -361,14 +361,42 @@ def initialize(data = {})
361361
end
362362
end
363363

364+
class << self
365+
# @api private
366+
#
367+
# @param depth [Integer]
368+
#
369+
# @return [String]
370+
def inspect(depth: 0)
371+
return super() if depth.positive?
372+
373+
depth = depth.succ
374+
deferred = fields.transform_values do |field|
375+
type, required, nilable = field.fetch_values(:type, :required, :nilable)
376+
-> do
377+
[
378+
Orb::Internal::Type::Converter.inspect(type, depth: depth),
379+
!required || nilable ? "nil" : nil
380+
].compact.join(" | ")
381+
end
382+
.tap { _1.define_singleton_method(:inspect) { call } }
383+
end
384+
385+
"#{name}[#{deferred.inspect}]"
386+
end
387+
end
388+
389+
# @api private
390+
#
364391
# @return [String]
365392
def inspect
366-
rows = self.class.known_fields.keys.map do
367-
"#{_1}=#{@data.key?(_1) ? public_send(_1) : ''}"
393+
rows = @data.map do
394+
"#{_1}=#{self.class.known_fields.key?(_1) ? public_send(_1).inspect : ''}"
368395
rescue Orb::Errors::ConversionError
369-
"#{_1}=#{@data.fetch(_1)}"
396+
"#{_1}=#{_2.inspect}"
370397
end
371-
"#<#{self.class.name}:0x#{object_id.to_s(16)} #{rows.join(' ')}>"
398+
399+
"#<#{self.class}:0x#{object_id.to_s(16)} #{rows.join(' ')}>"
372400
end
373401
end
374402
end

lib/orb/internal/type/base_page.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def to_enum = super(:auto_paging_each)
3636
def initialize(client:, req:, headers:, page_data:)
3737
@client = client
3838
@req = req
39+
@model = req.fetch(:model)
3940
super()
4041
end
4142

lib/orb/internal/type/boolean.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ module Type
1111
class Boolean
1212
extend Orb::Internal::Type::Converter
1313

14+
private_class_method :new
15+
1416
# @param other [Object]
1517
#
1618
# @return [Boolean]

lib/orb/internal/type/converter.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ def dump(value, state:)
4949
end
5050
end
5151

52+
# @api private
53+
#
54+
# @param depth [Integer]
55+
#
56+
# @return [String]
57+
def inspect(depth: 0)
58+
super()
59+
end
60+
5261
# rubocop:enable Lint/UnusedMethodArgument
5362

5463
class << self
@@ -240,6 +249,21 @@ def dump(target, value, state: {can_retry: true})
240249
Orb::Internal::Type::Unknown.dump(value, state: state)
241250
end
242251
end
252+
253+
# @api private
254+
#
255+
# @param target [Object]
256+
# @param depth [Integer]
257+
#
258+
# @return [String]
259+
def inspect(target, depth:)
260+
case target
261+
in Orb::Internal::Type::Converter
262+
target.inspect(depth: depth.succ)
263+
else
264+
target.inspect
265+
end
266+
end
243267
end
244268
end
245269
end

lib/orb/internal/type/enum.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,19 @@ def coerce(value, state:)
103103
# #
104104
# # @return [Symbol, Object]
105105
# def dump(value, state:) = super
106+
107+
# @api private
108+
#
109+
# @param depth [Integer]
110+
#
111+
# @return [String]
112+
def inspect(depth: 0)
113+
# rubocop:disable Layout/LineLength
114+
return super() if depth.positive?
115+
116+
"#{name}[#{values.map { Orb::Internal::Type::Converter.inspect(_1, depth: depth.succ) }.join(' | ')}]"
117+
# rubocop:enable Layout/LineLength
118+
end
106119
end
107120
end
108121
end

lib/orb/internal/type/hash_of.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ module Type
1313
class HashOf
1414
include Orb::Internal::Type::Converter
1515

16+
private_class_method :new
17+
1618
# @param type_info [Hash{Symbol=>Object}, Proc, Orb::Internal::Type::Converter, Class]
1719
#
1820
# @param spec [Hash{Symbol=>Object}] .
@@ -140,7 +142,18 @@ def dump(value, state:)
140142
# @option spec [Boolean] :"nil?"
141143
def initialize(type_info, spec = {})
142144
@item_type_fn = Orb::Internal::Type::Converter.type_info(type_info || spec)
143-
@nilable = spec[:nil?]
145+
@nilable = spec.fetch(:nil?, false)
146+
end
147+
148+
# @api private
149+
#
150+
# @param depth [Integer]
151+
#
152+
# @return [String]
153+
def inspect(depth: 0)
154+
# rubocop:disable Layout/LineLength
155+
"#{self.class}[#{[Orb::Internal::Type::Converter.inspect(item_type, depth: depth.succ), nilable? ? 'nil' : nil].compact.join(' | ')}]"
156+
# rubocop:enable Layout/LineLength
144157
end
145158
end
146159
end

0 commit comments

Comments
 (0)