Class: OpenAI::Internal::Type::HashOf Abstract Private

Inherits:
Object
  • Object
show all
Includes:
Converter, Util::SorbetRuntimeSupport
Defined in:
lib/openai/internal/type/hash_of.rb,
sig/openai/internal/type/hash_of.rbs

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

This class is abstract.

Hash of items of a given type.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::SorbetRuntimeSupport

#const_missing, #define_sorbet_constant!, #sorbet_constant_defined?, to_sorbet_type

Methods included from Converter

coerce, coerce_with_error, dump, inspect, meta_info, new_coerce_state, type_info

Constructor Details

#initialize(type_info, spec = {}) ⇒ HashOf

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.

Returns a new instance of HashOf.

Parameters:

  • type_info (Hash{Symbol=>Object}, Proc, OpenAI::Internal::Type::Converter, Class)
  • spec (Hash{Symbol=>Object}) (defaults to: {})

    .

    @option spec [NilClass, TrueClass, FalseClass, Integer, Float, Symbol] :const

    @option spec [Proc] :enum

    @option spec [Proc] :union

    @option spec [Boolean] :"nil?"



176
177
178
179
180
# File 'lib/openai/internal/type/hash_of.rb', line 176

def initialize(type_info, spec = {})
  @item_type_fn = OpenAI::Internal::Type::Converter.type_info(type_info || spec)
  @meta = OpenAI::Internal::Type::Converter.meta_info(type_info, spec)
  @nilable = spec.fetch(:nil?, false)
end

Class Method Details

.[](type_info, spec = {}) ⇒ self

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.

Parameters:

  • type_info (Hash{Symbol=>Object}, Proc, OpenAI::Internal::Type::Converter, Class)
  • spec (Hash{Symbol=>Object})

    .

    @option spec [NilClass, TrueClass, FalseClass, Integer, Float, Symbol] :const

    @option spec [Proc] :enum

    @option spec [Proc] :union

    @option spec [Boolean] :"nil?"

Returns:

  • (self)


34
# File 'lib/openai/internal/type/hash_of.rb', line 34

def self.[](...) = new(...)

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:



65
66
67
# File 'lib/openai/internal/type/hash_of.rb', line 65

def ==(other)
  other.is_a?(OpenAI::Internal::Type::HashOf) && other.nilable? == nilable? && other.item_type == item_type
end

#===(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/openai/internal/type/hash_of.rb', line 41

def ===(other)
  type = item_type
  case other
  in Hash
    other.all? do |key, val|
      case [key, val]
      in [Symbol | String, ^type]
        true
      in [Symbol | String, nil]
        nilable?
      else
        false
      end
    end
  else
    false
  end
end

#coerce(value, state:) ⇒ Hash{Symbol=>Object}, 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.

Parameters:

  • value (Hash{Object=>Object}, Object)
  • state (Hash{Symbol=>Object})

    .

    @option state [Boolean] :translate_names

    @option state [Boolean] :strictness

    @option state [HashSymbol=>Object] :exactness

    @option state [Class] :error

    @option state [Integer] :branched

Returns:

  • (Hash{Symbol=>Object}, Object)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/openai/internal/type/hash_of.rb', line 91

def coerce(value, state:)
  exactness = state.fetch(:exactness)

  unless value.is_a?(Hash)
    exactness[:no] += 1
    state[:error] = TypeError.new("#{value.class} can't be coerced into #{Hash}")
    return value
  end

  target = item_type
  exactness[:yes] += 1
  previous_error = state.fetch(:error)
  last_error = nil
  converted = value.to_h do |key, val|
    k = key.is_a?(String) ? key.to_sym : key
    v = case [nilable?, val]
    in [true, nil]
      exactness[:yes] += 1
      nil
    else
      coerced, value_error = OpenAI::Internal::Type::Converter.coerce_with_error(target, val, state: state)
      last_error = value_error if value_error
      coerced
    end

    exactness[:no] += 1 unless k.is_a?(Symbol)
    [k, v]
  end

  state[:error] = last_error || previous_error
  converted
end

#dump(value, state:) ⇒ Hash{Symbol=>Object}, 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.

Parameters:

  • value (Hash{Object=>Object}, Object)
  • state (Hash{Symbol=>Object})

    .

    @option state [Boolean] :can_retry

Returns:

  • (Hash{Symbol=>Object}, Object)


133
134
135
136
137
138
139
140
141
142
# File 'lib/openai/internal/type/hash_of.rb', line 133

def dump(value, state:)
  target = item_type
  if value.is_a?(Hash)
    value.transform_values do
      OpenAI::Internal::Type::Converter.dump(target, _1, state: state)
    end
  else
    super
  end
end

#hashInteger

Returns:

  • (Integer)


72
# File 'lib/openai/internal/type/hash_of.rb', line 72

def hash = [self.class, item_type, nilable?].hash

#inspect(depth: 0) ⇒ String

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.

Parameters:

  • depth (Integer) (defaults to: 0)
  • depth: (Integer) (defaults to: 0)

Returns:

  • (String)


187
188
189
190
191
# File 'lib/openai/internal/type/hash_of.rb', line 187

def inspect(depth: 0)
  items = OpenAI::Internal::Type::Converter.inspect(item_type, depth: depth.succ)

  "#{self.class}[#{[items, nilable? ? "nil" : nil].compact.join(" | ")}]"
end

#item_typegeneric<Elem>

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.

Returns:

  • (generic<Elem>)


156
# File 'lib/openai/internal/type/hash_of.rb', line 156

protected def item_type = @item_type_fn.call

#nilable?Boolean

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.

Returns:



161
# File 'lib/openai/internal/type/hash_of.rb', line 161

protected def nilable? = @nilable

#to_sorbet_typeObject

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.

Returns:

  • (Object)


147
148
149
150
151
# File 'lib/openai/internal/type/hash_of.rb', line 147

def to_sorbet_type
  type = OpenAI::Internal::Util::SorbetRuntimeSupport.to_sorbet_type(item_type)
  type = T.nilable(type) if nilable?
  T::Hash[Symbol, type]
end