xdr 2.0.0 → 3.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/CHANGELOG.md +26 -7
- data/README.md +21 -19
- 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 +40 -41
- 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 +10 -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 +44 -36
- 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 +37 -150
- data/.gitignore +0 -15
- data/.travis.yml +0 -9
- data/.yardopts +0 -7
- data/Gemfile +0 -4
- data/Guardfile +0 -5
- data/Rakefile +0 -9
- data/examples/enum.rb +0 -30
- data/examples/struct.rb +0 -24
- data/examples/union.rb +0 -29
- data/spec/lib/xdr/array_spec.rb +0 -73
- data/spec/lib/xdr/bool_spec.rb +0 -43
- data/spec/lib/xdr/concerns/converts_to_xdr_spec.rb +0 -88
- data/spec/lib/xdr/concerns/reads_bytes_spec.rb +0 -31
- data/spec/lib/xdr/double_spec.rb +0 -38
- data/spec/lib/xdr/dsl/enum_spec.rb +0 -44
- data/spec/lib/xdr/dsl/struct_spec.rb +0 -29
- data/spec/lib/xdr/dsl/union_spec.rb +0 -69
- data/spec/lib/xdr/enum_spec.rb +0 -70
- data/spec/lib/xdr/float_spec.rb +0 -37
- data/spec/lib/xdr/hyper_spec.rb +0 -40
- data/spec/lib/xdr/int_spec.rb +0 -40
- data/spec/lib/xdr/opaque_spec.rb +0 -36
- data/spec/lib/xdr/option_spec.rb +0 -36
- data/spec/lib/xdr/quadruple_spec.rb +0 -14
- data/spec/lib/xdr/rpc/record_reader_spec.rb +0 -27
- data/spec/lib/xdr/string_spec.rb +0 -41
- data/spec/lib/xdr/struct_spec.rb +0 -101
- data/spec/lib/xdr/union_spec.rb +0 -248
- data/spec/lib/xdr/unsigned_hyper_spec.rb +0 -36
- data/spec/lib/xdr/unsigned_int_spec.rb +0 -36
- data/spec/lib/xdr/var_array_spec.rb +0 -71
- data/spec/lib/xdr/var_opaque_spec.rb +0 -43
- data/spec/lib/xdr/void_spec.rb +0 -46
- data/spec/spec_helper.rb +0 -15
- data/spec/support/matchers/eq_bytes.rb +0 -6
- data/xdr.gemspec +0 -28
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
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
@@ -1,17 +1,36 @@
|
|
1
|
-
# Changelog
|
1
|
+
# Changelog
|
2
2
|
|
3
|
-
All notable changes to this project will be documented in this
|
4
|
-
file. This project adheres to [Semantic Versioning](http://semver.org/).
|
3
|
+
All notable changes to this project will be documented in this file.
|
5
4
|
|
6
|
-
|
7
|
-
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
8
7
|
|
9
|
-
|
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))
|
10
11
|
|
12
|
+
|
13
|
+
## [3.0.2](https://github.com/astroband/ruby-xdr/compare/v3.0.1...v3.0.2) - 2020-10-20
|
14
|
+
### Changed
|
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))
|
20
|
+
|
21
|
+
|
22
|
+
## [3.0.1](https://github.com/astroband/ruby-xdr/compare/v3.0.0...v3.0.1) - 2020-06-10
|
23
|
+
### Added
|
24
|
+
- Add encoding parameter to Union#to_xdr ([#7](https://github.com/astroband/ruby-xdr/pull/7)).
|
25
|
+
|
26
|
+
### Bug Fixes
|
27
|
+
- Padding bytes are now properly validated when reading xdr values. According to the XDR spec, padding must be zeros.
|
28
|
+
|
29
|
+
|
30
|
+
## [1.0.0](https://github.com/astroband/ruby-xdr/compare/v0.1.0...v1.0.0) - 2015-10-02
|
11
31
|
### Added
|
12
32
|
- the `to_xdr` helpers can take a second parameter that will encode the resulting output to hex or base64 when requested.
|
13
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.
|
14
34
|
|
15
35
|
### Changed
|
16
|
-
|
17
36
|
- `from_xdr` raises an ArgumentError when the input data is not fully consumed
|
data/README.md
CHANGED
@@ -3,7 +3,9 @@
|
|
3
3
|
[](https://travis-ci.org/stellar/ruby-xdr)
|
4
4
|
[](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,22 +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 data types.
|
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.
|
99
102
|
|
100
|
-
See [ruby-stellar-base](http://github.com/
|
103
|
+
See [ruby-stellar-base](http://github.com/astroband/ruby-stellar-sdk/tree/master/base/generated) for an example.
|
101
104
|
|
102
105
|
## Contributing
|
103
106
|
|
104
|
-
1.
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
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,91 +1,90 @@
|
|
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
|
+
# @abstract
|
9
10
|
# @param val [Object] The object to serialize
|
10
|
-
# @param io [
|
11
|
-
#
|
11
|
+
# @param io [#write] an IO object to write to
|
12
|
+
# @return [void]
|
12
13
|
def write(val, io)
|
13
14
|
raise NotImplementedError, "implement in including class"
|
14
15
|
end
|
15
16
|
|
16
|
-
#
|
17
|
+
#
|
17
18
|
# Reads from the provided IO an instance of the implementing class
|
18
|
-
#
|
19
|
-
#
|
19
|
+
#
|
20
|
+
# @abstract
|
21
|
+
# @param io [#read] the io to read from
|
20
22
|
# @return [Object] the deserialized value
|
21
23
|
def read(io)
|
22
24
|
raise NotImplementedError, "implement in including class"
|
23
25
|
end
|
24
26
|
|
25
|
-
#
|
26
27
|
# Returns true if the value provided is compatible with this serializer class
|
27
|
-
#
|
28
|
+
#
|
29
|
+
# @abstract
|
28
30
|
# @param value [Object] the value to test
|
29
|
-
#
|
30
31
|
# @return [Boolean] true if valid, false otherwise
|
31
32
|
def valid?(value)
|
32
33
|
raise NotImplementedError, "implement in including class"
|
33
34
|
end
|
34
|
-
|
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
|
42
|
-
def to_xdr(val, encoding=
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
string.force_encoding("ASCII-8BIT")
|
42
|
+
def to_xdr(val, encoding = "raw")
|
43
|
+
io = StringIO.new
|
44
|
+
write(val, io)
|
45
|
+
raw = io.string.force_encoding("ASCII-8BIT")
|
47
46
|
|
48
|
-
case encoding
|
49
|
-
when
|
50
|
-
when
|
51
|
-
when
|
47
|
+
case String(encoding)
|
48
|
+
when "raw" then raw
|
49
|
+
when "hex" then raw.unpack1("H*")
|
50
|
+
when "base64" then Base64.strict_encode64(raw)
|
52
51
|
else
|
53
|
-
raise
|
52
|
+
raise ArgumentError, "Invalid encoding #{encoding.inspect}: must be 'raw', 'base64', or 'hex'"
|
54
53
|
end
|
55
54
|
end
|
56
|
-
|
57
|
-
#
|
55
|
+
|
58
56
|
# Deserializes an object from the provided string of bytes
|
59
|
-
#
|
57
|
+
#
|
60
58
|
# @param string [String] the bytes to read from
|
61
|
-
#
|
59
|
+
# @param encoding [:raw|:hex|:base64] decode the input before deserialization
|
62
60
|
# @return [Object] the deserialized value
|
63
|
-
def from_xdr(string, encoding=
|
64
|
-
raw = case encoding
|
65
|
-
when
|
66
|
-
when
|
67
|
-
when
|
61
|
+
def from_xdr(string, encoding = "raw")
|
62
|
+
raw = case String(encoding)
|
63
|
+
when "raw" then string
|
64
|
+
when "hex" then [string].pack("H*")
|
65
|
+
when "base64" then Base64.strict_decode64(string)
|
68
66
|
else
|
69
|
-
raise
|
70
|
-
|
67
|
+
raise ArgumentError, "Invalid encoding #{encoding.inspect}: must be 'raw', 'base64', or 'hex'"
|
68
|
+
end
|
71
69
|
|
72
70
|
io = StringIO.new(raw)
|
73
71
|
result = read(io)
|
74
72
|
|
75
73
|
if io.pos != io.length
|
76
|
-
raise
|
74
|
+
raise ArgumentError, "Input string not fully consumed! are you decoding the right xdr type?"
|
77
75
|
end
|
78
76
|
|
79
77
|
result
|
80
78
|
end
|
81
79
|
|
82
80
|
private
|
81
|
+
|
83
82
|
def padding_for(length)
|
84
83
|
case length % 4
|
85
|
-
when 0
|
86
|
-
when 1
|
87
|
-
when 2
|
88
|
-
when 3
|
84
|
+
when 0 then 0
|
85
|
+
when 1 then 3
|
86
|
+
when 2 then 2
|
87
|
+
when 3 then 1
|
89
88
|
end
|
90
89
|
end
|
91
90
|
end
|
@@ -1,8 +1,17 @@
|
|
1
1
|
module XDR::Concerns::ReadsBytes
|
2
2
|
private
|
3
|
+
|
3
4
|
def read_bytes(io, length)
|
4
5
|
io.read(length).tap do |bytes|
|
5
6
|
raise EOFError if bytes.nil? || bytes.length != length
|
6
7
|
end
|
7
8
|
end
|
8
|
-
|
9
|
+
|
10
|
+
def read_zeros(io, length)
|
11
|
+
read_bytes(io, length).each_byte do |byte|
|
12
|
+
raise XDR::ReadError unless byte == 0
|
13
|
+
end
|
14
|
+
|
15
|
+
nil
|
16
|
+
end
|
17
|
+
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{
|
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