Module: Knockapi::Internal::Type::Union Private

Includes:
Converter, Util::SorbetRuntimeSupport
Included in:
Models::InlineTenantRequest, Models::MessageDeliveryLog::Request::Body, Models::MessageDeliveryLog::Response::Body, Models::MessageGetContentResponse::Data, Models::MessageGetContentResponse::Data::MessageInAppFeedContent::Block, Models::Messages::BatchGetContentResponseItem::Data, Models::Messages::BatchGetContentResponseItem::Data::MessageInAppFeedContent::Block, Models::Recipient, Models::RecipientReference, Models::RecipientRequest, Models::Recipients::ChannelDataRequest::Data, Models::Recipients::DiscordChannelData::Connection, Models::Recipients::InlineChannelDataRequestItem, Models::Recipients::MsTeamsChannelData::Connection, Models::Recipients::PreferenceSet::Category, Models::Recipients::PreferenceSet::Category::PreferenceSetWorkflowCategorySettingObject::Channel, Models::Recipients::PreferenceSet::Channel, Models::Recipients::PreferenceSet::Workflow, Models::Recipients::PreferenceSet::Workflow::PreferenceSetWorkflowCategorySettingObject::Channel, Models::Recipients::PreferenceSetChannelTypes::Chat, Models::Recipients::PreferenceSetChannelTypes::Email, Models::Recipients::PreferenceSetChannelTypes::HTTP, Models::Recipients::PreferenceSetChannelTypes::InAppFeed, Models::Recipients::PreferenceSetChannelTypes::Push, Models::Recipients::PreferenceSetChannelTypes::SMS, Models::Recipients::PreferenceSetRequest::Category, Models::Recipients::PreferenceSetRequest::Category::PreferenceSetWorkflowCategorySettingObject::Channel, Models::Recipients::PreferenceSetRequest::Channel, Models::Recipients::PreferenceSetRequest::Workflow, Models::Recipients::PreferenceSetRequest::Workflow::PreferenceSetWorkflowCategorySettingObject::Channel, Models::Recipients::RecipientsChannelData::Data, Models::Recipients::SlackChannelData::Connection, Models::Users::FeedListItemsResponse::Block
Defined in:
lib/knockapi/internal/type/union.rb

Overview

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.

Examples:

# `recipient` is a `Knockapi::Recipient`
case recipient
when Knockapi::User
  puts(recipient.id)
when Knockapi::Object
  puts(recipient._typename)
else
  puts(recipient)
end

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, dump, inspect, meta_info, new_coerce_state, type_info

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

Returns:



120
121
122
# File 'lib/knockapi/internal/type/union.rb', line 120

def ==(other)
  Knockapi::Internal::Type::Union === other && other.derefed_variants == derefed_variants
end

#===(other) ⇒ Boolean

Parameters:

Returns:



109
110
111
112
113
# File 'lib/knockapi/internal/type/union.rb', line 109

def ===(other)
  known_variants.any? do |_, variant_fn|
    variant_fn.call === other
  end
end

#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.

Tries to efficiently coerce the given value to one of the known variants.

If the value cannot match any of the known variants, the coercion is considered non-viable and returns the original value.

Parameters:

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

    .

    @option state [Boolean] :translate_names

    @option state [Boolean] :strictness

    @option state [HashSymbol=>Object] :exactness

    @option state [Class<StandardError>] :error

    @option state [Integer] :branched

Returns:



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
# File 'lib/knockapi/internal/type/union.rb', line 151

def coerce(value, state:)
  if (target = resolve_variant(value))
    return Knockapi::Internal::Type::Converter.coerce(target, value, state: state)
  end

  strictness = state.fetch(:strictness)
  exactness = state.fetch(:exactness)

  alternatives = []
  known_variants.each do |_, variant_fn|
    target = variant_fn.call
    exact = state[:exactness] = {yes: 0, no: 0, maybe: 0}
    state[:branched] += 1

    coerced = Knockapi::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
    state[:error] = ArgumentError.new("no matching variant for #{value.inspect}")
    value
  in [[_, exact, coerced], *]
    exact.each { exactness[_1] += _2 }
    coerced
  end
    .tap { state[:exactness] = exactness }
ensure
  state[:strictness] = strictness
end

#dump(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.

Parameters:

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

    .

    @option state [Boolean] :can_retry

Returns:



199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/knockapi/internal/type/union.rb', line 199

def dump(value, state:)
  if (target = resolve_variant(value))
    return Knockapi::Internal::Type::Converter.dump(target, value, state: state)
  end

  known_variants.each do
    target = _2.call
    return Knockapi::Internal::Type::Converter.dump(target, value, state: state) if target === value
  end

  super
end

#hashInteger

Returns:

  • (Integer)


127
# File 'lib/knockapi/internal/type/union.rb', line 127

def hash = variants.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)

Returns:

  • (String)


235
236
237
238
239
240
241
242
243
244
# File 'lib/knockapi/internal/type/union.rb', line 235

def inspect(depth: 0)
  if depth.positive?
    return is_a?(Module) ? super() : self.class.name
  end

  members = variants.map { Knockapi::Internal::Type::Converter.inspect(_1, depth: depth.succ) }
  prefix = is_a?(Module) ? name : self.class.name

  "#{prefix}[#{members.join(' | ')}]"
end

#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:



215
216
217
218
219
220
221
222
223
224
225
# File 'lib/knockapi/internal/type/union.rb', line 215

def to_sorbet_type
  types = variants.map { Knockapi::Internal::Util::SorbetRuntimeSupport.to_sorbet_type(_1) }.uniq
  case types
  in []
    T.noreturn
  in [type]
    type
  else
    T.any(*types)
  end
end

#variantsArray<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.

All of the specified variants for this union.

Returns:



39
# File 'lib/knockapi/internal/type/union.rb', line 39

def variants = derefed_variants.map { _2 }