class Selenium::WebDriver::BiDi::Serialization::Union

@api private
end
variants(‘css’ => ‘BrowsingContext::CssLocator’)
discriminator ‘type’
class Locator < Serialization::Union
Subclassed (never instantiated) — each union holds its own dispatch table.
table dispatch; presence rules and a no-tag fallback cover unions without one.
Resolves a wire payload to the right Data variant: a shared discriminator gives

def build(**kwargs)

is a caller error (unlike an unknown inbound value), so it fails loudly.
(asserted at generation), so they match the kwargs by symbol. A mismatch here
Transport cannot). Dispatch keys are wire names equal to their ruby kwarg
so its typed as_json drives null-vs-absent per field (a flat hash through
Outbound mirror of from_json: build the variant the command's kwargs describe
def build(**kwargs)
  variant = outbound_variant(kwargs) ||
            raise(::ArgumentError, "no #{name} variant matches #{kwargs.inspect}")
  klass = Protocol.const_get(variant)
  # An omitted optional arrives as UNSET; forward only what was provided. A provided
  # key that isn't a field of the chosen variant is an invalid combination for this union.
  provided = kwargs.reject { |_, value| UNSET.equal?(value) }
  invalid = provided.keys - klass.fields.map(&:name)
  return klass.new(**provided) if invalid.empty?
  raise ::ArgumentError, "invalid combination for #{name}: #{invalid.join(', ')}"
end

def discriminator(wire_key, values = {})

inbound payload tag (a wire string) can be matched to the symbol-keyed table.
values maps each variant's discriminator symbol to its wire token, so an
def discriminator(wire_key, values = {})
  @discriminator = wire_key
  @discriminator_values = values
end

def fallback(path) = @fallback = path

def fallback(path) = @fallback = path

def from_json(json_payload)

no object to dispatch on, so it is returned unchanged.
A non-Hash payload is a bare scalar arm (e.g. input.Origin's "viewport") with
def from_json(json_payload)
  return json_payload unless json_payload.is_a?(::Hash)
  variant = select(json_payload)
  unless variant
    raise Error::WebDriverError,
          "#{name} received a variant not in this Selenium's BiDi schema: #{json_payload.inspect}"
  end
  Protocol.const_get(variant).from_json(json_payload)
end

def outbound_variant(kwargs)

rejected at construction (Data.new), not here.
An explicit nil kwarg still counts as supplied; a non-nullable field set to nil is
def outbound_variant(kwargs)
  tag = @discriminator ? kwargs.fetch(@discriminator.to_sym, UNSET) : UNSET
  variant_for(tag) { |k| kwargs.key?(k.to_sym) && !UNSET.equal?(kwargs[k.to_sym]) }
end

def payload_tag(json_payload)

unrecognized tag falls through as-is so select misses and from_json raises.
The wire tag mapped back to its variant symbol (the table's key); an
def payload_tag(json_payload)
  return UNSET unless @discriminator && json_payload.key?(@discriminator)
  wire = json_payload[@discriminator]
  @discriminator_values.key(wire) || wire
end

def presence(rules) = @presence = rules

def presence(rules) = @presence = rules

def select(json_payload)

"null" tag), so it is matched by key presence.
The discriminator value may legitimately be null (e.g. script.NullValue's
def select(json_payload)
  variant_for(payload_tag(json_payload)) { |k| json_payload.key?(k) }
end

def variant_for(tag, &supplied)

The matching variant's ref, or nil when none matches (the fallback if declared).
def variant_for(tag, &supplied)
  return @variants[tag] if !UNSET.equal?(tag) && @variants&.key?(tag)
  @presence&.each { |path, keys| return path if keys.all?(&supplied) }
  @fallback
end

def variants(table) = @variants = table

def variants(table) = @variants = table