Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC AnyRealmValue allow append Any and List can be initialised with a Swift Array. #8153

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
81 changes: 80 additions & 1 deletion RealmSwift/AnyRealmValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,24 @@
import Foundation
import Realm

/// Protocol representing the types that support `AnyRealmValue`
public protocol AnyRealm {}

extension NSNull: AnyRealm {}
extension Int: AnyRealm {}
extension Bool: AnyRealm {}
extension Float: AnyRealm {}
extension Double: AnyRealm {}
extension String: AnyRealm {}
extension Data: AnyRealm {}
extension Date: AnyRealm {}
extension ObjectId: AnyRealm {}
extension Decimal128: AnyRealm {}
extension UUID: AnyRealm {}
extension ObjectBase: AnyRealm {}

/// A enum for storing and retrieving values associated with an `AnyRealmValue` property.
public enum AnyRealmValue: Hashable {
public enum AnyRealmValue: AnyRealm, Hashable {
/// Represents `nil`
case none
/// An integer type.
Expand All @@ -47,6 +63,36 @@ public enum AnyRealmValue: Hashable {
/// A UUID type.
case uuid(UUID)

/// :nodoc:
static func convert<T>(_ value: T) -> AnyRealmValue {
switch value {
case let val as Int:
return .int(val)
case let val as Bool:
return .bool(val)
case let val as Float:
return .float(val)
case let val as Double:
return .double(val)
case let val as String:
return .string(val)
case let val as Data:
return .data(val)
case let val as Date:
return .date(val)
case let val as Object:
return .object(val)
case let val as ObjectId:
return .objectId(val)
case let val as Decimal128:
return .decimal128(val)
case let val as UUID:
return .uuid(val)
default:
return .none
}
}

/// Returns an `Int` if that is what the stored value is, otherwise `nil`.
public var intValue: Int? {
guard case let .int(i) = self else {
Expand Down Expand Up @@ -157,3 +203,36 @@ public enum AnyRealmValue: Hashable {
self = .none
}
}

extension AnyRealmValue: ExpressibleByIntegerLiteral {
public init(integerLiteral value: Int) {
self = AnyRealmValue.convert(value)
}
}

extension AnyRealmValue: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
self = AnyRealmValue.convert(value)
}
}

extension AnyRealmValue: ExpressibleByNilLiteral {
public init(nilLiteral: ()) {
self = .none
}
}

extension AnyRealmValue: ExpressibleByFloatLiteral {
public init(floatLiteral value: Float) {
self = AnyRealmValue.convert(value)
}
}

extension AnyRealmValue: ExpressibleByBooleanLiteral {
public typealias BooleanLiteralType = Bool
public init(booleanLiteral value: BooleanLiteralType) {
self = AnyRealmValue.convert(value)
}
}


20 changes: 20 additions & 0 deletions RealmSwift/List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import Realm.Private

Properties of `List` type defined on `Object` subclasses must be declared as `let` and cannot be `dynamic`.
*/

public final class List<Element: RealmCollectionValue>: RLMSwiftCollectionBase, RealmCollectionImpl {
internal var lastAccessedNames: NSMutableArray?

Expand Down Expand Up @@ -114,6 +115,10 @@ public final class List<Element: RealmCollectionValue>: RLMSwiftCollectionBase,
rlmArray.add(staticBridgeCast(fromSwift: object) as AnyObject)
}

public func append<Element: AnyRealm>(_ object: Element) {
rlmArray.add(staticBridgeCast(fromSwift: AnyRealmValue.convert(object)) as AnyObject)
}

/**
Appends the objects in the given sequence to the end of the list.

Expand All @@ -125,6 +130,14 @@ public final class List<Element: RealmCollectionValue>: RLMSwiftCollectionBase,
}
}

public func append(objectsIn objects: [Any]) where Element == AnyRealmValue {
for obj in objects {
if obj is AnyRealm {
rlmArray.add(staticBridgeCast(fromSwift: AnyRealmValue.convert(obj)) as AnyObject)
}
}
}

/**
Inserts an object at the given index.

Expand Down Expand Up @@ -241,6 +254,13 @@ public final class List<Element: RealmCollectionValue>: RLMSwiftCollectionBase,
}
}

extension List: ExpressibleByArrayLiteral {
public convenience init(arrayLiteral elements: Element...) {
self.init()
elements.forEach { append($0) }
}
}

extension List {
/**
Replace the given `subRange` of elements with `newElements`.
Expand Down