File: symbol.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 (212 lines) | stat: -rw-r--r-- 6,028 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# frozen_string_literal: true
# rubocop:todo all
# Copyright (C) 2009-2020 MongoDB Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module BSON

  # Injects behaviour for encoding and decoding symbol values to and from
  # raw bytes as specified by the BSON spec.
  #
  # @note Symbols are deprecated in the BSON spec, but they are still
  #   currently supported here for backwards compatibility.
  #
  # @see http://bsonspec.org/#/specification
  #
  # @since 2.0.0
  module Symbol

    # A symbol is type 0x0E in the BSON spec.
    #
    # @since 2.0.0
    BSON_TYPE = ::String.new(14.chr, encoding: BINARY).freeze

    # Symbols are serialized as strings as symbols are now removed from the
    # BSON specification. Therefore the bson_type when serializing must be a
    # string.
    #
    # @example Get the BSON type for the symbol.
    #   :test.bson_type
    #
    # @return [ String ] The single byte BSON type.
    #
    # @see http://bsonspec.org/#/specification
    #
    # @since 4.0.0
    def bson_type
      String::BSON_TYPE
    end

    # Get the symbol as encoded BSON.
    #
    # @example Get the symbol as encoded BSON.
    #   :test.to_bson
    #
    # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
    #
    # @see http://bsonspec.org/#/specification
    #
    # @since 2.0.0
    def to_bson(buffer = ByteBuffer.new)
      buffer.put_symbol(self)
    end

    # Get the symbol as a BSON key name encoded C symbol.
    #
    # @example Get the symbol as a key name.
    #   :test.to_bson_key
    #
    # @return [ String ] The encoded symbol as a BSON key.
    #
    # @see http://bsonspec.org/#/specification
    #
    # @since 2.0.0
    def to_bson_key
      self
    end

    # Converts the symbol to a normalized key in a BSON document.
    #
    # @example Convert the symbol to a normalized key.
    #   :test.to_bson_normalized_key
    #
    # @return [ String ] The symbol as a non interned string.
    #
    # @since 3.0.0
    def to_bson_normalized_key
      to_s
    end

    # Converts this object to a representation directly serializable to
    # Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json/extended-json.md).
    #
    # @return [ Hash ] The extended json representation.
    def as_extended_json(**_options)
      { "$symbol" => to_s }
    end

    class Raw
      include JSON

      # Create a BSON Symbol
      #
      # @param [ String | Symbol ] str_or_sym The symbol represented by this
      #   object. Can be specified as a Symbol or a String.
      #
      # @see http://bsonspec.org/#/specification
      def initialize(str_or_sym)
        unless str_or_sym.is_a?(String) || str_or_sym.is_a?(Symbol)
          raise ArgumentError, "BSON::Symbol::Raw must be given a symbol or a string, not #{str_or_sym}"
        end

        @symbol = str_or_sym.to_sym
      end

      # Get the underlying symbol as a Ruby symbol.
      #
      # @return [ Symbol ] The symbol represented by this BSON object.
      def to_sym
        @symbol
      end

      # Get the underlying symbol as a Ruby string.
      #
      # @return [ String ] The symbol as a string.
      def to_s
        @symbol.to_s
      end

      # Check equality of the raw bson symbol against another.
      #
      # @param [ Object ] other The object to check against.
      #
      # @return [ true, false ] If the objects are equal.
      def ==(other)
        return false unless other.is_a?(Raw)
        to_sym == other.to_sym
      end
      alias :eql? :==

      # Get the symbol as encoded BSON.
      #
      # @raise [ EncodingError ] If the symbol is not UTF-8.
      #
      # @return [ BSON::ByteBuffer ] The buffer with the encoded object.
      #
      # @see http://bsonspec.org/#/specification
      def to_bson(buffer = ByteBuffer.new)
        buffer.put_string(to_s)
      end

      def bson_type
        Symbol::BSON_TYPE
      end

      # Return a string representation of the raw symbol for use in
      # application-level JSON serialization. This method is intentionally
      # different from #as_extended_json.
      #
      # @example Get the raw symbol as a JSON-serializable object.
      #   raw_symbol.as_json
      #
      # @return [ String ] The raw symbol as a String.
      def as_json(*args)
        to_s
      end

      # Converts this object to a representation directly serializable to
      # Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json/extended-json.md).
      #
      # @return [ Hash ] The extended json representation.
      def as_extended_json(**_options)
        { '$symbol' => to_s }
      end
    end

    module ClassMethods

      # Deserialize a symbol from BSON.
      #
      # @param [ ByteBuffer ] buffer The byte buffer.
      #
      # @option options [ nil | :bson ] :mode Decoding mode to use.
      #
      # @return [ Symbol | BSON::Symbol::Raw ] The decoded symbol.
      #
      # @see http://bsonspec.org/#/specification
      #
      # @since 2.0.0
      def from_bson(buffer, **options)
        sym = buffer.get_string.intern

        if options[:mode] == :bson
          Raw.new(sym)
        else
          sym
        end
      end
    end

    # Register this type when the module is loaded.
    #
    # @since 2.0.0
    Registry::MAPPINGS[BSON_TYPE.ord] = ::Symbol
  end

  # Enrich the core Symbol class with this module.
  #
  # @since 2.0.0
  ::Symbol.send(:include, Symbol)
  ::Symbol.send(:extend, Symbol::ClassMethods)
end