Repository: realm/realm-swift
File: RealmSwift/Impl/SchemaDiscovery.swift (Swift)

Code before:
extension RLMProperty {
    internal convenience init(name: String, value: _RealmSchemaDiscoverable) {
        let valueType = Swift.type(of: value)
        self.init()
        self.name = name
        self.type = valueType._rlmType
        self.optional = valueType._rlmOptional
        value._rlmPopulateProperty(self)
        valueType._rlmPopulateProperty(self)
        if valueType._rlmRequireObjc {
            self.updateAccessors()
        }
    }

    /// Exposed for Macros.
    /// Important: Keep args in same order & default value as `@Persisted` property wrapper
    public convenience init<O: ObjectBase, V: _Persistable>(
        name: String,
        objectType _: O.Type,
        valueType _: V.Type,
        indexed: Bool = false,
        primaryKey: Bool = false,
        originProperty: String? = nil
    ) {
        self.init()
        self.name = name
        self.type = V._rlmType
        self.optional = V._rlmOptional
        self.indexed = primaryKey || indexed
        self.isPrimary = primaryKey
        self.linkOriginPropertyName = originProperty
        V._rlmPopulateProperty(self)
        V._rlmSetAccessor(self)
        self.swiftIvar = ivar_getOffset(class_getInstanceVariable(O.self, "_" + name)!)
    }
}

private func getModernProperties(_ object: ObjectBase) -> [RLMProperty] {
    let columnNames: [String: String] = type(of: object).propertiesMapping()
    return Mirror(reflecting: object).children.compactMap { prop in
        guard let label = prop.label else { return nil }
        guard let value = prop.value as? DiscoverablePersistedProperty else {
            return nil
        }
        let property = RLMProperty(name: label, value: value)
        property.swiftIvar = ivar_getOffset(class_getInstanceVariable(type(of: object), label)!)
        property.columnName = columnNames[property.name]
        return property
    }
}


Review comment (suggestion):
I have not actually verified that this works, but if it does this should be `@_spi(RealmSwiftPrivate)`  so that code using it has to do `@_spi(RealmSwiftPrivate) import RealmSwift` to reflect that this isn't a stable part of the API.

Code after:
internal protocol SchemaDiscoverable: _RealmSchemaDiscoverable {}
extension SchemaDiscoverable {
    public static var _rlmOptional: Bool { false }
    public static var _rlmRequireObjc: Bool { true }
    public func _rlmPopulateProperty(_ prop: RLMProperty) { }
    public static func _rlmPopulateProperty(_ prop: RLMProperty) { }
}

extension RLMProperty {
    internal convenience init(name: String, value: _RealmSchemaDiscoverable) {
        let valueType = Swift.type(of: value)
        self.init()
        self.name = name
        self.type = valueType._rlmType
        self.optional = valueType._rlmOptional
        value._rlmPopulateProperty(self)
        valueType._rlmPopulateProperty(self)
        if valueType._rlmRequireObjc {
            self.updateAccessors()
        }
    }

    /// Exposed for Macros.
    /// Important: Keep args in same order & default value as `@Persisted` property wrapper
    @_spi(RealmSwiftPrivate)
    public convenience init<O: ObjectBase, V: _Persistable>(
        name: String,
        objectType _: O.Type,
        valueType _: V.Type,
        indexed: Bool = false,
        primaryKey: Bool = false,
        originProperty: String? = nil
    ) {
        self.init()
        self.name = name
        self.type = V._rlmType
        self.optional = V._rlmOptional
        self.indexed = primaryKey || indexed
        self.isPrimary = primaryKey
        self.linkOriginPropertyName = originProperty
        V._rlmPopulateProperty(self)
        V._rlmSetAccessor(self)
        self.swiftIvar = ivar_getOffset(class_getInstanceVariable(O.self, "_" + name)!)
    }
}

private func getModernProperties(_ object: ObjectBase) -> [RLMProperty] {
    let columnNames: [String: String] = type(of: object).propertiesMapping()
    return Mirror(reflecting: object).children.compactMap { prop in
        guard let label = prop.label else { return nil }
        guard let value = prop.value as? DiscoverablePersistedProperty else {