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)
| 155 | // Returns a *NotSingularError when more than one Share entity is found. |
| 156 | // Returns a *NotFoundError when no Share entities are found. |
| 157 | func (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. |
| 173 | func (sq *ShareQuery) OnlyX(ctx context.Context) *Share { |
no test coverage detected