objectbox / objectbox/objectbox-swift

Generated code does not compile for immutable struct entities with Ids not named 'id'

Open
#105 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Swift
Stars
619
Forks
37
PR merge metrics
No merged PRs in 30d

Description

Is there an existing issue?
Build info
  • ObjectBox version: 4.4.0
  • OS: macOS 15.6.1
  • Device/chipset: Apple M1
Steps to reproduce
  1. create a struct with an Id field not named id that is annotated as the entity id
  2. run generator
Expected behavior

The generated code compiles and matches mutable class id annotation behavior.

Actual behavior

The generated put(struct(s) code always uses 'id' as the property name in the entity constructors.

Code
Code

Entity:

struct MyImmutableEntity: Entity {
    // objectbox: id
    let myId: Id
}

Generated:

extension ObjectBox.Box where E == MyImmutableEntity {

    func put(struct entity: MyImmutableEntity) throws -> MyImmutableEntity {
        let entityId: MyImmutableEntity.EntityBindingType.IdType = try self.put(entity)

        return MyImmutableEntity(
            id: entityId // Compile error, should be myId: entityId
        )
    }

    func put(structs entities: [MyImmutableEntity]) throws -> [MyImmutableEntity] {
        let entityIds: [MyImmutableEntity.EntityBindingType.IdType] = try self.putAndReturnIDs(entities)
        var newEntities = [MyImmutableEntity]()
        newEntities.reserveCapacity(entities.count)

        for i in 0 ..< min(entities.count, entityIds.count) {
            let entity = entities[i]
            let entityId = entityIds[i]

            newEntities.append(MyImmutableEntity(
                id: entityId // Compile error, should be myId: entityId
            ))
        }

        return newEntities
    }
}

I'm not at all familiar with Sourcery but from looking around it seems that the issue is here and here.

I assume the constructor template should be something like:

{{ prop.swiftName }}: entityId

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the EntityInfo.stencil locations linked in the issue, around lines 292 and 320, and inspect how the entity ID property is selected for immutable structs. Reproduce the example with an ID named myId, then update the generator so the generated constructors use the annotated Swift property name and confirm the generated code compiles.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
databases
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.