Module: FinchAPI::Type::Converter Private
- Defined in:
- lib/finch-api/type/converter.rb
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Class Method Summary collapse
-
.coerce(target, value, state: {strictness: true, exactness: {yes: 0, no: 0, maybe: 0}, branched: 0}) ⇒ Object
private
Based on ‘target`, transform `value` into `target`, to the extent possible:.
- .dump(target, value) ⇒ Object private
- .type_info(spec) ⇒ Proc private
Instance Method Summary collapse
- #coerce(value, state:) ⇒ Object private
- #dump(value) ⇒ Object private
Class Method Details
.coerce(target, value, state: {strictness: true, exactness: {yes: 0, no: 0, maybe: 0}, branched: 0}) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Based on ‘target`, transform `value` into `target`, to the extent possible:
1. if the given `value` conforms to `target` already, return the given `value`
2. if it's possible and safe to convert the given `value` to `target`, then the
converted value
3. otherwise, the given `value` unaltered
The coercion process is subject to improvement between minor release versions.
See https://docs.pydantic.dev/latest/concepts/unions/#smart-mode
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/finch-api/type/converter.rb', line 116 def coerce( target, value, state: {strictness: true, exactness: {yes: 0, no: 0, maybe: 0}, branched: 0} ) strictness, exactness = state.fetch_values(:strictness, :exactness) case target in FinchAPI::Type::Converter return target.coerce(value, state: state) in Class if value.is_a?(target) exactness[:yes] += 1 return value end case target in -> { _1 <= NilClass } exactness[value.nil? ? :yes : :maybe] += 1 return nil in -> { _1 <= Integer } if value.is_a?(Integer) exactness[:yes] += 1 return value elsif strictness == :strong = "no implicit conversion of #{value.class} into #{target.inspect}" raise TypeError.new() else Kernel.then do return Integer(value).tap { exactness[:maybe] += 1 } rescue ArgumentError, TypeError end end in -> { _1 <= Float } if value.is_a?(Numeric) exactness[:yes] += 1 return Float(value) elsif strictness == :strong = "no implicit conversion of #{value.class} into #{target.inspect}" raise TypeError.new() else Kernel.then do return Float(value).tap { exactness[:maybe] += 1 } rescue ArgumentError, TypeError end end in -> { _1 <= String } case value in String | Symbol | Numeric exactness[value.is_a?(Numeric) ? :maybe : :yes] += 1 return value.to_s else if strictness == :strong = "no implicit conversion of #{value.class} into #{target.inspect}" raise TypeError.new() end end in -> { _1 <= Date || _1 <= Time } Kernel.then do return target.parse(value).tap { exactness[:yes] += 1 } rescue ArgumentError, TypeError => e raise e if strictness == :strong end in -> { _1 <= IO } if value.is_a?(String) exactness[:yes] += 1 return StringIO.new(value.b) else end in Symbol if (value.is_a?(Symbol) || value.is_a?(String)) && value.to_sym == target exactness[:yes] += 1 return target elsif strictness == :strong = "cannot convert non-matching #{value.class} into #{target.inspect}" raise ArgumentError.new() end else end exactness[:no] += 1 value end |
.dump(target, value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
205 206 207 |
# File 'lib/finch-api/type/converter.rb', line 205 def dump(target, value) target.is_a?(FinchAPI::Type::Converter) ? target.dump(value) : FinchAPI::Unknown.dump(value) end |
.type_info(spec) ⇒ Proc
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/finch-api/type/converter.rb', line 58 def type_info(spec) case spec in Proc spec in Hash type_info(spec.slice(:const, :enum, :union).first&.last) in true | false -> { FinchAPI::BooleanModel } in FinchAPI::Type::Converter | Class | Symbol -> { spec } in NilClass | Integer | Float -> { spec.class } end end |
Instance Method Details
#coerce(value, state:) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
22 |
# File 'lib/finch-api/type/converter.rb', line 22 def coerce(value, state:) = (raise NotImplementedError) |
#dump(value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/finch-api/type/converter.rb', line 29 def dump(value) case value in Array value.map { FinchAPI::Unknown.dump(_1) } in Hash value.transform_values { FinchAPI::Unknown.dump(_1) } in FinchAPI::BaseModel value.class.dump(value) else value end end |