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

Support arrays in Meow $set and $unset queries #322

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,25 @@ jobs:
strategy:
fail-fast: false
matrix:
swift-version: ['5.7', '5.7.1', '5.7.2']
mongodb-version: ['4.2', '4.4', '5.0', '6.0']
swift-version: ["5.7", "5.8"]
mongodb-version: ["4.2", "4.4", "5.0", "6.0", "7.0"]
steps:
- name: Check out
uses: actions/checkout@v3

- name: Install Swift
uses: swift-actions/setup-swift@v1
with:
swift-version: ${{ matrix.swift-version }}

- name: Start MongoDB
uses: supercharge/[email protected]
with:
mongodb-version: ${{ matrix.mongodb-version }}
mongodb-replica-set: mk-rs

- name: Run tests
run: swift test

- name: Check out
uses: actions/checkout@v3

- name: Install Swift
uses: swift-actions/setup-swift@v1
with:
swift-version: ${{ matrix.swift-version }}

- name: Start MongoDB
uses: supercharge/[email protected]
with:
mongodb-version: ${{ matrix.mongodb-version }}
mongodb-replica-set: mk-rs

- name: Run tests
run: swift test
# macos:
# runs-on: macos-12
# steps:
Expand Down
35 changes: 34 additions & 1 deletion Sources/Meow/KeyPathModel/KeyPathModels.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ extension QuerySubject where T: Sequence {
}
}

public protocol KeyPathQueryable: Codable {}
public protocol KeyPathQueryable: Codable {
static func resolveFieldPath<T: Codable>(_ field: KeyPath<Self, QueryableField<T>>) -> [String]
static func resolveFieldPath<T: Codable>(_ field: KeyPath<Self, Field<T>>) -> [String]
}

public protocol KeyPathQueryableModel: ReadableModel, KeyPathQueryable {}

extension CodingUserInfoKey {
Expand Down Expand Up @@ -272,6 +276,17 @@ public struct QueryableField<Value> {

internal var isInvalid: Bool { key == nil }

public subscript<T: Codable>(dynamicMember keyPath: KeyPath<Value, QueryableField<T>>) -> QueryableField<T> where Value: KeyPathQueryable {
var subKeys = Value.resolveFieldPath(keyPath)
let lastKey = subKeys.isEmpty ? nil : subKeys.removeLast()

return QueryableField<T>(
parents: parents + subKeys,
key: lastKey,
value: nil
)
}

public subscript<T>(dynamicMember keyPath: KeyPath<Value, QueryableField<T>>) -> QueryableField<T> {
if let key = key, let value = value {
let subField = value[keyPath: keyPath]
Expand All @@ -291,6 +306,24 @@ public struct QueryableField<Value> {
}
}

extension QueryableField where Value: Sequence {
public subscript<T: Codable>(dynamicMember keyPath: KeyPath<Value.Element, QueryableField<T>>) -> QueryableField<T> where Value.Element: KeyPathQueryable {
var subKeys = Value.Element.resolveFieldPath(keyPath)
let lastKey = subKeys.isEmpty ? nil : subKeys.removeLast()
var parents = parents
if let key = self.key {
parents.append(key)
parents.append(contentsOf: subKeys)
}
parents.append("$")
return QueryableField<T>(
parents: parents,
key: lastKey,
value: nil
)
}
}

extension QueryableField: _QueryableFieldRepresentable where Value: Codable {
public var wrapper: _QueryableFieldWrapper<Value> {
.init(wrapped: .queryableField(self))
Expand Down
18 changes: 18 additions & 0 deletions Sources/Meow/KeyPathModel/MeowCollection+KeyPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,24 @@ public struct ModelUpdateQuery<M: KeyPathQueryableModel & MutableModel> {
unsetField(at: keyPath)
}
}

/// Adds an atomic `$set` to the update query that updates the field corresponding to `keyPath` to the `newValue`
public mutating func setField<PE: PrimitiveEncodable & Codable>(at keyPath: KeyPath<M, QueryableField<PE>>, to newValue: PE) throws {
let path = M.resolveFieldPath(keyPath).joined(separator: ".")
let newValue = try newValue.encodePrimitive()
set[path] = newValue
}

/// Sets the `Result`'s `keyPath` to a constant `newValue`
public mutating func setField<PE: PrimitiveEncodable & Codable>(at keyPath: KeyPath<M, QueryableField<PE?>>, to newValue: PE?) throws {
let path = M.resolveFieldPath(keyPath).joined(separator: ".")
if let newValue = newValue {
let newValue = try newValue.encodePrimitive()
set[path] = newValue
} else {
unset[path] = ""
}
}

/// Adds an atomic `$unset` to the update query that updates the field corresponding to `keyPath` to be removed
public mutating func unsetField<Value: Codable>(at keyPath: WritableKeyPath<M, QueryableField<Value?>>) {
Expand Down
27 changes: 23 additions & 4 deletions Sources/Meow/KeyPathModel/TestDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,33 @@ struct KeyedTestDecodingContainer<Key: CodingKey>: KeyedDecodingContainerProtoco
}
}

struct ArrayElementCodingKey: CodingKey {
var stringValue: String { "$" }
var intValue: Int? { nil }

init() {}
init?(intValue: Int) { nil }
init?(stringValue: String) { nil }
}

struct UnkeyedTestDecodingContainer: UnkeyedDecodingContainer {
let decoder: TestDecoder
var codingPath: [CodingKey] { decoder.codingPath }
var count: Int? { nil }
var currentIndex: Int { 0 }
var isAtEnd: Bool { true }

func decode<T>(_ type: T.Type) throws -> T {
throw CannotActuallyDecode()
func decode<T: Decodable>(_ type: T.Type) throws -> T {
var decoder = self.decoder
decoder.codingPath.append(ArrayElementCodingKey())

return try T(from: decoder)
}

func nestedUnkeyedContainer() throws -> UnkeyedDecodingContainer {
throw CannotActuallyDecode()
var decoder = self.decoder
decoder.codingPath.append(ArrayElementCodingKey())
return UnkeyedTestDecodingContainer(decoder: decoder)
}

func decodeNil() throws -> Bool {
Expand All @@ -90,7 +104,12 @@ struct UnkeyedTestDecodingContainer: UnkeyedDecodingContainer {
}

func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type) throws -> KeyedDecodingContainer<NestedKey> where NestedKey : CodingKey {
throw CannotActuallyDecode()
var decoder = self.decoder
decoder.codingPath.append(ArrayElementCodingKey())

return KeyedDecodingContainer(
KeyedTestDecodingContainer<NestedKey>(decoder: decoder)
)
}
}

Expand Down