Skip to content

Commit 8c6ebea

Browse files
chore: collect metadata from type DSL
1 parent 008e80f commit 8c6ebea

File tree

10 files changed

+159
-12
lines changed

10 files changed

+159
-12
lines changed

lib/onebusaway_sdk/internal/type/array_of.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ def to_sorbet_type
148148
# @option spec [Boolean] :"nil?"
149149
def initialize(type_info, spec = {})
150150
@item_type_fn = OnebusawaySDK::Internal::Type::Converter.type_info(type_info || spec)
151+
@meta = OnebusawaySDK::Internal::Type::Converter.meta_info(type_info, spec)
151152
@nilable = spec.fetch(:nil?, false)
152153
end
153154

lib/onebusaway_sdk/internal/type/base_model.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def fields
5252
#
5353
# @option spec [Boolean] :"nil?"
5454
private def add_field(name_sym, required:, type_info:, spec:)
55+
meta = OnebusawaySDK::Internal::Type::Converter.meta_info(type_info, spec)
5556
type_fn, info =
5657
case type_info
5758
in Proc | OnebusawaySDK::Internal::Type::Converter | Class
@@ -81,7 +82,8 @@ def fields
8182
required: required,
8283
nilable: nilable,
8384
const: const,
84-
type_fn: type_fn
85+
type_fn: type_fn,
86+
meta: meta
8587
}
8688

8789
define_method(setter) do |value|

lib/onebusaway_sdk/internal/type/converter.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,33 @@ def type_info(spec)
9898
end
9999
end
100100

101+
# @api private
102+
#
103+
# @param type_info [Hash{Symbol=>Object}, Proc, OnebusawaySDK::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, OnebusawaySDK::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+
101128
# @api private
102129
#
103130
# @param translate_names [Boolean]

lib/onebusaway_sdk/internal/type/hash_of.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ def to_sorbet_type
168168
# @option spec [Boolean] :"nil?"
169169
def initialize(type_info, spec = {})
170170
@item_type_fn = OnebusawaySDK::Internal::Type::Converter.type_info(type_info || spec)
171+
@meta = OnebusawaySDK::Internal::Type::Converter.meta_info(type_info, spec)
171172
@nilable = spec.fetch(:nil?, false)
172173
end
173174

lib/onebusaway_sdk/internal/type/union.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ module Union
1212
#
1313
# All of the specified variant info for this union.
1414
#
15-
# @return [Array<Array(Symbol, Proc)>]
15+
# @return [Array<Array(Symbol, Proc, Hash{Symbol=>Object})>]
1616
private def known_variants = (@known_variants ||= [])
1717

1818
# @api private
1919
#
20-
# @return [Array<Array(Symbol, Object)>]
20+
# @return [Array<Array(Symbol, Object, Hash{Symbol=>Object})>]
2121
protected def derefed_variants
22-
known_variants.map { |key, variant_fn| [key, variant_fn.call] }
22+
known_variants.map { |key, variant_fn, meta| [key, variant_fn.call, meta] }
2323
end
2424

2525
# All of the specified variants for this union.
2626
#
2727
# @return [Array<Object>]
28-
def variants = derefed_variants.map(&:last)
28+
def variants = derefed_variants.map { _2 }
2929

3030
# @api private
3131
#
@@ -51,12 +51,13 @@ def variants = derefed_variants.map(&:last)
5151
#
5252
# @option spec [Boolean] :"nil?"
5353
private def variant(key, spec = nil)
54+
meta = OnebusawaySDK::Internal::Type::Converter.meta_info(nil, spec)
5455
variant_info =
5556
case key
5657
in Symbol
57-
[key, OnebusawaySDK::Internal::Type::Converter.type_info(spec)]
58+
[key, OnebusawaySDK::Internal::Type::Converter.type_info(spec), meta]
5859
in Proc | OnebusawaySDK::Internal::Type::Converter | Class | Hash
59-
[nil, OnebusawaySDK::Internal::Type::Converter.type_info(key)]
60+
[nil, OnebusawaySDK::Internal::Type::Converter.type_info(key), meta]
6061
end
6162

6263
known_variants << variant_info
@@ -79,7 +80,8 @@ def variants = derefed_variants.map(&:last)
7980
return nil if key == OnebusawaySDK::Internal::OMIT
8081

8182
key = key.to_sym if key.is_a?(String)
82-
known_variants.find { |k,| k == key }&.last&.call
83+
_, found = known_variants.find { |k,| k == key }
84+
found&.call
8385
else
8486
nil
8587
end

rbi/onebusaway_sdk/internal/type/converter.rbi

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,64 @@ module OnebusawaySDK
9595
def self.type_info(spec)
9696
end
9797

98+
# @api private
99+
sig do
100+
params(
101+
type_info:
102+
T.any(
103+
{
104+
const:
105+
T.nilable(
106+
T.any(NilClass, T::Boolean, Integer, Float, Symbol)
107+
),
108+
enum:
109+
T.nilable(
110+
T.proc.returns(
111+
OnebusawaySDK::Internal::Type::Converter::Input
112+
)
113+
),
114+
union:
115+
T.nilable(
116+
T.proc.returns(
117+
OnebusawaySDK::Internal::Type::Converter::Input
118+
)
119+
)
120+
},
121+
T.proc.returns(
122+
OnebusawaySDK::Internal::Type::Converter::Input
123+
),
124+
OnebusawaySDK::Internal::Type::Converter::Input
125+
),
126+
spec:
127+
T.any(
128+
{
129+
const:
130+
T.nilable(
131+
T.any(NilClass, T::Boolean, Integer, Float, Symbol)
132+
),
133+
enum:
134+
T.nilable(
135+
T.proc.returns(
136+
OnebusawaySDK::Internal::Type::Converter::Input
137+
)
138+
),
139+
union:
140+
T.nilable(
141+
T.proc.returns(
142+
OnebusawaySDK::Internal::Type::Converter::Input
143+
)
144+
)
145+
},
146+
T.proc.returns(
147+
OnebusawaySDK::Internal::Type::Converter::Input
148+
),
149+
OnebusawaySDK::Internal::Type::Converter::Input
150+
)
151+
).returns(OnebusawaySDK::Internal::AnyHash)
152+
end
153+
def self.meta_info(type_info, spec)
154+
end
155+
98156
# @api private
99157
sig do
100158
params(translate_names: T::Boolean).returns(

rbi/onebusaway_sdk/internal/type/union.rbi

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ module OnebusawaySDK
1616
T::Array[
1717
[
1818
T.nilable(Symbol),
19-
T.proc.returns(OnebusawaySDK::Internal::Type::Converter::Input)
19+
T.proc.returns(OnebusawaySDK::Internal::Type::Converter::Input),
20+
OnebusawaySDK::Internal::AnyHash
2021
]
2122
]
2223
)
@@ -25,7 +26,13 @@ module OnebusawaySDK
2526
end
2627

2728
# @api private
28-
sig { returns(T::Array[[T.nilable(Symbol), T.anything]]) }
29+
sig do
30+
returns(
31+
T::Array[
32+
[T.nilable(Symbol), T.anything, OnebusawaySDK::Internal::AnyHash]
33+
]
34+
)
35+
end
2936
protected def derefed_variants
3037
end
3138

sig/onebusaway_sdk/internal/type/converter.rbs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@ module OnebusawaySDK
3939
| OnebusawaySDK::Internal::Type::Converter::input spec
4040
) -> (^-> top)
4141

42+
def self.meta_info: (
43+
{
44+
const: (nil | bool | Integer | Float | Symbol)?,
45+
enum: ^-> OnebusawaySDK::Internal::Type::Converter::input?,
46+
union: ^-> OnebusawaySDK::Internal::Type::Converter::input?
47+
}
48+
| ^-> OnebusawaySDK::Internal::Type::Converter::input
49+
| OnebusawaySDK::Internal::Type::Converter::input type_info,
50+
{
51+
const: (nil | bool | Integer | Float | Symbol)?,
52+
enum: ^-> OnebusawaySDK::Internal::Type::Converter::input?,
53+
union: ^-> OnebusawaySDK::Internal::Type::Converter::input?
54+
}
55+
| ^-> OnebusawaySDK::Internal::Type::Converter::input
56+
| OnebusawaySDK::Internal::Type::Converter::input spec
57+
) -> ::Hash[Symbol, top]
58+
4259
def self.new_coerce_state: (
4360
?translate_names: bool
4461
) -> OnebusawaySDK::Internal::Type::Converter::coerce_state

sig/onebusaway_sdk/internal/type/union.rbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ module OnebusawaySDK
55
include OnebusawaySDK::Internal::Type::Converter
66
include OnebusawaySDK::Internal::Util::SorbetRuntimeSupport
77

8-
private def self.known_variants: -> ::Array[[Symbol?, (^-> OnebusawaySDK::Internal::Type::Converter::input)]]
8+
private def self.known_variants: -> ::Array[[Symbol?, (^-> OnebusawaySDK::Internal::Type::Converter::input), ::Hash[Symbol, top]]]
99

10-
def self.derefed_variants: -> ::Array[[Symbol?, top]]
10+
def self.derefed_variants: -> ::Array[[Symbol?, top, ::Hash[Symbol, top]]]
1111

1212
def self.variants: -> ::Array[top]
1313

test/onebusaway_sdk/internal/type/base_model_test.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,3 +687,35 @@ def test_equality
687687
end
688688
end
689689
end
690+
691+
class OnebusawaySDK::Test::MetaInfoTest < Minitest::Test
692+
A1 = OnebusawaySDK::Internal::Type::ArrayOf[Integer, nil?: true, doc: "dog"]
693+
H1 = OnebusawaySDK::Internal::Type::HashOf[-> { String }, nil?: true, doc: "dawg"]
694+
695+
class M1 < OnebusawaySDK::Internal::Type::BaseModel
696+
required :a, Integer, doc: "dog"
697+
optional :b, -> { String }, nil?: true, doc: "dawg"
698+
end
699+
700+
module U1
701+
extend OnebusawaySDK::Internal::Type::Union
702+
703+
variant -> { Integer }, const: 2, doc: "dog"
704+
variant -> { String }, doc: "dawg"
705+
end
706+
707+
def test_meta_retrieval
708+
m1 = A1.instance_variable_get(:@meta)
709+
m2 = H1.instance_variable_get(:@meta)
710+
assert_equal({doc: "dog"}, m1)
711+
assert_equal({doc: "dawg"}, m2)
712+
713+
ma, mb = M1.fields.fetch_values(:a, :b)
714+
assert_equal({doc: "dog"}, ma.fetch(:meta))
715+
assert_equal({doc: "dawg"}, mb.fetch(:meta))
716+
717+
ua, ub = U1.send(:known_variants).map(&:last)
718+
assert_equal({doc: "dog"}, ua)
719+
assert_equal({doc: "dawg"}, ub)
720+
end
721+
end

0 commit comments

Comments
 (0)