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

Method Only

ent/entity_query.go:181–194  ·  view source on GitHub ↗

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

(ctx context.Context)

Source from the content-addressed store, hash-verified

179// Returns a *NotSingularError when more than one Entity entity is found.
180// Returns a *NotFoundError when no Entity entities are found.
181func (eq *EntityQuery) Only(ctx context.Context) (*Entity, error) {
182 nodes, err := eq.Limit(2).All(setContextOp(ctx, eq.ctx, "Only"))
183 if err != nil {
184 return nil, err
185 }
186 switch len(nodes) {
187 case 1:
188 return nodes[0], nil
189 case 0:
190 return nil, &NotFoundError{entity.Label}
191 default:
192 return nil, &NotSingularError{entity.Label}
193 }
194}
195
196// OnlyX is like Only, but panics if an error occurs.
197func (eq *EntityQuery) OnlyX(ctx context.Context) *Entity {

Callers 1

OnlyXMethod · 0.95

Calls 3

LimitMethod · 0.95
setContextOpFunction · 0.85
AllMethod · 0.45

Tested by

no test coverage detected