Class: Yes::Command::Api::Commands::Deserializer

Inherits:
Object
  • Object
show all
Extended by:
Yes::Core::OpenTelemetry::Trackable
Defined in:
lib/yes/command/api/commands/deserializer.rb

Overview

Deserializes command data hashes into command instances. Supports V1, V2, and command group class resolution.

Constant Summary collapse

DeserializationFailed =
Class.new(Yes::Core::Error)

Class Method Summary collapse

Class Method Details

.call(command_data) ⇒ Array<Yes::Core::Command>

Deserializes command data into command instances.

Parameters:

  • command_data (Array<Hash>)

    commands to deserialize

Returns:

  • (Array<Yes::Core::Command>)

    deserialized commands

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/yes/command/api/commands/deserializer.rb', line 20

def call(command_data)
  failed = { invalid: [], not_found: [] }
  commands = []

  command_data.each do |command|
    commands << Kernel.const_get(command_class_name(command)).new(
      { metadata: command[:metadata] }.merge(command[:data])
    ).tap do |cmd|
      singleton_class.current_span&.add_event('Deserialized',
                                              attributes: { 'command' => cmd.to_json })
    end
  rescue NameError
    failed[:not_found] << command
  rescue Yes::Core::Command::Invalid
    failed[:invalid] << command
  end

  if failed.values.flatten.any?
    singleton_class.current_span&.status = ::OpenTelemetry::Trace::Status.error('Deserialization failed')
    singleton_class.current_span&.add_attributes({ 'failed' => failed.to_json })

    raise DeserializationFailed.new(extra: failed)
  end

  commands
end