xdr 0.0.3 → 0.0.4
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 +4 -4
- data/.gitignore +1 -0
- data/examples/union.rb +0 -7
- data/lib/xdr/dsl/union.rb +14 -7
- data/lib/xdr/union.rb +21 -4
- data/lib/xdr/version.rb +1 -1
- data/spec/lib/xdr/dsl/union_spec.rb +48 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bc2054a14c8b40897a81b9758fe8f7d48fa5be07
|
4
|
+
data.tar.gz: 3bddeb7f3628cb88feae1800467e060080b8b942
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b22b70f5094869e0f09d9819b63746eab3416416eb4a7c2128043888e587dc4e7a5e7a7452e9df925d302d9f771883ca72b76615c9588059c15d9fec793ff59d
|
7
|
+
data.tar.gz: 6d8bdbafec1e37e2a6a9ade6d139ebd94cf297277e9ff25b676560d6f1d85f70fd8e7e71123d01170e5fcc09a285253a9ce56c303b1fb4f2d84f08e52082c329
|
data/.gitignore
CHANGED
data/examples/union.rb
CHANGED
data/lib/xdr/dsl/union.rb
CHANGED
@@ -10,7 +10,7 @@ module XDR::DSL::Union
|
|
10
10
|
def switch(switch, arm=nil)
|
11
11
|
raise ArgumentError, "`switch_on` not defined yet" if self.switch_type.nil?
|
12
12
|
|
13
|
-
switch =
|
13
|
+
switch = normalize_switch_declaration(switch)
|
14
14
|
self.switches = self.switches.merge(switch => arm)
|
15
15
|
end
|
16
16
|
|
@@ -22,11 +22,18 @@ module XDR::DSL::Union
|
|
22
22
|
end
|
23
23
|
|
24
24
|
private
|
25
|
-
def
|
26
|
-
case
|
27
|
-
when self.switch_type
|
28
|
-
|
29
|
-
|
25
|
+
def normalize_switch_declaration(switch)
|
26
|
+
case
|
27
|
+
when switch.is_a?(self.switch_type)
|
28
|
+
switch
|
29
|
+
when self.switch_type.valid?(switch)
|
30
|
+
switch
|
31
|
+
when switch == :default
|
32
|
+
switch
|
33
|
+
when self.switch_type.respond_to?(:from_name)
|
34
|
+
self.switch_type.from_name(switch)
|
35
|
+
else
|
36
|
+
raise ArgumentError, "Cannot normalize switch: #{switch.inspect} to type: #{self.switch_type}"
|
30
37
|
end
|
31
38
|
end
|
32
|
-
end
|
39
|
+
end
|
data/lib/xdr/union.rb
CHANGED
@@ -21,7 +21,11 @@ class XDR::Union
|
|
21
21
|
attribute_method_suffix '!'
|
22
22
|
|
23
23
|
def self.arm_for_switch(switch)
|
24
|
-
|
24
|
+
begin
|
25
|
+
switch = normalize_switch switch
|
26
|
+
rescue ArgumentError => e
|
27
|
+
raise XDR::InvalidSwitchError, e.message
|
28
|
+
end
|
25
29
|
|
26
30
|
result = switches.fetch(switch, :switch_not_found)
|
27
31
|
result = switches.fetch(:default, :switch_not_found) if result == :switch_not_found
|
@@ -33,6 +37,19 @@ class XDR::Union
|
|
33
37
|
result
|
34
38
|
end
|
35
39
|
|
40
|
+
def self.normalize_switch(switch)
|
41
|
+
case
|
42
|
+
when switch.is_a?(self.switch_type)
|
43
|
+
switch
|
44
|
+
when self.switch_type.valid?(switch)
|
45
|
+
switch
|
46
|
+
when self.switch_type.respond_to?(:from_name)
|
47
|
+
self.switch_type.from_name(switch)
|
48
|
+
else
|
49
|
+
raise ArgumentError, "Cannot normalize switch: #{switch.inspect} to type: #{self.switch_type}"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
36
53
|
def self.read(io)
|
37
54
|
switch = switch_type.read(io)
|
38
55
|
arm = arm_for_switch(switch)
|
@@ -51,11 +68,11 @@ class XDR::Union
|
|
51
68
|
val.is_a?(self)
|
52
69
|
end
|
53
70
|
|
54
|
-
def initialize(switch
|
71
|
+
def initialize(switch=:__unset__, value=:void)
|
55
72
|
@switch = nil
|
56
73
|
@arm = nil
|
57
74
|
@value = nil
|
58
|
-
set(switch, value)
|
75
|
+
set(switch, value) unless switch == :__unset__
|
59
76
|
end
|
60
77
|
|
61
78
|
def to_xdr
|
@@ -63,7 +80,7 @@ class XDR::Union
|
|
63
80
|
end
|
64
81
|
|
65
82
|
def set(switch, value=:void)
|
66
|
-
@switch =
|
83
|
+
@switch = self.class.normalize_switch switch
|
67
84
|
@arm = self.class.arm_for_switch @switch
|
68
85
|
|
69
86
|
raise XDR::InvalidValueError unless valid_for_arm_type(value, @arm)
|
data/lib/xdr/version.rb
CHANGED
@@ -19,4 +19,51 @@ describe XDR::DSL::Union, "#switch" do
|
|
19
19
|
member :error, 1
|
20
20
|
seal
|
21
21
|
end
|
22
|
-
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe XDR::DSL::Union, "#switch_on" do
|
25
|
+
klass = nil
|
26
|
+
|
27
|
+
it "allows int types" do
|
28
|
+
expect do
|
29
|
+
klass = Class.new(XDR::Union) do
|
30
|
+
switch_on XDR::Int, :type
|
31
|
+
switch 0
|
32
|
+
switch 1
|
33
|
+
end
|
34
|
+
end.to_not raise_error
|
35
|
+
|
36
|
+
expect{ klass.new(0) }.to_not raise_error
|
37
|
+
expect{ klass.new(1) }.to_not raise_error
|
38
|
+
expect{ klass.new(2) }.to raise_error(XDR::InvalidSwitchError)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "allows unsigned int types" do
|
42
|
+
expect do
|
43
|
+
klass = Class.new(XDR::Union) do
|
44
|
+
switch_on XDR::UnsignedInt, :type
|
45
|
+
switch 0
|
46
|
+
switch 1
|
47
|
+
end
|
48
|
+
end.to_not raise_error
|
49
|
+
|
50
|
+
expect{ klass.new(0) }.to_not raise_error
|
51
|
+
expect{ klass.new(1) }.to_not raise_error
|
52
|
+
expect{ klass.new(2) }.to raise_error(XDR::InvalidSwitchError)
|
53
|
+
expect{ klass.new(-1) }.to raise_error(XDR::InvalidSwitchError)
|
54
|
+
end
|
55
|
+
|
56
|
+
it "allows bool types", :focus do
|
57
|
+
klass = nil
|
58
|
+
|
59
|
+
expect do
|
60
|
+
klass = Class.new(XDR::Union) do
|
61
|
+
switch_on XDR::Bool, :type
|
62
|
+
switch true
|
63
|
+
end
|
64
|
+
end.to_not raise_error
|
65
|
+
|
66
|
+
expect{ klass.new(true) }.to_not raise_error
|
67
|
+
expect{ klass.new(false) }.to raise_error(XDR::InvalidSwitchError)
|
68
|
+
end
|
69
|
+
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: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Scott Fleckenstein
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-07-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|