mirror of
https://github.com/ent/ent.git
synced 2026-05-28 09:49:08 +03:00
entc/gen: add <E>Loaded for <T>Edges (#319)
This helps to determine if the edge was loaded (or requested) in eager-loading even if it's empty.
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
|
||||
"github.com/facebookincubator/ent/dialect/sql"
|
||||
"github.com/facebookincubator/ent/examples/start/ent/car"
|
||||
"github.com/facebookincubator/ent/examples/start/ent/user"
|
||||
)
|
||||
|
||||
// Car is the model entity for the Car schema.
|
||||
@@ -34,6 +35,23 @@ type Car struct {
|
||||
type CarEdges struct {
|
||||
// Owner holds the value of the owner edge.
|
||||
Owner *User
|
||||
// loadedTypes holds the information for reporting if a
|
||||
// type was loaded (or requested) in eager-loading or not.
|
||||
loadedTypes [1]bool
|
||||
}
|
||||
|
||||
// OwnerWithError returns the Owner value or an error if the edge
|
||||
// was not loaded in eager-loading, or loaded but was not found.
|
||||
func (e CarEdges) OwnerWithError() (*User, error) {
|
||||
if e.loadedTypes[0] {
|
||||
if e.Owner == nil {
|
||||
// The edge owner was loaded in eager-loading,
|
||||
// but was not found.
|
||||
return nil, &NotFoundError{label: user.Label}
|
||||
}
|
||||
return e.Owner, nil
|
||||
}
|
||||
return nil, &NotLoadedError{edge: "owner"}
|
||||
}
|
||||
|
||||
// scanValues returns the types for scanning values from sql.Rows.
|
||||
|
||||
@@ -294,9 +294,12 @@ func (cq *CarQuery) Select(field string, fields ...string) *CarSelect {
|
||||
|
||||
func (cq *CarQuery) sqlAll(ctx context.Context) ([]*Car, error) {
|
||||
var (
|
||||
nodes []*Car = []*Car{}
|
||||
withFKs = cq.withFKs
|
||||
_spec = cq.querySpec()
|
||||
nodes = []*Car{}
|
||||
withFKs = cq.withFKs
|
||||
_spec = cq.querySpec()
|
||||
loadedTypes = [1]bool{
|
||||
cq.withOwner != nil,
|
||||
}
|
||||
)
|
||||
if cq.withOwner != nil {
|
||||
withFKs = true
|
||||
@@ -318,6 +321,7 @@ func (cq *CarQuery) sqlAll(ctx context.Context) ([]*Car, error) {
|
||||
return fmt.Errorf("ent: Assign called without calling ScanValues")
|
||||
}
|
||||
node := nodes[len(nodes)-1]
|
||||
node.Edges.loadedTypes = loadedTypes
|
||||
return node.assignValues(values...)
|
||||
}
|
||||
if err := sqlgraph.QueryNodes(ctx, cq.driver, _spec); err != nil {
|
||||
|
||||
@@ -129,12 +129,12 @@ func IsNotSingular(err error) bool {
|
||||
|
||||
// NotLoadedError returns when trying to get a node that was not loaded by the query.
|
||||
type NotLoadedError struct {
|
||||
label string
|
||||
edge string
|
||||
}
|
||||
|
||||
// Error implements the error interface.
|
||||
func (e *NotLoadedError) Error() string {
|
||||
return fmt.Sprintf("ent: %s not loaded", e.label)
|
||||
return fmt.Sprintf("ent: %s edge was not loaded", e.edge)
|
||||
}
|
||||
|
||||
// IsNotLoaded returns a boolean indicating whether the error is a not loaded error.
|
||||
|
||||
@@ -30,6 +30,18 @@ type Group struct {
|
||||
type GroupEdges struct {
|
||||
// Users holds the value of the users edge.
|
||||
Users []*User
|
||||
// loadedTypes holds the information for reporting if a
|
||||
// type was loaded (or requested) in eager-loading or not.
|
||||
loadedTypes [1]bool
|
||||
}
|
||||
|
||||
// UsersWithError returns the Users value or an error if the edge
|
||||
// was not loaded in eager-loading.
|
||||
func (e GroupEdges) UsersWithError() ([]*User, error) {
|
||||
if e.loadedTypes[0] {
|
||||
return e.Users, nil
|
||||
}
|
||||
return nil, &NotLoadedError{edge: "users"}
|
||||
}
|
||||
|
||||
// scanValues returns the types for scanning values from sql.Rows.
|
||||
|
||||
@@ -294,8 +294,11 @@ func (gq *GroupQuery) Select(field string, fields ...string) *GroupSelect {
|
||||
|
||||
func (gq *GroupQuery) sqlAll(ctx context.Context) ([]*Group, error) {
|
||||
var (
|
||||
nodes []*Group = []*Group{}
|
||||
_spec = gq.querySpec()
|
||||
nodes = []*Group{}
|
||||
_spec = gq.querySpec()
|
||||
loadedTypes = [1]bool{
|
||||
gq.withUsers != nil,
|
||||
}
|
||||
)
|
||||
_spec.ScanValues = func() []interface{} {
|
||||
node := &Group{config: gq.config}
|
||||
@@ -308,6 +311,7 @@ func (gq *GroupQuery) sqlAll(ctx context.Context) ([]*Group, error) {
|
||||
return fmt.Errorf("ent: Assign called without calling ScanValues")
|
||||
}
|
||||
node := nodes[len(nodes)-1]
|
||||
node.Edges.loadedTypes = loadedTypes
|
||||
return node.assignValues(values...)
|
||||
}
|
||||
if err := sqlgraph.QueryNodes(ctx, gq.driver, _spec); err != nil {
|
||||
|
||||
@@ -34,6 +34,27 @@ type UserEdges struct {
|
||||
Cars []*Car
|
||||
// Groups holds the value of the groups edge.
|
||||
Groups []*Group
|
||||
// loadedTypes holds the information for reporting if a
|
||||
// type was loaded (or requested) in eager-loading or not.
|
||||
loadedTypes [2]bool
|
||||
}
|
||||
|
||||
// CarsWithError returns the Cars value or an error if the edge
|
||||
// was not loaded in eager-loading.
|
||||
func (e UserEdges) CarsWithError() ([]*Car, error) {
|
||||
if e.loadedTypes[0] {
|
||||
return e.Cars, nil
|
||||
}
|
||||
return nil, &NotLoadedError{edge: "cars"}
|
||||
}
|
||||
|
||||
// GroupsWithError returns the Groups value or an error if the edge
|
||||
// was not loaded in eager-loading.
|
||||
func (e UserEdges) GroupsWithError() ([]*Group, error) {
|
||||
if e.loadedTypes[1] {
|
||||
return e.Groups, nil
|
||||
}
|
||||
return nil, &NotLoadedError{edge: "groups"}
|
||||
}
|
||||
|
||||
// scanValues returns the types for scanning values from sql.Rows.
|
||||
|
||||
@@ -319,8 +319,12 @@ func (uq *UserQuery) Select(field string, fields ...string) *UserSelect {
|
||||
|
||||
func (uq *UserQuery) sqlAll(ctx context.Context) ([]*User, error) {
|
||||
var (
|
||||
nodes []*User = []*User{}
|
||||
_spec = uq.querySpec()
|
||||
nodes = []*User{}
|
||||
_spec = uq.querySpec()
|
||||
loadedTypes = [2]bool{
|
||||
uq.withCars != nil,
|
||||
uq.withGroups != nil,
|
||||
}
|
||||
)
|
||||
_spec.ScanValues = func() []interface{} {
|
||||
node := &User{config: uq.config}
|
||||
@@ -333,6 +337,7 @@ func (uq *UserQuery) sqlAll(ctx context.Context) ([]*User, error) {
|
||||
return fmt.Errorf("ent: Assign called without calling ScanValues")
|
||||
}
|
||||
node := nodes[len(nodes)-1]
|
||||
node.Edges.loadedTypes = loadedTypes
|
||||
return node.assignValues(values...)
|
||||
}
|
||||
if err := sqlgraph.QueryNodes(ctx, uq.driver, _spec); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user