rlp 0.5.3 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rlp.rb +2 -0
- data/lib/rlp/constant.rb +4 -2
- data/lib/rlp/data.rb +2 -0
- data/lib/rlp/decode.rb +16 -11
- data/lib/rlp/decode_lazy.rb +2 -0
- data/lib/rlp/encode.rb +2 -0
- data/lib/rlp/error.rb +3 -1
- data/lib/rlp/lazy_list.rb +2 -0
- data/lib/rlp/sedes.rb +2 -0
- data/lib/rlp/sedes/big_endian_int.rb +2 -0
- data/lib/rlp/sedes/binary.rb +4 -1
- data/lib/rlp/sedes/countable_list.rb +2 -0
- data/lib/rlp/sedes/list.rb +5 -0
- data/lib/rlp/sedes/raw.rb +2 -0
- data/lib/rlp/sedes/serializable.rb +52 -13
- data/lib/rlp/utils.rb +2 -0
- data/lib/rlp/version.rb +3 -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: 3a7e6cfb6c2ff329ee45d38680304abe966afa38
|
4
|
+
data.tar.gz: 9a9c72a1be75da655162c63dbc351c10e621aadc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5ae8922fc6fd5b0b641d03103218ebc2368562156cf1220469ab15dbcbde92436dd8a543473339c036d70e59af227c9f44614df75bc5b36f9bf188b0be20dd5a
|
7
|
+
data.tar.gz: 54c60053ea2ccbb85efa95d0ce310a1415854cf4873f478cc0c163118fceef6b130e005d361b71cf09308f80c3bf757362820249e7fc4cfcff2287ce8a4078a9
|
data/lib/rlp.rb
CHANGED
data/lib/rlp/constant.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# -*- encoding : ascii-8bit -*-
|
2
|
+
|
1
3
|
module RLP
|
2
4
|
module Constant
|
3
5
|
SHORT_LENGTH_LIMIT = 56
|
@@ -6,7 +8,7 @@ module RLP
|
|
6
8
|
PRIMITIVE_PREFIX_OFFSET = 0x80
|
7
9
|
LIST_PREFIX_OFFSET = 0xc0
|
8
10
|
|
9
|
-
BYTE_ZERO = "\x00".
|
10
|
-
BYTE_EMPTY = ''.
|
11
|
+
BYTE_ZERO = "\x00".freeze
|
12
|
+
BYTE_EMPTY = ''.freeze
|
11
13
|
end
|
12
14
|
end
|
data/lib/rlp/data.rb
CHANGED
data/lib/rlp/decode.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# -*- encoding : ascii-8bit -*-
|
2
|
+
|
1
3
|
module RLP
|
2
4
|
module Decode
|
3
5
|
include Constant
|
@@ -14,13 +16,15 @@ module RLP
|
|
14
16
|
# is updated whenever one of its fields changes or prevent such changes
|
15
17
|
# entirely ({RLP::Sedes::Serializable} does the latter).
|
16
18
|
#
|
17
|
-
# @param
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
19
|
+
# @param options (Hash) deserialization options:
|
20
|
+
#
|
21
|
+
# * sedes (Object) an object implementing a function `deserialize(code)`
|
22
|
+
# which will be applied after decoding, or `nil` if no deserialization
|
23
|
+
# should be performed
|
24
|
+
# * strict (Boolean) if false inputs that are longer than necessary don't
|
25
|
+
# cause an exception
|
26
|
+
# * (any options left) (Hash) additional keyword arguments passed to the
|
27
|
+
# deserializer
|
24
28
|
#
|
25
29
|
# @return [Object] the decoded and maybe deserialized object
|
26
30
|
#
|
@@ -28,8 +32,10 @@ module RLP
|
|
28
32
|
# the root item and `strict` is true
|
29
33
|
# @raise [RLP::Error::DeserializationError] if the deserialization fails
|
30
34
|
#
|
31
|
-
def decode(rlp,
|
35
|
+
def decode(rlp, **options)
|
32
36
|
rlp = str_to_bytes(rlp)
|
37
|
+
sedes = options.delete(:sedes)
|
38
|
+
strict = options.has_key?(:strict) ? options.delete(:strict) : true
|
33
39
|
|
34
40
|
begin
|
35
41
|
item, next_start = consume_item(rlp, 0)
|
@@ -40,8 +46,7 @@ module RLP
|
|
40
46
|
raise DecodingError.new("RLP string ends with #{rlp.size - next_start} superfluous bytes", rlp) if next_start != rlp.size && strict
|
41
47
|
|
42
48
|
if sedes
|
43
|
-
|
44
|
-
obj = sedes.is_a?(Sedes::Serializable) ?
|
49
|
+
obj = sedes.instance_of?(Class) && sedes.include?(Sedes::Serializable) ?
|
45
50
|
sedes.deserialize(item, **options) :
|
46
51
|
sedes.deserialize(item)
|
47
52
|
|
@@ -102,7 +107,7 @@ module RLP
|
|
102
107
|
elsif b0 < LIST_PREFIX_OFFSET + SHORT_LENGTH_LIMIT # short list
|
103
108
|
[:list, b0 - LIST_PREFIX_OFFSET, start + 1]
|
104
109
|
else # long list
|
105
|
-
raise DecodingError.new('Length starts with zero bytes
|
110
|
+
raise DecodingError.new('Length starts with zero bytes', rlp) if rlp.slice(start+1) == BYTE_ZERO
|
106
111
|
|
107
112
|
ll = b0 - LIST_PREFIX_OFFSET - SHORT_LENGTH_LIMIT + 1
|
108
113
|
l = big_endian_to_int rlp[(start+1)...(start+1+ll)]
|
data/lib/rlp/decode_lazy.rb
CHANGED
data/lib/rlp/encode.rb
CHANGED
data/lib/rlp/error.rb
CHANGED
data/lib/rlp/lazy_list.rb
CHANGED
data/lib/rlp/sedes.rb
CHANGED
data/lib/rlp/sedes/binary.rb
CHANGED
data/lib/rlp/sedes/list.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# -*- encoding : ascii-8bit -*-
|
2
|
+
|
1
3
|
module RLP
|
2
4
|
module Sedes
|
3
5
|
##
|
@@ -32,6 +34,9 @@ module RLP
|
|
32
34
|
begin
|
33
35
|
result.push sedes.serialize(element)
|
34
36
|
rescue SerializationError => e
|
37
|
+
puts "List.serialize"
|
38
|
+
puts e
|
39
|
+
puts e.backtrace[0,20].join("\n")
|
35
40
|
raise ListSerializationError.new(obj: obj, element_exception: e, index: i)
|
36
41
|
end
|
37
42
|
end
|
data/lib/rlp/sedes/raw.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# -*- encoding : ascii-8bit -*-
|
2
|
+
|
1
3
|
module RLP
|
2
4
|
module Sedes
|
3
5
|
##
|
@@ -32,6 +34,10 @@ module RLP
|
|
32
34
|
end
|
33
35
|
end
|
34
36
|
|
37
|
+
def inherit_serializable_fields!
|
38
|
+
@serializable_fields = superclass.serializable_fields
|
39
|
+
end
|
40
|
+
|
35
41
|
def serializable_fields
|
36
42
|
@serializable_fields
|
37
43
|
end
|
@@ -50,11 +56,16 @@ module RLP
|
|
50
56
|
begin
|
51
57
|
serializable_sedes.serialize(field_values)
|
52
58
|
rescue ListSerializationError => e
|
59
|
+
puts "Serializable.serialize"
|
60
|
+
puts e
|
61
|
+
puts e.backtrace[0,20].join("\n")
|
53
62
|
raise ObjectSerializationError.new(obj: obj, sedes: self, list_exception: e)
|
54
63
|
end
|
55
64
|
end
|
56
65
|
|
57
|
-
def deserialize(serial,
|
66
|
+
def deserialize(serial, **options)
|
67
|
+
exclude = options.delete(:exclude)
|
68
|
+
|
58
69
|
begin
|
59
70
|
values = serializable_sedes.deserialize(serial)
|
60
71
|
rescue ListDeserializationError => e
|
@@ -64,10 +75,18 @@ module RLP
|
|
64
75
|
params = Hash[*serializable_fields.keys.zip(values).flatten(1)]
|
65
76
|
params.delete_if {|field, value| exclude.include?(field) } if exclude
|
66
77
|
|
67
|
-
obj = self.new params.merge(
|
78
|
+
obj = self.new params.merge(options)
|
68
79
|
obj.instance_variable_set :@_mutable, false
|
69
80
|
obj
|
70
81
|
end
|
82
|
+
|
83
|
+
##
|
84
|
+
# Create a new sedes considering only a reduced set of fields.
|
85
|
+
#
|
86
|
+
def exclude(excluded_fields)
|
87
|
+
fields = serializable_fields.dup.delete_if {|k, v| excluded_fields.include?(k) }
|
88
|
+
Class.new(self).tap {|cls| cls.set_serializable_fields fields }
|
89
|
+
end
|
71
90
|
end
|
72
91
|
|
73
92
|
class <<self
|
@@ -79,34 +98,49 @@ module RLP
|
|
79
98
|
attr_accessor :_cached_rlp
|
80
99
|
|
81
100
|
def initialize(*args)
|
82
|
-
serializable_initialize(
|
101
|
+
serializable_initialize parse_field_args(args)
|
83
102
|
end
|
84
103
|
|
85
|
-
|
86
|
-
|
104
|
+
##
|
105
|
+
# Mimic python's argument syntax, accept both normal arguments and named
|
106
|
+
# arguments. Normal argument overrides named argument.
|
107
|
+
#
|
108
|
+
def parse_field_args(args)
|
109
|
+
h = {}
|
87
110
|
|
111
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
88
112
|
field_set = self.class.serializable_fields.keys
|
89
113
|
|
90
|
-
self.class.serializable_fields.keys
|
91
|
-
|
92
|
-
|
114
|
+
fields = self.class.serializable_fields.keys[0,args.size]
|
115
|
+
fields.zip(args).each do |(field, arg)|
|
116
|
+
h[field] = arg
|
93
117
|
field_set.delete field
|
94
118
|
end
|
95
119
|
|
96
120
|
options.each do |field, value|
|
97
121
|
if field_set.include?(field)
|
98
|
-
|
122
|
+
h[field] = value
|
99
123
|
field_set.delete field
|
100
124
|
end
|
101
125
|
end
|
102
126
|
|
127
|
+
h
|
128
|
+
end
|
129
|
+
|
130
|
+
def serializable_initialize(fields)
|
131
|
+
make_mutable!
|
132
|
+
|
133
|
+
field_set = self.class.serializable_fields.keys
|
134
|
+
fields.each do |field, value|
|
135
|
+
_set_field field, value
|
136
|
+
field_set.delete field
|
137
|
+
end
|
138
|
+
|
103
139
|
raise TypeError, "Not all fields initialized" unless field_set.size == 0
|
104
140
|
end
|
105
141
|
|
106
142
|
def _set_field(field, value)
|
107
|
-
unless instance_variable_defined?(:@_mutable)
|
108
|
-
@_mutable = true
|
109
|
-
end
|
143
|
+
make_mutable! unless instance_variable_defined?(:@_mutable)
|
110
144
|
|
111
145
|
if mutable? || !self.class.serializable_fields.has_key?(field)
|
112
146
|
instance_variable_set :"@#{field}", value
|
@@ -125,7 +159,7 @@ module RLP
|
|
125
159
|
end
|
126
160
|
|
127
161
|
def make_immutable!
|
128
|
-
|
162
|
+
make_mutable!
|
129
163
|
self.class.serializable_fields.keys.each do |field|
|
130
164
|
::RLP::Utils.make_immutable! send(field)
|
131
165
|
end
|
@@ -133,6 +167,11 @@ module RLP
|
|
133
167
|
@_mutable = false
|
134
168
|
self
|
135
169
|
end
|
170
|
+
|
171
|
+
def make_mutable!
|
172
|
+
@_mutable = true
|
173
|
+
end
|
174
|
+
|
136
175
|
end
|
137
176
|
end
|
138
177
|
end
|
data/lib/rlp/utils.rb
CHANGED
data/lib/rlp/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rlp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jan Xie
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-03-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|