Uploaded image for project: 'Swift Driver'
  1. Swift Driver
  2. SWIFT-255

AnyBSONValue should conform to Hashable

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor - P4
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 0.0.8
    • Component/s: None
    • Labels:

      Description

      BSONValue does not currently conform to Hashable, but it should, for the same reason it should conform to Equatable, since all BSONValue's are themselves Hashable.

      Jason Flax provided a sketch for what the implementation of this may look like:

      private func _hash(into hasher: inout Hasher, bsonValue: BSONValue?) {
          switch (bsonValue) {
          case (let value as Int):
              hasher.combine(value)
          case (let value as Int32):
              hasher.combine(value)
          case (let value as Int64):
              hasher.combine(value)
          case (let value as Double):
              hasher.combine(value)
          case (let value as Decimal128):
              hasher.combine(value.data)
          case (let value as Bool):
              hasher.combine(value)
          case (let value as String):
              hasher.combine(value)
          case (let value as RegularExpression):
              hasher.combine(value.options)
              hasher.combine(value.pattern)
          case (let value as Timestamp):
              hasher.combine(value.timestamp)
          case (let value as Date):
              hasher.combine(value.timeIntervalSince1970)
          case (_ as MinKey):
              hasher.combine(1)
          case (_ as MaxKey):
              hasher.combine(1)
          case (let value as ObjectId):
              hasher.combine(value.description)
          case (let value as CodeWithScope):
              hasher.combine(value.code)
              _hash(into: &hasher, bsonValue: value.scope)
          case (let value as Binary):
              hasher.combine(value.data)
          case (let value as Document):
              hasher.combine(value.canonicalExtendedJSON)
          case (let value as [BSONValue?]): // TODO: SWIFT-242
              return value.forEach { _hash(into: &hasher, bsonValue: $0!) }
          default: break
          }
      }
       
      extension BSONValue {
          func hash(into hasher: inout Hasher) {
              _hash(into: &hasher, bsonValue: self)
          }
      }
      

      It is worth noting that unlike SWIFT-254, this does (should) not depend on SWIFT-215 to be implemented.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              patrick.freed Patrick Freed
              Reporter:
              may.hoque May Hoque
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: