Enforce use of any instead of interface{} (#30588)

Этот коммит содержится в:
Ben Schumacher
2025-03-31 10:44:34 +02:00
коммит произвёл GitHub
родитель 9aa4818c71
Коммит 166a676fe5
78 изменённых файлов: 268 добавлений и 262 удалений

Просмотреть файл

@@ -46,7 +46,7 @@ func NewFromAPI(api common.LogAPI, dmer poster.DMer, logLevel logger.LogLevel, i
return New(logger.New(api), dmer, logLevel, includeContext, userIDs...)
}
func (l *adminCCLogger) Debugf(format string, args ...interface{}) {
func (l *adminCCLogger) Debugf(format string, args ...any) {
l.Logger.Debugf(format, args...)
message := fmt.Sprintf(format, args...)
if logger.Level(l.logLevel) >= 4 {
@@ -54,7 +54,7 @@ func (l *adminCCLogger) Debugf(format string, args ...interface{}) {
}
}
func (l *adminCCLogger) Errorf(format string, args ...interface{}) {
func (l *adminCCLogger) Errorf(format string, args ...any) {
l.Logger.Errorf(format, args...)
message := fmt.Sprintf(format, args...)
if logger.Level(l.logLevel) >= 1 {
@@ -62,7 +62,7 @@ func (l *adminCCLogger) Errorf(format string, args ...interface{}) {
}
}
func (l *adminCCLogger) Infof(format string, args ...interface{}) {
func (l *adminCCLogger) Infof(format string, args ...any) {
l.Logger.Infof(format, args...)
message := fmt.Sprintf(format, args...)
if logger.Level(l.logLevel) >= 3 {
@@ -70,7 +70,7 @@ func (l *adminCCLogger) Infof(format string, args ...interface{}) {
}
}
func (l *adminCCLogger) Warnf(format string, args ...interface{}) {
func (l *adminCCLogger) Warnf(format string, args ...any) {
l.Logger.Warnf(format, args...)
message := fmt.Sprintf(format, args...)
if logger.Level(l.logLevel) >= 2 {
@@ -86,7 +86,7 @@ func (l *adminCCLogger) logToAdmins(level, message string) {
_ = l.dmAdmins("(log " + level + ") " + message)
}
func (l *adminCCLogger) dmAdmins(format string, args ...interface{}) error {
func (l *adminCCLogger) dmAdmins(format string, args ...any) error {
for _, id := range l.userIDs {
_, err := l.dmer.DM(id, format, args)
if err != nil {

Просмотреть файл

@@ -30,7 +30,7 @@ func New(api common.LogAPI) Logger {
func (l *defaultLogger) With(logContext LogContext) Logger {
newLogger := *l
if len(newLogger.logContext) == 0 {
newLogger.logContext = map[string]interface{}{}
newLogger.logContext = map[string]any{}
}
for k, v := range logContext {
newLogger.logContext[k] = v
@@ -41,7 +41,7 @@ func (l *defaultLogger) With(logContext LogContext) Logger {
func (l *defaultLogger) WithError(err error) Logger {
newLogger := *l
if len(newLogger.logContext) == 0 {
newLogger.logContext = map[string]interface{}{}
newLogger.logContext = map[string]any{}
}
newLogger.logContext[ErrorKey] = err.Error()
return &newLogger
@@ -57,25 +57,25 @@ func (l *defaultLogger) Timed() Logger {
})
}
func (l *defaultLogger) Debugf(format string, args ...interface{}) {
func (l *defaultLogger) Debugf(format string, args ...any) {
measure(l.logContext)
message := fmt.Sprintf(format, args...)
l.logAPI.LogDebug(message, toKeyValuePairs(l.logContext)...)
}
func (l *defaultLogger) Errorf(format string, args ...interface{}) {
func (l *defaultLogger) Errorf(format string, args ...any) {
measure(l.logContext)
message := fmt.Sprintf(format, args...)
l.logAPI.LogError(message, toKeyValuePairs(l.logContext)...)
}
func (l *defaultLogger) Infof(format string, args ...interface{}) {
func (l *defaultLogger) Infof(format string, args ...any) {
measure(l.logContext)
message := fmt.Sprintf(format, args...)
l.logAPI.LogInfo(message, toKeyValuePairs(l.logContext)...)
}
func (l *defaultLogger) Warnf(format string, args ...interface{}) {
func (l *defaultLogger) Warnf(format string, args ...any) {
measure(l.logContext)
message := fmt.Sprintf(format, args...)
l.logAPI.LogWarn(message, toKeyValuePairs(l.logContext)...)

Просмотреть файл

@@ -24,7 +24,7 @@ const (
)
// LogContext defines the context for the logs.
type LogContext map[string]interface{}
type LogContext map[string]any
// Logger defines an object able to log messages.
type Logger interface {
@@ -37,13 +37,13 @@ type Logger interface {
// Timed add a timed log context.
Timed() Logger
// Debugf logs a formatted string as a debug message.
Debugf(format string, args ...interface{})
Debugf(format string, args ...any)
// Errorf logs a formatted string as an error message.
Errorf(format string, args ...interface{})
Errorf(format string, args ...any)
// Infof logs a formatted string as an info message.
Infof(format string, args ...interface{})
Infof(format string, args ...any)
// Warnf logs a formatted string as an warning message.
Warnf(format string, args ...interface{})
Warnf(format string, args ...any)
}
func measure(lc LogContext) {
@@ -70,7 +70,7 @@ func Level(l LogLevel) int {
return 0
}
func toKeyValuePairs(in map[string]interface{}) (out []interface{}) {
func toKeyValuePairs(in map[string]any) (out []any) {
for k, v := range in {
out = append(out, k, v)
}

Просмотреть файл

@@ -7,11 +7,11 @@ func NewNilLogger() Logger {
return &nilLogger{}
}
func (l *nilLogger) With(LogContext) Logger { return l }
func (l *nilLogger) WithError(error) Logger { return l }
func (l *nilLogger) Context() LogContext { return nil }
func (l *nilLogger) Timed() Logger { return l }
func (l *nilLogger) Debugf(string, ...interface{}) {}
func (l *nilLogger) Errorf(string, ...interface{}) {}
func (l *nilLogger) Infof(string, ...interface{}) {}
func (l *nilLogger) Warnf(string, ...interface{}) {}
func (l *nilLogger) With(LogContext) Logger { return l }
func (l *nilLogger) WithError(error) Logger { return l }
func (l *nilLogger) Context() LogContext { return nil }
func (l *nilLogger) Timed() Logger { return l }
func (l *nilLogger) Debugf(string, ...any) {}
func (l *nilLogger) Errorf(string, ...any) {}
func (l *nilLogger) Infof(string, ...any) {}
func (l *nilLogger) Warnf(string, ...any) {}

Просмотреть файл

@@ -37,7 +37,7 @@ func NewFromAPI(api common.LogAPI, logLevel logger.LogLevel, tracker telemetry.T
return New(logger.New(api), logLevel, tracker)
}
func (l *telemetryLogger) Debugf(format string, args ...interface{}) {
func (l *telemetryLogger) Debugf(format string, args ...any) {
l.Logger.Debugf(format, args...)
message := fmt.Sprintf(format, args...)
if logger.Level(l.logLevel) >= 4 {
@@ -45,7 +45,7 @@ func (l *telemetryLogger) Debugf(format string, args ...interface{}) {
}
}
func (l *telemetryLogger) Errorf(format string, args ...interface{}) {
func (l *telemetryLogger) Errorf(format string, args ...any) {
l.Logger.Errorf(format, args...)
message := fmt.Sprintf(format, args...)
if logger.Level(l.logLevel) >= 1 {
@@ -53,7 +53,7 @@ func (l *telemetryLogger) Errorf(format string, args ...interface{}) {
}
}
func (l *telemetryLogger) Infof(format string, args ...interface{}) {
func (l *telemetryLogger) Infof(format string, args ...any) {
l.Logger.Infof(format, args...)
message := fmt.Sprintf(format, args...)
if logger.Level(l.logLevel) >= 3 {
@@ -61,7 +61,7 @@ func (l *telemetryLogger) Infof(format string, args ...interface{}) {
}
}
func (l *telemetryLogger) Warnf(format string, args ...interface{}) {
func (l *telemetryLogger) Warnf(format string, args ...any) {
l.Logger.Warnf(format, args...)
message := fmt.Sprintf(format, args...)
if logger.Level(l.logLevel) >= 2 {
@@ -70,7 +70,7 @@ func (l *telemetryLogger) Warnf(format string, args ...interface{}) {
}
func (l *telemetryLogger) logToTelemetry(level, message string) {
properties := map[string]interface{}{}
properties := map[string]any{}
properties["message"] = message
for k, v := range l.Context() {
properties["context_"+k] = fmt.Sprintf("%v", v)

Просмотреть файл

@@ -19,7 +19,7 @@ func NewTestLogger() Logger {
func (l *testLogger) With(logContext LogContext) Logger {
newl := *l
if len(newl.logContext) == 0 {
newl.logContext = map[string]interface{}{}
newl.logContext = map[string]any{}
}
for k, v := range logContext {
newl.logContext[k] = v
@@ -30,7 +30,7 @@ func (l *testLogger) With(logContext LogContext) Logger {
func (l *testLogger) WithError(err error) Logger {
newl := *l
if len(newl.logContext) == 0 {
newl.logContext = map[string]interface{}{}
newl.logContext = map[string]any{}
}
newl.logContext[ErrorKey] = err.Error()
return &newl
@@ -46,7 +46,7 @@ func (l *testLogger) Timed() Logger {
})
}
func (l *testLogger) logf(prefix, format string, args ...interface{}) {
func (l *testLogger) logf(prefix, format string, args ...any) {
out := fmt.Sprintf(prefix+": "+format, args...)
if len(l.logContext) > 0 {
measure(l.logContext)
@@ -55,7 +55,7 @@ func (l *testLogger) logf(prefix, format string, args ...interface{}) {
l.TB.Log(out)
}
func (l *testLogger) Debugf(format string, args ...interface{}) { l.logf("DEBUG", format, args...) }
func (l *testLogger) Errorf(format string, args ...interface{}) { l.logf("ERROR", format, args...) }
func (l *testLogger) Infof(format string, args ...interface{}) { l.logf("INFO", format, args...) }
func (l *testLogger) Warnf(format string, args ...interface{}) { l.logf("WARN", format, args...) }
func (l *testLogger) Debugf(format string, args ...any) { l.logf("DEBUG", format, args...) }
func (l *testLogger) Errorf(format string, args ...any) { l.logf("ERROR", format, args...) }
func (l *testLogger) Infof(format string, args ...any) { l.logf("INFO", format, args...) }
func (l *testLogger) Warnf(format string, args ...any) { l.logf("WARN", format, args...) }

Просмотреть файл

@@ -20,7 +20,7 @@ func NewPoster(postAPI PostAPI, id string) Poster {
}
// DM posts a simple Direct Message to the specified user
func (p *defaultPoster) DM(mattermostUserID, format string, args ...interface{}) (string, error) {
func (p *defaultPoster) DM(mattermostUserID, format string, args ...any) (string, error) {
post := &model.Post{
Message: fmt.Sprintf(format, args...),
}
@@ -44,7 +44,7 @@ func (p *defaultPoster) DMWithAttachments(mattermostUserID string, attachments .
}
// Ephemeral sends an ephemeral message to a user
func (p *defaultPoster) Ephemeral(userID, channelID, format string, args ...interface{}) {
func (p *defaultPoster) Ephemeral(userID, channelID, format string, args ...any) {
post := &model.Post{
UserId: p.id,
ChannelId: channelID,
@@ -53,7 +53,7 @@ func (p *defaultPoster) Ephemeral(userID, channelID, format string, args ...inte
p.postAPI.SendEphemeralPost(userID, post)
}
func (p *defaultPoster) UpdatePostByID(postID, format string, args ...interface{}) error {
func (p *defaultPoster) UpdatePostByID(postID, format string, args ...any) error {
post, err := p.postAPI.GetPost(postID)
if err != nil {
return err

Просмотреть файл

@@ -30,7 +30,7 @@ func TestInterface(t *testing.T) {
func TestDM(t *testing.T) {
format := "test format, string: %s int: %d value: %v"
args := []interface{}{"some string", 5, 8.423}
args := []any{"some string", 5, 8.423}
expectedMessage := "test format, string: some string int: 5 value: 8.423"
expectedPostID := "expected-post-id"
@@ -153,7 +153,7 @@ func TestDMWithAttachments(t *testing.T) {
func TestEphemeral(t *testing.T) {
format := "test format, string: %s int: %d value: %v"
args := []interface{}{"some string", 5, 8.423}
args := []any{"some string", 5, 8.423}
expectedMessage := "test format, string: some string int: 5 value: 8.423"
channelID := "some-channel"
@@ -194,7 +194,7 @@ func TestEphemeral(t *testing.T) {
func TestUpdatePostByID(t *testing.T) {
format := "test format, string: %s int: %d value: %v"
args := []interface{}{"some string", 5, 8.423}
args := []any{"some string", 5, 8.423}
expectedMessage := "test format, string: some string int: 5 value: 8.423"
postID := "some-post-id"
@@ -364,7 +364,7 @@ func TestUpdatePost(t *testing.T) {
func TestUpdatePosterID(t *testing.T) {
format := "test format, string: %s int: %d value: %v"
args := []interface{}{"some string", 5, 8.423}
args := []any{"some string", 5, 8.423}
expectedMessage := "test format, string: some string int: 5 value: 8.423"
expectedPostID := "expected-post-id"

Просмотреть файл

@@ -16,10 +16,10 @@ type Poster interface {
DMWithAttachments(mattermostUserID string, attachments ...*model.SlackAttachment) (string, error)
// Ephemeral sends an ephemeral message to a user
Ephemeral(mattermostUserID, channelID, format string, args ...interface{})
Ephemeral(mattermostUserID, channelID, format string, args ...any)
// UpdatePostByID updates the post with postID with the formatted message
UpdatePostByID(postID, format string, args ...interface{}) error
UpdatePostByID(postID, format string, args ...any) error
// DeletePost deletes a single post
DeletePost(postID string) error
@@ -34,5 +34,5 @@ type Poster interface {
// DMer defines an entity that can send Direct Messages
type DMer interface {
// DM posts a simple Direct Message to the specified user
DM(mattermostUserID, format string, args ...interface{}) (string, error)
DM(mattermostUserID, format string, args ...any) (string, error)
}