MCPcopy Index your code
hub / github.com/cloudreve/cloudreve / Only

Method Only

ent/node_query.go:133–146  ·  view source on GitHub ↗

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

(ctx context.Context)

Source from the content-addressed store, hash-verified

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

Callers 15

OnlyXMethod · 0.95
GetMethod · 0.45
GetMethod · 0.45
GetMethod · 0.45
GetMethod · 0.45
GetMethod · 0.45
GetMethod · 0.45
GetMethod · 0.45
GetMethod · 0.45
GetMethod · 0.45
GetMethod · 0.45
GetMethod · 0.45

Calls 3

LimitMethod · 0.95
setContextOpFunction · 0.85
AllMethod · 0.45

Tested by

no test coverage detected