Skip to content

Commit

Permalink
Added missing types, not ideal, but they're there. Now for tests...È
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegn committed Sep 14, 2015
1 parent daf09bb commit 9e53e27
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 19 deletions.
18 changes: 9 additions & 9 deletions src/bson.cr
Expand Up @@ -3,29 +3,29 @@ require "./bson/*"

module BSON

alias ValueType = Float64 | String | Array(ValueType) | Document | ObjectId | Bool | Time | Regex | Nil | Int32 | Int64 | Binary
alias ValueType = Float64 | String | Document | Array(ValueType) | Binary | Undefined | ObjectId | Bool | Time | Nil | Regex | DBPointer | Code | Symbol | CodeWithScope | Int32 | Timestamp | Int64 | MinKey | MaxKey

TYPES = Hash{
1 => Float64,
2 => String,
3 => Document,
4 => Array(ValueType),
5 => Binary,
6 => Nil, # Undefined (deprecated)
6 => Undefined, # deprecated
7 => ObjectId,
8 => Bool,
9 => Time,
10 => Nil,
0x0B => Regex,
# 0x0C => DBRef, # Deprecated
# 0x0D => Code,
# 0x0E => Symbol, # Deprecated
# 0x0F => CodeWithScope,
0x0C => DBPointer, # deprecated
0x0D => Code,
0x0E => Symbol, # deprecated
0x0F => CodeWithScope,
0x10 => Int32,
# 0x11 => Timestamp,
0x11 => Timestamp,
0x12 => Int64
# 0xFF => MinKey,
# 0x7F => MaxKey
0xFF => MinKey, # internal
0x7F => MaxKey # internal
}

TYPES_BY_CLASS = TYPES.invert
Expand Down
20 changes: 19 additions & 1 deletion src/bson/code.cr
@@ -1,4 +1,22 @@
module BSON
class Code# < String
class Code

property :javascript

def initialize(@javascript = "")
end

def self.from_bson(bson : IO)
new String.from_bson_bytes(bson.next_bytes(Int32.from_bson(bson)))
end

def to_bson(bson : IO)
javascript.to_bson(bson)
end

def bson_size
javascript.bson_size
end

end
end
27 changes: 27 additions & 0 deletions src/bson/code_with_scope.cr
@@ -0,0 +1,27 @@
module BSON
class CodeWithScope

getter :javascript, :scope

def initialize(@javascript = "", @scope = Document.new)
end

def self.from_bson(bson : IO)
bson.read(4) # Throw away the total length.
js = String.from_bson_bytes(bson.next_bytes(Int32.from_bson(bson)))
scope = Document.from_bson(bson)
new(js, scope)
end

def to_bson(bson : IO)
bson_size.to_bson(bson)
js.to_bson(bson)
scope.to_bson(bson)
end

def bson_size
4 + scope.bson_size + 4 + javascript.bson_size
end

end
end
22 changes: 22 additions & 0 deletions src/bson/db_pointer.cr
@@ -0,0 +1,22 @@
module BSON
class DBPointer

property :pointer

def initialize(@pointer = "")
end

def self.from_bson(bson : IO)
new String.from_bson_bytes(bson.next_bytes(Int32.from_bson(bson)))
end

def to_bson(bson : IO)
pointer.to_bson(bson)
end

def bson_size
pointer.bson_size
end

end
end
8 changes: 8 additions & 0 deletions src/bson/max_key.cr
@@ -0,0 +1,8 @@
require "./specialized"

module BSON
struct MaxKey
include Specialized
extend Specialized::ClassMethods
end
end
8 changes: 8 additions & 0 deletions src/bson/min_key.cr
@@ -0,0 +1,8 @@
require "./specialized"

module BSON
struct MinKey
include Specialized
extend Specialized::ClassMethods
end
end
19 changes: 19 additions & 0 deletions src/bson/specialized.cr
@@ -0,0 +1,19 @@
module BSON
module Specialized

def to_bson(bson : IO)
# Do nothing
end

def bson_size
0
end

module ClassMethods
def from_bson(bson : IO)
new
end
end

end
end
22 changes: 22 additions & 0 deletions src/bson/symbol.cr
@@ -0,0 +1,22 @@
module BSON
class Symbol

property :string

def initialize(@string = "")
end

def self.from_bson(bson : IO)
new String.from_bson_bytes(bson.next_bytes(Int32.from_bson(bson)))
end

def to_bson(bson : IO)
string.to_bson(bson)
end

def bson_size
string.bson_size
end

end
end
23 changes: 23 additions & 0 deletions src/bson/timestamp.cr
@@ -0,0 +1,23 @@
module BSON
class Timestamp

getter :time, :increment

def initialize(@increment : Int32, @time : Time)
end

def self.from_bson(bson : IO)
new(Int32.from_bson(bson), Time.epoch(Int32.from_bson(bson)))
end

def to_bson(bson : IO)
increment.to_bson(bson)
time.epoch.to_bson(bson) # Int32
end

def bson_size
sizeof(Int32) + sizeof(Int32)
end

end
end
17 changes: 17 additions & 0 deletions src/bson/undefined.cr
@@ -0,0 +1,17 @@
module BSON
class Undefined

def self.from_bson(bson : IO)
nil
end

def to_bson(bson : IO)
# Do nothing
end

def bson_size
0
end

end
end
9 changes: 0 additions & 9 deletions src/core_ext/symbol.cr

This file was deleted.

0 comments on commit 9e53e27

Please sign in to comment.