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

Method Only

ent/share_query.go:157–170  ·  view source on GitHub ↗

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

(ctx context.Context)

Source from the content-addressed store, hash-verified

155// Returns a *NotSingularError when more than one Share entity is found.
156// Returns a *NotFoundError when no Share entities are found.
157func (sq *ShareQuery) Only(ctx context.Context) (*Share, error) {
158 nodes, err := sq.Limit(2).All(setContextOp(ctx, sq.ctx, "Only"))
159 if err != nil {
160 return nil, err
161 }
162 switch len(nodes) {
163 case 1:
164 return nodes[0], nil
165 case 0:
166 return nil, &NotFoundError{share.Label}
167 default:
168 return nil, &NotSingularError{share.Label}
169 }
170}
171
172// OnlyX is like Only, but panics if an error occurs.
173func (sq *ShareQuery) OnlyX(ctx context.Context) *Share {

Callers 1

OnlyXMethod · 0.95

Calls 3

LimitMethod · 0.95
setContextOpFunction · 0.85
AllMethod · 0.45

Tested by

no test coverage detected