Selecting fields using key path with @Field models #402

Closed
opened 2025-12-29 15:30:59 +01:00 by adam · 2 comments
Owner

Originally created by @pinkplus on GitHub (Jan 31, 2023).

Let's say we have the following model, with fields defined using the @Field property wrapper:

class Person: CoreStoreObject {
    @Field.Stored("name")
    var name: String = ""
    
    @Field.Stored("age")
    var age: Int = 0
}

The following query using key path will fail to compile:

let ages = try dataStack.queryAttributes(
    From<Person>()
        .select(\.$age)
)

This is surprising to me. The readme contains an example to query using key path, so I thought it should work.

I noticed one difference between my code and the example code. In the example code, the model is probably defined using Value.Required (because it's doing .select(\.age) without $). On the other hand, my code uses the @Field property wrapper.

I looked into the implementation of From.select(). It seems to support ValueContainer, but not FieldContainer. I guess this is the reason -- is this intentional?

Originally created by @pinkplus on GitHub (Jan 31, 2023). Let's say we have the following model, with fields defined using the `@Field` property wrapper: ```swift class Person: CoreStoreObject { @Field.Stored("name") var name: String = "" @Field.Stored("age") var age: Int = 0 } ``` The following query using key path will fail to compile: ```swift let ages = try dataStack.queryAttributes( From<Person>() .select(\.$age) ) ``` This is surprising to me. The readme contains an example to query using key path, so I thought it should work. I noticed one difference between my code and the example code. In the example code, the model is probably defined using `Value.Required` (because it's doing `.select(\.age)` without `$`). On the other hand, my code uses the `@Field` property wrapper. I looked into the implementation of `From.select()`. It seems to support `ValueContainer`, but not `FieldContainer`. I guess this is the reason -- is this intentional?
adam closed this issue 2025-12-29 15:30:59 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Feb 1, 2023):

@pinkplus The demo app shows the syntax you need:

let ages: [[String: Any] = try dataStack.queryAttributes(
    From<Person>()
        .select(NSDictionary.self, .attribute(\.$age))
)

Admittedly, queryAttributes() currently only supporting NSDictionary as the type parameter is a bit clunky.

@JohnEstropia commented on GitHub (Feb 1, 2023): @pinkplus The [demo app](https://github.com/JohnEstropia/CoreStore/blob/1ed819b38d61a04845ad5512a0c09636dad4d7ff/Demo/Sources/Demos/Modern/TimeZonesDemo/Modern.TimeZonesDemo.MainView.swift#L105) shows the syntax you need: ``` let ages: [[String: Any] = try dataStack.queryAttributes( From<Person>() .select(NSDictionary.self, .attribute(\.$age)) ) ``` Admittedly, `queryAttributes()` currently only supporting `NSDictionary` as the type parameter is a bit clunky.
Author
Owner

@pinkplus commented on GitHub (Feb 4, 2023):

Hmm... Thanks.

@pinkplus commented on GitHub (Feb 4, 2023): Hmm... Thanks.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#402