Class: BinaryCodec::STArray

Inherits:
SerializedType show all
Defined in:
lib/binary-codec/types/st_array.rb

Instance Attribute Summary

Attributes inherited from SerializedType

#bytes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SerializedType

from_bytes, from_hex, from_json, get_type_by_name, #to_byte_sink, #to_bytes, #to_hex, #value_of

Constructor Details

#initialize(byte_buf = nil) ⇒ STArray

Returns a new instance of STArray.



5
6
7
# File 'lib/binary-codec/types/st_array.rb', line 5

def initialize(byte_buf = nil)
  super(byte_buf || [])
end

Class Method Details

.from(value, definitions = nil) ⇒ STArray

Creates a new STArray instance from a value.

Parameters:

  • value (STArray, String, Array<Hash>)

    The value to convert.

  • definitions (Definitions, nil) (defaults to: nil)

    Optional definitions.

Returns:

  • (STArray)

    The created instance.

Raises:

  • (StandardError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/binary-codec/types/st_array.rb', line 13

def self.from(value, definitions = nil)
  return value if value.is_a?(STArray)
  definitions ||= Definitions.instance

  if value.is_a?(String)
    return STArray.new(hex_to_bytes(value))
  end

  if value.is_a?(Array)
    bytes = []
    value.each do |item|
      obj = STObject.from(item, nil, definitions)
      bytes.concat(obj.to_bytes)
      bytes.concat([0xF1]) # ArrayItemEndMarker
    end
    bytes.concat([0xF1]) # ArrayEndMarker
    return STArray.new(bytes)
  end

  raise StandardError, "Cannot construct STArray from #{value.class}"
end

.from_parser(parser, _hint = nil) ⇒ STArray

Creates an STArray instance from a parser.

Parameters:

  • parser (BinaryParser)

    The parser to read from.

  • _hint (Integer, nil) (defaults to: nil)

    Unused hint.

Returns:

  • (STArray)

    The created instance.



39
40
41
42
43
44
45
46
47
48
# File 'lib/binary-codec/types/st_array.rb', line 39

def self.from_parser(parser, _hint = nil)
  bytes = []
  until parser.end?(1) # Look ahead for end marker
    obj = STObject.from_parser(parser)
    bytes.concat(obj.to_bytes)
    bytes.concat(parser.read(1)) # Should be 0xF1 (ArrayItemEndMarker)
  end
  parser.read(1) # Consume 0xF1 (ArrayEndMarker)
  STArray.new(bytes)
end

Instance Method Details

#to_json(_definitions = nil, _field_name = nil) ⇒ Array<Hash>

Returns the JSON representation of the STArray.

Parameters:

  • _definitions (Definitions, nil) (defaults to: nil)

    Unused.

  • _field_name (String, nil) (defaults to: nil)

    Unused.

Returns:

  • (Array<Hash>)

    The JSON representation.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/binary-codec/types/st_array.rb', line 54

def to_json(_definitions = nil, _field_name = nil)
  parser = BinaryParser.new(to_hex)
  result = []
  until parser.end?
    obj = STObject.from_parser(parser)
    result << JSON.parse(obj.to_json)
    # In xrpl.js, array items are STObjects. 
    # After each STObject, there might be an ArrayItemEndMarker if we're not at the end.
    # But wait, STObject.from_parser already reads until ObjectEndMarker.
    # STArray in XRPL is a list of objects, each ending with ObjectEndMarker.
    # The whole array ends with ArrayEndMarker (0xF1).
    # Actually, standard XRPL STArray: [FieldHeader][STObject][FieldHeader][STObject]...[0xF1]
    # Wait, I need to check how xrpl.js handles STArray.
  end
  result
end