Skip to content

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

@eurycea

Description

@eurycea

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions