Skip to content

Commit

Permalink
use bert tag for complex types
Browse files Browse the repository at this point in the history
  • Loading branch information
mojombo committed Oct 12, 2009
1 parent 75a4db8 commit cc9d189
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 45 deletions.
50 changes: 31 additions & 19 deletions lib/bert/decoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,44 @@ def self.convert(item)
case item
when TrueClass, FalseClass
item.to_s.to_sym
when [:nil, :nil]
nil
when Erl::List
item.map { |x| convert(x) }
when Array
case item.first
when :dict
item[1].inject({}) do |acc, x|
acc[convert(x[0])] = convert(x[1]); acc
end
when :bool
item[1]
when :time
Time.at(item[1] * 1_000_000 + item[2], item[3])
when :regex
options = 0
options |= Regexp::EXTENDED if item[2].include?(:extended)
options |= Regexp::IGNORECASE if item[2].include?(:caseless)
options |= Regexp::MULTILINE if item[2].include?(:multiline)
Regexp.new(item[1], options)
else
Tuple.new(item.map { |x| convert(x) })
if item[0] == :bert
convert_bert(item)
else
Tuple.new(item.map { |x| convert(x) })
end
else
item
end
end

# Convert complex types.
# +item+ is the complex type array
#
# Returns the converted Ruby object
def self.convert_bert(item)
case item[1]
when :nil
nil
when :dict
item[2].inject({}) do |acc, x|
acc[convert(x[0])] = convert(x[1]); acc
end
when :bool
item[2]
when :time
Time.at(item[2] * 1_000_000 + item[3], item[4])
when :regex
options = 0
options |= Regexp::EXTENDED if item[3].include?(:extended)
options |= Regexp::IGNORECASE if item[3].include?(:caseless)
options |= Regexp::MULTILINE if item[3].include?(:multiline)
Regexp.new(item[2], options)
else
nil
end
end
end
end
10 changes: 5 additions & 5 deletions lib/bert/encoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ def self.convert(item)
when Hash
pairs = Erl::List[]
item.each_pair { |k, v| pairs << [convert(k), convert(v)] }
[:dict, pairs]
[:bert, :dict, pairs]
when Tuple
item.map { |x| convert(x) }
when Array
Erl::List.new(item.map { |x| convert(x) })
when nil
[:nil, :nil]
[:bert, :nil]
when TrueClass, FalseClass
[:bool, item.to_s.to_sym]
[:bert, :bool, item.to_s.to_sym]
when Time
[:time, item.to_i / 1_000_000, item.to_i % 1_000_000, item.usec]
[:bert, :time, item.to_i / 1_000_000, item.to_i % 1_000_000, item.usec]
when Regexp
options = []
options << :caseless if item.options & Regexp::IGNORECASE > 0
options << :extended if item.options & Regexp::EXTENDED > 0
options << :multiline if item.options & Regexp::MULTILINE > 0
[:regex, item.source, options]
[:bert, :regex, item.source, options]
else
item
end
Expand Down
4 changes: 2 additions & 2 deletions test/bert_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class BertTest < Test::Unit::TestCase
setup do
time = Time.at(1254976067)
@ruby = t[:user, {:name => 'TPW'}, [/cat/i, 9.9], time, nil, true, false, :true, :false]
@bert = "\203h\td\000\004userh\002d\000\004dictl\000\000\000\001h\002d\000\004namem\000\000\000\003TPWjl\000\000\000\002h\003d\000\005regexm\000\000\000\003cath\001d\000\bcaselessc9.900000000000000e+00\000\000\000\000\000\000\000\000\000\000jh\004d\000\004timeb\000\000\004\346b\000\016\344\303a\000h\002d\000\003nild\000\003nilh\002d\000\004boold\000\004trueh\002d\000\004boold\000\005falsed\000\004trued\000\005false"
@ebin = "<<131,104,9,100,0,4,117,115,101,114,104,2,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,3,100,0,5,114,101,103,101,120,109,0,0,0,3,99,97,116,104,1,100,0,8,99,97,115,101,108,101,115,115,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,4,100,0,4,116,105,109,101,98,0,0,4,230,98,0,14,228,195,97,0,104,2,100,0,3,110,105,108,100,0,3,110,105,108,104,2,100,0,4,98,111,111,108,100,0,4,116,114,117,101,104,2,100,0,4,98,111,111,108,100,0,5,102,97,108,115,101,100,0,4,116,114,117,101,100,0,5,102,97,108,115,101>>"
@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\003cath\001d\000\bcaselessc9.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\003d\000\004bertd\000\004boold\000\004trueh\003d\000\004bertd\000\004boold\000\005falsed\000\004trued\000\005false"
@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,104,1,100,0,8,99,97,115,101,108,101,115,115,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,3,100,0,4,98,101,114,116,100,0,4,98,111,111,108,100,0,4,116,114,117,101,104,3,100,0,4,98,101,114,116,100,0,4,98,111,111,108,100,0,5,102,97,108,115,101,100,0,4,116,114,117,101,100,0,5,102,97,108,115,101>>"
end

should "encode" do
Expand Down
18 changes: 9 additions & 9 deletions test/decoder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,55 @@
class DecoderTest < Test::Unit::TestCase
context "BERT Decoder complex type converter" do
should "convert nil" do
before = [:nil, :nil]
before = [:bert, :nil]
after = nil
assert_equal after, BERT::Decoder.convert(before)
end

should "convert nested nil" do
before = [[:nil, :nil], [[:nil, :nil]]]
before = [[:bert, :nil], [[:bert, :nil]]]
after = [nil, [nil]]
assert_equal after, BERT::Decoder.convert(before)
end

should "convert hashes" do
before = [:dict, [[:foo, 'bar']]]
before = [:bert, :dict, [[:foo, 'bar']]]
after = {:foo => 'bar'}
assert_equal after, BERT::Decoder.convert(before)
end

should "convert empty hashes" do
before = [:dict, []]
before = [:bert, :dict, []]
after = {}
assert_equal after, BERT::Decoder.convert(before)
end

should "convert nested hashes" do
before = [:dict, [[:foo, [:dict, [[:baz, 'bar']]]]]]
before = [:bert, :dict, [[:foo, [:bert, :dict, [[:baz, 'bar']]]]]]
after = {:foo => {:baz => 'bar'}}
assert_equal after, BERT::Decoder.convert(before)
end

should "convert true" do
before = [:bool, true]
before = [:bert, :bool, true]
after = true
assert_equal after, BERT::Decoder.convert(before)
end

should "convert false" do
before = [:bool, false]
before = [:bert, :bool, false]
after = false
assert_equal after, BERT::Decoder.convert(before)
end

should "convert times" do
before = [:time, 1254, 976067, 0]
before = [:bert, :time, 1254, 976067, 0]
after = Time.at(1254976067)
assert_equal after, BERT::Decoder.convert(before)
end

should "convert regexen" do
before = [:regex, '^c(a)t$', [:caseless, :extended]]
before = [:bert, :regex, '^c(a)t$', [:caseless, :extended]]
after = /^c(a)t$/ix
assert_equal after, BERT::Decoder.convert(before)
end
Expand Down
20 changes: 10 additions & 10 deletions test/encoder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@
class EncoderTest < Test::Unit::TestCase
context "BERT Encoder complex type converter" do
should "convert nil" do
assert_equal [:nil, :nil], BERT::Encoder.convert(nil)
assert_equal [:bert, :nil], BERT::Encoder.convert(nil)
end

should "convert nested nil" do
before = [nil, [nil]]
after = [[:nil, :nil], [[:nil, :nil]]]
after = [[:bert, :nil], [[:bert, :nil]]]
assert_equal after, BERT::Encoder.convert(before)
end

should "convert hashes" do
before = {:foo => 'bar'}
after = [:dict, [[:foo, 'bar']]]
after = [:bert, :dict, [[:foo, 'bar']]]
assert_equal after, BERT::Encoder.convert(before)
end

should "convert nested hashes" do
before = {:foo => {:baz => 'bar'}}
after = [:dict, [[:foo, [:dict, [[:baz, "bar"]]]]]]
after = [:bert, :dict, [[:foo, [:bert, :dict, [[:baz, "bar"]]]]]]
assert_equal after, BERT::Encoder.convert(before)
end

should "convert hash to tuple with array of tuples" do
arr = BERT::Encoder.convert({:foo => 'bar'})
assert arr.is_a?(Array)
assert arr[1].is_a?(Erl::List)
assert arr[1][0].is_a?(Array)
assert arr[2].is_a?(Erl::List)
assert arr[2][0].is_a?(Array)
end

should "convert tuple to array" do
Expand All @@ -49,25 +49,25 @@ class EncoderTest < Test::Unit::TestCase

should "convert true" do
before = true
after = [:bool, :true]
after = [:bert, :bool, :true]
assert_equal after, BERT::Encoder.convert(before)
end

should "convert false" do
before = false
after = [:bool, :false]
after = [:bert, :bool, :false]
assert_equal after, BERT::Encoder.convert(before)
end

should "convert times" do
before = Time.at(1254976067)
after = [:time, 1254, 976067, 0]
after = [:bert, :time, 1254, 976067, 0]
assert_equal after, BERT::Encoder.convert(before)
end

should "convert regexen" do
before = /^c(a)t$/ix
after = [:regex, '^c(a)t$', [:caseless, :extended]]
after = [:bert, :regex, '^c(a)t$', [:caseless, :extended]]
assert_equal after, BERT::Encoder.convert(before)
end

Expand Down

0 comments on commit cc9d189

Please sign in to comment.