Class: Kobako::Transport::Request

Inherits:
Data
  • Object
show all
Defined in:
lib/kobako/transport/request.rb,
sig/kobako/transport/request.rbs

Overview

Value object for a single guest-initiated Transport Request (docs/wire-codec.md Envelope Encoding → Request).

5-element msgpack array: [target, method_name, args, kwargs, block_given]. target is either a String (+"::"+, e.g. "MyService::KV") or a Handle. SPEC pins kwargs map keys to ext 0x00 Symbol; enforced at construction so the Value Object is the single source of truth. block_given is a Boolean signalling whether the guest call site supplied a block; the block body itself never crosses the wire.

Built on the class X < Data.define(...) subclass form so the class body is fully Steep-visible; see lib/kobako/outcome/panic.rb for the rationale.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target:, method_name:, args: [], kwargs: {}, block_given: false) ⇒ Request

Returns a new instance of Request.

Parameters:

  • target: (String, Kobako::Handle)
  • method_name: (String)
  • args: (Array[untyped]) (defaults to: [])
  • kwargs: (Hash[Symbol, untyped]) (defaults to: {})
  • block_given: (Boolean) (defaults to: false)

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/kobako/transport/request.rb', line 26

def initialize(target:, method_name:, args: [], kwargs: {}, block_given: false)
  unless target.is_a?(String) || target.is_a?(Kobako::Handle)
    raise ArgumentError, "Request target must be String or Kobako::Handle, got #{target.class}"
  end
  raise ArgumentError, "Request method_name must be String" unless method_name.is_a?(String)
  raise ArgumentError, "Request args must be Array"         unless args.is_a?(Array)
  unless block_given.is_a?(TrueClass) || block_given.is_a?(FalseClass)
    raise ArgumentError, "Request block_given must be Boolean, got #{block_given.class}"
  end

  validate_kwargs!(kwargs)
  super
end

Instance Attribute Details

#argsArray[untyped] (readonly)

Returns the value of attribute args.

Returns:

  • (Array[untyped])


6
7
8
# File 'sig/kobako/transport/request.rbs', line 6

def args
  @args
end

#block_givenBoolean (readonly)

Returns the value of attribute block_given.

Returns:

  • (Boolean)


8
9
10
# File 'sig/kobako/transport/request.rbs', line 8

def block_given
  @block_given
end

#kwargsHash[Symbol, untyped] (readonly)

Returns the value of attribute kwargs.

Returns:

  • (Hash[Symbol, untyped])


7
8
9
# File 'sig/kobako/transport/request.rbs', line 7

def kwargs
  @kwargs
end

#method_nameString (readonly)

Returns the value of attribute method_name.

Returns:

  • (String)


5
6
7
# File 'sig/kobako/transport/request.rbs', line 5

def method_name
  @method_name
end

#targetString, Kobako::Handle (readonly)

Returns the value of attribute target.

Returns:



4
5
6
# File 'sig/kobako/transport/request.rbs', line 4

def target
  @target
end

Class Method Details

.decode(bytes) ⇒ Request

Decode bytes into a Kobako::Transport::Request. Raises Codec::InvalidType when the envelope is not the expected 5-element msgpack array, or when the Value Object's construction invariants reject the decoded fields.

Parameters:

  • bytes (String)

Returns:



49
50
51
52
53
54
55
56
57
58
# File 'lib/kobako/transport/request.rb', line 49

def self.decode(bytes)
  Codec::Decoder.decode(bytes) do |arr|
    unless arr.is_a?(Array) && arr.length == 5
      raise Codec::InvalidType, "Request envelope is malformed (expected a 5-element array)"
    end

    target, method_name, args, kwargs, block_given = arr
    new(target: target, method_name: method_name, args: args, kwargs: kwargs, block_given: block_given)
  end
end

.newRequest

Parameters:

  • target: (String, Kobako::Handle)
  • method_name: (String)
  • args: (Array[untyped])
  • kwargs: (Hash[Symbol, untyped])
  • block_given: (Boolean)

Returns:



10
# File 'sig/kobako/transport/request.rbs', line 10

def self.new: (target: String | Kobako::Handle, method_name: String, ?args: Array[untyped], ?kwargs: Hash[Symbol, untyped], ?block_given: bool) -> Request

Instance Method Details

#==Boolean

Parameters:

  • other (Object)

Returns:

  • (Boolean)


20
# File 'sig/kobako/transport/request.rbs', line 20

def ==: (untyped other) -> bool

#encodeString

Encode this Request to msgpack bytes. The Value Object's own invariants are the contract; this method does not re-check the shape.

Returns:

  • (String)


42
43
44
# File 'lib/kobako/transport/request.rb', line 42

def encode
  Codec::Encoder.encode([target, method_name, args, kwargs, block_given])
end

#hashInteger

Returns:

  • (Integer)


22
# File 'sig/kobako/transport/request.rbs', line 22

def hash: () -> Integer

#validate_kwargs!(kwargs) ⇒ void

This method returns an undefined value.

Parameters:

  • kwargs (Hash[untyped, untyped])

Raises:

  • (ArgumentError)


62
63
64
65
66
67
68
# File 'lib/kobako/transport/request.rb', line 62

def validate_kwargs!(kwargs)
  raise ArgumentError, "Request kwargs must be Hash" unless kwargs.is_a?(Hash)

  kwargs.each_key do |k|
    raise ArgumentError, "Request kwargs keys must be Symbol, got #{k.class}" unless k.is_a?(Symbol)
  end
end

#withRequest

Parameters:

  • target: (String, Kobako::Handle)
  • method_name: (String)
  • args: (Array[untyped])
  • kwargs: (Hash[Symbol, untyped])
  • block_given: (Boolean)

Returns:



18
# File 'sig/kobako/transport/request.rbs', line 18

def with: (?target: String | Kobako::Handle, ?method_name: String, ?args: Array[untyped], ?kwargs: Hash[Symbol, untyped], ?block_given: bool) -> Request