diff --git a/auth/middleware.go b/auth/middleware.go index 24fd0cc6869b4fa3f380b08e496fa0e0c76852e9..70ad49bab9b3eee3582af8191e711a877c5e2c26 100644 --- a/auth/middleware.go +++ b/auth/middleware.go @@ -440,7 +440,7 @@ return errors.Errorf(errors.Unauthorized, "Unknown user %s", username) } return FetchMetaProfile(ctx, username, user) } - cols := []interface{}{ + cols := []any{ &user.UserID, &user.Username, &user.Created, &user.Updated, &user.Email, @@ -476,7 +476,7 @@ Query: ` query RevocationStatus($hash: String!, $clientId: String) { tokenRevocationStatus(hash: $hash, clientId: $clientId) }`, - Variables: map[string]interface{}{ + Variables: map[string]any{ "hash": hex.EncodeToString(hash[:]), "clientId": clientID, }, diff --git a/client/graphql.go b/client/graphql.go index 57750f2ad602bc3b2b4dde38fa017a53ca305897..4f8c85f8abeaf122d6de07865bfada3360d3b7b4 100644 --- a/client/graphql.go +++ b/client/graphql.go @@ -20,7 +20,7 @@ ) type GraphQLQuery struct { Query string `json:"query"` - Variables map[string]interface{} `json:"variables"` + Variables map[string]any `json:"variables"` Uploads map[string]graphql.Upload `json:"-"` } @@ -31,7 +31,7 @@ NodeID string `json:"node_id"` } func Do(ctx context.Context, username string, svc string, - query GraphQLQuery, result interface{}, + query GraphQLQuery, result any, ) error { conf := config.ForContext(ctx) origin := config.GetAPI(conf, svc, false) @@ -159,7 +159,7 @@ svc, resp.StatusCode, string(respBody)) } var respData struct { - Data interface{} `json:"data"` + Data any `json:"data"` Errors gqlerror.List `json:"errors"` } respData.Data = result diff --git a/database/ql.go b/database/ql.go index 088b98af8cf179d317d0c5561dab8ca29c866fcb..f65d1ff5c303a5d515fbaa44c12a7a33717c1a9f 100644 --- a/database/ql.go +++ b/database/ql.go @@ -30,7 +30,7 @@ } return fields } -func Scan(ctx context.Context, m Model) []interface{} { +func Scan(ctx context.Context, m Model) []any { qlFields := collectFields(ctx) if len(qlFields) == 0 { // Collect all fields if we are not in an active graphql context @@ -45,7 +45,7 @@ sort.Slice(qlFields, func(a, b int) bool { return qlFields[a].Name < qlFields[b].Name }) - var fields []interface{} + var fields []any for _, qlField := range qlFields { if gqlFields, ok := m.Fields().GQL(qlField.Name); ok { for _, field := range gqlFields { diff --git a/database/sq.go b/database/sq.go index a2699308c4b5897edaf884fe18c929c889f30d2f..7891e318116c40b3411ab3c9ef8e2aa0b46f95a0 100644 --- a/database/sq.go +++ b/database/sq.go @@ -13,7 +13,7 @@ // fields for all of the data associated with a model. type FieldMap struct { SQL string GQL string - Ptr interface{} + Ptr any } type ModelFields struct { @@ -85,7 +85,7 @@ Model Select(q sq.SelectBuilder) sq.SelectBuilder } -func Select(ctx context.Context, cols ...interface{}) sq.SelectBuilder { +func Select(ctx context.Context, cols ...any) sq.SelectBuilder { q := sq.Select().PlaceholderFormat(sq.Dollar) for _, col := range cols { switch col := col.(type) { @@ -120,13 +120,13 @@ q := sq.Select().PlaceholderFormat(sq.Dollar) return q.Columns(cols...) } -func ScanAll(m Model) []interface{} { +func ScanAll(m Model) []any { fms := m.Fields().All() sort.Slice(fms, func(a, b int) bool { return fms[a].SQL < fms[b].SQL }) - var fields []interface{} + var fields []any for _, f := range fms { if f.SQL != "" { fields = append(fields, f.Ptr) diff --git a/feature/header.go b/feature/header.go index 1fb5a09789f22eb0b6ed2fbd0ae6719ca1011131..a21ba364de402b67f9a152f0aa7b75c68f6b03ce 100644 --- a/feature/header.go +++ b/feature/header.go @@ -3,6 +3,7 @@ import ( "context" "net/http" + "slices" "strings" ) @@ -39,10 +40,5 @@ // Returns true if the requested feature is enabled via the Accept-Features // HTTP header func Enabled(ctx context.Context, name string) bool { - for _, feat := range ForContext(ctx) { - if feat == name { - return true - } - } - return false + return slices.Contains(ForContext(ctx), name) } diff --git a/model/cursor.go b/model/cursor.go index 3dffa2029ef27222f26198debfee151afbfd806c..8302fffd05ed7f3577e031e06c049188ce495b58 100644 --- a/model/cursor.go +++ b/model/cursor.go @@ -15,7 +15,7 @@ Next string `json:"next"` Search string `json:"search"` } -func (cur *Cursor) UnmarshalGQL(v interface{}) error { +func (cur *Cursor) UnmarshalGQL(v any) error { enc, ok := v.(string) if !ok { return fmt.Errorf("cursor must be strings") diff --git a/model/id.go b/model/id.go index 1192d30db53cbc41ffe2f34e42a30a8a493404a7..96560bb5ef314abdb5e4bc8a54bdd126b68bd318 100644 --- a/model/id.go +++ b/model/id.go @@ -39,10 +39,10 @@ return base32Encoding.EncodeToString(id.uuid[:]) } func (id ID) MarshalGQL(w io.Writer) { - w.Write([]byte(fmt.Sprintf(`"%s"`, id.String()))) + w.Write(fmt.Appendf(nil, `"%s"`, id.String())) } -func (id *ID) UnmarshalGQL(v interface{}) error { +func (id *ID) UnmarshalGQL(v any) error { switch v := v.(type) { case string: bytes, err := base32Encoding.DecodeString(v) diff --git a/server/directives.go b/server/directives.go index 17f60c1b88603856b444b0eb472ea30d2a3ead5f..15f74bec80949b3e0e7ac7650471fb2b5c26a93c 100644 --- a/server/directives.go +++ b/server/directives.go @@ -9,8 +9,8 @@ "git.sr.ht/~sircmpwn/core-go/auth" ) -func Admin(ctx context.Context, obj interface{}, - next graphql.Resolver) (interface{}, error) { +func Admin(ctx context.Context, obj any, + next graphql.Resolver) (any, error) { if auth.ForContext(ctx).UserType != auth.USER_TYPE_ADMIN { return nil, fmt.Errorf("Access denied") @@ -19,8 +19,8 @@ return next(ctx) } -func AnonInternal(ctx context.Context, obj interface{}, - next graphql.Resolver) (interface{}, error) { +func AnonInternal(ctx context.Context, obj any, + next graphql.Resolver) (any, error) { if auth.ForContext(ctx).AuthMethod != auth.AUTH_ANON_INTERNAL { return nil, fmt.Errorf("Anonymous internal auth access denied") @@ -29,8 +29,8 @@ return next(ctx) } -func Internal(ctx context.Context, obj interface{}, - next graphql.Resolver) (interface{}, error) { +func Internal(ctx context.Context, obj any, + next graphql.Resolver) (any, error) { if auth.ForContext(ctx).AuthMethod != auth.AUTH_INTERNAL { return nil, fmt.Errorf("Internal auth access denied") @@ -39,8 +39,8 @@ return next(ctx) } -func Private(ctx context.Context, obj interface{}, - next graphql.Resolver) (interface{}, error) { +func Private(ctx context.Context, obj any, + next graphql.Resolver) (any, error) { user := auth.ForContext(ctx) switch user.AuthMethod { @@ -56,8 +56,8 @@ return nil, fmt.Errorf("Private auth access denied") } -func Access(ctx context.Context, obj interface{}, next graphql.Resolver, - scope string, kind string) (interface{}, error) { +func Access(ctx context.Context, obj any, next graphql.Resolver, + scope string, kind string) (any, error) { if err := auth.ForContext(ctx).Access(scope, kind); err != nil { return nil, err diff --git a/server/email.go b/server/email.go index 67d2c4a17563364da2bd42c5bff4e8522cdcdc10..37fc4ddd203692d6b237accd257fc49fbba67e3a 100644 --- a/server/email.go +++ b/server/email.go @@ -20,7 +20,7 @@ ) // Provides a graphql.RecoverFunc which will print the stack trace, and if // debug mode is not enabled, email it to the administrator. -func EmailRecover(ctx context.Context, _origErr interface{}) error { +func EmailRecover(ctx context.Context, _origErr any) error { origErr, ok := _origErr.(error) if !ok { log.Printf("Unexpected error in recover: %v\n", _origErr) diff --git a/valid/valid.go b/valid/valid.go index 9a49711b00daa6da4882c71d7550df74012bc2ed..994e4b41144d87d3eb871279bffa0dcc9f4b37a3 100644 --- a/valid/valid.go +++ b/valid/valid.go @@ -10,7 +10,7 @@ ) type Validation struct { ctx context.Context - input map[string]interface{} + input map[string]any } type ValidationError struct { @@ -23,18 +23,18 @@ func Error(ctx context.Context, field string, msg string) error { return &gqlerror.Error{ Message: msg, Path: graphql.GetPath(ctx), - Extensions: map[string]interface{}{ + Extensions: map[string]any{ "field": field, }, } } // Returns a new GraphQL error attached to the given field. -func Errorf(ctx context.Context, field string, msg string, items ...interface{}) error { +func Errorf(ctx context.Context, field string, msg string, items ...any) error { return &gqlerror.Error{ Message: fmt.Sprintf(msg, items...), Path: graphql.GetPath(ctx), - Extensions: map[string]interface{}{ + Extensions: map[string]any{ "field": field, }, } @@ -48,7 +48,7 @@ } } // Adds an input map to a validation context. -func (valid *Validation) WithInput(input map[string]interface{}) *Validation { +func (valid *Validation) WithInput(input map[string]any) *Validation { valid.input = input return valid } @@ -62,7 +62,7 @@ // Fetches an item from the validation context, which must have an input // registered. If the field is not present, the callback is not run. Otherwise, // the function is called with the value for the user to conduct further // validation with. -func (valid *Validation) Optional(name string, fn func(i interface{})) { +func (valid *Validation) Optional(name string, fn func(i any)) { if valid.input == nil { panic(fmt.Errorf("Attempted to validate fields without input")) } @@ -160,7 +160,7 @@ } // Creates a validation error unconditionally. func (valid *Validation) Error(msg string, - items ...interface{}) *ValidationError { + items ...any) *ValidationError { err := &gqlerror.Error{ Path: graphql.GetPath(valid.ctx), Message: fmt.Sprintf(msg, items...), @@ -175,7 +175,7 @@ // Asserts that a condition is true, recording a GraphQL error with the given // message if not. func (valid *Validation) Expect(cond bool, - msg string, items ...interface{}) *ValidationError { + msg string, items ...any) *ValidationError { if cond { return &ValidationError{valid: valid} } @@ -188,7 +188,7 @@ if err.err == nil { return err } if err.err.Extensions == nil { - err.err.Extensions = make(map[string]interface{}) + err.err.Extensions = make(map[string]any) } err.err.Extensions["field"] = field return err @@ -198,7 +198,7 @@ // Composes another assertion onto the same validation context which initially // created an error. Short-circuiting is used, such that if the earlier // condition failed, the new condition is not considered. func (err *ValidationError) And(cond bool, - msg string, items ...interface{}) *ValidationError { + msg string, items ...any) *ValidationError { if err.err != nil { return err } diff --git a/webhooks/context.go b/webhooks/context.go index 9867381b9118157316a30b64c971902003d091d3..a8a109c3e7756e48ff7e7db093c1cd1da885b322 100644 --- a/webhooks/context.go +++ b/webhooks/context.go @@ -26,18 +26,18 @@ type WebhookContext struct { Name string Event string User *auth.AuthContext - Payload interface{} + Payload any PayloadUUID uuid.UUID Subscription *WebhookSubscription } // Prepares an context for a specific webhook delivery. -func Context(ctx context.Context, payload interface{}) context.Context { +func Context(ctx context.Context, payload any) context.Context { return context.WithValue(ctx, payloadContextKey, payload) } // Returns the active payload for a webhook context. -func Payload(ctx context.Context) (interface{}, error) { +func Payload(ctx context.Context) (any, error) { payload := ctx.Value(payloadContextKey) if payload == nil { return nil, errors.New("Cannot use this resolver without an active webhook context") diff --git a/webhooks/legacy.go b/webhooks/legacy.go index ec62b0a499384220da664d884c4c66c0f22db4bc..d7b54f49f7ce0e656e00b8e3743d92164d5a936a 100644 --- a/webhooks/legacy.go +++ b/webhooks/legacy.go @@ -9,6 +9,7 @@ "io" "io/ioutil" "log" "net/http" + "slices" "strings" "time" @@ -134,11 +135,8 @@ // double check here to get the final list. sub.Events = strings.Split(events, ",") var valid bool - for _, e := range sub.Events { - if e == event { - valid = true - break - } + if slices.Contains(sub.Events, event) { + valid = true } if valid { diff --git a/webhooks/queue.go b/webhooks/queue.go index 1523bf96838a87aa2e98e64860e07d21e7384d57..77f4905d86a8b3ebbabe049da7690257eefe0927 100644 --- a/webhooks/queue.go +++ b/webhooks/queue.go @@ -65,7 +65,7 @@ // initiated the webhook delivery. It should instead be a fresh background // context which contains the necessary state for your application to process // the webhook resolvers. func (queue *WebhookQueue) Schedule(ctx context.Context, q sq.SelectBuilder, - name, event string, payloadUUID uuid.UUID, payload interface{}) { + name, event string, payloadUUID uuid.UUID, payload any) { err := queue.schedule(ctx, q, name, event, payloadUUID, payload) if err != nil { log.Printf("Failed to enqueue webhook deliveries: %v", err) @@ -73,7 +73,7 @@ } } func (queue *WebhookQueue) schedule(ctx context.Context, q sq.SelectBuilder, - name, event string, payloadUUID uuid.UUID, payload interface{}) error { + name, event string, payloadUUID uuid.UUID, payload any) error { // The following tasks are done during this process: // // 1. Fetch subscription details from the database