bert 1.1.2-java
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +8 -0
- data/History.txt +31 -0
- data/LICENSE +20 -0
- data/README.md +77 -0
- data/Rakefile +96 -0
- data/VERSION +1 -0
- data/bench/bench.rb +36 -0
- data/bench/decode_bench.rb +87 -0
- data/bench/encode_bench.rb +36 -0
- data/bench/results.txt +55 -0
- data/bert.gemspec +72 -0
- data/ext/bert/c/decode.c +438 -0
- data/ext/bert/c/extconf.rb +11 -0
- data/lib/bert.rb +25 -0
- data/lib/bert/bert.rb +21 -0
- data/lib/bert/decode.rb +248 -0
- data/lib/bert/decoder.rb +11 -0
- data/lib/bert/encode.rb +138 -0
- data/lib/bert/encoder.rb +45 -0
- data/lib/bert/types.rb +21 -0
- data/test/bert_test.rb +69 -0
- data/test/decoder_test.rb +91 -0
- data/test/encoder_test.rb +92 -0
- data/test/test_helper.rb +10 -0
- metadata +92 -0
data/lib/bert/encoder.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
module BERT
|
2
|
+
class Encoder
|
3
|
+
# Encode a Ruby object into a BERT.
|
4
|
+
# +ruby+ is the Ruby object
|
5
|
+
#
|
6
|
+
# Returns a BERT
|
7
|
+
def self.encode(ruby)
|
8
|
+
complex_ruby = convert(ruby)
|
9
|
+
Encode.encode(complex_ruby)
|
10
|
+
end
|
11
|
+
|
12
|
+
# Convert complex Ruby form in simple Ruby form.
|
13
|
+
# +item+ is the Ruby object to convert
|
14
|
+
#
|
15
|
+
# Returns the converted Ruby object
|
16
|
+
def self.convert(item)
|
17
|
+
case item
|
18
|
+
when Hash
|
19
|
+
pairs = []
|
20
|
+
item.each_pair { |k, v| pairs << t[convert(k), convert(v)] }
|
21
|
+
t[:bert, :dict, pairs]
|
22
|
+
when Tuple
|
23
|
+
Tuple.new(item.map { |x| convert(x) })
|
24
|
+
when Array
|
25
|
+
item.map { |x| convert(x) }
|
26
|
+
when nil
|
27
|
+
t[:bert, :nil]
|
28
|
+
when TrueClass
|
29
|
+
t[:bert, :true]
|
30
|
+
when FalseClass
|
31
|
+
t[:bert, :false]
|
32
|
+
when Time
|
33
|
+
t[:bert, :time, item.to_i / 1_000_000, item.to_i % 1_000_000, item.usec]
|
34
|
+
when Regexp
|
35
|
+
options = []
|
36
|
+
options << :caseless if item.options & Regexp::IGNORECASE > 0
|
37
|
+
options << :extended if item.options & Regexp::EXTENDED > 0
|
38
|
+
options << :multiline if item.options & Regexp::MULTILINE > 0
|
39
|
+
t[:bert, :regex, item.source, options]
|
40
|
+
else
|
41
|
+
item
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/bert/types.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module BERT
|
2
|
+
module Types
|
3
|
+
SMALL_INT = 97
|
4
|
+
INT = 98
|
5
|
+
SMALL_BIGNUM = 110
|
6
|
+
LARGE_BIGNUM = 111
|
7
|
+
FLOAT = 99
|
8
|
+
ATOM = 100
|
9
|
+
SMALL_TUPLE = 104
|
10
|
+
LARGE_TUPLE = 105
|
11
|
+
NIL = 106
|
12
|
+
STRING = 107
|
13
|
+
LIST = 108
|
14
|
+
BIN = 109
|
15
|
+
FUN = 117
|
16
|
+
NEW_FUN = 112
|
17
|
+
MAGIC = 131
|
18
|
+
MAX_INT = (1 << 27) -1
|
19
|
+
MIN_INT = -(1 << 27)
|
20
|
+
end
|
21
|
+
end
|
data/test/bert_test.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class BertTest < Test::Unit::TestCase
|
4
|
+
context "BERT" do
|
5
|
+
setup do
|
6
|
+
time = Time.at(1254976067)
|
7
|
+
@ruby = t[:user, {:name => 'TPW'}, [/cat/i, 9.9], time, nil, true, false, :true, :false]
|
8
|
+
@bert = "\203h\td\000\004userh\003d\000\004bertd\000\004dictl\000\000\000\001h\002d\000\004namem\000\000\000\003TPWjl\000\000\000\002h\004d\000\004bertd\000\005regexm\000\000\000\003catl\000\000\000\001d\000\bcaselessjc9.900000000000000e+00\000\000\000\000\000\000\000\000\000\000jh\005d\000\004bertd\000\004timeb\000\000\004\346b\000\016\344\303a\000h\002d\000\004bertd\000\003nilh\002d\000\004bertd\000\004trueh\002d\000\004bertd\000\005falsed\000\004trued\000\005false"
|
9
|
+
@ebin = "<<131,104,9,100,0,4,117,115,101,114,104,3,100,0,4,98,101,114,116,100,0,4,100,105,99,116,108,0,0,0,1,104,2,100,0,4,110,97,109,101,109,0,0,0,3,84,80,87,106,108,0,0,0,2,104,4,100,0,4,98,101,114,116,100,0,5,114,101,103,101,120,109,0,0,0,3,99,97,116,108,0,0,0,1,100,0,8,99,97,115,101,108,101,115,115,106,99,57,46,57,48,48,48,48,48,48,48,48,48,48,48,48,48,48,101,43,48,48,0,0,0,0,0,0,0,0,0,0,106,104,5,100,0,4,98,101,114,116,100,0,4,116,105,109,101,98,0,0,4,230,98,0,14,228,195,97,0,104,2,100,0,4,98,101,114,116,100,0,3,110,105,108,104,2,100,0,4,98,101,114,116,100,0,4,116,114,117,101,104,2,100,0,4,98,101,114,116,100,0,5,102,97,108,115,101,100,0,4,116,114,117,101,100,0,5,102,97,108,115,101>>"
|
10
|
+
end
|
11
|
+
|
12
|
+
should "encode" do
|
13
|
+
assert_equal @bert, BERT.encode(@ruby)
|
14
|
+
end
|
15
|
+
|
16
|
+
should "decode" do
|
17
|
+
assert_equal @ruby, BERT.decode(@bert)
|
18
|
+
end
|
19
|
+
|
20
|
+
should "ebin" do
|
21
|
+
assert_equal @ebin, BERT.ebin(@bert)
|
22
|
+
end
|
23
|
+
|
24
|
+
should "do roundtrips" do
|
25
|
+
dd = []
|
26
|
+
dd << 1
|
27
|
+
dd << 1.0
|
28
|
+
dd << :a
|
29
|
+
dd << t[]
|
30
|
+
dd << t[:a]
|
31
|
+
dd << t[:a, :b]
|
32
|
+
dd << t[t[:a, 1], t[:b, 2]]
|
33
|
+
dd << []
|
34
|
+
dd << [:a]
|
35
|
+
dd << [:a, 1]
|
36
|
+
dd << [[:a, 1], [:b, 2]]
|
37
|
+
dd << "a"
|
38
|
+
|
39
|
+
dd << nil
|
40
|
+
dd << true
|
41
|
+
dd << false
|
42
|
+
dd << {}
|
43
|
+
dd << {:a => 1}
|
44
|
+
dd << {:a => 1, :b => 2}
|
45
|
+
dd << Time.now
|
46
|
+
dd << /^c(a)t$/i
|
47
|
+
|
48
|
+
dd << 256**256 - 1
|
49
|
+
|
50
|
+
dd << :true
|
51
|
+
dd << :false
|
52
|
+
dd << :nil
|
53
|
+
|
54
|
+
dd.each do |d|
|
55
|
+
assert_equal d, BERT.decode(BERT.encode(d))
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# should "let me inspect it" do
|
60
|
+
# puts
|
61
|
+
# p @ruby
|
62
|
+
# ruby2 = BERT.decode(@bert)
|
63
|
+
# p ruby2
|
64
|
+
# bert2 = BERT.encode(ruby2)
|
65
|
+
# ruby3 = BERT.decode(bert2)
|
66
|
+
# p ruby3
|
67
|
+
# end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class DecoderTest < Test::Unit::TestCase
|
4
|
+
BERT_NIL = [131,104,2,100,0,4,98,101,114,116,100,0,3,110,105,108].pack('c*')
|
5
|
+
BERT_TRUE = [131,104,2,100,0,4,98,101,114,116,100,0,4,116,114,117,101].pack('c*')
|
6
|
+
BERT_FALSE = [131,104,2,100,0,4,98,101,114,116,100,0,5,102,97,108,115,101].pack('c*')
|
7
|
+
|
8
|
+
context "BERT Decoder complex type converter" do
|
9
|
+
should "convert nil" do
|
10
|
+
assert_equal nil, BERT::Decoder.decode(BERT_NIL)
|
11
|
+
end
|
12
|
+
|
13
|
+
should "convert nested nil" do
|
14
|
+
bert = [131,108,0,0,0,2,104,2,100,0,4,98,101,114,116,100,0,3,110,105,
|
15
|
+
108,108,0,0,0,1,104,2,100,0,4,98,101,114,116,100,0,3,110,105,
|
16
|
+
108,106,106].pack('c*')
|
17
|
+
assert_equal [nil, [nil]], BERT::Decoder.decode(bert)
|
18
|
+
end
|
19
|
+
|
20
|
+
should "convert hashes" do
|
21
|
+
bert = [131,104,3,100,0,4,98,101,114,116,100,0,4,100,105,99,116,108,
|
22
|
+
0,0,0,1,104,2,100,0,3,102,111,111,109,0,0,0,3,98,97,114,
|
23
|
+
106].pack('c*')
|
24
|
+
after = {:foo => 'bar'}
|
25
|
+
assert_equal after, BERT::Decoder.decode(bert)
|
26
|
+
end
|
27
|
+
|
28
|
+
should "convert empty hashes" do
|
29
|
+
bert = [131,104,3,100,0,4,98,101,114,116,100,0,4,100,105,99,116,
|
30
|
+
106].pack('c*')
|
31
|
+
after = {}
|
32
|
+
assert_equal after, BERT::Decoder.decode(bert)
|
33
|
+
end
|
34
|
+
|
35
|
+
should "convert nested hashes" do
|
36
|
+
bert = [131,104,3,100,0,4,98,101,114,116,100,0,4,100,105,99,116,108,0,
|
37
|
+
0,0,1,104,2,100,0,3,102,111,111,104,3,100,0,4,98,101,114,116,
|
38
|
+
100,0,4,100,105,99,116,108,0,0,0,1,104,2,100,0,3,98,97,122,109,
|
39
|
+
0,0,0,3,98,97,114,106,106].pack('c*')
|
40
|
+
after = {:foo => {:baz => 'bar'}}
|
41
|
+
assert_equal after, BERT::Decoder.decode(bert)
|
42
|
+
end
|
43
|
+
|
44
|
+
should "convert true" do
|
45
|
+
assert_equal true, BERT::Decoder.decode(BERT_TRUE)
|
46
|
+
end
|
47
|
+
|
48
|
+
should "convert false" do
|
49
|
+
assert_equal false, BERT::Decoder.decode(BERT_FALSE)
|
50
|
+
end
|
51
|
+
|
52
|
+
should "convert times" do
|
53
|
+
bert = [131,104,5,100,0,4,98,101,114,116,100,0,4,116,105,109,101,98,0,
|
54
|
+
0,4,230,98,0,14,228,195,97,0].pack('c*')
|
55
|
+
after = Time.at(1254976067)
|
56
|
+
assert_equal after, BERT::Decoder.decode(bert)
|
57
|
+
end
|
58
|
+
|
59
|
+
should "convert regexen" do
|
60
|
+
bert = [131,104,4,100,0,4,98,101,114,116,100,0,5,114,101,103,101,120,
|
61
|
+
109,0,0,0,7,94,99,40,97,41,116,36,108,0,0,0,2,100,0,8,99,97,
|
62
|
+
115,101,108,101,115,115,100,0,8,101,120,116,101,110,100,101,
|
63
|
+
100,106].pack('c*')
|
64
|
+
after = /^c(a)t$/ix
|
65
|
+
assert_equal after, BERT::Decoder.decode(bert)
|
66
|
+
end
|
67
|
+
|
68
|
+
should "leave other stuff alone" do
|
69
|
+
bert = [131,108,0,0,0,3,97,1,99,50,46,48,48,48,48,48,48,48,48,48,48,48,
|
70
|
+
48,48,48,48,48,48,48,48,48,101,43,48,48,0,0,0,0,0,108,0,0,0,2,
|
71
|
+
100,0,3,102,111,111,109,0,0,0,3,98,97,114,106,106].pack('c*')
|
72
|
+
after = [1, 2.0, [:foo, 'bar']]
|
73
|
+
assert_equal after, BERT::Decoder.decode(bert)
|
74
|
+
end
|
75
|
+
|
76
|
+
should "handle bignums" do
|
77
|
+
bert = [131,110,8,0,0,0,232,137,4,35,199,138].pack('c*')
|
78
|
+
assert_equal 10_000_000_000_000_000_000, BERT::Decoder.decode(bert)
|
79
|
+
end
|
80
|
+
|
81
|
+
should "handle bytelists" do
|
82
|
+
bert = [131,104,3,100,0,3,102,111,111,107,0,2,97,97,100,0,3,98,97,114].pack('c*')
|
83
|
+
assert_equal t[:foo, [97, 97], :bar], BERT::Decoder.decode(bert)
|
84
|
+
end
|
85
|
+
|
86
|
+
should "handle massive binaries" do
|
87
|
+
bert = [131,109,0,128,0,0].pack('c*') + ('a' * (8 * 1024 * 1024))
|
88
|
+
assert_equal (8 * 1024 * 1024), BERT::Decoder.decode(bert).size
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class EncoderTest < Test::Unit::TestCase
|
4
|
+
context "BERT Encoder complex type converter" do
|
5
|
+
should "convert nil" do
|
6
|
+
assert_equal [:bert, :nil], BERT::Encoder.convert(nil)
|
7
|
+
end
|
8
|
+
|
9
|
+
should "convert nested nil" do
|
10
|
+
before = [nil, [nil]]
|
11
|
+
after = [[:bert, :nil], [[:bert, :nil]]]
|
12
|
+
assert_equal after, BERT::Encoder.convert(before)
|
13
|
+
end
|
14
|
+
|
15
|
+
should "convert hashes" do
|
16
|
+
before = {:foo => 'bar'}
|
17
|
+
after = [:bert, :dict, [[:foo, 'bar']]]
|
18
|
+
assert_equal after, BERT::Encoder.convert(before)
|
19
|
+
end
|
20
|
+
|
21
|
+
should "convert nested hashes" do
|
22
|
+
before = {:foo => {:baz => 'bar'}}
|
23
|
+
after = [:bert, :dict, [[:foo, [:bert, :dict, [[:baz, "bar"]]]]]]
|
24
|
+
assert_equal after, BERT::Encoder.convert(before)
|
25
|
+
end
|
26
|
+
|
27
|
+
should "convert hash to tuple with array of tuples" do
|
28
|
+
arr = BERT::Encoder.convert({:foo => 'bar'})
|
29
|
+
assert arr.is_a?(Array)
|
30
|
+
assert arr[2].is_a?(Array)
|
31
|
+
assert arr[2][0].is_a?(Array)
|
32
|
+
end
|
33
|
+
|
34
|
+
should "convert tuple to array" do
|
35
|
+
arr = BERT::Encoder.convert(t[:foo, 2])
|
36
|
+
assert arr.is_a?(Array)
|
37
|
+
end
|
38
|
+
|
39
|
+
should "convert array to erl list" do
|
40
|
+
list = BERT::Encoder.convert([1, 2])
|
41
|
+
assert list.is_a?(Array)
|
42
|
+
end
|
43
|
+
|
44
|
+
should "convert an array in a tuple" do
|
45
|
+
arrtup = BERT::Encoder.convert(t[:foo, [1, 2]])
|
46
|
+
assert arrtup.is_a?(Array)
|
47
|
+
assert arrtup[1].is_a?(Array)
|
48
|
+
end
|
49
|
+
|
50
|
+
should "convert true" do
|
51
|
+
before = true
|
52
|
+
after = [:bert, :true]
|
53
|
+
assert_equal after, BERT::Encoder.convert(before)
|
54
|
+
end
|
55
|
+
|
56
|
+
should "convert false" do
|
57
|
+
before = false
|
58
|
+
after = [:bert, :false]
|
59
|
+
assert_equal after, BERT::Encoder.convert(before)
|
60
|
+
end
|
61
|
+
|
62
|
+
should "convert times" do
|
63
|
+
before = Time.at(1254976067)
|
64
|
+
after = [:bert, :time, 1254, 976067, 0]
|
65
|
+
assert_equal after, BERT::Encoder.convert(before)
|
66
|
+
end
|
67
|
+
|
68
|
+
should "convert regexen" do
|
69
|
+
before = /^c(a)t$/ix
|
70
|
+
after = [:bert, :regex, '^c(a)t$', [:caseless, :extended]]
|
71
|
+
assert_equal after, BERT::Encoder.convert(before)
|
72
|
+
end
|
73
|
+
|
74
|
+
should "properly convert types" do
|
75
|
+
ruby = t[:user, {:name => 'TPW'}, [/cat/i, 9.9], nil, true, false, :true, :false]
|
76
|
+
cruby = BERT::Encoder.convert(ruby)
|
77
|
+
assert cruby.instance_of?(BERT::Tuple)
|
78
|
+
assert cruby[0].instance_of?(Symbol)
|
79
|
+
assert cruby[1].instance_of?(BERT::Tuple)
|
80
|
+
end
|
81
|
+
|
82
|
+
should "handle bignums" do
|
83
|
+
bert = [131,110,8,0,0,0,232,137,4,35,199,138].pack('c*')
|
84
|
+
assert_equal bert, BERT::Encoder.encode(10_000_000_000_000_000_000)
|
85
|
+
end
|
86
|
+
|
87
|
+
should "leave other stuff alone" do
|
88
|
+
before = [1, 2.0, [:foo, 'bar']]
|
89
|
+
assert_equal before, BERT::Encoder.convert(before)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'shoulda'
|
4
|
+
|
5
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'ext', 'bert', 'c'))
|
7
|
+
|
8
|
+
load 'bert.rb'
|
9
|
+
|
10
|
+
puts "Using #{BERT::Decode.impl} implementation."
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bert
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.2
|
5
|
+
platform: java
|
6
|
+
authors:
|
7
|
+
- Tom Preston-Werner
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-04-03 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: thoughtbot-shoulda
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: BERT Serializiation for Ruby
|
26
|
+
email: tom@mojombo.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.md
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- History.txt
|
38
|
+
- LICENSE
|
39
|
+
- README.md
|
40
|
+
- Rakefile
|
41
|
+
- VERSION
|
42
|
+
- bench/bench.rb
|
43
|
+
- bench/decode_bench.rb
|
44
|
+
- bench/encode_bench.rb
|
45
|
+
- bench/results.txt
|
46
|
+
- bert.gemspec
|
47
|
+
- ext/bert/c/decode.c
|
48
|
+
- ext/bert/c/extconf.rb
|
49
|
+
- lib/bert.rb
|
50
|
+
- lib/bert/bert.rb
|
51
|
+
- lib/bert/decode.rb
|
52
|
+
- lib/bert/decoder.rb
|
53
|
+
- lib/bert/encode.rb
|
54
|
+
- lib/bert/encoder.rb
|
55
|
+
- lib/bert/types.rb
|
56
|
+
- test/bert_test.rb
|
57
|
+
- test/decoder_test.rb
|
58
|
+
- test/encoder_test.rb
|
59
|
+
- test/test_helper.rb
|
60
|
+
has_rdoc: true
|
61
|
+
homepage: http://github.com/mojombo/bert
|
62
|
+
licenses: []
|
63
|
+
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options:
|
66
|
+
- --charset=UTF-8
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: "0"
|
74
|
+
version:
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: "0"
|
80
|
+
version:
|
81
|
+
requirements: []
|
82
|
+
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.3.5
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: BERT Serializiation for Ruby
|
88
|
+
test_files:
|
89
|
+
- test/bert_test.rb
|
90
|
+
- test/decoder_test.rb
|
91
|
+
- test/encoder_test.rb
|
92
|
+
- test/test_helper.rb
|