MCPcopy Create free account
hub / github.com/cloudreve/cloudreve / Only

Method Only

ent/user_query.go:325–338  ·  view source on GitHub ↗

Only returns a single User entity found by the query, ensuring it only returns one. Returns a *NotSingularError when more than one User entity is found. Returns a *NotFoundError when no User entities are found.

(ctx context.Context)

Source from the content-addressed store, hash-verified

323// Returns a *NotSingularError when more than one User entity is found.
324// Returns a *NotFoundError when no User entities are found.
325func (uq *UserQuery) Only(ctx context.Context) (*User, error) {
326 nodes, err := uq.Limit(2).All(setContextOp(ctx, uq.ctx, "Only"))
327 if err != nil {
328 return nil, err
329 }
330 switch len(nodes) {
331 case 1:
332 return nodes[0], nil
333 case 0:
334 return nil, &NotFoundError{user.Label}
335 default:
336 return nil, &NotSingularError{user.Label}
337 }
338}
339
340// OnlyX is like Only, but panics if an error occurs.
341func (uq *UserQuery) OnlyX(ctx context.Context) *User {

Callers 1

OnlyXMethod · 0.95

Calls 3

LimitMethod · 0.95
setContextOpFunction · 0.85
AllMethod · 0.45

Tested by

no test coverage detected