module Anthropic::Internal::Type::Union

def ==(other)

Returns:
  • (Boolean) -

Parameters:
  • other (Object) --
def ==(other)
  # rubocop:disable Layout/LineLength
  other.is_a?(Module) && other.singleton_class <= Anthropic::Internal::Type::Union && other.derefed_variants == derefed_variants
  # rubocop:enable Layout/LineLength
end

def ===(other)

Returns:
  • (Boolean) -

Parameters:
  • other (Object) --
def ===(other)
  known_variants.any? do |_, variant_fn|
    variant_fn.call === other
  end
end

def coerce(value, state:)

Returns:
  • (Object) -

Options Hash: (**state)
  • :branched (Integer) --
  • :exactness (Hash{Symbol=>Object}) --
  • :strictness (Boolean, :strong) --

Parameters:
  • state (Hash{Symbol=>Object}) -- .
  • value (Object) --

Other tags:
    Api: - private
def coerce(value, state:)
  if (target = resolve_variant(value))
    return Anthropic::Internal::Type::Converter.coerce(target, value, state: state)
  end
  strictness = state.fetch(:strictness)
  exactness = state.fetch(:exactness)
  state[:strictness] = strictness == :strong ? true : strictness
  alternatives = []
  known_variants.each do |_, variant_fn|
    target = variant_fn.call
    exact = state[:exactness] = {yes: 0, no: 0, maybe: 0}
    state[:branched] += 1
    coerced = Anthropic::Internal::Type::Converter.coerce(target, value, state: state)
    yes, no, maybe = exact.values
    if (no + maybe).zero? || (!strictness && yes.positive?)
      exact.each { exactness[_1] += _2 }
      state[:exactness] = exactness
      return coerced
    elsif maybe.positive?
      alternatives << [[-yes, -maybe, no], exact, coerced]
    end
  end
  case alternatives.sort_by(&:first)
  in []
    exactness[:no] += 1
    if strictness == :strong
      message = "no possible conversion of #{value.class} into a variant of #{target.inspect}"
      raise ArgumentError.new(message)
    end
    value
  in [[_, exact, coerced], *]
    exact.each { exactness[_1] += _2 }
    coerced
  end
    .tap { state[:exactness] = exactness }
ensure
  state[:strictness] = strictness
end

def derefed_variants

Returns:
  • (Array) -

Other tags:
    Api: - private
def derefed_variants
ariants.map { |key, variant_fn| [key, variant_fn.call] }

def discriminator(property)

Parameters:
  • property (Symbol) --

Other tags:
    Api: - private
def discriminator(property)
roperty
bol
criminator = property

def dump(value, state:)

Returns:
  • (Object) -

Options Hash: (**state)
  • :can_retry (Boolean) --

Parameters:
  • state (Hash{Symbol=>Object}) -- .
  • value (Object) --

Other tags:
    Api: - private
def dump(value, state:)
  if (target = resolve_variant(value))
    return Anthropic::Internal::Type::Converter.dump(target, value, state: state)
  end
  known_variants.each do
    target = _2.call
    return Anthropic::Internal::Type::Converter.dump(target, value, state: state) if target === value
  end
  super
end

def known_variants = (@known_variants ||= [])

Returns:
  • (Array) -

Other tags:
    Api: - private
def known_variants = (@known_variants ||= [])

def resolve_variant(value)

Returns:
  • (Anthropic::Internal::Type::Converter, Class, nil) -

Parameters:
  • value (Object) --

Other tags:
    Api: - private
def resolve_variant(value)
@discriminator, value]
 Anthropic::Internal::Type::BaseModel]
e.class
mbol, Hash]
= value.fetch(@discriminator) do
lue.fetch(@discriminator.to_s, Anthropic::Internal::OMIT)
rn nil if key == Anthropic::Internal::OMIT
= key.to_sym if key.is_a?(String)
n_variants.find { |k,| k == key }&.last&.call

def variant(key, spec = nil)

Options Hash: (**spec)
  • :"nil?" (Boolean) --
  • :union (Proc) --
  • :enum (Proc) --
  • :const (NilClass, TrueClass, FalseClass, Integer, Float, Symbol) --

Parameters:
  • spec (Hash{Symbol=>Object}, Proc, Anthropic::Internal::Type::Converter, Class) -- .
  • key (Symbol, Hash{Symbol=>Object}, Proc, Anthropic::Internal::Type::Converter, Class) --

Other tags:
    Api: - private
def variant(key, spec = nil)
t_info =
 key
ymbol
ey, Anthropic::Internal::Type::Converter.type_info(spec)]
roc | Anthropic::Internal::Type::Converter | Class | Hash
il, Anthropic::Internal::Type::Converter.type_info(key)]
variants << variant_info

def variants = derefed_variants.map(&:last)

Returns:
  • (Array) -
    def variants = derefed_variants.map(&:last)