Details
-
Improvement
-
Status: Closed
-
Minor - P4
-
Resolution: Fixed
-
None
-
None
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.