xdr 3.0.2 → 3.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +15 -10
- data/README.md +20 -20
- data/lib/xdr/array.rb +3 -3
- data/lib/xdr/bool.rb +7 -7
- data/lib/xdr/concerns/array_converter.rb +2 -2
- data/lib/xdr/concerns/converts_to_xdr.rb +11 -11
- data/lib/xdr/concerns/float_converter.rb +1 -1
- data/lib/xdr/concerns/integer_converter.rb +1 -1
- data/lib/xdr/concerns/reads_bytes.rb +2 -1
- data/lib/xdr/concerns/string_converter.rb +1 -1
- data/lib/xdr/double.rb +2 -3
- data/lib/xdr/dsl/enum.rb +8 -9
- data/lib/xdr/dsl/struct.rb +1 -2
- data/lib/xdr/dsl/union.rb +12 -12
- data/lib/xdr/dsl.rb +1 -1
- data/lib/xdr/enum.rb +6 -6
- data/lib/xdr/float.rb +2 -3
- data/lib/xdr/hyper.rb +2 -2
- data/lib/xdr/int.rb +2 -2
- data/lib/xdr/namespace.rb +7 -9
- data/lib/xdr/opaque.rb +5 -5
- data/lib/xdr/option.rb +5 -5
- data/lib/xdr/quadruple.rb +1 -1
- data/lib/xdr/rpc/record.rb +2 -2
- data/lib/xdr/rpc/record_reader.rb +7 -7
- data/lib/xdr/string.rb +4 -4
- data/lib/xdr/struct.rb +15 -16
- data/lib/xdr/struct_validator.rb +1 -1
- data/lib/xdr/union.rb +31 -32
- data/lib/xdr/union_validator.rb +1 -1
- data/lib/xdr/unsigned_hyper.rb +2 -2
- data/lib/xdr/unsigned_int.rb +2 -2
- data/lib/xdr/var_array.rb +4 -4
- data/lib/xdr/var_opaque.rb +3 -3
- data/lib/xdr/version.rb +1 -1
- data/lib/xdr/void.rb +1 -1
- data/lib/xdr.rb +16 -10
- metadata +13 -54
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5e73176f8adac880de7eb0ed3761730773c9dadac9f572e9a9fd1f75ff396488
|
4
|
+
data.tar.gz: ea7c2d4f8fc312f33d4fb81be7a9074a6ed2f6e5dec2cccb41f7c00cdabff248
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f6825750389ef2f19882533f169c9de4c05a264fa19228fcdbed3f3276196473d6e6e2cac75c420926a097f2543a71100cc17ad0784864e1c18c24da66aa455
|
7
|
+
data.tar.gz: 3c04035a3532da2c732aa47664a9bf4cfe79616806f3193948d5c8c37aa2c2862153cb2a74fe13b0afcb30d25f2f1f39729f510ac67655891a9d466de767ba64
|
data/CHANGELOG.md
CHANGED
@@ -5,24 +5,29 @@ All notable changes to this project will be documented in this file.
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
7
|
|
8
|
-
|
8
|
+
### [3.0.3](https://github.com/astroband/ruby-xdr/compare/v3.0.2...v3.0.3) (2022-02-18)
|
9
|
+
### Bug Fixes
|
10
|
+
- make `XDR::Option` write empty strings as values ([#15](https://github.com/astroband/ruby-xdr/issues/15)) ([ccab229](https://github.com/astroband/ruby-xdr/commit/ccab22928a1104dee525211b51d1d567079d27ba))
|
9
11
|
|
10
|
-
|
12
|
+
|
13
|
+
## [3.0.2](https://github.com/astroband/ruby-xdr/compare/v3.0.1...v3.0.2) - 2020-10-20
|
11
14
|
### Changed
|
12
|
-
- Relax lower bound for ActiveSupport dependency to support usage in Rails 4.2 (
|
15
|
+
- Relax lower bound for ActiveSupport dependency to support usage in Rails 4.2 ([#12](https://github.com/astroband/ruby-xdr/pull/12))
|
16
|
+
|
17
|
+
### Bug Fixes
|
18
|
+
- Fix non-bang arm accessors for XDR::Union ([#13](https://github.com/astroband/ruby-xdr/pull/13))
|
19
|
+
- Fix .from_xdr encoding param to accept symbols ([#14](https://github.com/astroband/ruby-xdr/pull/14))
|
13
20
|
|
14
|
-
### Fixed
|
15
|
-
- Fix non-bang arm accessors for XDR::Union ((#13)[https://github.com/stellar/ruby-xdr/pull/13])
|
16
|
-
- Fix .from_xdr encoding param to accept symbols ((#14)[https://github.com/stellar/ruby-xdr/pull/14])
|
17
21
|
|
18
|
-
## [3.0.1](https://github.com/
|
22
|
+
## [3.0.1](https://github.com/astroband/ruby-xdr/compare/v3.0.0...v3.0.1) - 2020-06-10
|
19
23
|
### Added
|
20
|
-
- Add encoding parameter to Union#to_xdr (
|
24
|
+
- Add encoding parameter to Union#to_xdr ([#7](https://github.com/astroband/ruby-xdr/pull/7)).
|
21
25
|
|
22
|
-
###
|
26
|
+
### Bug Fixes
|
23
27
|
- Padding bytes are now properly validated when reading xdr values. According to the XDR spec, padding must be zeros.
|
24
28
|
|
25
|
-
|
29
|
+
|
30
|
+
## [1.0.0](https://github.com/astroband/ruby-xdr/compare/v0.1.0...v1.0.0) - 2015-10-02
|
26
31
|
### Added
|
27
32
|
- the `to_xdr` helpers can take a second parameter that will encode the resulting output to hex or base64 when requested.
|
28
33
|
- the `from_xdr` helpers can take a second parameter that will trigger a decode from hex or base64 of the provided string before decoding from xdr.
|
data/README.md
CHANGED
@@ -3,7 +3,9 @@
|
|
3
3
|
[![Build Status](https://travis-ci.org/stellar/ruby-xdr.svg?branch=master)](https://travis-ci.org/stellar/ruby-xdr)
|
4
4
|
[![Code Climate](https://codeclimate.com/github/stellar/ruby-xdr/badges/gpa.svg)](https://codeclimate.com/github/stellar/ruby-xdr)
|
5
5
|
|
6
|
-
XDR is an open data format, specified in [RFC 4506](http://tools.ietf.org/html/rfc4506.html).
|
6
|
+
XDR is an open data format, specified in [RFC 4506](http://tools.ietf.org/html/rfc4506.html). This library provides a
|
7
|
+
way to read and write XDR data from ruby. It can read/write all of the primitive XDR types and also provides facilities
|
8
|
+
to define readers for the compound XDR types (enums, structs and unions)
|
7
9
|
|
8
10
|
## Installation
|
9
11
|
|
@@ -15,28 +17,28 @@ gem 'xdr'
|
|
15
17
|
|
16
18
|
And then execute:
|
17
19
|
|
18
|
-
|
20
|
+
bundle
|
19
21
|
|
20
22
|
Or install it yourself as:
|
21
23
|
|
22
|
-
|
24
|
+
gem install xdr
|
23
25
|
|
24
26
|
## Usage
|
25
27
|
|
26
28
|
```ruby
|
27
29
|
# Reading/writing a primitive values
|
28
|
-
XDR::Bool.to_xdr(false)
|
30
|
+
XDR::Bool.to_xdr(false) # => "\x00\x00\x00\x00"
|
29
31
|
XDR::Bool.from_xdr("\x00\x00\x00\x01") # => true
|
30
32
|
|
31
33
|
# Reading/writing arrays
|
32
|
-
XDR::Array[XDR::Int,2].to_xdr([1,2]) # => "\x00\x00\x00\x01\x00\x00\x00\x02"
|
33
|
-
XDR::Array[XDR::Int,2].from_xdr("\x00\x00\x00\x03\x00\x00\x00\x04") # => [3,4]
|
34
|
+
XDR::Array[XDR::Int, 2].to_xdr([1, 2]) # => "\x00\x00\x00\x01\x00\x00\x00\x02"
|
35
|
+
XDR::Array[XDR::Int, 2].from_xdr("\x00\x00\x00\x03\x00\x00\x00\x04") # => [3,4]
|
34
36
|
|
35
37
|
# Defining an enum
|
36
38
|
|
37
39
|
class ResultType < XDR::Enum
|
38
|
-
member :ok,
|
39
|
-
member :error,
|
40
|
+
member :ok, 0
|
41
|
+
member :error, 1
|
40
42
|
seal
|
41
43
|
end
|
42
44
|
|
@@ -88,24 +90,22 @@ Result.from_xdr("\x00\x00\x00\x00") # => #<Result ...>
|
|
88
90
|
|
89
91
|
## Thread safety
|
90
92
|
|
91
|
-
Code generated by `xdrgen`, which targets this library, uses autoload extensively.
|
92
|
-
|
93
|
-
work around this, any module including `XDR::Namespace` can be forced to load
|
93
|
+
Code generated by `xdrgen`, which targets this library, uses autoload extensively. Since autoloading is not thread-safe,
|
94
|
+
neither is code generated from xdrgen. To work around this, any module including `XDR::Namespace` can be forced to load
|
94
95
|
all of it's children by calling `load_all!` on the module.
|
95
96
|
|
96
97
|
## Code generation
|
97
98
|
|
98
|
-
ruby-xdr by itself does not have any ability to parse XDR IDL files and produce a parser for your custom
|
99
|
-
|
100
|
-
|
99
|
+
ruby-xdr by itself does not have any ability to parse XDR IDL files and produce a parser for your custom data types.
|
100
|
+
Instead, that is the responsibility of [xdrgen](http://github.com/stellar/xdrgen). `xdrgen` will take your `.x` files
|
101
|
+
and produce a set of ruby files that target this library to allow for your own custom types.
|
101
102
|
|
102
103
|
See [ruby-stellar-base](http://github.com/astroband/ruby-stellar-sdk/tree/master/base/generated) for an example.
|
103
104
|
|
104
105
|
## Contributing
|
105
106
|
|
106
|
-
1.
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
5. Create a new Pull Request
|
107
|
+
1. Fork the repo ( https://github.com/astroband/ruby-xdr/fork )
|
108
|
+
1. Create your feature branch (`git checkout -b my-new-feature`)
|
109
|
+
1. Commit your changes (`git commit -am 'Add some feature'`)
|
110
|
+
1. Push to the branch (`git push origin my-new-feature`)
|
111
|
+
1. Create a new Pull Request
|
data/lib/xdr/array.rb
CHANGED
@@ -6,7 +6,7 @@ class XDR::Array
|
|
6
6
|
|
7
7
|
def initialize(child_type, length)
|
8
8
|
@child_type = child_type
|
9
|
-
@length
|
9
|
+
@length = length
|
10
10
|
end
|
11
11
|
|
12
12
|
def write(val, io)
|
@@ -19,10 +19,10 @@ class XDR::Array
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def read(io)
|
22
|
-
@length.times.map{ @child_type.read(io) }
|
22
|
+
@length.times.map { @child_type.read(io) }
|
23
23
|
end
|
24
24
|
|
25
25
|
def valid?(val)
|
26
26
|
super(val) && val.length == @length
|
27
27
|
end
|
28
|
-
end
|
28
|
+
end
|
data/lib/xdr/bool.rb
CHANGED
@@ -3,22 +3,22 @@ module XDR::Bool
|
|
3
3
|
|
4
4
|
def self.write(val, io)
|
5
5
|
case val
|
6
|
-
when true
|
7
|
-
when false
|
8
|
-
else
|
6
|
+
when true then XDR::Int.write(1, io)
|
7
|
+
when false then XDR::Int.write(0, io)
|
8
|
+
else; raise XDR::WriteError, "Invalid bool value: #{val}"
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
12
|
def self.read(io)
|
13
13
|
val = XDR::Int.read(io)
|
14
14
|
case val
|
15
|
-
when 0
|
16
|
-
when 1
|
17
|
-
else
|
15
|
+
when 0 then false
|
16
|
+
when 1 then true
|
17
|
+
else; raise XDR::ReadError, "Invalid bool value: #{val}"
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
21
|
def self.valid?(val)
|
22
22
|
val == true || val == false
|
23
23
|
end
|
24
|
-
end
|
24
|
+
end
|
@@ -1,9 +1,9 @@
|
|
1
|
-
require
|
1
|
+
require "base64"
|
2
2
|
|
3
3
|
module XDR::Concerns::ConvertsToXDR
|
4
4
|
include XDR::Concerns::ReadsBytes
|
5
5
|
|
6
|
-
#
|
6
|
+
#
|
7
7
|
# Serialized the provided `val` to xdr and writes it to `io`
|
8
8
|
#
|
9
9
|
# @abstract
|
@@ -14,7 +14,7 @@ module XDR::Concerns::ConvertsToXDR
|
|
14
14
|
raise NotImplementedError, "implement in including class"
|
15
15
|
end
|
16
16
|
|
17
|
-
#
|
17
|
+
#
|
18
18
|
# Reads from the provided IO an instance of the implementing class
|
19
19
|
#
|
20
20
|
# @abstract
|
@@ -35,7 +35,7 @@ module XDR::Concerns::ConvertsToXDR
|
|
35
35
|
|
36
36
|
# Serialized the provided val to xdr, returning a string
|
37
37
|
# of the serialized data
|
38
|
-
#
|
38
|
+
#
|
39
39
|
# @param val [Object] the value to serialize
|
40
40
|
# @param encoding [:raw|:hex|:base64] encode the result with specified codec
|
41
41
|
# @return [String] the produced bytes
|
@@ -46,7 +46,7 @@ module XDR::Concerns::ConvertsToXDR
|
|
46
46
|
|
47
47
|
case String(encoding)
|
48
48
|
when "raw" then raw
|
49
|
-
when "hex" then raw.
|
49
|
+
when "hex" then raw.unpack1("H*")
|
50
50
|
when "base64" then Base64.strict_encode64(raw)
|
51
51
|
else
|
52
52
|
raise ArgumentError, "Invalid encoding #{encoding.inspect}: must be 'raw', 'base64', or 'hex'"
|
@@ -65,13 +65,13 @@ module XDR::Concerns::ConvertsToXDR
|
|
65
65
|
when "base64" then Base64.strict_decode64(string)
|
66
66
|
else
|
67
67
|
raise ArgumentError, "Invalid encoding #{encoding.inspect}: must be 'raw', 'base64', or 'hex'"
|
68
|
-
|
68
|
+
end
|
69
69
|
|
70
70
|
io = StringIO.new(raw)
|
71
71
|
result = read(io)
|
72
72
|
|
73
73
|
if io.pos != io.length
|
74
|
-
raise
|
74
|
+
raise ArgumentError, "Input string not fully consumed! are you decoding the right xdr type?"
|
75
75
|
end
|
76
76
|
|
77
77
|
result
|
@@ -81,10 +81,10 @@ module XDR::Concerns::ConvertsToXDR
|
|
81
81
|
|
82
82
|
def padding_for(length)
|
83
83
|
case length % 4
|
84
|
-
when 0
|
85
|
-
when 1
|
86
|
-
when 2
|
87
|
-
when 3
|
84
|
+
when 0 then 0
|
85
|
+
when 1 then 3
|
86
|
+
when 2 then 2
|
87
|
+
when 3 then 1
|
88
88
|
end
|
89
89
|
end
|
90
90
|
end
|
data/lib/xdr/double.rb
CHANGED
data/lib/xdr/dsl/enum.rb
CHANGED
@@ -1,17 +1,16 @@
|
|
1
1
|
module XDR::DSL::Enum
|
2
|
-
|
3
2
|
def member(name, value)
|
4
|
-
raise ArgumentError, "#{self} is sealed" if self.sealed
|
5
|
-
raise ArgumentError, "#{value} is not Integer" unless value.is_a?(Integer)
|
6
|
-
raise ArgumentError, "#{value} is already used" unless
|
7
|
-
|
8
3
|
name = name.to_s.underscore
|
9
4
|
|
5
|
+
raise ArgumentError, "#{self} is sealed" if sealed
|
6
|
+
raise ArgumentError, "#{value} is not Integer" unless value.is_a?(Integer)
|
7
|
+
raise ArgumentError, "#{value} is already used" if members.key?(name)
|
8
|
+
|
10
9
|
instance = new(name, value)
|
11
|
-
self.members =
|
12
|
-
self.by_value =
|
10
|
+
self.members = members.merge(name => instance)
|
11
|
+
self.by_value = by_value.merge(instance.value => instance)
|
13
12
|
|
14
|
-
class_eval <<-EOS, __FILE__, __LINE__
|
13
|
+
class_eval <<-EOS, __FILE__, __LINE__ + 1
|
15
14
|
def self.#{name}
|
16
15
|
members["#{name}"]
|
17
16
|
end
|
@@ -21,4 +20,4 @@ module XDR::DSL::Enum
|
|
21
20
|
def seal
|
22
21
|
self.sealed = true
|
23
22
|
end
|
24
|
-
end
|
23
|
+
end
|
data/lib/xdr/dsl/struct.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
module XDR::DSL::Struct
|
2
2
|
def attribute(name, type)
|
3
|
-
|
4
3
|
unless type.is_a?(XDR::Concerns::ConvertsToXDR)
|
5
4
|
raise ArgumentError, "#{type} does not convert to xdr"
|
6
5
|
end
|
7
6
|
|
8
|
-
self.fields =
|
7
|
+
self.fields = fields.merge(name => type)
|
9
8
|
|
10
9
|
define_method name do
|
11
10
|
read_attribute(name)
|
data/lib/xdr/dsl/union.rb
CHANGED
@@ -1,39 +1,39 @@
|
|
1
1
|
module XDR::DSL::Union
|
2
2
|
def switch_on(type, name)
|
3
|
-
raise ArgumentError if
|
3
|
+
raise ArgumentError if switch_type.present?
|
4
4
|
self.switch_type = type
|
5
5
|
self.switch_name = name
|
6
6
|
|
7
7
|
alias_method name, :switch
|
8
8
|
end
|
9
9
|
|
10
|
-
def switch(switch, arm=nil)
|
11
|
-
raise ArgumentError, "`switch_on` not defined yet" if
|
10
|
+
def switch(switch, arm = nil)
|
11
|
+
raise ArgumentError, "`switch_on` not defined yet" if switch_type.nil?
|
12
12
|
|
13
13
|
switch = normalize_switch_declaration(switch)
|
14
|
-
self.switches =
|
14
|
+
self.switches = switches.merge(switch => arm)
|
15
15
|
end
|
16
16
|
|
17
17
|
def attribute(name, type)
|
18
18
|
raise ArgumentError, "#{type} does not convert to xdr" unless type.is_a?(XDR::Concerns::ConvertsToXDR)
|
19
19
|
|
20
|
-
self.arms =
|
20
|
+
self.arms = arms.merge(name => type)
|
21
21
|
define_attribute_methods name
|
22
22
|
end
|
23
23
|
|
24
24
|
private
|
25
|
+
|
25
26
|
def normalize_switch_declaration(switch)
|
26
|
-
|
27
|
-
when switch.is_a?(self.switch_type)
|
27
|
+
if switch.is_a?(switch_type)
|
28
28
|
switch
|
29
|
-
|
29
|
+
elsif switch_type.valid?(switch)
|
30
30
|
switch
|
31
|
-
|
31
|
+
elsif switch == :default
|
32
32
|
switch
|
33
|
-
|
34
|
-
|
33
|
+
elsif switch_type.respond_to?(:from_name)
|
34
|
+
switch_type.from_name(switch)
|
35
35
|
else
|
36
|
-
raise ArgumentError, "Cannot normalize switch: #{switch.inspect} to type: #{
|
36
|
+
raise ArgumentError, "Cannot normalize switch: #{switch.inspect} to type: #{switch_type}"
|
37
37
|
end
|
38
38
|
end
|
39
39
|
end
|
data/lib/xdr/dsl.rb
CHANGED
data/lib/xdr/enum.rb
CHANGED
@@ -7,7 +7,7 @@ class XDR::Enum
|
|
7
7
|
class_attribute :sealed
|
8
8
|
self.members = ActiveSupport::OrderedHash.new.with_indifferent_access
|
9
9
|
self.by_value = ActiveSupport::OrderedHash.new
|
10
|
-
self.sealed
|
10
|
+
self.sealed = false
|
11
11
|
|
12
12
|
def self.write(val, io)
|
13
13
|
raise XDR::WriteError, "Invalid enum value: #{val.inspect}" unless val.is_a?(self)
|
@@ -21,14 +21,14 @@ class XDR::Enum
|
|
21
21
|
raise XDR::EnumValueError, "Unknown #{name} member: #{value}" if result.blank?
|
22
22
|
end
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
def self.valid?(val)
|
26
26
|
val.is_a?(self)
|
27
27
|
end
|
28
28
|
|
29
29
|
def self.from_name(name)
|
30
30
|
normalized = name.to_s.underscore
|
31
|
-
members[normalized].tap do |r|
|
31
|
+
members[normalized].tap do |r|
|
32
32
|
raise XDR::EnumNameError, "#{name} is not a member of #{self.name}" if r.blank?
|
33
33
|
end
|
34
34
|
end
|
@@ -37,12 +37,12 @@ class XDR::Enum
|
|
37
37
|
attr_reader :value
|
38
38
|
|
39
39
|
def initialize(name, value)
|
40
|
-
raise ArgumentError, "#{self.class} is sealed" if
|
41
|
-
@name
|
40
|
+
raise ArgumentError, "#{self.class} is sealed" if sealed
|
41
|
+
@name = name
|
42
42
|
@value = value
|
43
43
|
end
|
44
44
|
|
45
45
|
def to_s
|
46
46
|
"#{self.class.name}.#{@name}(#{@value})"
|
47
47
|
end
|
48
|
-
end
|
48
|
+
end
|
data/lib/xdr/float.rb
CHANGED
data/lib/xdr/hyper.rb
CHANGED
data/lib/xdr/int.rb
CHANGED
data/lib/xdr/namespace.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
#
|
1
|
+
#
|
2
2
|
# A thin wrapper around ActiveSupport::Autoload configured to always eager_load
|
3
3
|
# a child. By calling `load_all!` on the namespace module, all children will
|
4
4
|
# be eager loaded, allowing us to more easily operate in a multi-threaded
|
5
5
|
# environment.
|
6
|
-
#
|
6
|
+
#
|
7
7
|
module XDR::Namespace
|
8
8
|
extend ActiveSupport::Concern
|
9
9
|
|
@@ -14,13 +14,11 @@ module XDR::Namespace
|
|
14
14
|
module ClassMethods
|
15
15
|
def load_all!
|
16
16
|
constants.each do |const_name|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
raise e unless e.message =~ /uninitialized constant #{const_name}/
|
22
|
-
end
|
17
|
+
const = const_get const_name
|
18
|
+
const.public_send :load_all! if const.respond_to? :load_all!
|
19
|
+
rescue NameError => e
|
20
|
+
raise e unless /uninitialized constant #{const_name}/.match?(e.message)
|
23
21
|
end
|
24
22
|
end
|
25
23
|
end
|
26
|
-
end
|
24
|
+
end
|
data/lib/xdr/opaque.rb
CHANGED
@@ -12,17 +12,17 @@ class XDR::Opaque
|
|
12
12
|
def read(io)
|
13
13
|
# read and return @length bytes
|
14
14
|
# throw away @padding bytes
|
15
|
-
read_bytes(io, @length).tap{ read_zeros(io, @padding) }
|
15
|
+
read_bytes(io, @length).tap { read_zeros(io, @padding) }
|
16
16
|
end
|
17
17
|
|
18
|
-
def write(val,io)
|
18
|
+
def write(val, io)
|
19
19
|
length = val.bytesize
|
20
|
-
|
20
|
+
|
21
21
|
if val.length != @length
|
22
|
-
raise XDR::WriteError, "Value length is #{length}, must be #{@length}"
|
22
|
+
raise XDR::WriteError, "Value length is #{length}, must be #{@length}"
|
23
23
|
end
|
24
24
|
|
25
25
|
io.write val
|
26
26
|
io.write "\x00" * @padding
|
27
27
|
end
|
28
|
-
end
|
28
|
+
end
|
data/lib/xdr/option.rb
CHANGED
@@ -6,16 +6,16 @@ class XDR::Option
|
|
6
6
|
attr_reader :child_type
|
7
7
|
|
8
8
|
def initialize(child_type)
|
9
|
-
#TODO, raise an error if child_type is not ConvertToXDR
|
9
|
+
# TODO, raise an error if child_type is not ConvertToXDR
|
10
10
|
@child_type = child_type
|
11
11
|
end
|
12
12
|
|
13
13
|
def write(val, io)
|
14
|
-
if val.
|
14
|
+
if val.nil?
|
15
|
+
XDR::Bool.write(false, io)
|
16
|
+
else
|
15
17
|
XDR::Bool.write(true, io)
|
16
18
|
@child_type.write(val, io)
|
17
|
-
else
|
18
|
-
XDR::Bool.write(false, io)
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
@@ -27,4 +27,4 @@ class XDR::Option
|
|
27
27
|
def valid?(val)
|
28
28
|
val.nil? || @child_type.valid?(val)
|
29
29
|
end
|
30
|
-
end
|
30
|
+
end
|
data/lib/xdr/quadruple.rb
CHANGED
data/lib/xdr/rpc/record.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
class XDR::RPC::RecordReader
|
2
2
|
include XDR::Concerns::ReadsBytes
|
3
3
|
|
4
|
-
LAST_MASK
|
4
|
+
LAST_MASK = 0x80000000
|
5
5
|
LENGTH_MASK = 0x7FFFFFFF
|
6
6
|
|
7
7
|
def read(io)
|
8
|
-
header
|
9
|
-
length
|
10
|
-
last
|
8
|
+
header = read_bytes(io, 4).unpack1("L>")
|
9
|
+
length = header & LENGTH_MASK
|
10
|
+
last = (header & LAST_MASK) > 0
|
11
11
|
raw_content = read_bytes(io, length)
|
12
|
-
content
|
13
|
-
|
12
|
+
content = StringIO.new(raw_content)
|
13
|
+
|
14
14
|
XDR::RPC::Record.new(last, length, content)
|
15
15
|
end
|
16
|
-
end
|
16
|
+
end
|
data/lib/xdr/string.rb
CHANGED
@@ -3,8 +3,8 @@ class XDR::String
|
|
3
3
|
include XDR::Concerns::StringConverter
|
4
4
|
|
5
5
|
singleton_class.send(:alias_method, :[], :new)
|
6
|
-
|
7
|
-
def initialize(length=XDR::MAX_SIZE)
|
6
|
+
|
7
|
+
def initialize(length = XDR::MAX_SIZE)
|
8
8
|
@length = length
|
9
9
|
end
|
10
10
|
|
@@ -31,6 +31,6 @@ class XDR::String
|
|
31
31
|
|
32
32
|
# read and return length bytes
|
33
33
|
# throw away padding bytes
|
34
|
-
read_bytes(io, length).tap{ read_zeros(io, padding) }
|
34
|
+
read_bytes(io, length).tap { read_zeros(io, padding) }
|
35
35
|
end
|
36
|
-
end
|
36
|
+
end
|
data/lib/xdr/struct.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "base64"
|
2
2
|
|
3
3
|
class XDR::Struct
|
4
4
|
include ActiveModel::Model
|
@@ -7,8 +7,8 @@ class XDR::Struct
|
|
7
7
|
extend XDR::Concerns::ConvertsToXDR
|
8
8
|
extend XDR::DSL::Struct
|
9
9
|
|
10
|
-
attribute_method_prefix
|
11
|
-
attribute_method_suffix
|
10
|
+
attribute_method_prefix "read_"
|
11
|
+
attribute_method_suffix "write_"
|
12
12
|
|
13
13
|
class_attribute :fields
|
14
14
|
self.fields = ActiveSupport::OrderedHash.new
|
@@ -36,26 +36,25 @@ class XDR::Struct
|
|
36
36
|
val.is_a?(self)
|
37
37
|
end
|
38
38
|
|
39
|
-
def initialize(attributes={})
|
39
|
+
def initialize(attributes = {})
|
40
40
|
@attributes = {}
|
41
41
|
super
|
42
42
|
end
|
43
43
|
|
44
|
-
|
45
44
|
#
|
46
45
|
# Serializes struct to xdr, return a string of bytes
|
47
46
|
#
|
48
|
-
# @param format
|
47
|
+
# @param format [:raw, :hex, :base64] The encoding used for the bytes produces, one of (:raw, :hex, :base64)
|
49
48
|
#
|
50
49
|
# @return [String] The encoded bytes of this struct
|
51
|
-
def to_xdr(format
|
50
|
+
def to_xdr(format = :raw)
|
52
51
|
raw = self.class.to_xdr(self)
|
53
52
|
|
54
53
|
case format
|
55
|
-
when :raw
|
56
|
-
when :hex
|
57
|
-
when :base64
|
58
|
-
else
|
54
|
+
when :raw then raw
|
55
|
+
when :hex then raw.unpack1("H*")
|
56
|
+
when :base64 then Base64.strict_encode64(raw)
|
57
|
+
else
|
59
58
|
raise ArgumentError, "Invalid format #{format.inspect}; must be :raw, :hex, or :base64"
|
60
59
|
end
|
61
60
|
end
|
@@ -63,18 +62,18 @@ class XDR::Struct
|
|
63
62
|
#
|
64
63
|
# Compares two structs for equality
|
65
64
|
#
|
66
|
-
def ==
|
65
|
+
def ==(other)
|
67
66
|
return false unless other.is_a?(self.class)
|
68
|
-
other.attributes ==
|
67
|
+
other.attributes == attributes
|
69
68
|
end
|
70
69
|
|
71
|
-
def eql?
|
70
|
+
def eql?(other)
|
72
71
|
return false unless other.is_a?(self.class)
|
73
|
-
other.attributes.eql?
|
72
|
+
other.attributes.eql? attributes
|
74
73
|
end
|
75
74
|
|
76
75
|
def hash
|
77
|
-
[self.class,
|
76
|
+
[self.class, attributes].hash
|
78
77
|
end
|
79
78
|
|
80
79
|
def read_attribute(attr)
|
data/lib/xdr/struct_validator.rb
CHANGED
data/lib/xdr/union.rb
CHANGED
@@ -5,20 +5,19 @@ class XDR::Union
|
|
5
5
|
extend XDR::Concerns::ConvertsToXDR
|
6
6
|
extend XDR::DSL::Union
|
7
7
|
|
8
|
-
|
9
8
|
class_attribute :arms
|
10
9
|
class_attribute :switches
|
11
10
|
class_attribute :switch_type
|
12
11
|
class_attribute :switch_name
|
13
|
-
attr_reader
|
14
|
-
attr_reader
|
12
|
+
attr_reader :switch
|
13
|
+
attr_reader :arm
|
15
14
|
|
16
|
-
self.arms
|
17
|
-
self.switches
|
15
|
+
self.arms = ActiveSupport::OrderedHash.new
|
16
|
+
self.switches = ActiveSupport::OrderedHash.new
|
18
17
|
self.switch_type = nil
|
19
18
|
self.switch_name = nil
|
20
19
|
|
21
|
-
attribute_method_suffix
|
20
|
+
attribute_method_suffix "!"
|
22
21
|
|
23
22
|
def self.arm_for_switch(switch)
|
24
23
|
begin
|
@@ -38,56 +37,55 @@ class XDR::Union
|
|
38
37
|
end
|
39
38
|
|
40
39
|
def self.normalize_switch(switch)
|
41
|
-
|
42
|
-
when switch.is_a?(self.switch_type)
|
40
|
+
if switch.is_a?(switch_type)
|
43
41
|
switch
|
44
|
-
|
42
|
+
elsif switch_type.valid?(switch)
|
45
43
|
switch
|
46
|
-
|
47
|
-
|
44
|
+
elsif switch_type.respond_to?(:from_name)
|
45
|
+
switch_type.from_name(switch)
|
48
46
|
else
|
49
|
-
raise ArgumentError, "Cannot normalize switch: #{switch.inspect} to type: #{
|
47
|
+
raise ArgumentError, "Cannot normalize switch: #{switch.inspect} to type: #{switch_type}"
|
50
48
|
end
|
51
49
|
end
|
52
50
|
|
53
51
|
def self.read(io)
|
54
|
-
switch
|
55
|
-
arm
|
52
|
+
switch = switch_type.read(io)
|
53
|
+
arm = arm_for_switch(switch)
|
56
54
|
arm_type = arms[arm] || XDR::Void
|
57
|
-
value
|
55
|
+
value = arm_type.read(io)
|
58
56
|
new(switch, value)
|
59
57
|
end
|
60
58
|
|
61
59
|
def self.write(val, io)
|
62
60
|
switch_type.write(val.switch, io)
|
63
61
|
arm_type = arms[val.arm] || XDR::Void
|
64
|
-
arm_type.write(val.get,io)
|
62
|
+
arm_type.write(val.get, io)
|
65
63
|
end
|
66
64
|
|
67
65
|
def self.valid?(val)
|
68
66
|
val.is_a?(self)
|
69
67
|
end
|
70
68
|
|
71
|
-
def initialize(switch
|
72
|
-
@switch
|
73
|
-
@arm
|
74
|
-
@value
|
69
|
+
def initialize(switch = :__unset__, value = :void)
|
70
|
+
@switch = nil
|
71
|
+
@arm = nil
|
72
|
+
@value = nil
|
75
73
|
set(switch, value) unless switch == :__unset__
|
76
74
|
end
|
77
75
|
|
78
76
|
#
|
79
77
|
# Serializes struct to xdr, return a string of bytes
|
80
78
|
#
|
81
|
-
# @param format
|
79
|
+
# @param format [:raw, :hex, :base64] The encoding used for the bytes produces, one of (:raw, :hex, :base64)
|
82
80
|
#
|
83
81
|
# @return [String] The encoded bytes of this struct
|
84
|
-
def to_xdr(format
|
82
|
+
def to_xdr(format = :raw)
|
85
83
|
self.class.to_xdr(self, format)
|
86
84
|
end
|
87
85
|
|
88
|
-
def set(switch, value
|
86
|
+
def set(switch, value = :void)
|
89
87
|
@switch = self.class.normalize_switch switch
|
90
|
-
@arm
|
88
|
+
@arm = self.class.arm_for_switch @switch
|
91
89
|
|
92
90
|
raise XDR::InvalidValueError unless valid_for_arm_type(value, @arm)
|
93
91
|
|
@@ -100,7 +98,7 @@ class XDR::Union
|
|
100
98
|
@value unless @value == :void
|
101
99
|
end
|
102
100
|
|
103
|
-
|
101
|
+
alias_method :get, :value
|
104
102
|
|
105
103
|
def attribute(attr)
|
106
104
|
return nil unless @arm.to_s == attr
|
@@ -117,23 +115,24 @@ class XDR::Union
|
|
117
115
|
#
|
118
116
|
# Compares two unions for equality
|
119
117
|
#
|
120
|
-
def ==
|
118
|
+
def ==(other)
|
121
119
|
return false unless other.is_a?(self.class)
|
122
|
-
return false unless other.switch ==
|
123
|
-
other.value ==
|
120
|
+
return false unless other.switch == switch
|
121
|
+
other.value == value
|
124
122
|
end
|
125
123
|
|
126
124
|
def eql?(other)
|
127
125
|
return false unless other.is_a?(self.class)
|
128
|
-
return false unless other.switch.eql?
|
129
|
-
other.value.eql?
|
126
|
+
return false unless other.switch.eql? switch
|
127
|
+
other.value.eql? value
|
130
128
|
end
|
131
|
-
|
129
|
+
|
132
130
|
def hash
|
133
|
-
[self.class,
|
131
|
+
[self.class, switch, value].hash
|
134
132
|
end
|
135
133
|
|
136
134
|
private
|
135
|
+
|
137
136
|
def valid_for_arm_type(value, arm)
|
138
137
|
arm_type = arms[@arm]
|
139
138
|
|
data/lib/xdr/union_validator.rb
CHANGED
data/lib/xdr/unsigned_hyper.rb
CHANGED
data/lib/xdr/unsigned_int.rb
CHANGED
data/lib/xdr/var_array.rb
CHANGED
@@ -4,8 +4,8 @@ class XDR::VarArray
|
|
4
4
|
|
5
5
|
singleton_class.send(:alias_method, :[], :new)
|
6
6
|
|
7
|
-
def initialize(child_type, length=XDR::MAX_SIZE)
|
8
|
-
@child_type
|
7
|
+
def initialize(child_type, length = XDR::MAX_SIZE)
|
8
|
+
@child_type = child_type
|
9
9
|
@length = length
|
10
10
|
end
|
11
11
|
|
@@ -29,10 +29,10 @@ class XDR::VarArray
|
|
29
29
|
raise XDR::ReadError, "VarArray length #{length} is greater than max #{@length}"
|
30
30
|
end
|
31
31
|
|
32
|
-
length.times.map{ @child_type.read(io) }
|
32
|
+
length.times.map { @child_type.read(io) }
|
33
33
|
end
|
34
34
|
|
35
35
|
def valid?(val)
|
36
36
|
super(val) && val.length <= @length
|
37
37
|
end
|
38
|
-
end
|
38
|
+
end
|
data/lib/xdr/var_opaque.rb
CHANGED
@@ -4,7 +4,7 @@ class XDR::VarOpaque
|
|
4
4
|
|
5
5
|
singleton_class.send(:alias_method, :[], :new)
|
6
6
|
|
7
|
-
def initialize(length=XDR::MAX_SIZE)
|
7
|
+
def initialize(length = XDR::MAX_SIZE)
|
8
8
|
@length = length
|
9
9
|
end
|
10
10
|
|
@@ -31,6 +31,6 @@ class XDR::VarOpaque
|
|
31
31
|
|
32
32
|
# read and return length bytes
|
33
33
|
# throw away padding bytes
|
34
|
-
read_bytes(io, length).tap{ read_zeros(io, padding) }
|
34
|
+
read_bytes(io, length).tap { read_zeros(io, padding) }
|
35
35
|
end
|
36
|
-
end
|
36
|
+
end
|
data/lib/xdr/version.rb
CHANGED
data/lib/xdr/void.rb
CHANGED
data/lib/xdr.rb
CHANGED
@@ -56,17 +56,23 @@ module XDR
|
|
56
56
|
autoload :ArrayConverter
|
57
57
|
end
|
58
58
|
|
59
|
-
class Error < StandardError
|
60
|
-
class ReadError < Error ; end
|
61
|
-
class EnumValueError < ReadError ; end
|
62
|
-
class EnumNameError < ReadError ; end
|
63
|
-
class WriteError < Error ; end
|
59
|
+
class Error < StandardError; end
|
64
60
|
|
65
|
-
class
|
66
|
-
|
67
|
-
class
|
61
|
+
class ReadError < Error; end
|
62
|
+
|
63
|
+
class EnumValueError < ReadError; end
|
64
|
+
|
65
|
+
class EnumNameError < ReadError; end
|
66
|
+
|
67
|
+
class WriteError < Error; end
|
68
|
+
|
69
|
+
class InvalidSwitchError < Error; end
|
70
|
+
|
71
|
+
class InvalidValueError < Error; end
|
72
|
+
|
73
|
+
class ArmNotSetError < Error; end
|
68
74
|
|
69
75
|
mattr_accessor :logger
|
70
|
-
self.logger = ActiveSupport::Logger.new(
|
71
|
-
|
76
|
+
self.logger = ActiveSupport::Logger.new($stdout)
|
77
|
+
logger.level = Logger::WARN
|
72
78
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xdr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.
|
4
|
+
version: 3.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Scott Fleckenstein
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-02-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
version: '4.2'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '
|
22
|
+
version: '8.0'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,7 +29,7 @@ dependencies:
|
|
29
29
|
version: '4.2'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '
|
32
|
+
version: '8.0'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: activemodel
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -39,7 +39,7 @@ dependencies:
|
|
39
39
|
version: '4.2'
|
40
40
|
- - "<"
|
41
41
|
- !ruby/object:Gem::Version
|
42
|
-
version: '
|
42
|
+
version: '8.0'
|
43
43
|
type: :runtime
|
44
44
|
prerelease: false
|
45
45
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -49,49 +49,7 @@ dependencies:
|
|
49
49
|
version: '4.2'
|
50
50
|
- - "<"
|
51
51
|
- !ruby/object:Gem::Version
|
52
|
-
version: '
|
53
|
-
- !ruby/object:Gem::Dependency
|
54
|
-
name: rake
|
55
|
-
requirement: !ruby/object:Gem::Requirement
|
56
|
-
requirements:
|
57
|
-
- - "~>"
|
58
|
-
- !ruby/object:Gem::Version
|
59
|
-
version: '13.0'
|
60
|
-
type: :development
|
61
|
-
prerelease: false
|
62
|
-
version_requirements: !ruby/object:Gem::Requirement
|
63
|
-
requirements:
|
64
|
-
- - "~>"
|
65
|
-
- !ruby/object:Gem::Version
|
66
|
-
version: '13.0'
|
67
|
-
- !ruby/object:Gem::Dependency
|
68
|
-
name: rspec
|
69
|
-
requirement: !ruby/object:Gem::Requirement
|
70
|
-
requirements:
|
71
|
-
- - "~>"
|
72
|
-
- !ruby/object:Gem::Version
|
73
|
-
version: '3.0'
|
74
|
-
type: :development
|
75
|
-
prerelease: false
|
76
|
-
version_requirements: !ruby/object:Gem::Requirement
|
77
|
-
requirements:
|
78
|
-
- - "~>"
|
79
|
-
- !ruby/object:Gem::Version
|
80
|
-
version: '3.0'
|
81
|
-
- !ruby/object:Gem::Dependency
|
82
|
-
name: simplecov
|
83
|
-
requirement: !ruby/object:Gem::Requirement
|
84
|
-
requirements:
|
85
|
-
- - "~>"
|
86
|
-
- !ruby/object:Gem::Version
|
87
|
-
version: '0.19'
|
88
|
-
type: :development
|
89
|
-
prerelease: false
|
90
|
-
version_requirements: !ruby/object:Gem::Requirement
|
91
|
-
requirements:
|
92
|
-
- - "~>"
|
93
|
-
- !ruby/object:Gem::Version
|
94
|
-
version: '0.19'
|
52
|
+
version: '8.0'
|
95
53
|
description:
|
96
54
|
email:
|
97
55
|
- scott@stellar.org
|
@@ -141,14 +99,15 @@ files:
|
|
141
99
|
- lib/xdr/var_opaque.rb
|
142
100
|
- lib/xdr/version.rb
|
143
101
|
- lib/xdr/void.rb
|
144
|
-
homepage: https://github.com/
|
102
|
+
homepage: https://github.com/astroband/ruby-xdr
|
145
103
|
licenses:
|
146
104
|
- Apache-2.0
|
147
105
|
metadata:
|
148
|
-
bug_tracker_uri: https://github.com/
|
149
|
-
changelog_uri: https://github.com/
|
150
|
-
documentation_uri: https://rubydoc.info/gems/xdr/3.0.
|
151
|
-
|
106
|
+
bug_tracker_uri: https://github.com/astroband/ruby-xdr/issues
|
107
|
+
changelog_uri: https://github.com/astroband/ruby-xdr/blob/v3.0.3/CHANGELOG.md
|
108
|
+
documentation_uri: https://rubydoc.info/gems/xdr/3.0.3/
|
109
|
+
github_repo: https://github.com/astroband/ruby-xdr
|
110
|
+
source_code_uri: https://github.com/astroband/ruby-xdr/tree/v3.0.3
|
152
111
|
post_install_message:
|
153
112
|
rdoc_options: []
|
154
113
|
require_paths:
|
@@ -164,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
164
123
|
- !ruby/object:Gem::Version
|
165
124
|
version: '0'
|
166
125
|
requirements: []
|
167
|
-
rubygems_version: 3.
|
126
|
+
rubygems_version: 3.3.5
|
168
127
|
signing_key:
|
169
128
|
specification_version: 4
|
170
129
|
summary: XDR Helper Library
|