Skip to content

Commit 57cde94

Browse files
committed
Improve readability of generics and arguments
1 parent 802272b commit 57cde94

File tree

1 file changed

+59
-29
lines changed

1 file changed

+59
-29
lines changed

coderd/authzquery/authz.go

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,15 @@ func LogNotAuthorizedError(ctx context.Context, logger slog.Logger, err error) e
5151
}
5252

5353
// insert is the same as insertWithReturn, but does not return the inserted object.
54-
func insert[ArgumentType any,
55-
Insert func(ctx context.Context, arg ArgumentType) error](
56-
// Arguments
54+
func insert[
55+
ArgumentType any,
56+
Insert func(ctx context.Context, arg ArgumentType) error,
57+
](
5758
logger slog.Logger,
5859
authorizer rbac.Authorizer,
5960
object rbac.Objecter,
60-
insertFunc Insert) Insert {
61+
insertFunc Insert,
62+
) Insert {
6163
return func(ctx context.Context, arg ArgumentType) error {
6264
_, err := insertWithReturn(logger, authorizer, object, func(ctx context.Context, arg ArgumentType) (rbac.Objecter, error) {
6365
return rbac.Object{}, insertFunc(ctx, arg)
@@ -69,13 +71,16 @@ func insert[ArgumentType any,
6971
// insertWithReturn runs an rbac.ActionCreate on the rbac object argument before
7072
// running the insertFunc. The insertFunc is expected to return the object that
7173
// was inserted.
72-
func insertWithReturn[ObjectType any, ArgumentType any,
73-
Insert func(ctx context.Context, arg ArgumentType) (ObjectType, error)](
74-
// Arguments
74+
func insertWithReturn[
75+
ObjectType any,
76+
ArgumentType any,
77+
Insert func(ctx context.Context, arg ArgumentType) (ObjectType, error),
78+
](
7579
logger slog.Logger,
7680
authorizer rbac.Authorizer,
7781
object rbac.Objecter,
78-
insertFunc Insert) Insert {
82+
insertFunc Insert,
83+
) Insert {
7984
return func(ctx context.Context, arg ArgumentType) (empty ObjectType, err error) {
8085
// Fetch the rbac subject
8186
act, ok := ActorFromContext(ctx)
@@ -94,39 +99,49 @@ func insertWithReturn[ObjectType any, ArgumentType any,
9499
}
95100
}
96101

97-
func deleteQ[ObjectType rbac.Objecter, ArgumentType any,
102+
func deleteQ[
103+
ObjectType rbac.Objecter,
104+
ArgumentType any,
98105
Fetch func(ctx context.Context, arg ArgumentType) (ObjectType, error),
99-
Delete func(ctx context.Context, arg ArgumentType) error](
106+
Delete func(ctx context.Context, arg ArgumentType) error,
107+
](
100108
// Arguments
101109
logger slog.Logger,
102110
authorizer rbac.Authorizer,
103111
fetchFunc Fetch,
104-
deleteFunc Delete) Delete {
112+
deleteFunc Delete,
113+
) Delete {
105114
return fetchAndExec(logger, authorizer,
106115
rbac.ActionDelete, fetchFunc, deleteFunc)
107116
}
108117

109-
func updateWithReturn[ObjectType rbac.Objecter,
118+
func updateWithReturn[
119+
ObjectType rbac.Objecter,
110120
ArgumentType any,
111121
Fetch func(ctx context.Context, arg ArgumentType) (ObjectType, error),
112-
UpdateQuery func(ctx context.Context, arg ArgumentType) (ObjectType, error)](
122+
UpdateQuery func(ctx context.Context, arg ArgumentType) (ObjectType, error),
123+
](
113124
// Arguments
114125
logger slog.Logger,
115126
authorizer rbac.Authorizer,
116127
fetchFunc Fetch,
117-
updateQuery UpdateQuery) UpdateQuery {
128+
updateQuery UpdateQuery,
129+
) UpdateQuery {
118130
return fetchAndQuery(logger, authorizer, rbac.ActionUpdate, fetchFunc, updateQuery)
119131
}
120132

121-
func update[ObjectType rbac.Objecter,
133+
func update[
134+
ObjectType rbac.Objecter,
122135
ArgumentType any,
123136
Fetch func(ctx context.Context, arg ArgumentType) (ObjectType, error),
124-
Exec func(ctx context.Context, arg ArgumentType) error](
137+
Exec func(ctx context.Context, arg ArgumentType) error,
138+
](
125139
// Arguments
126140
logger slog.Logger,
127141
authorizer rbac.Authorizer,
128142
fetchFunc Fetch,
129-
updateExec Exec) Exec {
143+
updateExec Exec,
144+
) Exec {
130145
return fetchAndExec(logger, authorizer, rbac.ActionUpdate, fetchFunc, updateExec)
131146
}
132147

@@ -137,12 +152,16 @@ func update[ObjectType rbac.Objecter,
137152
// The database query function will **ALWAYS** hit the database, even if the
138153
// user cannot read the resource. This is because the resource details are
139154
// required to run a proper authorization check.
140-
func fetch[ArgumentType any, ObjectType rbac.Objecter,
141-
DatabaseFunc func(ctx context.Context, arg ArgumentType) (ObjectType, error)](
155+
func fetch[
156+
ArgumentType any,
157+
ObjectType rbac.Objecter,
158+
DatabaseFunc func(ctx context.Context, arg ArgumentType) (ObjectType, error),
159+
](
142160
// Arguments
143161
logger slog.Logger,
144162
authorizer rbac.Authorizer,
145-
f DatabaseFunc) DatabaseFunc {
163+
f DatabaseFunc,
164+
) DatabaseFunc {
146165
return func(ctx context.Context, arg ArgumentType) (empty ObjectType, err error) {
147166
// Fetch the rbac subject
148167
act, ok := ActorFromContext(ctx)
@@ -169,16 +188,19 @@ func fetch[ArgumentType any, ObjectType rbac.Objecter,
169188
// fetchAndExec uses fetchAndQuery but only returns the error. The naming comes
170189
// from SQL 'exec' functions which only return an error.
171190
// See fetchAndQuery for more information.
172-
func fetchAndExec[ObjectType rbac.Objecter,
191+
func fetchAndExec[
192+
ObjectType rbac.Objecter,
173193
ArgumentType any,
174194
Fetch func(ctx context.Context, arg ArgumentType) (ObjectType, error),
175-
Exec func(ctx context.Context, arg ArgumentType) error](
195+
Exec func(ctx context.Context, arg ArgumentType) error,
196+
](
176197
// Arguments
177198
logger slog.Logger,
178199
authorizer rbac.Authorizer,
179200
action rbac.Action,
180201
fetchFunc Fetch,
181-
execFunc Exec) Exec {
202+
execFunc Exec,
203+
) Exec {
182204
f := fetchAndQuery(logger, authorizer, action, fetchFunc, func(ctx context.Context, arg ArgumentType) (empty ObjectType, err error) {
183205
return empty, execFunc(ctx, arg)
184206
})
@@ -192,15 +214,19 @@ func fetchAndExec[ObjectType rbac.Objecter,
192214
// The fetch is used to know which rbac object the action should be asserted on
193215
// **before** the query runs. The returns from the fetch are only used to
194216
// assert rbac. The final return of this function comes from the Query function.
195-
func fetchAndQuery[ObjectType rbac.Objecter, ArgumentType any,
217+
func fetchAndQuery[
218+
ObjectType rbac.Objecter,
219+
ArgumentType any,
196220
Fetch func(ctx context.Context, arg ArgumentType) (ObjectType, error),
197-
Query func(ctx context.Context, arg ArgumentType) (ObjectType, error)](
221+
Query func(ctx context.Context, arg ArgumentType) (ObjectType, error),
222+
](
198223
// Arguments
199224
logger slog.Logger,
200225
authorizer rbac.Authorizer,
201226
action rbac.Action,
202227
fetchFunc Fetch,
203-
queryFunc Query) Query {
228+
queryFunc Query,
229+
) Query {
204230
return func(ctx context.Context, arg ArgumentType) (empty ObjectType, err error) {
205231
// Fetch the rbac subject
206232
act, ok := ActorFromContext(ctx)
@@ -226,11 +252,15 @@ func fetchAndQuery[ObjectType rbac.Objecter, ArgumentType any,
226252

227253
// fetchWithPostFilter is like fetch, but works with lists of objects.
228254
// SQL filters are much more optimal.
229-
func fetchWithPostFilter[ArgumentType any, ObjectType rbac.Objecter,
230-
DatabaseFunc func(ctx context.Context, arg ArgumentType) ([]ObjectType, error)](
255+
func fetchWithPostFilter[
256+
ArgumentType any,
257+
ObjectType rbac.Objecter,
258+
DatabaseFunc func(ctx context.Context, arg ArgumentType) ([]ObjectType, error),
259+
](
231260
// Arguments
232261
authorizer rbac.Authorizer,
233-
f DatabaseFunc) DatabaseFunc {
262+
f DatabaseFunc,
263+
) DatabaseFunc {
234264
return func(ctx context.Context, arg ArgumentType) (empty []ObjectType, err error) {
235265
// Fetch the rbac subject
236266
act, ok := ActorFromContext(ctx)

0 commit comments

Comments
 (0)