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

Add relation getters on base model structs #1439

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

parnic
Copy link
Contributor

@parnic parnic commented Feb 3, 2025

This prevents having to check the pointer before accessing a relation.

It was already safe to access a nil R's Get() method, but this removes some of the extra typing and nil checks as a convenience.

Previously:

var office *models.Office
if employee != nil {
    office = employee.R.GetOffice()
}

With this helper:

office := employee.GetOffice()

It is much more useful, however, when accessing nested relationships. Instead of

var c *models.RelC
if m != nil && m.R.GetA() != nil && m.R.GetA().R.GetB() != nil {
    c = m.R.GetA().R.GetB().R.GetC()
}

we can do

c := m.GetA().GetB().GetC()

This prevents having to check the pointer before accessing a relation.

It was already safe to access a nil R's Get() method, but this removes some of the extra typing and nil checks as a convenience.

Previously:
```go
var office *models.Office
if employee != nil {
    office = employee.R.GetOffice()
}
```

With this helper:
```go
office := employee.GetOffice()
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant