File: binary_vector.rb

package info (click to toggle)
ruby-bson 5.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,828 kB
  • sloc: ruby: 11,712; ansic: 1,427; java: 514; makefile: 8
file content (78 lines) | stat: -rw-r--r-- 1,834 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# frozen_string_literal: true

require 'runners/common_driver'

module BSON
  module BinaryVector
    class Spec < CommonDriver::Spec
      def initialize(file)
        super
        @valid = @invalid = nil
      end

      def tests
        @spec['tests'].collect do |test|
          BSON::BinaryVector::Test.new(self, test)
        end
      end

      def valid_tests
        tests.select(&:valid?)
      end

      def invalid_tests
        tests.reject(&:valid?)
      end
    end

    class Test
      attr_reader :canonical_bson, :description, :dtype, :padding, :vector

      def initialize(spec, test)
        @spec = spec
        @description = test['description']
        @valid = test['valid']
        @vector = ExtJSON.parse_obj(test['vector'])
        @dtype_hex = test['dtype_hex']
        @dtype_alias = test['dtype_alias']
        @dtype = @dtype_alias.downcase.to_sym
        @padding = test['padding']
        @canonical_bson = test['canonical_bson']
      end

      def valid?
        @valid
      end

      def document_from_canonical_bson
        bson_bytes = decode_hex(@canonical_bson)
        buffer = BSON::ByteBuffer.new(bson_bytes)
        BSON::Document.from_bson(buffer)
      end

      def canonical_bson_from_document(use_vector_type: false, validate_vector_data: false)
        args = if use_vector_type
                 [ BSON::Vector.new(@vector, @dtype, @padding) ]
               else
                 [ @vector, @dtype, @padding ]
               end
        {
          @spec.test_key => BSON::Binary.from_vector(
            *args,
            validate_vector_data: validate_vector_data
          ),
        }.to_bson.to_s
      end

      def bson
        decode_hex(@canonical_bson)
      end

      private

      def decode_hex(obj)
        [ obj ].pack('H*')
      end
    end
  end
end