orlandos-nl / MongoKitten

Native MongoDB driver for Swift, written in Swift
MIT License
717 stars 102 forks source link

Meow: Support more types in makePartialUpdate #342

Open sliemeobn opened 4 months ago

sliemeobn commented 4 months ago

Currently the ModelUpdater type is very limited, only allowing access to properties that are a Primitive.

This excludes a wide variety of property types, such as Optionals, RawRepresentables, References, or arrays of these types.

    public subscript<P: Primitive>(dynamicMember keyPath: WritableKeyPath<M, QueryableField<P>>) -> P {
        get {
            self.update.model[keyPath: keyPath].value!
        }
        set {
            self.update.model[keyPath: keyPath].value = newValue

            let path = M.resolveFieldPath(keyPath)
            self.update.changes[path] = newValue
        }
    }

A solution could be to constrain this access to PrimitiveEncodable & Codable instead of Primitive. However, the encodePrimitive() function is potentially throwing, which is not ideal in the set accessor.

Alternatively, we could add a lot of typed overloads similar to the ModelUpdateQuery.setField functions.