MCPcopy Index your code
hub / github.com/coder/coder / ExpectOne

Function ExpectOne

coderd/database/modelqueries.go:28–43  ·  view source on GitHub ↗

ExpectOne can be used to convert a ':many:' query into a ':one' query. To reduce the quantity of SQL queries, a :many with a filter is used. These filters sometimes are expected to return just 1 row. A :many query will never return a sql.ErrNoRows, but a :one does. This function will correct the er

(ret []T, err error)

Source from the content-addressed store, hash-verified

26// A :many query will never return a sql.ErrNoRows, but a :one does.
27// This function will correct the error for the empty set.
28func ExpectOne[T any](ret []T, err error) (T, error) {
29 var empty T
30 if err != nil {
31 return empty, err
32 }
33
34 if len(ret) == 0 {
35 return empty, sql.ErrNoRows
36 }
37
38 if len(ret) > 1 {
39 return empty, xerrors.Errorf("too many rows returned, expected 1")
40 }
41
42 return ret[0], nil
43}
44
45// customQuerier encompasses all non-generated queries.
46// It provides a flexible way to write queries for cases

Callers 9

TestReconcileSystemRoleFunction · 0.92
TestReconcileSystemRolesFunction · 0.92
TestExpectOneFunction · 0.92
UpdateCustomRoleMethod · 0.92
UpdateMemberRolesMethod · 0.92
TestGeneratorFunction · 0.92
WorkspaceOwnerFunction · 0.92
patchGroupMethod · 0.92

Calls 1

ErrorfMethod · 0.45

Tested by 4

TestReconcileSystemRoleFunction · 0.74
TestReconcileSystemRolesFunction · 0.74
TestExpectOneFunction · 0.74
TestGeneratorFunction · 0.74