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

Method Only

ent/task_query.go:132–145  ·  view source on GitHub ↗

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

(ctx context.Context)

Source from the content-addressed store, hash-verified

130// Returns a *NotSingularError when more than one Task entity is found.
131// Returns a *NotFoundError when no Task entities are found.
132func (tq *TaskQuery) Only(ctx context.Context) (*Task, error) {
133 nodes, err := tq.Limit(2).All(setContextOp(ctx, tq.ctx, "Only"))
134 if err != nil {
135 return nil, err
136 }
137 switch len(nodes) {
138 case 1:
139 return nodes[0], nil
140 case 0:
141 return nil, &NotFoundError{task.Label}
142 default:
143 return nil, &NotSingularError{task.Label}
144 }
145}
146
147// OnlyX is like Only, but panics if an error occurs.
148func (tq *TaskQuery) OnlyX(ctx context.Context) *Task {

Callers 1

OnlyXMethod · 0.95

Calls 3

LimitMethod · 0.95
setContextOpFunction · 0.85
AllMethod · 0.45

Tested by

no test coverage detected