objectbox-generator icon indicating copy to clipboard operation
objectbox-generator copied to clipboard

objectbox-go generator can not handle a struct with multiple lazy loaded relations

Open sgbell opened this issue 3 years ago • 2 comments

Hi Guys, Just messing around with objectbox-go for use as data storage for my app, and I came across the idea of lazy loading related objects. I found that If I create a struct with more than 1 lazy loaded relation, the generator breaks badly, to the point that if I remove all but 1 of the lazy defines on a relation, it works fine.

As I'm new to golang, I don't think I'm in the right headspace yet to fix it myself, but that being said, I can point you in the direction I think needs the fix... internal/generator/go/templates/binding.go seems to be the first part to have issues, as with multiple lazy loaded relations, the templated code at line 165 misses a new line in the template, and causes errors in the code. But then it totally dies somewhere in the templated code after line 193.

Granted those line numbers are from looking at the current source of the repo.

Modules involved:

github.com/objectbox/objectbox-generator v0.13.0
github.com/objectbox/objectbox-go v1.6.1

Golang version: go version go1.17.9 linux/amd64

Below is my model.go file for the generator to have fun with.

package model

//go:generate go run github.com/objectbox/objectbox-go/cmd/objectbox-gogen
type Token struct {
	Id         uint64
	Token      string
	ExpiryDate uint64 `objectbox:"date"`
}

type Track struct {
	Id               uint64
	path             string `objectbox:"index:hash64"`
	artistName       string `objectbox:"index:hash64"`
	songName         string `objectbox:"index:hash64"`
	albumName        string `objectbox:"index:hash64"`
	albumTrackNumber int
}

type Playlist struct {
	Id             uint64
	CurrentTrackId uint16
	Elapsed        int
	Tracks         []*Track
}

type Friend struct {
	Id       uint64
	friendId uint64
}
type User struct {
	Id              uint64 // going to be an internal objectBoxId
	FirstName       string
	LastName        string
	EmailAddress    string `objectbox:"index:hash64"`
	Password        string
	Enabled         bool
	Tokens          []*Token    `objectbox:"lazy"`
	Tracks          []*Track    `objectbox:"lazy"`
	Playlists       []*Playlist `objectbox:"lazy"`
	SharedPlaylists []*Playlist `objectbox:"lazy"`
	Friends         []*Friend   `objectbox:"lazy"`
}

output of command: go generate ./...

Generating ObjectBox bindings for model.go
failed to format generated binding file model.obx.go: 1632:8: expected ';', found 'if' (and 3 more errors)
exit status 2
internal/model/model.go:3: running "go": exit status 1

sgbell avatar Jun 04 '22 14:06 sgbell

Team, this bug is still present. I'm also experiencing the issue. Why hasn't the fix been merged?

doffi88 avatar Mar 27 '25 22:03 doffi88

@doffi88 Thanks for reporting! The Relations documentation suggests that lazy loading is not the default. As a workaround, maybe try to remove the lazy annotation and use eagerly loaded relations.

greenrobot-team avatar Apr 01 '25 06:04 greenrobot-team