MCPcopy Create free account
hub / github.com/crowdsecurity/crowdsec / Only

Method Only

pkg/database/ent/event_query.go:133–146  ·  view source on GitHub ↗

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

(ctx context.Context)

Source from the content-addressed store, hash-verified

131// Returns a *NotSingularError when more than one Event entity is found.
132// Returns a *NotFoundError when no Event entities are found.
133func (_q *EventQuery) Only(ctx context.Context) (*Event, error) {
134 nodes, err := _q.Limit(2).All(setContextOp(ctx, _q.ctx, ent.OpQueryOnly))
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{event.Label}
143 default:
144 return nil, &NotSingularError{event.Label}
145 }
146}
147
148// OnlyX is like Only, but panics if an error occurs.
149func (_q *EventQuery) OnlyX(ctx context.Context) *Event {

Callers 1

OnlyXMethod · 0.95

Calls 3

LimitMethod · 0.95
setContextOpFunction · 0.85
AllMethod · 0.45

Tested by

no test coverage detected