From e066bb86481db855545dfd818df17a7d25fb37b6 Mon Sep 17 00:00:00 2001 From: songlq <31207055+songlonqi-java@users.noreply.github.com> Date: Wed, 27 May 2026 15:55:47 +0800 Subject: [PATCH 1/5] fix(diskcache): restore writer after rotate failure --- diskcache/diskcache.go | 2 +- diskcache/get.go | 16 +++++------ diskcache/put.go | 21 ++++++++++---- diskcache/rotate.go | 27 +++++++++++++++-- diskcache/rotate_test.go | 62 ++++++++++++++++++++++++++++++++++++++++ diskcache/write_file.go | 45 +++++++++++++++++++++++++++++ 6 files changed, 155 insertions(+), 18 deletions(-) create mode 100644 diskcache/write_file.go diff --git a/diskcache/diskcache.go b/diskcache/diskcache.go index 3fb4c9da..dfc56dd5 100644 --- a/diskcache/diskcache.go +++ b/diskcache/diskcache.go @@ -164,7 +164,7 @@ func (c *DiskCache) Pretty() string { } if c.rfd != nil { - arr = append(arr, "cur-read: "+c.rfd.Name()) + arr = append(arr, "cur-read: "+c.readFileName()) } else { arr = append(arr, "no-Get()") } diff --git a/diskcache/get.go b/diskcache/get.go index 61b8d01e..fe40bcb9 100644 --- a/diskcache/get.go +++ b/diskcache/get.go @@ -113,13 +113,13 @@ retry: if n, err = c.rfd.Read(c.batchHeader); err != nil || n != dataHeaderLen { if err != nil && !errors.Is(err, io.EOF) { l.Errorf("read %d bytes header error: %s", dataHeaderLen, err.Error()) - err = WrapFileOperationError(OpRead, err, c.path, c.rfd.Name()). + err = WrapFileOperationError(OpRead, err, c.path, c.readFileName()). WithDetails(fmt.Sprintf("header_read: expected=%d, actual=%d", dataHeaderLen, n)) } else if n > 0 && n != dataHeaderLen { l.Errorf("invalid header length: %d, expect %d", n, dataHeaderLen) err = NewCacheError(OpRead, ErrUnexpectedReadSize, fmt.Sprintf("header_size_mismatch: expected=%d, actual=%d", dataHeaderLen, n)). - WithPath(c.path).WithFile(c.rfd.Name()) + WithPath(c.path).WithFile(c.readFileName()) } // On bad datafile, just ignore and delete the file. @@ -135,7 +135,7 @@ retry: if uint32(nbytes) == EOFHint { // EOF if err := c.switchNextFile(); err != nil { - return WrapGetError(err, c.path, c.rfd.Name()). + return WrapGetError(err, c.path, c.readFileName()). WithDetails("eof_encountered_during_get") } @@ -156,23 +156,23 @@ retry: if len(readbuf) < nbytes { // seek to next read position if x, err := c.rfd.Seek(int64(nbytes), io.SeekCurrent); err != nil { - return WrapFileOperationError(OpSeek, err, c.path, c.rfd.Name()). + return WrapFileOperationError(OpSeek, err, c.path, c.readFileName()). WithDetails(fmt.Sprintf("failed_to_seek_past_data: data_size=%d", nbytes)) } else { l.Warnf("got %d bytes to buffer with len %d, seek to new read position %d, drop %d bytes within file %s", nbytes, len(readbuf), x, nbytes, c.curReadfile) droppedDataVec.WithLabelValues(c.path, reasonTooSmallReadBuffer).Observe(float64(nbytes)) - return WrapGetError(ErrTooSmallReadBuf, c.path, c.rfd.Name()). + return WrapGetError(ErrTooSmallReadBuf, c.path, c.readFileName()). WithDetails(fmt.Sprintf("buffer_too_small: required=%d, provided=%d", nbytes, len(readbuf))) } } if n, err := c.rfd.Read(readbuf[:nbytes]); err != nil { - return WrapFileOperationError(OpRead, err, c.path, c.rfd.Name()). + return WrapFileOperationError(OpRead, err, c.path, c.readFileName()). WithDetails(fmt.Sprintf("data_read: expected=%d, actual=%d", nbytes, n)) } else if n != nbytes { - return WrapGetError(ErrUnexpectedReadSize, c.path, c.rfd.Name()). + return WrapGetError(ErrUnexpectedReadSize, c.path, c.readFileName()). WithDetails(fmt.Sprintf("partial_read: expected=%d, actual=%d", nbytes, n)) } @@ -184,7 +184,7 @@ retry: // seek back if !c.noFallbackOnError { if _, serr := c.rfd.Seek(-int64(dataHeaderLen+nbytes), io.SeekCurrent); serr != nil { - return WrapFileOperationError(OpSeek, serr, c.path, c.rfd.Name()). + return WrapFileOperationError(OpSeek, serr, c.path, c.readFileName()). WithDetails(fmt.Sprintf("fallback_seek_failed: offset=%d", -int64(dataHeaderLen+nbytes))) } diff --git a/diskcache/put.go b/diskcache/put.go index 4c5d370e..bfd47be3 100644 --- a/diskcache/put.go +++ b/diskcache/put.go @@ -51,22 +51,26 @@ func (c *DiskCache) Put(data []byte) error { fmt.Sprintf("max_size=%d, actual_size=%d", c.maxDataSize, len(data))) } + if err := c.ensureWriteFile(); err != nil { + return WrapPutError(err, c.path, len(data)).WithDetails("failed_to_open_write_file") + } + hdr := make([]byte, dataHeaderLen) binary.LittleEndian.PutUint32(hdr, uint32(len(data))) if _, err := c.wfd.Write(hdr); err != nil { - return WrapFileOperationError(OpWrite, err, c.path, c.wfd.Name()). + return WrapFileOperationError(OpWrite, err, c.path, c.writeFileName()). WithDetails("failed_to_write_header") } if _, err := c.wfd.Write(data); err != nil { - return WrapFileOperationError(OpWrite, err, c.path, c.wfd.Name()). + return WrapFileOperationError(OpWrite, err, c.path, c.writeFileName()). WithDetails("failed_to_write_data") } if !c.noSync { if err := c.wfd.Sync(); err != nil { - return WrapFileOperationError(OpSync, err, c.path, c.wfd.Name()). + return WrapFileOperationError(OpSync, err, c.path, c.writeFileName()). WithDetails("failed_to_sync_write") } } @@ -116,15 +120,20 @@ func (c *DiskCache) StreamPut(r io.Reader, size int) error { fmt.Sprintf("size_exceeded: max=%d, actual=%d", c.maxDataSize, size)).WithPath(c.path) } + if err := c.ensureWriteFile(); err != nil { + return NewCacheError(OpStreamPut, err, "failed_to_open_write_file"). + WithPath(c.path) + } + if startOffset, err = c.wfd.Seek(0, io.SeekCurrent); err != nil { - return WrapFileOperationError(OpSeek, err, c.path, c.wfd.Name()). + return WrapFileOperationError(OpSeek, err, c.path, c.writeFileName()). WithDetails("failed_to_get_current_position") } defer func() { if total > 0 && err != nil { // fallback to origin position if _, serr := c.wfd.Seek(startOffset, io.SeekStart); serr != nil { - c.LastErr = WrapFileOperationError(OpSeek, serr, c.path, c.wfd.Name()). + c.LastErr = WrapFileOperationError(OpSeek, serr, c.path, c.writeFileName()). WithDetails(fmt.Sprintf("failed_to_fallback_to_position_%d", startOffset)) } } @@ -135,7 +144,7 @@ func (c *DiskCache) StreamPut(r io.Reader, size int) error { if size > 0 { binary.LittleEndian.PutUint32(c.batchHeader, uint32(size)) if _, err := c.wfd.Write(c.batchHeader); err != nil { - return WrapFileOperationError(OpWrite, err, c.path, c.wfd.Name()). + return WrapFileOperationError(OpWrite, err, c.path, c.writeFileName()). WithDetails("failed_to_write_stream_header") } } diff --git a/diskcache/rotate.go b/diskcache/rotate.go index 6637e46a..f14076fe 100644 --- a/diskcache/rotate.go +++ b/diskcache/rotate.go @@ -34,10 +34,16 @@ func (c *DiskCache) rotate() error { datafilesVec.WithLabelValues(c.path).Set(float64(len(c.dataFiles))) }() + if err := c.ensureWriteFile(); err != nil { + return NewCacheError(OpRotate, err, "failed_to_open_write_file_before_rotate"). + WithPath(c.path) + } + + batchSizeBeforeEOF := c.curBatchSize eof := make([]byte, dataHeaderLen) binary.LittleEndian.PutUint32(eof, EOFHint) if _, err := c.wfd.Write(eof); err != nil { // append EOF to file end - return WrapFileOperationError(OpWrite, err, c.path, c.wfd.Name()). + return WrapFileOperationError(OpWrite, err, c.path, c.writeFileName()). WithDetails("failed_to_write_eof_marker_during_rotate") } @@ -69,13 +75,28 @@ func (c *DiskCache) rotate() error { // close current writing file if err := c.wfd.Close(); err != nil { - return WrapFileOperationError(OpClose, err, c.path, c.wfd.Name()). + return WrapFileOperationError(OpClose, err, c.path, c.writeFileName()). WithDetails("failed_to_close_write_file_during_rotate") } c.wfd = nil // rename data -> data.0004 if err := os.Rename(c.curWriteFile, newfile); err != nil { + if fi, statErr := os.Stat(c.curWriteFile); statErr == nil && !fi.IsDir() { + if truncErr := os.Truncate(c.curWriteFile, batchSizeBeforeEOF); truncErr != nil { + return NewCacheError(OpRotate, WrapRotateError(err, c.path, c.curWriteFile, newfile), + fmt.Sprintf("failed_to_restore_write_file_size_after_rename_failure: size=%d, error=%v", + batchSizeBeforeEOF, truncErr)). + WithPath(c.path) + } + } + + if openErr := c.openWriteFile(); openErr != nil { + return NewCacheError(OpRotate, WrapRotateError(err, c.path, c.curWriteFile, newfile), + fmt.Sprintf("failed_to_restore_write_file_after_rename_failure: %v", openErr)). + WithPath(c.path) + } + return WrapRotateError(err, c.path, c.curWriteFile, newfile). WithDetails("failed_to_rename_file_during_rotate") } @@ -116,7 +137,7 @@ func (c *DiskCache) removeCurrentReadingFile() error { if c.rfd != nil { if err := c.rfd.Close(); err != nil { - return WrapFileOperationError(OpClose, err, c.path, c.rfd.Name()). + return WrapFileOperationError(OpClose, err, c.path, c.readFileName()). WithDetails("failed_to_close_read_file_during_removal") } c.rfd = nil diff --git a/diskcache/rotate_test.go b/diskcache/rotate_test.go index e852ef88..f1ac22db 100644 --- a/diskcache/rotate_test.go +++ b/diskcache/rotate_test.go @@ -8,6 +8,9 @@ package diskcache import ( "bytes" "errors" + "os" + "path/filepath" + "runtime" T "testing" "github.com/stretchr/testify/assert" @@ -122,3 +125,62 @@ func TestRotate(t *T.T) { }) }) } + +func TestRotateRecoverWriteFileOnRenameFailure(t *T.T) { + if runtime.GOOS == "windows" { + t.Skip("removing an open file is not supported on windows") + } + + p := t.TempDir() + c, err := Open(WithPath(p), WithBatchSize(1024*1024)) + require.NoError(t, err) + t.Cleanup(func() { + assert.NoError(t, c.Close()) + ResetMetrics() + }) + + require.NoError(t, c.Put([]byte("before"))) + require.NoError(t, os.Remove(c.curWriteFile)) + + require.Error(t, c.Rotate()) + require.NotNil(t, c.wfd) + require.NoError(t, c.Put([]byte("after"))) +} + +func TestRotateTruncateEOFOnRenameFailure(t *T.T) { + p := t.TempDir() + c, err := Open(WithPath(p), WithBatchSize(1024*1024)) + require.NoError(t, err) + t.Cleanup(func() { + assert.NoError(t, c.Close()) + ResetMetrics() + }) + + newfile := filepath.Join(p, "data.00000000000000000000000000000000") + require.NoError(t, os.Mkdir(newfile, 0o755)) + + before := []byte("before") + after := []byte("after") + + require.NoError(t, c.Put(before)) + require.Error(t, c.Rotate()) + require.NotNil(t, c.wfd) + + require.NoError(t, os.Remove(newfile)) + require.NoError(t, c.Put(after)) + require.NoError(t, c.Rotate()) + + var got [][]byte + for { + err := c.Get(func(data []byte) error { + got = append(got, append([]byte(nil), data...)) + return nil + }) + if errors.Is(err, ErrNoData) { + break + } + require.NoError(t, err) + } + + require.Equal(t, [][]byte{before, after}, got) +} diff --git a/diskcache/write_file.go b/diskcache/write_file.go new file mode 100644 index 00000000..378c4081 --- /dev/null +++ b/diskcache/write_file.go @@ -0,0 +1,45 @@ +// Unless explicitly stated otherwise all files in this repository are licensed +// under the MIT License. +// This product includes software developed at Guance Cloud (https://www.guance.com/). +// Copyright 2021-present Guance, Inc. + +package diskcache + +import "os" + +func (c *DiskCache) writeFileName() string { + if c.wfd != nil { + return c.wfd.Name() + } + + if c.curWriteFile != "" { + return c.curWriteFile + } + + return c.path +} + +func (c *DiskCache) readFileName() string { + if c.rfd != nil { + return c.rfd.Name() + } + + if c.curReadfile != "" { + return c.curReadfile + } + + return c.path +} + +func (c *DiskCache) ensureWriteFile() error { + if c.wfd != nil { + return nil + } + + if c.curWriteFile == "" { + return WrapFileOperationError(OpCreate, os.ErrInvalid, c.path, c.writeFileName()). + WithDetails("write_file_path_not_set") + } + + return c.openWriteFile() +} From de5375eb78303f2c3031536ca69c23ea18eae0a8 Mon Sep 17 00:00:00 2001 From: songlq <31207055+songlonqi-java@users.noreply.github.com> Date: Wed, 27 May 2026 15:55:47 +0800 Subject: [PATCH 2/5] fix(diskcache): restore writer after rotate failure --- diskcache/diskcache.go | 2 +- diskcache/get.go | 16 +++++------ diskcache/put.go | 21 ++++++++++---- diskcache/rotate.go | 58 +++++++++++++++++++++++++++---------- diskcache/rotate_test.go | 62 ++++++++++++++++++++++++++++++++++++++++ diskcache/write_file.go | 45 +++++++++++++++++++++++++++++ 6 files changed, 174 insertions(+), 30 deletions(-) create mode 100644 diskcache/write_file.go diff --git a/diskcache/diskcache.go b/diskcache/diskcache.go index 3fb4c9da..dfc56dd5 100644 --- a/diskcache/diskcache.go +++ b/diskcache/diskcache.go @@ -164,7 +164,7 @@ func (c *DiskCache) Pretty() string { } if c.rfd != nil { - arr = append(arr, "cur-read: "+c.rfd.Name()) + arr = append(arr, "cur-read: "+c.readFileName()) } else { arr = append(arr, "no-Get()") } diff --git a/diskcache/get.go b/diskcache/get.go index 61b8d01e..fe40bcb9 100644 --- a/diskcache/get.go +++ b/diskcache/get.go @@ -113,13 +113,13 @@ retry: if n, err = c.rfd.Read(c.batchHeader); err != nil || n != dataHeaderLen { if err != nil && !errors.Is(err, io.EOF) { l.Errorf("read %d bytes header error: %s", dataHeaderLen, err.Error()) - err = WrapFileOperationError(OpRead, err, c.path, c.rfd.Name()). + err = WrapFileOperationError(OpRead, err, c.path, c.readFileName()). WithDetails(fmt.Sprintf("header_read: expected=%d, actual=%d", dataHeaderLen, n)) } else if n > 0 && n != dataHeaderLen { l.Errorf("invalid header length: %d, expect %d", n, dataHeaderLen) err = NewCacheError(OpRead, ErrUnexpectedReadSize, fmt.Sprintf("header_size_mismatch: expected=%d, actual=%d", dataHeaderLen, n)). - WithPath(c.path).WithFile(c.rfd.Name()) + WithPath(c.path).WithFile(c.readFileName()) } // On bad datafile, just ignore and delete the file. @@ -135,7 +135,7 @@ retry: if uint32(nbytes) == EOFHint { // EOF if err := c.switchNextFile(); err != nil { - return WrapGetError(err, c.path, c.rfd.Name()). + return WrapGetError(err, c.path, c.readFileName()). WithDetails("eof_encountered_during_get") } @@ -156,23 +156,23 @@ retry: if len(readbuf) < nbytes { // seek to next read position if x, err := c.rfd.Seek(int64(nbytes), io.SeekCurrent); err != nil { - return WrapFileOperationError(OpSeek, err, c.path, c.rfd.Name()). + return WrapFileOperationError(OpSeek, err, c.path, c.readFileName()). WithDetails(fmt.Sprintf("failed_to_seek_past_data: data_size=%d", nbytes)) } else { l.Warnf("got %d bytes to buffer with len %d, seek to new read position %d, drop %d bytes within file %s", nbytes, len(readbuf), x, nbytes, c.curReadfile) droppedDataVec.WithLabelValues(c.path, reasonTooSmallReadBuffer).Observe(float64(nbytes)) - return WrapGetError(ErrTooSmallReadBuf, c.path, c.rfd.Name()). + return WrapGetError(ErrTooSmallReadBuf, c.path, c.readFileName()). WithDetails(fmt.Sprintf("buffer_too_small: required=%d, provided=%d", nbytes, len(readbuf))) } } if n, err := c.rfd.Read(readbuf[:nbytes]); err != nil { - return WrapFileOperationError(OpRead, err, c.path, c.rfd.Name()). + return WrapFileOperationError(OpRead, err, c.path, c.readFileName()). WithDetails(fmt.Sprintf("data_read: expected=%d, actual=%d", nbytes, n)) } else if n != nbytes { - return WrapGetError(ErrUnexpectedReadSize, c.path, c.rfd.Name()). + return WrapGetError(ErrUnexpectedReadSize, c.path, c.readFileName()). WithDetails(fmt.Sprintf("partial_read: expected=%d, actual=%d", nbytes, n)) } @@ -184,7 +184,7 @@ retry: // seek back if !c.noFallbackOnError { if _, serr := c.rfd.Seek(-int64(dataHeaderLen+nbytes), io.SeekCurrent); serr != nil { - return WrapFileOperationError(OpSeek, serr, c.path, c.rfd.Name()). + return WrapFileOperationError(OpSeek, serr, c.path, c.readFileName()). WithDetails(fmt.Sprintf("fallback_seek_failed: offset=%d", -int64(dataHeaderLen+nbytes))) } diff --git a/diskcache/put.go b/diskcache/put.go index 4c5d370e..bfd47be3 100644 --- a/diskcache/put.go +++ b/diskcache/put.go @@ -51,22 +51,26 @@ func (c *DiskCache) Put(data []byte) error { fmt.Sprintf("max_size=%d, actual_size=%d", c.maxDataSize, len(data))) } + if err := c.ensureWriteFile(); err != nil { + return WrapPutError(err, c.path, len(data)).WithDetails("failed_to_open_write_file") + } + hdr := make([]byte, dataHeaderLen) binary.LittleEndian.PutUint32(hdr, uint32(len(data))) if _, err := c.wfd.Write(hdr); err != nil { - return WrapFileOperationError(OpWrite, err, c.path, c.wfd.Name()). + return WrapFileOperationError(OpWrite, err, c.path, c.writeFileName()). WithDetails("failed_to_write_header") } if _, err := c.wfd.Write(data); err != nil { - return WrapFileOperationError(OpWrite, err, c.path, c.wfd.Name()). + return WrapFileOperationError(OpWrite, err, c.path, c.writeFileName()). WithDetails("failed_to_write_data") } if !c.noSync { if err := c.wfd.Sync(); err != nil { - return WrapFileOperationError(OpSync, err, c.path, c.wfd.Name()). + return WrapFileOperationError(OpSync, err, c.path, c.writeFileName()). WithDetails("failed_to_sync_write") } } @@ -116,15 +120,20 @@ func (c *DiskCache) StreamPut(r io.Reader, size int) error { fmt.Sprintf("size_exceeded: max=%d, actual=%d", c.maxDataSize, size)).WithPath(c.path) } + if err := c.ensureWriteFile(); err != nil { + return NewCacheError(OpStreamPut, err, "failed_to_open_write_file"). + WithPath(c.path) + } + if startOffset, err = c.wfd.Seek(0, io.SeekCurrent); err != nil { - return WrapFileOperationError(OpSeek, err, c.path, c.wfd.Name()). + return WrapFileOperationError(OpSeek, err, c.path, c.writeFileName()). WithDetails("failed_to_get_current_position") } defer func() { if total > 0 && err != nil { // fallback to origin position if _, serr := c.wfd.Seek(startOffset, io.SeekStart); serr != nil { - c.LastErr = WrapFileOperationError(OpSeek, serr, c.path, c.wfd.Name()). + c.LastErr = WrapFileOperationError(OpSeek, serr, c.path, c.writeFileName()). WithDetails(fmt.Sprintf("failed_to_fallback_to_position_%d", startOffset)) } } @@ -135,7 +144,7 @@ func (c *DiskCache) StreamPut(r io.Reader, size int) error { if size > 0 { binary.LittleEndian.PutUint32(c.batchHeader, uint32(size)) if _, err := c.wfd.Write(c.batchHeader); err != nil { - return WrapFileOperationError(OpWrite, err, c.path, c.wfd.Name()). + return WrapFileOperationError(OpWrite, err, c.path, c.writeFileName()). WithDetails("failed_to_write_stream_header") } } diff --git a/diskcache/rotate.go b/diskcache/rotate.go index 6637e46a..1473525e 100644 --- a/diskcache/rotate.go +++ b/diskcache/rotate.go @@ -34,10 +34,16 @@ func (c *DiskCache) rotate() error { datafilesVec.WithLabelValues(c.path).Set(float64(len(c.dataFiles))) }() + if err := c.ensureWriteFile(); err != nil { + return NewCacheError(OpRotate, err, "failed_to_open_write_file_before_rotate"). + WithPath(c.path) + } + + batchSizeBeforeEOF := c.curBatchSize eof := make([]byte, dataHeaderLen) binary.LittleEndian.PutUint32(eof, EOFHint) if _, err := c.wfd.Write(eof); err != nil { // append EOF to file end - return WrapFileOperationError(OpWrite, err, c.path, c.wfd.Name()). + return WrapFileOperationError(OpWrite, err, c.path, c.writeFileName()). WithDetails("failed_to_write_eof_marker_during_rotate") } @@ -69,39 +75,61 @@ func (c *DiskCache) rotate() error { // close current writing file if err := c.wfd.Close(); err != nil { - return WrapFileOperationError(OpClose, err, c.path, c.wfd.Name()). + return WrapFileOperationError(OpClose, err, c.path, c.writeFileName()). WithDetails("failed_to_close_write_file_during_rotate") } c.wfd = nil // rename data -> data.0004 + renamed := true + var rotateErr error if err := os.Rename(c.curWriteFile, newfile); err != nil { - return WrapRotateError(err, c.path, c.curWriteFile, newfile). + renamed = false + rotateErr = WrapRotateError(err, c.path, c.curWriteFile, newfile). WithDetails("failed_to_rename_file_during_rotate") + + if fi, statErr := os.Stat(c.curWriteFile); statErr == nil && !fi.IsDir() { + if truncErr := os.Truncate(c.curWriteFile, batchSizeBeforeEOF); truncErr != nil { + rotateErr = NewCacheError(OpRotate, rotateErr, + fmt.Sprintf("failed_to_restore_write_file_size_after_rename_failure: size=%d, error=%v", + batchSizeBeforeEOF, truncErr)). + WithPath(c.path) + } + } } // new file added, add it's size to cache size - if fi, err := os.Stat(newfile); err == nil { - if fi.Size() > dataHeaderLen { - c.size.Add(fi.Size()) - sizeVec.WithLabelValues(c.path).Add(float64(fi.Size())) - putBytesVec.WithLabelValues(c.path).Observe(float64(fi.Size())) + if renamed { + if fi, err := os.Stat(newfile); err == nil { + if fi.Size() > dataHeaderLen { + c.size.Add(fi.Size()) + sizeVec.WithLabelValues(c.path).Add(float64(fi.Size())) + putBytesVec.WithLabelValues(c.path).Observe(float64(fi.Size())) + } + } else { + // Non-critical error: log but don't fail rotation + l.Warnf("failed to stat rotated file %s: %v", newfile, err) } + + c.dataFiles = append(c.dataFiles, newfile) + sort.Strings(c.dataFiles) } else { - // Non-critical error: log but don't fail rotation - l.Warnf("failed to stat rotated file %s: %v", newfile, err) + l.Errorf("%s", rotateErr) } - c.dataFiles = append(c.dataFiles, newfile) - sort.Strings(c.dataFiles) - // reopen new write file if err := c.openWriteFile(); err != nil { + if rotateErr != nil { + return NewCacheError(OpRotate, rotateErr, + fmt.Sprintf("failed_to_open_write_file_after_rotate_error: %v", err)). + WithPath(c.path) + } + return NewCacheError(OpRotate, err, "failed_to_open_new_write_file"). WithPath(c.path) } - return nil + return rotateErr } // after file read on EOF, remove the file. @@ -116,7 +144,7 @@ func (c *DiskCache) removeCurrentReadingFile() error { if c.rfd != nil { if err := c.rfd.Close(); err != nil { - return WrapFileOperationError(OpClose, err, c.path, c.rfd.Name()). + return WrapFileOperationError(OpClose, err, c.path, c.readFileName()). WithDetails("failed_to_close_read_file_during_removal") } c.rfd = nil diff --git a/diskcache/rotate_test.go b/diskcache/rotate_test.go index e852ef88..f1ac22db 100644 --- a/diskcache/rotate_test.go +++ b/diskcache/rotate_test.go @@ -8,6 +8,9 @@ package diskcache import ( "bytes" "errors" + "os" + "path/filepath" + "runtime" T "testing" "github.com/stretchr/testify/assert" @@ -122,3 +125,62 @@ func TestRotate(t *T.T) { }) }) } + +func TestRotateRecoverWriteFileOnRenameFailure(t *T.T) { + if runtime.GOOS == "windows" { + t.Skip("removing an open file is not supported on windows") + } + + p := t.TempDir() + c, err := Open(WithPath(p), WithBatchSize(1024*1024)) + require.NoError(t, err) + t.Cleanup(func() { + assert.NoError(t, c.Close()) + ResetMetrics() + }) + + require.NoError(t, c.Put([]byte("before"))) + require.NoError(t, os.Remove(c.curWriteFile)) + + require.Error(t, c.Rotate()) + require.NotNil(t, c.wfd) + require.NoError(t, c.Put([]byte("after"))) +} + +func TestRotateTruncateEOFOnRenameFailure(t *T.T) { + p := t.TempDir() + c, err := Open(WithPath(p), WithBatchSize(1024*1024)) + require.NoError(t, err) + t.Cleanup(func() { + assert.NoError(t, c.Close()) + ResetMetrics() + }) + + newfile := filepath.Join(p, "data.00000000000000000000000000000000") + require.NoError(t, os.Mkdir(newfile, 0o755)) + + before := []byte("before") + after := []byte("after") + + require.NoError(t, c.Put(before)) + require.Error(t, c.Rotate()) + require.NotNil(t, c.wfd) + + require.NoError(t, os.Remove(newfile)) + require.NoError(t, c.Put(after)) + require.NoError(t, c.Rotate()) + + var got [][]byte + for { + err := c.Get(func(data []byte) error { + got = append(got, append([]byte(nil), data...)) + return nil + }) + if errors.Is(err, ErrNoData) { + break + } + require.NoError(t, err) + } + + require.Equal(t, [][]byte{before, after}, got) +} diff --git a/diskcache/write_file.go b/diskcache/write_file.go new file mode 100644 index 00000000..378c4081 --- /dev/null +++ b/diskcache/write_file.go @@ -0,0 +1,45 @@ +// Unless explicitly stated otherwise all files in this repository are licensed +// under the MIT License. +// This product includes software developed at Guance Cloud (https://www.guance.com/). +// Copyright 2021-present Guance, Inc. + +package diskcache + +import "os" + +func (c *DiskCache) writeFileName() string { + if c.wfd != nil { + return c.wfd.Name() + } + + if c.curWriteFile != "" { + return c.curWriteFile + } + + return c.path +} + +func (c *DiskCache) readFileName() string { + if c.rfd != nil { + return c.rfd.Name() + } + + if c.curReadfile != "" { + return c.curReadfile + } + + return c.path +} + +func (c *DiskCache) ensureWriteFile() error { + if c.wfd != nil { + return nil + } + + if c.curWriteFile == "" { + return WrapFileOperationError(OpCreate, os.ErrInvalid, c.path, c.writeFileName()). + WithDetails("write_file_path_not_set") + } + + return c.openWriteFile() +} From 85ba694d5e5a3c5740a7379b9e1aac2b653688e0 Mon Sep 17 00:00:00 2001 From: songlq <31207055+songlonqi-java@users.noreply.github.com> Date: Tue, 30 Jun 2026 14:05:53 +0800 Subject: [PATCH 3/5] fix diskcache --- diskcache/write_file.go | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/diskcache/write_file.go b/diskcache/write_file.go index 378c4081..e1c75333 100644 --- a/diskcache/write_file.go +++ b/diskcache/write_file.go @@ -12,11 +12,7 @@ func (c *DiskCache) writeFileName() string { return c.wfd.Name() } - if c.curWriteFile != "" { - return c.curWriteFile - } - - return c.path + return c.curWriteFile } func (c *DiskCache) readFileName() string { @@ -24,11 +20,7 @@ func (c *DiskCache) readFileName() string { return c.rfd.Name() } - if c.curReadfile != "" { - return c.curReadfile - } - - return c.path + return c.curReadfile } func (c *DiskCache) ensureWriteFile() error { From 7433abad9926f09a658ef37d439f895f8b4307b3 Mon Sep 17 00:00:00 2001 From: songlq <31207055+songlonqi-java@users.noreply.github.com> Date: Fri, 10 Jul 2026 14:21:44 +0800 Subject: [PATCH 4/5] fix otel --- otlp/traces_parse.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/otlp/traces_parse.go b/otlp/traces_parse.go index 8e85ee47..0cf97213 100644 --- a/otlp/traces_parse.go +++ b/otlp/traces_parse.go @@ -352,7 +352,10 @@ func TraceSourceTypeFromTags(tags point.KVs) string { } func DBHostFromAttributes(resourceAttrs, spanAttrs map[string]string) string { - if resourceAttrs["db.system"] == "" && spanAttrs["db.system"] == "" { + if resourceAttrs["db.system"] == "" && + resourceAttrs["db.system.name"] == "" && + spanAttrs["db.system"] == "" && + spanAttrs["db.system.name"] == "" { return "" } if host := spanAttrs["server.address"]; host != "" { From e0f82d3e65b5b246c307323ef100564eb91442c5 Mon Sep 17 00:00:00 2001 From: songlq <31207055+songlonqi-java@users.noreply.github.com> Date: Fri, 10 Jul 2026 17:33:26 +0800 Subject: [PATCH 5/5] make lint --- aggregate/aggr_config.go | 7 +- aggregate/aggr_test.go | 2 +- aggregate/algo_count_distinct.go | 2 +- aggregate/business_coverage_more_test.go | 2 +- aggregate/derived_metric_collector.go | 5 +- aggregate/tail-sampling_test.go | 66 +++++----- aggregate/timewheel.go | 8 +- aggregate/timewheel_benchmark_test.go | 6 +- aggregate/timewheel_test.go | 8 +- aggregate/tsdata_v2_test.go | 6 +- aggregate/windows.go | 2 +- aggregate/windows_test.go | 4 +- cmd/diskcache/main.go | 7 +- dialtesting/browser.go | 18 +-- dialtesting/browser_test.go | 2 +- dialtesting/browserdial/chrome/chrome.go | 4 +- dialtesting/browserdial/runner/runner.go | 18 +-- dialtesting/grpc.go | 15 +-- dialtesting/grpc_script.go | 2 +- dialtesting/http.go | 11 +- dialtesting/http_script.go | 4 +- dialtesting/http_script_test.go | 2 +- dialtesting/icmp.go | 11 +- dialtesting/multi.go | 33 ++--- dialtesting/multi_test.go | 10 +- dialtesting/ssl.go | 11 +- dialtesting/ssl_test.go | 10 +- dialtesting/task.go | 6 +- dialtesting/tcp.go | 11 +- dialtesting/traceroute.go | 4 +- dialtesting/traceroute_other.go | 5 +- dialtesting/websocket.go | 11 +- diskcache/drop_test.go | 6 +- diskcache/errors.go | 4 +- diskcache/get_test.go | 22 ++-- diskcache/lock_contention_test.go | 24 ++-- diskcache/metric_test.go | 13 +- diskcache/open_test.go | 6 +- diskcache/put_test.go | 8 +- diskcache/rotate_test.go | 2 +- filter/ast.go | 32 ++--- filter/eval.go | 10 +- filter/eval_test.go | 58 ++++----- filter/lex.go | 4 +- filter/parse.go | 8 +- filter/parse_test.go | 2 +- lineproto/lineproto.go | 27 ++-- lineproto/lineproto_test.go | 151 ++++++++++++----------- lineproto/lp.go | 15 +-- lineproto/lp_test.go | 46 +++---- logger/logger_ctx.go | 68 +++++----- logger/logger_test.go | 12 +- logger/tcpudp_test.go | 10 +- metrics/http_test.go | 2 +- metrics/metrics_test.go | 6 +- network/http/api_metric.go | 2 +- network/http/api_wrap.go | 4 +- network/http/api_wrap_test.go | 10 +- network/http/err.go | 20 +-- network/http/err_test.go | 8 +- network/http/gin.go | 10 +- network/ws/epoll_linux.go | 3 +- network/ws/ws.go | 12 +- network/ws/ws_test.go | 8 +- oss.go | 6 +- otlp/string.go | 5 +- pkg/hash/fnv1a.go | 4 +- pkg/hash/fnv1a_test.go | 7 +- point/category_test.go | 9 +- point/check_test.go | 64 +++++----- point/decode_test.go | 8 +- point/encode_test.go | 14 +-- point/equal.go | 9 +- point/equal_test.go | 68 +++++----- point/json.go | 2 +- point/json_test.go | 6 +- point/kvs.go | 4 +- point/kvs_test.go | 6 +- point/lp_test.go | 140 ++++++++++----------- point/new_point_test.go | 64 +++++----- point/pbpoint_test.go | 14 +-- point/point_test.go | 40 +++--- point/ptpool_test.go | 10 +- point/rand.go | 2 +- point/rand_deprecated.go | 6 +- prom2metrics.go | 6 +- testutil/http.go | 2 +- testutil/http_test.go | 9 +- testutil/t.go | 8 +- tracer/option.go | 2 +- tracer/tracer.go | 10 +- utils.go | 8 +- whitelist.go | 4 +- 93 files changed, 692 insertions(+), 771 deletions(-) diff --git a/aggregate/aggr_config.go b/aggregate/aggr_config.go index 7ef97fc5..967fbae7 100644 --- a/aggregate/aggr_config.go +++ b/aggregate/aggr_config.go @@ -3,6 +3,7 @@ package aggregate import ( "encoding/json" "fmt" + "maps" "sort" "strings" "time" @@ -23,7 +24,7 @@ type AggregatorConfigure struct { calcs map[uint64]Calculator } -func (ac *AggregatorConfigure) UnmarshalTOML(data interface{}) error { +func (ac *AggregatorConfigure) UnmarshalTOML(data any) error { type rawAggregatorConfigure struct { DefaultWindow any `json:"default_window"` AggregateRules []*AggregateRule `json:"aggregate_rules"` @@ -143,9 +144,7 @@ func (cfg *AggregationAlgoConfig) ToAggregationAlgo() *AggregationAlgo { if len(cfg.AddTags) > 0 { algo.AddTags = make(map[string]string, len(cfg.AddTags)) - for k, v := range cfg.AddTags { - algo.AddTags[k] = v - } + maps.Copy(algo.AddTags, cfg.AddTags) } switch { diff --git a/aggregate/aggr_test.go b/aggregate/aggr_test.go index 7e1222a3..60ea60b3 100644 --- a/aggregate/aggr_test.go +++ b/aggregate/aggr_test.go @@ -14,7 +14,7 @@ import ( func otelHistograms(n int) []*point.Point { pts := make([]*point.Point, 0) - for i := 0; i < n; i++ { + for i := range n { var kvs point.KVs kvs = kvs.AddTag("service", "tmall"). AddTag("agent_version", "1.30"). diff --git a/aggregate/algo_count_distinct.go b/aggregate/algo_count_distinct.go index cbfe333b..8aa569b3 100644 --- a/aggregate/algo_count_distinct.go +++ b/aggregate/algo_count_distinct.go @@ -141,7 +141,7 @@ func (c *algoCountDistinct) count() int64 { if word == ^uint64(0) { continue } - for bit := 0; bit < 64; bit++ { + for bit := range 64 { if word&(uint64(1)<= len(dataBuf) { - n = len(dataBuf) - } + n := min( + // in KB + (sampleMin+(r.Int()%sampleMax))*1024, len(dataBuf)) start := r.Int() % len(dataBuf) diff --git a/dialtesting/browser.go b/dialtesting/browser.go index 379c6eb4..0cc95447 100644 --- a/dialtesting/browser.go +++ b/dialtesting/browser.go @@ -10,6 +10,7 @@ import ( "encoding/json" "errors" "fmt" + "maps" "os" "strings" "sync" @@ -545,7 +546,7 @@ func (t *BrowserTask) checkResult() (reasons []string, succFlag bool) { return reasons, false } -func (t *BrowserTask) getResults() (tags map[string]string, fields map[string]interface{}) { +func (t *BrowserTask) getResults() (tags map[string]string, fields map[string]any) { cfg, _ := t.parseBrowserConfig() result := t.lastViewportResult() name := firstNonEmpty(t.result.Name, cfg.Name, t.Name) @@ -556,12 +557,8 @@ func (t *BrowserTask) getResults() (tags map[string]string, fields map[string]in "status": "FAIL", "browser_engine": t.effectiveEngine(), } - for k, v := range cfg.Tags { - tags[k] = v - } - for k, v := range t.Tags { - tags[k] = v - } + maps.Copy(tags, cfg.Tags) + maps.Copy(tags, t.Tags) if result.viewport.Width > 0 && result.viewport.Height > 0 { tags["viewport"] = fmt.Sprintf("%dx%d", result.viewport.Width, result.viewport.Height) } else if viewports := t.effectiveViewports(); len(viewports) > 0 { @@ -573,7 +570,7 @@ func (t *BrowserTask) getResults() (tags map[string]string, fields map[string]in if responseTime == 0 { responseTime = int64(t.duration) / 1000 } - fields = map[string]interface{}{ + fields = map[string]any{ "response_time": responseTime, "success": int64(-1), "last_step": int64(lastBrowserStep(t.result.Steps)), @@ -1079,10 +1076,7 @@ func (t *BrowserTask) retryInterval() time.Duration { if t.RetryOptions == nil || !t.RetryOptions.Enabled || t.RetryOptions.Count <= 0 { return 0 } - interval := t.RetryOptions.IntervalSec - if interval < 5 { - interval = 5 - } + interval := max(t.RetryOptions.IntervalSec, 5) if interval > 300 { interval = 300 } diff --git a/dialtesting/browser_test.go b/dialtesting/browser_test.go index df20a9e8..444d2162 100644 --- a/dialtesting/browser_test.go +++ b/dialtesting/browser_test.go @@ -995,7 +995,7 @@ func newBrowserTaskForTest() *BrowserTask { return task } -func mustJSON(t *testing.T, value interface{}) string { +func mustJSON(t *testing.T, value any) string { t.Helper() data, err := json.Marshal(value) require.NoError(t, err) diff --git a/dialtesting/browserdial/chrome/chrome.go b/dialtesting/browserdial/chrome/chrome.go index 34603344..b855adc0 100644 --- a/dialtesting/browserdial/chrome/chrome.go +++ b/dialtesting/browserdial/chrome/chrome.go @@ -495,8 +495,8 @@ func extractTraceID(rawURL string, headers ...network.Headers) string { func traceFromURL(rawURL string) string { for _, marker := range []string{"trace_id=", "traceid=", "traceId="} { - if index := strings.Index(rawURL, marker); index >= 0 { - value := rawURL[index+len(marker):] + if _, after, ok := strings.Cut(rawURL, marker); ok { + value := after if cut := strings.IndexAny(value, "&#"); cut >= 0 { value = value[:cut] } diff --git a/dialtesting/browserdial/runner/runner.go b/dialtesting/browserdial/runner/runner.go index a082ebd4..6ba76ebe 100644 --- a/dialtesting/browserdial/runner/runner.go +++ b/dialtesting/browserdial/runner/runner.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "maps" "os" "path/filepath" "strings" @@ -192,10 +193,7 @@ func runLoaded(ctx context.Context, loaded script.Script, options Options, runID if engineName == "lightpanda" && strings.TrimSpace(loaded.ProxyURL) != "" { logIgnoredOption(options, "proxy_url", "lightpanda does not support proxy_url") } - maxAttempts := options.RetryCount + 1 - if maxAttempts < 1 { - maxAttempts = 1 - } + maxAttempts := max(options.RetryCount+1, 1) var last Result retryRecords := make([]evidence.RetryRecord, 0, maxAttempts) @@ -386,9 +384,7 @@ func isZeroPerformance(metrics evidence.PerformanceMetrics) bool { func browserConfig(loaded script.Script, options Options, vars map[string]string) (BrowserConfig, error) { headers := cloneStringMap(loaded.Headers) - for key, value := range options.Headers { - headers[key] = value - } + maps.Copy(headers, options.Headers) cookies := make([]BrowserCookie, 0, len(loaded.Cookies)+len(options.Cookies)) for _, cookie := range append(append([]script.Cookie{}, loaded.Cookies...), options.Cookies...) { value := cookie.Value @@ -817,17 +813,13 @@ func classifyFailureType(err error, steps []evidence.StepResult, failReason stri func mergeTags(first map[string]string, second map[string]string) map[string]string { out := cloneStringMap(first) - for key, value := range second { - out[key] = value - } + maps.Copy(out, second) return out } func cloneStringMap(input map[string]string) map[string]string { out := map[string]string{} - for key, value := range input { - out[key] = value - } + maps.Copy(out, input) return out } diff --git a/dialtesting/grpc.go b/dialtesting/grpc.go index 8020274a..eb992cdc 100644 --- a/dialtesting/grpc.go +++ b/dialtesting/grpc.go @@ -13,6 +13,7 @@ import ( "encoding/json" "errors" "fmt" + "maps" "net" "strings" "text/template" @@ -451,9 +452,7 @@ func (t *GRPCTask) findMethodAmongProtofiles() (*pdesc.MethodDescriptor, error) func buildExtendedProtoMap(protoFiles map[string]string) (map[string]string, error) { extendedMap := make(map[string]string, len(protoFiles)) - for k, v := range protoFiles { - extendedMap[k] = v - } + maps.Copy(extendedMap, protoFiles) var missingImports []string for _, content := range protoFiles { for _, imp := range extractImports(content) { @@ -733,7 +732,7 @@ func (t *GRPCTask) checkResult() ([]string, bool) { return reasons, succFlag } -func (t *GRPCTask) getResults() (tags map[string]string, fields map[string]interface{}) { +func (t *GRPCTask) getResults() (tags map[string]string, fields map[string]any) { tags = map[string]string{ "name": t.Name, "server": t.Server, @@ -742,7 +741,7 @@ func (t *GRPCTask) getResults() (tags map[string]string, fields map[string]inter "proto": "grpc", } - fields = map[string]interface{}{ + fields = map[string]any{ "response_time": int64(t.reqCost) / 1000, "success": int64(-1), } @@ -756,11 +755,9 @@ func (t *GRPCTask) getResults() (tags map[string]string, fields map[string]inter tags["dest_host"] = hostnames[0] } - for k, v := range t.Tags { - tags[k] = v - } + maps.Copy(tags, t.Tags) - message := map[string]interface{}{} + message := map[string]any{} reasons, succFlag := t.checkResult() diff --git a/dialtesting/grpc_script.go b/dialtesting/grpc_script.go index 92ee2377..ac3e69c4 100644 --- a/dialtesting/grpc_script.go +++ b/dialtesting/grpc_script.go @@ -102,7 +102,7 @@ func runPipelineGRPC(script string, response *ScriptGRPCRequestResponse, vars *V } messageString := string(messageBytes) - fileds := map[string]interface{}{ + fileds := map[string]any{ "message": messageString, } diff --git a/dialtesting/http.go b/dialtesting/http.go index 217e7044..923463df 100644 --- a/dialtesting/http.go +++ b/dialtesting/http.go @@ -18,6 +18,7 @@ import ( "encoding/json" "fmt" "io" + "maps" "mime/multipart" "net" "net/http" @@ -121,7 +122,7 @@ func (t *HTTPTask) metricName() string { return `http_dial_testing` } -func (t *HTTPTask) getResults() (tags map[string]string, fields map[string]interface{}) { +func (t *HTTPTask) getResults() (tags map[string]string, fields map[string]any) { tags = map[string]string{ "name": t.Name, "url": t.URL, @@ -140,7 +141,7 @@ func (t *HTTPTask) getResults() (tags map[string]string, fields map[string]inter tags["proto"] = t.req.Proto } - fields = map[string]interface{}{ + fields = map[string]any{ "response_time": int64(t.reqCost) / 1000, // unit: us "response_body_size": int64(len(t.respBody)), "success": int64(-1), @@ -152,11 +153,9 @@ func (t *HTTPTask) getResults() (tags map[string]string, fields map[string]inter tags["status_code_class"] = fmt.Sprintf(`%dxx`, t.resp.StatusCode/100) } - for k, v := range t.Tags { - tags[k] = v - } + maps.Copy(tags, t.Tags) - message := map[string]interface{}{ + message := map[string]any{ "request_body": t.reqBody, "request_header": t.reqHeader, } diff --git a/dialtesting/http_script.go b/dialtesting/http_script.go index b72270b6..b3c03c44 100644 --- a/dialtesting/http_script.go +++ b/dialtesting/http_script.go @@ -38,7 +38,7 @@ type ScriptHTTPResult struct { ErrorMessage string `json:"error_message"` } -type Vars map[string]interface{} +type Vars map[string]any type ScriptResult struct { Result ScriptHTTPResult `json:"result"` @@ -128,7 +128,7 @@ func runPipeline(script string, response *ScriptHTTPRequestResponse, vars *Vars) return nil, fmt.Errorf("message marshal failed: %w", err) } - fileds := map[string]interface{}{ + fileds := map[string]any{ "message": messageString, } diff --git a/dialtesting/http_script_test.go b/dialtesting/http_script_test.go index 3ff80a29..243f51db 100644 --- a/dialtesting/http_script_test.go +++ b/dialtesting/http_script_test.go @@ -41,6 +41,6 @@ func TestPostScriptDo(t *testing.T) { assert.True(t, result.Result.IsFailed) // nolint: unconvert - assert.True(t, reflect.DeepEqual([]interface{}([]interface{}{"value1", "value2"}), result.Vars["header"])) + assert.True(t, reflect.DeepEqual([]any([]any{"value1", "value2"}), result.Vars["header"])) assert.Equal(t, "token", result.Vars["token"]) } diff --git a/dialtesting/icmp.go b/dialtesting/icmp.go index 19cfac42..c1ccdc25 100644 --- a/dialtesting/icmp.go +++ b/dialtesting/icmp.go @@ -11,6 +11,7 @@ import ( "encoding/json" "errors" "fmt" + "maps" "math" "math/rand" "net" @@ -206,7 +207,7 @@ func (t *ICMPTask) checkResult() (reasons []string, succFlag bool) { return reasons, succFlag } -func (t *ICMPTask) getResults() (tags map[string]string, fields map[string]interface{}) { +func (t *ICMPTask) getResults() (tags map[string]string, fields map[string]any) { tags = map[string]string{ "name": t.Name, "dest_host": t.Host, @@ -214,7 +215,7 @@ func (t *ICMPTask) getResults() (tags map[string]string, fields map[string]inter "proto": "icmp", } - fields = map[string]interface{}{ + fields = map[string]any{ "average_round_trip_time_in_millis": t.round(t.avgRoundTripTime/1000, 3), "average_round_trip_time": t.avgRoundTripTime, "min_round_trip_time_in_millis": t.round(t.minRoundTripTime/1000, 3), @@ -229,9 +230,7 @@ func (t *ICMPTask) getResults() (tags map[string]string, fields map[string]inter "success": int64(-1), } - for k, v := range t.Tags { - tags[k] = v - } + maps.Copy(tags, t.Tags) if t.EnableTraceroute { fields["hops"] = 0 @@ -248,7 +247,7 @@ func (t *ICMPTask) getResults() (tags map[string]string, fields map[string]inter } } - message := map[string]interface{}{} + message := map[string]any{} reasons, succFlag := t.checkResult() if t.reqError != "" { diff --git a/dialtesting/multi.go b/dialtesting/multi.go index ca3a3add..e5bef622 100644 --- a/dialtesting/multi.go +++ b/dialtesting/multi.go @@ -9,6 +9,7 @@ import ( "encoding/json" "errors" "fmt" + "maps" "text/template" "time" ) @@ -44,7 +45,7 @@ type MultiStep struct { Value int `json:"value,omitempty"` // wait seconds for wait task ExtractedVars []MultiExtractedVar `json:"extracted_vars,omitempty"` - result map[string]interface{} + result map[string]any postScriptResult *ScriptResult } @@ -80,8 +81,8 @@ func (t *MultiTask) metricName() string { return `multi_dial_testing` } -func (t *MultiTask) getResults() (tags map[string]string, fields map[string]interface{}) { - fields = map[string]interface{}{ +func (t *MultiTask) getResults() (tags map[string]string, fields map[string]any) { + fields = map[string]any{ "success": -1, "response_time": int64(t.duration) / 1000, "last_step": t.lastStep, @@ -91,9 +92,7 @@ func (t *MultiTask) getResults() (tags map[string]string, fields map[string]inte "status": "FAIL", "name": t.Name, } - for k, v := range t.Tags { - tags[k] = v - } + maps.Copy(tags, t.Tags) if t.lastHTTPStep > -1 { step := t.Steps[t.lastHTTPStep] @@ -108,7 +107,7 @@ func (t *MultiTask) getResults() (tags map[string]string, fields map[string]inte } } - steps := []map[string]interface{}{} + steps := []map[string]any{} for _, s := range t.Steps { // extraced vars @@ -126,11 +125,9 @@ func (t *MultiTask) getResults() (tags map[string]string, fields map[string]inte extractedVars = append(extractedVars, ev) } - result := map[string]interface{}{} + result := map[string]any{} if s.result != nil { - for k, v := range s.result { - result[k] = v - } + maps.Copy(result, s.result) } result["extracted_vars"] = extractedVars steps = append(steps, result) @@ -142,14 +139,14 @@ func (t *MultiTask) getResults() (tags map[string]string, fields map[string]inte return tags, fields } -func (t *MultiTask) runHTTPStep(step *MultiStep) (map[string]interface{}, error) { +func (t *MultiTask) runHTTPStep(step *MultiStep) (map[string]any, error) { var err error var task ITask runCount := 0 maxCount := 1 interval := time.Millisecond - result := map[string]interface{}{} + result := map[string]any{} if step == nil { return nil, errors.New("step should not be nil") } @@ -193,9 +190,7 @@ func (t *MultiTask) runHTTPStep(step *MultiStep) (map[string]interface{}, error) for k, v := range tags { result[k] = v } - for k, v := range fields { - result[k] = v - } + maps.Copy(result, fields) } if httpTask.postScriptResult != nil { // set extracted vars @@ -251,7 +246,7 @@ func (t *MultiTask) run() error { isLastStepFailed := false for i, step := range t.Steps { - step.result = map[string]interface{}{ + step.result = map[string]any{ "type": step.Type, "allow_failure": step.AllowFailure, "task": step.TaskString, @@ -280,9 +275,7 @@ func (t *MultiTask) run() error { result, err := t.runHTTPStep(step) - for k, v := range result { - step.result[k] = v - } + maps.Copy(step.result, result) if err != nil { step.result["fail_reason"] = fmt.Sprintf("run task failed: %s", err.Error()) if !step.AllowFailure { diff --git a/dialtesting/multi_test.go b/dialtesting/multi_test.go index 682079c9..9ee1d7d9 100644 --- a/dialtesting/multi_test.go +++ b/dialtesting/multi_test.go @@ -72,7 +72,7 @@ type cs struct { Name string Task *MultiTask IsFailed bool - Check func(t assert.TestingT, tags map[string]string, fields map[string]interface{}) error + Check func(t assert.TestingT, tags map[string]string, fields map[string]any) error GlobalVars map[string]Variable } @@ -143,9 +143,9 @@ func makeCases(serverURL string) []cs { Value: "global_var_value", }, }, - Check: func(t assert.TestingT, tags map[string]string, fields map[string]interface{}) error { + Check: func(t assert.TestingT, tags map[string]string, fields map[string]any) error { assert.Equal(t, "FAIL", tags["status"]) - msg := map[string]interface{}{} + msg := map[string]any{} message, ok := fields["message"].(string) assert.True(t, ok) assert.NoError(t, json.Unmarshal([]byte(message), &msg)) @@ -195,7 +195,7 @@ func makeCases(serverURL string) []cs { }, } }(), - Check: func(t assert.TestingT, tags map[string]string, fields map[string]interface{}) error { + Check: func(t assert.TestingT, tags map[string]string, fields map[string]any) error { assert.Equal(t, "OK", tags["status"]) steps := []MultiStep{} str, ok := fields["steps"].(string) @@ -250,7 +250,7 @@ func makeCases(serverURL string) []cs { Value: "global_var_value", }, }, - Check: func(t assert.TestingT, tags map[string]string, fields map[string]interface{}) error { + Check: func(t assert.TestingT, tags map[string]string, fields map[string]any) error { assert.Equal(t, "OK", tags["status"]) vars := []ConfigVar{} configVarstring, ok := fields["config_vars"].(string) diff --git a/dialtesting/ssl.go b/dialtesting/ssl.go index 5bfb296f..6f097423 100644 --- a/dialtesting/ssl.go +++ b/dialtesting/ssl.go @@ -11,6 +11,7 @@ import ( "encoding/json" "errors" "fmt" + "maps" "net" "strings" "text/template" @@ -166,7 +167,7 @@ func (t *SSLTask) checkResult() (reasons []string, succFlag bool) { return reasons, succFlag } -func (t *SSLTask) getResults() (tags map[string]string, fields map[string]interface{}) { +func (t *SSLTask) getResults() (tags map[string]string, fields map[string]any) { tags = map[string]string{ "name": t.Name, "dest_host": t.Host, @@ -180,13 +181,11 @@ func (t *SSLTask) getResults() (tags map[string]string, fields map[string]interf tags["server_name"] = t.ServerName } - for k, v := range t.Tags { - tags[k] = v - } + maps.Copy(tags, t.Tags) responseTime := int64(t.reqCost) / 1000 tlsHandshakeTime := int64(t.tlsHandshakeCost) / 1000 - fields = map[string]interface{}{ + fields = map[string]any{ "response_time": responseTime, "tls_handshake_time": tlsHandshakeTime, "success": int64(-1), @@ -209,7 +208,7 @@ func (t *SSLTask) getResults() (tags map[string]string, fields map[string]interf fields["ssl_cert_issuer"] = t.certIssuer } - message := map[string]interface{}{} + message := map[string]any{} var ( reasons []string succFlag bool diff --git a/dialtesting/ssl_test.go b/dialtesting/ssl_test.go index 10e57925..27fe1288 100644 --- a/dialtesting/ssl_test.go +++ b/dialtesting/ssl_test.go @@ -360,7 +360,7 @@ func TestSSLTaskRenderTemplate(t *testing.T) { require.NoError(t, err) sslTask := task.(*SSLTask) - err = sslTask.renderTemplate(map[string]interface{}{ + err = sslTask.renderTemplate(map[string]any{ "host": func() string { return "example.org" }, "port": func() string { return "443" }, "server": func() string { return "sni.example.org" }, @@ -398,15 +398,15 @@ func TestSSLTaskRenderTemplate(t *testing.T) { assert.Equal(t, "Bad CA", sslTask.SuccessWhen[0].Issuer[1].NotContains) assert.Equal(t, "TLS1.3", sslTask.SuccessWhen[0].TLSVersion[0].Is) assert.Equal(t, "TLS1\\.0", sslTask.SuccessWhen[0].TLSVersion[1].NotMatchRegex) - require.NoError(t, sslTask.renderSuccessWhen(nil, map[string]interface{}{})) + require.NoError(t, sslTask.renderSuccessWhen(nil, map[string]any{})) sslTask.rawTask = nil sslTask.SetTaskJSONString("") - err = sslTask.renderTemplate(map[string]interface{}{}) + err = sslTask.renderTemplate(map[string]any{}) require.Error(t, err) assert.Contains(t, err.Error(), "new raw task failed") - err = (&SSLTask{Task: &Task{}, rawTask: nil}).renderTemplate(map[string]interface{}{}) + err = (&SSLTask{Task: &Task{}, rawTask: nil}).renderTemplate(map[string]any{}) require.Error(t, err) assert.Contains(t, err.Error(), "new raw task failed") @@ -419,7 +419,7 @@ func TestSSLTaskRenderTemplate(t *testing.T) { }, }) require.NoError(t, err) - err = badTask.(*SSLTask).renderTemplate(map[string]interface{}{}) + err = badTask.(*SSLTask).renderTemplate(map[string]any{}) require.Error(t, err) assert.Contains(t, err.Error(), "render response time failed") } diff --git a/dialtesting/task.go b/dialtesting/task.go index 4f7941ae..b6c6a99a 100644 --- a/dialtesting/task.go +++ b/dialtesting/task.go @@ -82,7 +82,7 @@ type TaskChild interface { run() error init() error checkResult() ([]string, bool) - getResults() (map[string]string, map[string]interface{}) + getResults() (map[string]string, map[string]any) stop() check() error clear() @@ -118,7 +118,7 @@ type ITask interface { Clear() CheckResult() ([]string, bool) Class() string - GetResults() (map[string]string, map[string]interface{}) + GetResults() (map[string]string, map[string]any) PostURLStr() string MetricName() string Stop() @@ -399,7 +399,7 @@ func (t *Task) SetIsTemplate(isTemplate bool) { t.isTemplate = isTemplate } -func (t *Task) GetResults() (tags map[string]string, fields map[string]interface{}) { +func (t *Task) GetResults() (tags map[string]string, fields map[string]any) { tags, fields = t.child.getResults() // add config_vars diff --git a/dialtesting/tcp.go b/dialtesting/tcp.go index 17154575..30cca89f 100644 --- a/dialtesting/tcp.go +++ b/dialtesting/tcp.go @@ -10,6 +10,7 @@ import ( "encoding/json" "errors" "fmt" + "maps" "net" "strings" "text/template" @@ -166,7 +167,7 @@ func (t *TCPTask) checkResult() (reasons []string, succFlag bool) { return reasons, succFlag } -func (t *TCPTask) getResults() (tags map[string]string, fields map[string]interface{}) { +func (t *TCPTask) getResults() (tags map[string]string, fields map[string]any) { tags = map[string]string{ "name": t.Name, "dest_host": t.Host, @@ -179,7 +180,7 @@ func (t *TCPTask) getResults() (tags map[string]string, fields map[string]interf responseTime := int64(t.reqCost) / 1000 // us responseTimeWithDNS := int64(t.reqCost+t.reqDNSCost) / 1000 // us - fields = map[string]interface{}{ + fields = map[string]any{ "response_time": responseTime, "response_time_with_dns": responseTimeWithDNS, "success": int64(-1), @@ -204,11 +205,9 @@ func (t *TCPTask) getResults() (tags map[string]string, fields map[string]interf } } - for k, v := range t.Tags { - tags[k] = v - } + maps.Copy(tags, t.Tags) - message := map[string]interface{}{} + message := map[string]any{} reasons, succFlag := t.checkResult() if t.reqError != "" { diff --git a/dialtesting/traceroute.go b/dialtesting/traceroute.go index ceddf32f..2f5cb51f 100644 --- a/dialtesting/traceroute.go +++ b/dialtesting/traceroute.go @@ -88,7 +88,7 @@ func mean(v []float64) float64 { return 0 } - for i := 0; i < n; i++ { + for i := range n { res += v[i] } return res / float64(n) @@ -98,7 +98,7 @@ func variance(v []float64) float64 { var res float64 = 0 m := mean(v) var n int = len(v) - for i := 0; i < n; i++ { + for i := range n { res += (v[i] - m) * (v[i] - m) } if n <= 1 { diff --git a/dialtesting/traceroute_other.go b/dialtesting/traceroute_other.go index dfcee670..b1554a7f 100644 --- a/dialtesting/traceroute_other.go +++ b/dialtesting/traceroute_other.go @@ -4,7 +4,6 @@ // Copyright 2021-present Guance, Inc. //go:build !windows -// +build !windows package dialtesting @@ -38,7 +37,7 @@ type Traceroute struct { routes []*Route response chan *Response - stopCh chan interface{} + stopCh chan any packetCh chan *Packet receivePacketsCh chan *receivePacket id uint32 @@ -67,7 +66,7 @@ func (t *Traceroute) init() { t.routes = make([]*Route, 0) t.response = make(chan *Response) - t.stopCh = make(chan interface{}) + t.stopCh = make(chan any) t.packetCh = make(chan *Packet) t.receivePacketsCh = make(chan *receivePacket, 5000) diff --git a/dialtesting/websocket.go b/dialtesting/websocket.go index 8c69db45..0533953a 100644 --- a/dialtesting/websocket.go +++ b/dialtesting/websocket.go @@ -12,6 +12,7 @@ import ( "encoding/json" "errors" "fmt" + "maps" "net" "net/http" "net/url" @@ -196,7 +197,7 @@ func (t *WebsocketTask) checkResult() (reasons []string, succFlag bool) { return reasons, succFlag } -func (t *WebsocketTask) getResults() (tags map[string]string, fields map[string]interface{}) { +func (t *WebsocketTask) getResults() (tags map[string]string, fields map[string]any) { tags = map[string]string{ "name": t.Name, "url": t.URL, @@ -207,7 +208,7 @@ func (t *WebsocketTask) getResults() (tags map[string]string, fields map[string] responseTime := int64(t.reqCost+t.reqDNSCost) / 1000 // us responseTimeWithDNS := int64(t.reqCost+t.reqDNSCost) / 1000 // us - fields = map[string]interface{}{ + fields = map[string]any{ "response_time": responseTime, "response_time_with_dns": responseTimeWithDNS, "response_message": t.responseMessage, @@ -221,11 +222,9 @@ func (t *WebsocketTask) getResults() (tags map[string]string, fields map[string] fields[`ssl_cert_expires_in_days`] = (t.sslCertNotAfter - time.Now().UnixMicro()) / (24 * time.Hour).Microseconds() } - for k, v := range t.Tags { - tags[k] = v - } + maps.Copy(tags, t.Tags) - message := map[string]interface{}{} + message := map[string]any{} reasons, succFlag := t.checkResult() if t.reqError != "" { diff --git a/diskcache/drop_test.go b/diskcache/drop_test.go index 892cfdd8..e7a8bfad 100644 --- a/diskcache/drop_test.go +++ b/diskcache/drop_test.go @@ -77,9 +77,7 @@ func TestDropDuringGet(t *T.T) { sample := bytes.Repeat([]byte("hello"), 7351) wg := sync.WaitGroup{} - wg.Add(1) - go func() { // fast put - defer wg.Done() + wg.Go(func() { // fast put n := 0 for { if err := c.Put(sample); err != nil { @@ -92,7 +90,7 @@ func TestDropDuringGet(t *T.T) { return } } - }() + }) time.Sleep(time.Second) // wait new data write diff --git a/diskcache/errors.go b/diskcache/errors.go index df47db4a..8ccacb75 100644 --- a/diskcache/errors.go +++ b/diskcache/errors.go @@ -223,8 +223,8 @@ func isTemporaryError(err error) bool { } // GetErrorContext extracts useful context information from errors. -func GetErrorContext(err error) map[string]interface{} { - context := make(map[string]interface{}) +func GetErrorContext(err error) map[string]any { + context := make(map[string]any) if err == nil { return context diff --git a/diskcache/get_test.go b/diskcache/get_test.go index 99ac1552..e2a69f7c 100644 --- a/diskcache/get_test.go +++ b/diskcache/get_test.go @@ -48,7 +48,7 @@ func TestGetPut(t *T.T) { ok := false - for i := 0; i < 10; i++ { + for range 10 { if err := dq.BufGet(buf, func(msg []byte) error { t.Logf("msg: %p/%d", msg, len(msg)) t.Logf("msg: %p/%d", buf, len(buf)) @@ -98,7 +98,7 @@ func TestGetPut(t *T.T) { ok := false - for i := 0; i < 10; i++ { + for range 10 { if err := dq.Get(func(msg []byte) error { t.Logf("get message: %q\n", string(msg)) ok = true @@ -125,7 +125,7 @@ func TestDropInvalidDataFile(t *T.T) { // put some data and rotate 10 datafiles data := make([]byte, 100) - for i := 0; i < 10; i++ { + for i := range 10 { assert.NoError(t, c.Put(data)) assert.NoError(t, c.rotate()) @@ -410,7 +410,7 @@ func TestPutGet(t *T.T) { c, err := Open(WithPath(p), WithNoPos(true)) assert.NoError(t, err) - for i := 0; i < 10; i++ { // write 10kb + for range 10 { // write 10kb require.NoError(t, c.Put(kbdata), "cache: %s", c) } @@ -470,7 +470,7 @@ func TestPutGet(t *T.T) { assert.NoError(t, err) - for i := 0; i < ndata; i++ { // write 10 data + for range ndata { // write 10 data require.NoError(t, c.Put(testData), "cache: %s", c) } @@ -535,7 +535,7 @@ func TestDelayPosDump(t *T.T) { testData := []byte("0123456789") ndata := 10 - for i := 0; i < ndata; i++ { // write 10 data + for range ndata { // write 10 data require.NoError(t, c.Put(testData), "cache: %s", c) } @@ -543,7 +543,7 @@ func TestDelayPosDump(t *T.T) { require.NoError(t, c.rotate()) // create n read pos - for i := 0; i < 6; i++ { + for range 6 { assert.NoError(t, c.Get(func(data []byte) error { assert.Len(t, data, len(testData)) return nil @@ -586,7 +586,7 @@ func TestDelayPosDump(t *T.T) { testData := []byte("0123456789") ndata := 10 - for i := 0; i < ndata; i++ { // write 10 data + for range ndata { // write 10 data require.NoError(t, c.Put(testData), "cache: %s", c) } @@ -594,7 +594,7 @@ func TestDelayPosDump(t *T.T) { require.NoError(t, c.rotate()) // create n read pos - for i := 0; i < 3; i++ { + for range 3 { assert.NoError(t, c.Get(func(data []byte) error { assert.Len(t, data, len(testData)) return nil @@ -639,7 +639,7 @@ func TestDelayPosDump(t *T.T) { testData := []byte("0123456789") ndata := 10 - for i := 0; i < ndata; i++ { // write 10 data + for range ndata { // write 10 data require.NoError(t, c.Put(testData), "cache: %s", c) } @@ -647,7 +647,7 @@ func TestDelayPosDump(t *T.T) { require.NoError(t, c.rotate()) // create n read pos - for i := 0; i < 3; i++ { + for range 3 { assert.NoError(t, c.Get(func(data []byte) error { assert.Len(t, data, len(testData)) return nil diff --git a/diskcache/lock_contention_test.go b/diskcache/lock_contention_test.go index 5f381b24..2ef9df81 100644 --- a/diskcache/lock_contention_test.go +++ b/diskcache/lock_contention_test.go @@ -31,9 +31,7 @@ func TestLockContention(t *testing.T) { contentionStarted := make(chan bool, 1) // First goroutine holds lock - wg.Add(1) - go func() { - defer wg.Done() + wg.Go(func() { mu.Lock() defer mu.Unlock() @@ -42,15 +40,13 @@ func TestLockContention(t *testing.T) { // Hold lock for a bit time.Sleep(50 * time.Millisecond) - }() + }) // Wait for first lock to be acquired <-contentionStarted // Second goroutine tries to lock (will contend) - wg.Add(1) - go func() { - defer wg.Done() + wg.Go(func() { start := time.Now() mu.Lock() // This should wait due to contention duration := time.Since(start) @@ -60,7 +56,7 @@ func TestLockContention(t *testing.T) { if duration < 10*time.Millisecond { t.Errorf("Contented lock didn't wait enough: %v", duration) } - }() + }) wg.Wait() @@ -117,21 +113,17 @@ func TestInstrumentedRWMutex(t *testing.T) { readStarted := make(chan bool, 1) // Start read lock holder - wg.Add(1) - go func() { - defer wg.Done() + wg.Go(func() { rwmu.RLock() defer rwmu.RUnlock() readStarted <- true time.Sleep(30 * time.Millisecond) - }() + }) <-readStarted // Try to get write lock while read is held - wg.Add(1) - go func() { - defer wg.Done() + wg.Go(func() { start := time.Now() rwmu.Lock() // Should wait for read to complete duration := time.Since(start) @@ -140,7 +132,7 @@ func TestInstrumentedRWMutex(t *testing.T) { if duration < 10*time.Millisecond { t.Errorf("Write lock during read didn't wait enough: %v", duration) } - }() + }) wg.Wait() } diff --git a/diskcache/metric_test.go b/diskcache/metric_test.go index 792d2667..cb889fb5 100644 --- a/diskcache/metric_test.go +++ b/diskcache/metric_test.go @@ -23,10 +23,7 @@ func getSamples(data []byte) []byte { r := rand.New(rand.NewSource(time.Now().UnixNano())) // at least 1/10 of data - n := (len(data)/10 + (r.Int() % len(data))) - if n >= len(data) { - n = len(data) - } + n := min((len(data)/10 + (r.Int() % len(data))), len(data)) start := r.Int() % len(data) @@ -87,7 +84,7 @@ func TestPutGetMetrics(t *T.T) { data := make([]byte, bsize/2) - for i := 0; i < 10; i++ { + for range 10 { c.Put(data) } @@ -243,7 +240,7 @@ func TestPerfConcurrentPutGet(t *T.T) { // 10 worker wg := sync.WaitGroup{} wg.Add(10) - for i := 0; i < 10; i++ { + for range 10 { go func() { defer wg.Done() for { @@ -285,7 +282,7 @@ func TestPerfConcurrentPutGet(t *T.T) { // put/get each 10 worker wg := sync.WaitGroup{} wg.Add(20) - for i := 0; i < 10; i++ { + for range 10 { go func() { defer wg.Done() for { @@ -301,7 +298,7 @@ func TestPerfConcurrentPutGet(t *T.T) { }() } - for i := 0; i < 10; i++ { + for range 10 { go func() { defer wg.Done() for { diff --git a/diskcache/open_test.go b/diskcache/open_test.go index befd5ec3..4dc0ac24 100644 --- a/diskcache/open_test.go +++ b/diskcache/open_test.go @@ -131,13 +131,11 @@ func TestOpen(t *T.T) { var wg sync.WaitGroup - wg.Add(1) // hold the cache for 5 seconds - go func() { - defer wg.Done() + wg.Go(func() { time.Sleep(time.Second * 5) assert.NoError(t, c.Close()) - }() + }) c2, err := Open(WithPath(p), WithNoLock(true)) assert.NoError(t, err) // no error on re-Open() without lock diff --git a/diskcache/put_test.go b/diskcache/put_test.go index 5c3ac126..75d6cb27 100644 --- a/diskcache/put_test.go +++ b/diskcache/put_test.go @@ -198,7 +198,7 @@ func TestConcurrentPutGet(t *T.T) { assert.NoError(t, err) wg.Add(concurrency * 2) - for i := 0; i < concurrency; i++ { + for i := range concurrency { go fnPut(c, i) go fnGet(c, i) } @@ -230,13 +230,13 @@ func TestConcurrentPutGet(t *T.T) { assert.NoError(t, err) wg.Add(concurrency) - for i := 0; i < concurrency; i++ { + for i := range concurrency { go fnPut(c, i) } wg.Wait() // wait Put done: there should be drop(4*32K*1000) > 32M wg.Add(concurrency) - for i := 0; i < concurrency; i++ { + for i := range concurrency { go fnGet(c, i) } wg.Wait() @@ -354,7 +354,7 @@ func TestPutOnCapacityReached(t *T.T) { total := int64(0) wg.Add(10) - for i := 0; i < 10; i++ { + for range 10 { go func() { defer wg.Done() n := 0 diff --git a/diskcache/rotate_test.go b/diskcache/rotate_test.go index f1ac22db..e087aeaa 100644 --- a/diskcache/rotate_test.go +++ b/diskcache/rotate_test.go @@ -29,7 +29,7 @@ func TestRotate(t *T.T) { _1kb := make([]byte, 1024) // put 3kb - for i := 0; i < 4; i++ { + for range 4 { assert.NoError(t, c.Put(_1kb)) } diff --git a/filter/ast.go b/filter/ast.go index c819e192..2b07da36 100644 --- a/filter/ast.go +++ b/filter/ast.go @@ -172,9 +172,9 @@ func (n *OrderByElem) MarshalJSON() ([]byte, error) { switch n.Opt { case OrderAsc: - return []byte(fmt.Sprintf(output, b, "asc")), nil + return fmt.Appendf(nil, output, b, "asc"), nil case OrderDesc: - return []byte(fmt.Sprintf(output, b, "desc")), nil + return fmt.Appendf(nil, output, b, "desc"), nil } return []byte(`""`), nil } @@ -265,7 +265,7 @@ func getFuncArgList(nl NodeList) FuncArgList { // SearchAfter 深度分页. type SearchAfter struct { - Vals []interface{} `json:"vals,omitempty"` + Vals []any `json:"vals,omitempty"` } // Pos pos. @@ -299,22 +299,22 @@ func (n *Fill) MarshalJSON() ([]byte, error) { switch n.FillType { case FillNil: - return []byte(fmt.Sprintf(output1, Nil)), nil + return fmt.Appendf(nil, output1, Nil), nil case FillInt: - return []byte(fmt.Sprintf(output2, "integer", "integer_val", n.Int)), nil + return fmt.Appendf(nil, output2, "integer", "integer_val", n.Int), nil case FillFloat: - return []byte(fmt.Sprintf(output2, "float", "float_val", n.Float)), nil + return fmt.Appendf(nil, output2, "float", "float_val", n.Float), nil case FillStr: - return []byte(fmt.Sprintf(output2, "str", "str_val", fmt.Sprintf(`"%s"`, n.Str))), nil + return fmt.Appendf(nil, output2, "str", "str_val", fmt.Sprintf(`"%s"`, n.Str)), nil case FillLinear: - return []byte(fmt.Sprintf(output1, "linear")), nil + return fmt.Appendf(nil, output1, "linear"), nil case FillPrevious: - return []byte(fmt.Sprintf(output1, "previous")), nil + return fmt.Appendf(nil, output1, "previous"), nil } return []byte(`""`), nil @@ -654,7 +654,7 @@ type Regex struct { } func (e *Regex) MarshalJSON() ([]byte, error) { - return []byte(fmt.Sprintf(`"%s"`, e.String())), nil + return fmt.Appendf(nil, `"%s"`, e.String()), nil } func (e *Regex) Type() ValueType { return "" /* TODO */ } @@ -766,10 +766,10 @@ type StaticCast struct { func (e *StaticCast) MarshalJSON() ([]byte, error) { const res = `{"%s":"%s"}` if e.IsInt { - return []byte(fmt.Sprintf(res, "int", e.Val.String())), nil + return fmt.Appendf(nil, res, "int", e.Val.String()), nil } if e.IsFloat { - return []byte(fmt.Sprintf(res, "float", e.Val.String())), nil + return fmt.Appendf(nil, res, "float", e.Val.String()), nil } return nil, fmt.Errorf("unreachable") } @@ -798,10 +798,10 @@ type Statement interface { // OuterFunc outerFunc. type OuterFunc struct { - Func *FuncExpr `json:"func,omitempty"` - FuncArgVals []interface{} `json:"func_arg_vals,omitempty"` - FuncArgTypes []string `json:"func_arg_types,omitempty"` - FuncArgNames []string `json:"func_arg_names,omitempty"` + Func *FuncExpr `json:"func,omitempty"` + FuncArgVals []any `json:"func_arg_vals,omitempty"` + FuncArgTypes []string `json:"func_arg_types,omitempty"` + FuncArgNames []string `json:"func_arg_names,omitempty"` } type OuterFuncs struct { diff --git a/filter/eval.go b/filter/eval.go index b49644a1..60c33519 100644 --- a/filter/eval.go +++ b/filter/eval.go @@ -105,7 +105,7 @@ func almostEqual(a, b float64) bool { return math.Abs(a-b) <= float64EqualityThreshold } -func toFloat64(f interface{}) float64 { +func toFloat64(f any) float64 { switch v := f.(type) { case float32: return float64(v) @@ -117,7 +117,7 @@ func toFloat64(f interface{}) float64 { } } -func toInt64(i interface{}) int64 { +func toInt64(i any) int64 { switch v := i.(type) { case int: return int64(v) @@ -143,7 +143,7 @@ func toInt64(i interface{}) int64 { } } -func binEval(op ItemType, lhs, rhs interface{}) bool { +func binEval(op ItemType, lhs, rhs any) bool { if _, ok := rhs.(*Regex); ok { if _, isStr := lhs.(string); !isStr { log.Warnf("non-string(type %s) can not match with regexp", reflect.TypeOf(lhs)) @@ -302,8 +302,8 @@ func (e *BinaryExpr) singleEval(data KVs) bool { } // first: fetch right-handle-symbol - var lit interface{} - var arr []interface{} + var lit any + var arr []any switch rhs := e.RHS.(type) { case *StringLiteral: lit = rhs.Val diff --git a/filter/eval_test.go b/filter/eval_test.go index 14959359..30ee3e26 100644 --- a/filter/eval_test.go +++ b/filter/eval_test.go @@ -16,98 +16,98 @@ func TestExprConditions(t *testing.T) { in string source string tags map[string]string - fields map[string]interface{} + fields map[string]any pass bool }{ { in: "{ abc notmatch []}", - fields: map[string]interface{}{"abc": "abc123"}, + fields: map[string]any{"abc": "abc123"}, pass: false, }, { in: "{ abc match ['g(-z]+ng wrong regex']} # invalid regexp", - fields: map[string]interface{}{"abc": "abc123"}, + fields: map[string]any{"abc": "abc123"}, pass: false, }, { in: "{ abc match ['a.*']}", - fields: map[string]interface{}{"abc": "abc123"}, + fields: map[string]any{"abc": "abc123"}, pass: true, }, { in: "{ abc match ['a.*']}", - fields: map[string]interface{}{"abc": "abc123"}, + fields: map[string]any{"abc": "abc123"}, pass: true, }, { in: "{ source = re(`.*`) and (abc match ['a.*'])}", - fields: map[string]interface{}{"abc": "abc123"}, + fields: map[string]any{"abc": "abc123"}, tags: map[string]string{"source": "12345"}, pass: true, }, { in: "{ abc notmatch ['a.*'] or xyz match ['.*']}", - fields: map[string]interface{}{"abc": "abc123"}, + fields: map[string]any{"abc": "abc123"}, tags: map[string]string{"xyz": "def"}, pass: true, }, { in: "{abc notin [1.1,1.2,1.3] and (a > 1 || c< 0)}", - fields: map[string]interface{}{"abc": int64(4), "a": int64(-1), "c": int64(-2)}, + fields: map[string]any{"abc": int64(4), "a": int64(-1), "c": int64(-2)}, pass: true, }, { in: "{a notin [1,2,3,4]}", - fields: map[string]interface{}{"a": int64(4)}, + fields: map[string]any{"a": int64(4)}, pass: false, }, { in: "{abc notin [1,2,3]}", - fields: map[string]interface{}{"abc": int64(4)}, + fields: map[string]any{"abc": int64(4)}, pass: true, }, { in: ";;;{a > 1, b > 1 or c > 1, xx != 123 };;;; {xyz > 1};;;", - fields: map[string]interface{}{"a": int64(2), "c": "xyz"}, + fields: map[string]any{"a": int64(2), "c": "xyz"}, pass: false, }, { in: "{a > 1, b > 1 or c > 1}", - fields: map[string]interface{}{"a": int64(2), "c": "xyz"}, + fields: map[string]any{"a": int64(2), "c": "xyz"}, pass: false, }, { in: "{a > 1, b > 1 or c = 'xyz'}", - fields: map[string]interface{}{"a": int64(2), "c": "xyz", "b": false}, + fields: map[string]any{"a": int64(2), "c": "xyz", "b": false}, pass: true, }, { in: "{xxx < 111}; {a > 1, b > 1 or c = 'xyz'}", - fields: map[string]interface{}{"a": int64(2), "c": "xyz", "b": false}, + fields: map[string]any{"a": int64(2), "c": "xyz", "b": false}, pass: true, }, { in: `{host = re("^nginx_.*$")}`, - fields: map[string]interface{}{"host": "nginx_abc"}, + fields: map[string]any{"host": "nginx_abc"}, pass: true, }, { in: "{host = re(`nginx_*`)}", - fields: map[string]interface{}{"host": "abcdef"}, + fields: map[string]any{"host": "abcdef"}, pass: false, }, @@ -208,25 +208,25 @@ func TestExprConditions(t *testing.T) { { in: "{ abc = re(`nginx_*`)}", // abc is nil - fields: map[string]interface{}{"host": "abcdef"}, + fields: map[string]any{"host": "abcdef"}, pass: false, }, { in: "{ false = re(`nginx_*`)}", - fields: map[string]interface{}{"host": "abcdef"}, + fields: map[string]any{"host": "abcdef"}, pass: false, }, { in: "{ 123 = re(`nginx_*`)}", - fields: map[string]interface{}{"host": "abcdef"}, + fields: map[string]any{"host": "abcdef"}, pass: false, }, { in: "{ 3.14 = re(`nginx_*`)}", - fields: map[string]interface{}{"host": "abcdef"}, + fields: map[string]any{"host": "abcdef"}, pass: false, }, @@ -259,11 +259,11 @@ func TestConditions(t *testing.T) { cases := []struct { in WhereConditions tags map[string]string - fields map[string]interface{} + fields map[string]any pass bool }{ { // multi conditions - fields: map[string]interface{}{"a": int64(2), "c": "xyz"}, + fields: map[string]any{"a": int64(2), "c": "xyz"}, in: WhereConditions{ &WhereCondition{ conditions: []Node{ @@ -289,7 +289,7 @@ func TestConditions(t *testing.T) { }, { - fields: map[string]interface{}{"a": int64(2)}, + fields: map[string]any{"a": int64(2)}, in: WhereConditions{ &WhereCondition{ conditions: []Node{ @@ -306,7 +306,7 @@ func TestConditions(t *testing.T) { { pass: true, - fields: map[string]interface{}{"a": "abc"}, + fields: map[string]any{"a": "abc"}, in: WhereConditions{ &WhereCondition{ conditions: []Node{ @@ -388,8 +388,8 @@ func TestConditions(t *testing.T) { func TestBinEval(t *testing.T) { cases := []struct { op ItemType - lhs interface{} - rhs interface{} + lhs any + rhs any pass bool }{ { @@ -480,11 +480,11 @@ func TestEval(t *testing.T) { cases := []struct { cond *BinaryExpr tags map[string]string - fields map[string]interface{} + fields map[string]any pass bool }{ { - fields: map[string]interface{}{"a": int64(3)}, + fields: map[string]any{"a": int64(3)}, cond: &BinaryExpr{ Op: GTE, LHS: &Identifier{Name: "a"}, @@ -494,7 +494,7 @@ func TestEval(t *testing.T) { }, { - fields: map[string]interface{}{"a": float64(3.14)}, + fields: map[string]any{"a": float64(3.14)}, cond: &BinaryExpr{ Op: GT, LHS: &Identifier{Name: "a"}, diff --git a/filter/lex.go b/filter/lex.go index 915f7db9..f4047b70 100644 --- a/filter/lex.go +++ b/filter/lex.go @@ -50,7 +50,7 @@ func (i ItemType) IsKeyword() bool { return i > keywordsStart && i < keywordsEn type ItemType int func (i *ItemType) MarshalJSON() ([]byte, error) { - return []byte(fmt.Sprintf(`"%s"`, reflect.ValueOf(i))), nil + return fmt.Appendf(nil, `"%s"`, reflect.ValueOf(i)), nil } const ( @@ -549,7 +549,7 @@ func (l *Lexer) emit(t ItemType) { l.scannedItem = true } -func (l *Lexer) errorf(format string, args ...interface{}) stateFn { +func (l *Lexer) errorf(format string, args ...any) stateFn { *l.itemp = Item{ERROR, l.start, fmt.Sprintf(format, args...)} l.scannedItem = true diff --git a/filter/parse.go b/filter/parse.go index f122a81b..ccd1896e 100644 --- a/filter/parse.go +++ b/filter/parse.go @@ -22,7 +22,7 @@ import ( var ( parserPool = sync.Pool{ - New: func() interface{} { + New: func() any { return &parser{} }, } @@ -34,7 +34,7 @@ type parser struct { lex Lexer yyParser yyParserImpl - parseResult interface{} + parseResult any lastClosing Pos errs ParseErrors warns ParseErrors @@ -130,11 +130,11 @@ func (p *parser) addParseWarn(pr *PositionRange, err error) { }) } -func (p *parser) addParseErrf(pr *PositionRange, format string, args ...interface{}) { +func (p *parser) addParseErrf(pr *PositionRange, format string, args ...any) { p.addParseErr(pr, fmt.Errorf(format, args...)) } -func (p *parser) addParseWarnf(pr *PositionRange, format string, args ...interface{}) { +func (p *parser) addParseWarnf(pr *PositionRange, format string, args ...any) { p.addParseWarn(pr, fmt.Errorf(format, args...)) } diff --git a/filter/parse_test.go b/filter/parse_test.go index 167ce314..e1e51b14 100644 --- a/filter/parse_test.go +++ b/filter/parse_test.go @@ -15,7 +15,7 @@ import ( func TestParse(t *testing.T) { cases := []struct { in string - expected interface{} + expected any fail bool }{ { diff --git a/lineproto/lineproto.go b/lineproto/lineproto.go index 82e060c3..5659e12e 100644 --- a/lineproto/lineproto.go +++ b/lineproto/lineproto.go @@ -12,6 +12,7 @@ import ( "fmt" "math" "reflect" + "slices" "sort" "strings" "time" @@ -190,19 +191,15 @@ func NewDefaultOption() *Option { } func (opt *Option) checkDisabledField(f string) error { - for _, x := range opt.DisabledFieldKeys { - if f == x { - return fmt.Errorf("field key `%s' disabled", f) - } + if slices.Contains(opt.DisabledFieldKeys, f) { + return fmt.Errorf("field key `%s' disabled", f) } return nil } func (opt *Option) checkDisabledTag(t string) error { - for _, x := range opt.DisabledTagKeys { - if t == x { - return fmt.Errorf("tag key `%s' disabled", t) - } + if slices.Contains(opt.DisabledTagKeys, t) { + return fmt.Errorf("tag key `%s' disabled", t) } return nil } @@ -268,7 +265,7 @@ func ParsePoints(data []byte, opt *Option) ([]*influxdb.Point, error) { func MakeLineProtoPoint(name string, tags map[string]string, - fields map[string]interface{}, + fields map[string]any, opt *Option, ) (*influxdb.Point, error) { pt, _, err := MakeLineProtoPointWithWarnings(name, tags, fields, opt) @@ -277,7 +274,7 @@ func MakeLineProtoPoint(name string, func MakeLineProtoPointWithWarnings(name string, tags map[string]string, - fields map[string]interface{}, + fields map[string]any, opt *Option, ) (pt *influxdb.Point, warnings []*PointWarning, err error) { warnings = []*PointWarning{} @@ -334,7 +331,7 @@ func MakeLineProtoPointWithWarnings(name string, func MakeLineProtoPointV2(name string, tags map[string]string, - fields map[string]interface{}, + fields map[string]any, opt *Option, ) (*Point, error) { pt, _, err := MakeLineProtoPointWithWarningsV2(name, tags, fields, opt) @@ -343,7 +340,7 @@ func MakeLineProtoPointV2(name string, func MakeLineProtoPointWithWarningsV2(name string, tags map[string]string, - fields map[string]interface{}, + fields map[string]any, opt *Option, ) (pt *Point, warnings []*PointWarning, err error) { warnings = []*PointWarning{} @@ -496,7 +493,7 @@ func checkPointV2(p *Point, opt *Option) error { return nil } -func checkTagFieldSameKey(tags map[string]string, fields map[string]interface{}, warnings *[]*PointWarning) error { +func checkTagFieldSameKey(tags map[string]string, fields map[string]any, warnings *[]*PointWarning) error { if tags == nil || fields == nil { return nil } @@ -528,7 +525,7 @@ func trimSuffixAll(s, sfx string) string { return x } -func checkField(k string, v interface{}, opt *Option, pointWarnings *[]*PointWarning) (interface{}, error) { +func checkField(k string, v any, opt *Option, pointWarnings *[]*PointWarning) (any, error) { if strings.Contains(k, ".") && !opt.EnablePointInKey { return nil, fmt.Errorf("invalid field key `%s': found `.'", k) } @@ -606,7 +603,7 @@ func checkField(k string, v interface{}, opt *Option, pointWarnings *[]*PointWar } } -func checkFields(fields map[string]interface{}, opt *Option, pointWarnings *[]*PointWarning) error { +func checkFields(fields map[string]any, opt *Option, pointWarnings *[]*PointWarning) error { // warnings: WarnMaxFields warnings := []*PointWarning{} diff --git a/lineproto/lineproto_test.go b/lineproto/lineproto_test.go index b7c35730..e8420217 100644 --- a/lineproto/lineproto_test.go +++ b/lineproto/lineproto_test.go @@ -8,6 +8,7 @@ package lineproto import ( "fmt" "math" + "strings" "testing" "time" @@ -66,7 +67,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { tname string // test name name string tags map[string]string - fields map[string]interface{} + fields map[string]any ts time.Time opt *Option expect string @@ -76,17 +77,17 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { { tname: `64k-field-value-length`, name: "some", - fields: map[string]interface{}{ + fields: map[string]any{ "key": func() string { const str = "1234567890" - var out string + var out strings.Builder for { - out += str - if len(out) > 64*1024 { + out.WriteString(str) + if len(out.String()) > 64*1024 { break } } - return out + return out.String() }(), }, opt: func() *Option { @@ -98,21 +99,21 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { expect: fmt.Sprintf(`some key="%s" 123`, func() string { const str = "1234567890" - var out string + var out strings.Builder for { - out += str - if len(out) > 64*1024 { + out.WriteString(str) + if len(out.String()) > 64*1024 { break } } - return out + return out.String() }()), }, { tname: `max-field-value-length`, name: "some", - fields: map[string]interface{}{"key": "too-long-field-value-123"}, + fields: map[string]any{"key": "too-long-field-value-123"}, opt: func() *Option { opt := NewDefaultOption() opt.MaxFieldValueLen = 2 @@ -126,7 +127,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { { tname: `max-field-key-length`, name: "some", - fields: map[string]interface{}{"too-long-field-key": "123"}, + fields: map[string]any{"too-long-field-key": "123"}, opt: func() *Option { opt := NewDefaultOption() opt.MaxFieldKeyLen = 2 @@ -140,7 +141,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { { tname: `max-tag-value-length`, name: "some", - fields: map[string]interface{}{"f1": 1}, + fields: map[string]any{"f1": 1}, tags: map[string]string{"key": "too-long-tag-value-123"}, opt: func() *Option { opt := NewDefaultOption() @@ -154,7 +155,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { { tname: `disable-string-field`, name: "some", - fields: map[string]interface{}{"f1": 1, "f2": "this is a string"}, + fields: map[string]any{"f1": 1, "f2": "this is a string"}, tags: map[string]string{"key": "string"}, opt: func() *Option { opt := NewDefaultOption() @@ -169,7 +170,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { { tname: `max tag key length`, name: "some", - fields: map[string]interface{}{"f1": 1}, + fields: map[string]any{"f1": 1}, tags: map[string]string{"too-long-tag-key": "123"}, opt: func() *Option { opt := NewDefaultOption() @@ -184,7 +185,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { { tname: `empty measurement name`, name: "", // empty - fields: map[string]interface{}{"f.1": 1, "f2": uint64(32)}, + fields: map[string]any{"f.1": 1, "f2": uint64(32)}, tags: map[string]string{"t.1": "abc", "t2": "32"}, opt: func() *Option { @@ -200,7 +201,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { { tname: `enable point in metric point`, name: "abc", - fields: map[string]interface{}{"f.1": 1, "f2": uint64(32)}, + fields: map[string]any{"f.1": 1, "f2": uint64(32)}, tags: map[string]string{"t.1": "abc", "t2": "32"}, opt: func() *Option { @@ -216,7 +217,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { { tname: `enable point in metric point`, name: "abc", - fields: map[string]interface{}{"f.1": 1, "f2": uint64(32)}, + fields: map[string]any{"f.1": 1, "f2": uint64(32)}, tags: map[string]string{"t1": "abc", "t2": "32"}, opt: func() *Option { @@ -231,7 +232,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { { tname: `with disabled field keys`, name: "abc", - fields: map[string]interface{}{"f1": 1, "f2": uint64(32)}, + fields: map[string]any{"f1": 1, "f2": uint64(32)}, tags: map[string]string{"t1": "abc", "t2": "32"}, opt: func() *Option { @@ -246,7 +247,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { { tname: `with disabled tag keys`, name: "abc", - fields: map[string]interface{}{"f1": 1, "f2": uint64(32)}, + fields: map[string]any{"f1": 1, "f2": uint64(32)}, tags: map[string]string{"t1": "abc", "t2": "32"}, opt: func() *Option { @@ -261,7 +262,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { { tname: `int exceed int64-max under non-strict mode`, name: "abc", - fields: map[string]interface{}{"f1": 1, "f2": uint64(32)}, + fields: map[string]any{"f1": 1, "f2": uint64(32)}, expect: "abc f1=1i,f2=32i 123", opt: func() *Option { opt := NewDefaultOption() @@ -275,7 +276,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { { tname: `int exceed int64-max under non-strict mode`, name: "abc", - fields: map[string]interface{}{"f1": 1, "f2": uint64(math.MaxInt64) + 1}, + fields: map[string]any{"f1": 1, "f2": uint64(math.MaxInt64) + 1}, expect: "abc f1=1i 123", // f2 dropped warnTypes: []string{WarnMaxFieldValueInt}, opt: func() *Option { @@ -291,7 +292,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { { tname: `int exceed int64-max under strict mode`, name: "abc", - fields: map[string]interface{}{"f1": 1, "f2": uint64(math.MaxInt64) + 1}, + fields: map[string]any{"f1": 1, "f2": uint64(math.MaxInt64) + 1}, opt: func() *Option { opt := NewDefaultOption() @@ -305,7 +306,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { { tname: `extra tags and field exceed max tags`, name: "abc", - fields: map[string]interface{}{"f1": 1, "f2": "3"}, + fields: map[string]any{"f1": 1, "f2": "3"}, tags: map[string]string{"t1": "def", "t2": "abc"}, opt: func() *Option { @@ -326,7 +327,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { { tname: `extra tags exceed max tags`, name: "abc", - fields: map[string]interface{}{"f1": 1}, + fields: map[string]any{"f1": 1}, tags: map[string]string{"t1": "def", "t2": "abc"}, warnTypes: []string{WarnMaxTags}, opt: func() *Option { @@ -346,7 +347,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { { tname: `extra tags not exceed max tags`, name: "abc", - fields: map[string]interface{}{"f1": 1}, + fields: map[string]any{"f1": 1}, tags: map[string]string{"t1": "def", "t2": "abc"}, expect: "abc,etag1=1,etag2=2,t1=def,t2=abc f1=1i 123", @@ -367,7 +368,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { { tname: `only extra tags`, name: "abc", - fields: map[string]interface{}{"f1": 1}, + fields: map[string]any{"f1": 1}, expect: "abc,etag1=1,etag2=2 f1=1i 123", opt: func() *Option { @@ -387,7 +388,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { { tname: `exceed max tags`, name: "abc", - fields: map[string]interface{}{"f1": 1, "f2": nil}, + fields: map[string]any{"f1": 1, "f2": nil}, tags: map[string]string{"t1": "def", "t2": "abc"}, opt: func() *Option { opt := NewDefaultOption() @@ -402,7 +403,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { { tname: `exceed max field`, name: "abc", - fields: map[string]interface{}{"f1": 1, "f2": 2}, + fields: map[string]any{"f1": 1, "f2": 2}, tags: map[string]string{"t1": "def"}, opt: func() *Option { opt := NewDefaultOption() @@ -418,7 +419,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { { tname: `field key with "."`, name: "abc", - fields: map[string]interface{}{"f1.a": 1}, + fields: map[string]any{"f1.a": 1}, tags: map[string]string{"t1.a": "def"}, opt: NewDefaultOption(), fail: true, @@ -427,7 +428,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { { tname: `field key with "."`, name: "abc", - fields: map[string]interface{}{"f1.a": 1}, + fields: map[string]any{"f1.a": 1}, tags: map[string]string{"t1": "def"}, opt: NewDefaultOption(), fail: true, @@ -436,7 +437,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { { tname: `tag key with "."`, name: "abc", - fields: map[string]interface{}{"f1": 1, "f2": nil}, + fields: map[string]any{"f1": 1, "f2": nil}, tags: map[string]string{"t1.a": "def"}, opt: NewDefaultOption(), fail: true, @@ -445,7 +446,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { { tname: `nil field, not allowed`, name: "abc", - fields: map[string]interface{}{"f1": 1, "f2": nil}, + fields: map[string]any{"f1": 1, "f2": nil}, tags: map[string]string{"t1": "def"}, opt: func() *Option { opt := NewDefaultOption() @@ -458,7 +459,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { { tname: `same key in field and tag`, name: "abc", - fields: map[string]interface{}{"f1": 1, "f2": 2}, + fields: map[string]any{"f1": 1, "f2": 2}, tags: map[string]string{"f1": "def"}, opt: func() *Option { opt := NewDefaultOption() @@ -472,7 +473,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { { tname: `no tag`, name: "abc", - fields: map[string]interface{}{"f1": 1}, + fields: map[string]any{"f1": 1}, tags: nil, opt: func() *Option { opt := NewDefaultOption() @@ -494,7 +495,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { { tname: `field-val with '\n'`, name: "abc", - fields: map[string]interface{}{"f1": `abc + fields: map[string]any{"f1": `abc 123`}, opt: func() *Option { opt := NewDefaultOption() @@ -517,7 +518,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { 2`: `def 456\`, }, - fields: map[string]interface{}{"f1": 123}, + fields: map[string]any{"f1": 123}, opt: func() *Option { opt := NewDefaultOption() opt.Time = time.Unix(0, 123) @@ -538,7 +539,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { 2`: `def 456\`, }, - fields: map[string]interface{}{"f1": 123}, + fields: map[string]any{"f1": 123}, opt: func() *Option { opt := NewDefaultOption() opt.Time = time.Unix(0, 123) @@ -551,7 +552,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { tname: `ok case`, name: "abc", tags: nil, - fields: map[string]interface{}{"f1": 123}, + fields: map[string]any{"f1": 123}, opt: func() *Option { opt := NewDefaultOption() opt.Time = time.Unix(0, 123) @@ -565,7 +566,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { tname: `tag key with backslash`, name: "abc", tags: map[string]string{"tag1": "val1", `tag2\`: `val2\`}, - fields: map[string]interface{}{"f1": 123}, + fields: map[string]any{"f1": 123}, opt: func() *Option { opt := NewDefaultOption() opt.Time = time.Unix(0, 123) @@ -578,7 +579,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { tname: `auto fix tag-key, tag-value under non-strict mode`, name: "abc", tags: map[string]string{"tag1": "val1", `tag2\`: `val2\`}, - fields: map[string]interface{}{"f1": 123}, + fields: map[string]any{"f1": 123}, opt: func() *Option { opt := NewDefaultOption() @@ -594,7 +595,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { tname: `under strict: error`, name: "abc", tags: map[string]string{"tag1": "val1", `tag2\`: `val2\`}, - fields: map[string]interface{}{"f1": 123}, + fields: map[string]any{"f1": 123}, opt: func() *Option { opt := NewDefaultOption() @@ -610,7 +611,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { tname: `under strict: field is nil`, name: "abc", tags: map[string]string{"tag1": "val1", `tag2`: `val2`}, - fields: map[string]interface{}{"f1": 123, "f2": nil}, + fields: map[string]any{"f1": 123, "f2": nil}, opt: func() *Option { opt := NewDefaultOption() @@ -626,7 +627,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { tname: `under strict: field is map`, name: "abc", tags: map[string]string{"tag1": "val1", `tag2`: `val2`}, - fields: map[string]interface{}{"f1": 123, "f2": map[string]interface{}{"a": "b"}}, + fields: map[string]any{"f1": 123, "f2": map[string]any{"a": "b"}}, opt: func() *Option { opt := NewDefaultOption() @@ -642,7 +643,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { tname: `under strict: field is object`, name: "abc", tags: map[string]string{"tag1": "val1", `tag2`: `val2`}, - fields: map[string]interface{}{"f1": 123, "f2": struct{ a string }{a: "abc"}}, + fields: map[string]any{"f1": 123, "f2": struct{ a string }{a: "abc"}}, opt: func() *Option { opt := NewDefaultOption() @@ -658,7 +659,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { tname: `under non-strict, ignore nil field`, name: "abc", tags: map[string]string{"tag1": "val1", `tag2\`: `val2\`}, - fields: map[string]interface{}{"f1": 123, "f2": nil}, + fields: map[string]any{"f1": 123, "f2": nil}, opt: func() *Option { opt := NewDefaultOption() @@ -675,7 +676,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { tname: `under strict, utf8 characters in metric-name`, name: "abc≈≈≈≈øøππ†®", tags: map[string]string{"tag1": "val1", `tag2`: `val2`}, - fields: map[string]interface{}{"f1": 123}, + fields: map[string]any{"f1": 123}, opt: func() *Option { opt := NewDefaultOption() @@ -691,7 +692,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { tname: `under strict, utf8 characters in metric-name, fields, tags`, name: "abc≈≈≈≈øøππ†®", tags: map[string]string{"tag1": "val1", `tag2`: `val2`, "tag3": `ºª•¶§∞¢£`}, - fields: map[string]interface{}{"f1": 123, "f2": "¡™£¢∞§¶•ªº"}, + fields: map[string]any{"f1": 123, "f2": "¡™£¢∞§¶•ªº"}, opt: func() *Option { opt := NewDefaultOption() opt.Time = time.Unix(0, 123) @@ -721,7 +722,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { tname: `new line in field`, name: "abc", tags: map[string]string{"tag1": "val1"}, - fields: map[string]interface{}{ + fields: map[string]any{ "f1": `aaa bbb ccc`, @@ -777,7 +778,7 @@ func TestMakeLineProtoPointWithWarnings(t *testing.T) { func TestParsePoint(t *testing.T) { newPoint := func(m string, tags map[string]string, - fields map[string]interface{}, + fields map[string]any, ts ...time.Time, ) *influxdb.Point { pt, err := influxdb.NewPoint(m, tags, fields, ts...) @@ -800,7 +801,7 @@ func TestParsePoint(t *testing.T) { }{ { name: `32mb-field`, - data: []byte(fmt.Sprintf(`abc f1="%s" 123`, __32mbString)), + data: fmt.Appendf(nil, `abc f1="%s" 123`, __32mbString), opt: func() *Option { opt := NewDefaultOption() opt.MaxFieldValueLen = 32 * 1024 * 1024 @@ -811,13 +812,13 @@ func TestParsePoint(t *testing.T) { expect: []*influxdb.Point{ newPoint("abc", nil, - map[string]interface{}{"f1": __32mbString}, + map[string]any{"f1": __32mbString}, time.Unix(0, 123)), }, }, { name: `65k-field`, - data: []byte(fmt.Sprintf(`abc f1="%s" 123`, __65kbString)), + data: fmt.Appendf(nil, `abc f1="%s" 123`, __65kbString), opt: func() *Option { opt := NewDefaultOption() opt.MaxFieldValueLen = 0 @@ -828,7 +829,7 @@ func TestParsePoint(t *testing.T) { expect: []*influxdb.Point{ newPoint("abc", nil, - map[string]interface{}{"f1": __65kbString}, + map[string]any{"f1": __65kbString}, time.Unix(0, 123)), }, }, @@ -888,17 +889,17 @@ abc f1=1i,f2=2,f3="abc" 789 expect: []*influxdb.Point{ newPoint("abc", nil, - map[string]interface{}{"f1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 123)), newPoint("abc", nil, - map[string]interface{}{"f1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 456)), newPoint("abc", nil, - map[string]interface{}{"f1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 789)), }, }, @@ -943,7 +944,7 @@ abc f1=1i,f2=2,f3="abc" 789 expect: []*influxdb.Point{ newPoint("abc", map[string]string{"tag1": "1", "tag2": "2"}, - map[string]interface{}{"f1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 123)), }, }, @@ -955,7 +956,7 @@ abc f1=1i,f2=2,f3="abc" 789 expect: []*influxdb.Point{ newPoint("abc", nil, - map[string]interface{}{"f1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 123)), }, }, @@ -973,17 +974,17 @@ abc f1=1i,f2=2,f3="abc" 789 expect: []*influxdb.Point{ newPoint("abc", nil, - map[string]interface{}{"f1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 123)), newPoint("abc", nil, - map[string]interface{}{"f1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 456)), newPoint("abc", nil, - map[string]interface{}{"f1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 789)), }, }, @@ -1004,7 +1005,7 @@ abc f1=1i,f2=2,f3="abc" 789 expect: []*influxdb.Point{ newPoint("abc", map[string]string{"tag1": "1", "tag2": "2"}, - map[string]interface{}{"f1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 123)), }, }, @@ -1019,7 +1020,7 @@ abc f1=1i,f2=2,f3="abc" 789 expect: []*influxdb.Point{ newPoint("abc", map[string]string{`tag1\`: "1", "tag2": `2`}, - map[string]interface{}{"f1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 123)), }, }, @@ -1034,7 +1035,7 @@ abc f1=1i,f2=2,f3="abc" 789 expect: []*influxdb.Point{ newPoint("abc", map[string]string{`tag1`: "1,", "tag2": `2\`, "tag3": `3`}, - map[string]interface{}{"f1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 123)), }, }, @@ -1049,7 +1050,7 @@ abc f1=1i,f2=2,f3="abc" 789 expect: []*influxdb.Point{ newPoint("abc", map[string]string{`tag\1`: "1", "tag2": `2\34`}, - map[string]interface{}{"f1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 123)), }, }, @@ -1100,7 +1101,7 @@ abc f1=1i,f2=2,f3="abc" 789 expect: []*influxdb.Point{ newPoint("abc", map[string]string{"callback-added-tag": "callback-added-tag-value"}, - map[string]interface{}{"f1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 123)), }, }, @@ -1205,9 +1206,9 @@ func TestParseLineProto(t *testing.T) { { name: `65kb-field-key`, - data: []byte(fmt.Sprintf(`abc,tag1=1,tag2=2 "%s"="hello" 123`, func() string { + data: fmt.Appendf(nil, `abc,tag1=1,tag2=2 "%s"="hello" 123`, func() string { return __65kbString - }())), + }()), fail: true, prec: "n", @@ -1215,9 +1216,9 @@ func TestParseLineProto(t *testing.T) { { name: `65kb-tag-key`, - data: []byte(fmt.Sprintf(`abc,tag1=1,%s=2 f1="hello" 123`, func() string { + data: fmt.Appendf(nil, `abc,tag1=1,%s=2 f1="hello" 123`, func() string { return __65kbString - }())), + }()), fail: true, prec: "n", @@ -1225,9 +1226,9 @@ func TestParseLineProto(t *testing.T) { { name: `65kb-measurement-name`, - data: []byte(fmt.Sprintf(`%s,tag1=1,t2=2 f1="hello" 123`, func() string { + data: fmt.Appendf(nil, `%s,tag1=1,t2=2 f1="hello" 123`, func() string { return __65kbString - }())), + }()), fail: true, prec: "n", @@ -1235,9 +1236,9 @@ func TestParseLineProto(t *testing.T) { { name: `32mb-field`, - data: []byte(fmt.Sprintf(`abc,tag1=1,tag2=2 f3="%s" 123`, func() string { + data: fmt.Appendf(nil, `abc,tag1=1,tag2=2 f3="%s" 123`, func() string { return __32mbString - }())), + }()), check: func(pts models.Points) error { if len(pts) != 1 { diff --git a/lineproto/lp.go b/lineproto/lp.go index 398ab4a3..a6ee6417 100644 --- a/lineproto/lp.go +++ b/lineproto/lp.go @@ -59,13 +59,13 @@ func ConvertPrecisionToV2(precision string) (Precision, error) { type Point struct { Name string Tags map[string]string - Fields map[string]interface{} + Fields map[string]any Time time.Time } func NewPoint(name string, tags map[string]string, - fields map[string]interface{}, + fields map[string]any, t ...time.Time, ) (*Point, error) { var tm time.Time @@ -92,7 +92,7 @@ func (p *Point) AddTag(key, val string) { p.Tags[key] = val } -func (p *Point) AddField(key string, val interface{}) error { +func (p *Point) AddField(key string, val any) error { // check the val value if _, ok := InterfaceToValue(val); !ok { return fmt.Errorf("unsupported line protocol field interface{}: %T, [%v]", val, val) @@ -154,7 +154,7 @@ func Parse(data []byte, opt *Option) ([]*Point, error) { for dec.Next() { pt := &Point{ Tags: make(map[string]string), - Fields: make(map[string]interface{}), + Fields: make(map[string]any), } m, err := dec.Measurement() @@ -227,7 +227,7 @@ func Parse(data []byte, opt *Option) ([]*Point, error) { return pts, nil } -func InterfaceToValue(x interface{}) (lp.Value, bool) { +func InterfaceToValue(x any) (lp.Value, bool) { switch y := x.(type) { case int64, uint64, float64, bool, string, []byte: // do nothing @@ -288,10 +288,7 @@ func (le *LineEncoder) EnableLax() { func (le *LineEncoder) AppendPoint(pt *Point) error { le.Encoder.StartLine(pt.Name) - maxLen := len(pt.Tags) - if len(pt.Fields) > maxLen { - maxLen = len(pt.Fields) - } + maxLen := max(len(pt.Fields), len(pt.Tags)) keys := make([]string, 0, maxLen) diff --git a/lineproto/lp_test.go b/lineproto/lp_test.go index c5a65a22..bac72401 100644 --- a/lineproto/lp_test.go +++ b/lineproto/lp_test.go @@ -28,7 +28,7 @@ func TestUnsafeBytesToString(t *testing.T) { } func TestInterfaceToValue(t *testing.T) { - ints := []interface{}{ + ints := []any{ -5, int8(-5), int16(-5), @@ -71,7 +71,7 @@ func TestPointString(t *testing.T) { "t1": cliutils.CreateRandomString(100), "t2": cliutils.CreateRandomString(100), } - fields := map[string]interface{}{ + fields := map[string]any{ "f1": int64(1024), "f2": 1024.2048, "f3": cliutils.CreateRandomString(128), @@ -174,7 +174,7 @@ func TestNewLineEncoder(t *testing.T) { pts []struct { measurement string tags map[string]string - fields map[string]interface{} + fields map[string]any time time.Time } opt *Option @@ -185,7 +185,7 @@ func TestNewLineEncoder(t *testing.T) { pts: []struct { measurement string tags map[string]string - fields map[string]interface{} + fields map[string]any time time.Time }{ { @@ -194,7 +194,7 @@ func TestNewLineEncoder(t *testing.T) { "t2": cliutils.CreateRandomString(100), "t1": cliutils.CreateRandomString(100), }, - fields: map[string]interface{}{ + fields: map[string]any{ "f3": int64(1024), "f1": 1024.2048, "f2": cliutils.CreateRandomString(128), @@ -207,7 +207,7 @@ func TestNewLineEncoder(t *testing.T) { "aaa": cliutils.CreateRandomString(110), "AAA": cliutils.CreateRandomString(110), }, - fields: map[string]interface{}{ + fields: map[string]any{ "f1": int64(1024), "f5": 1024.2048, "f3": cliutils.CreateRandomString(138), @@ -221,7 +221,7 @@ func TestNewLineEncoder(t *testing.T) { "t31": cliutils.CreateRandomString(100), "t22": cliutils.CreateRandomString(120), }, - fields: map[string]interface{}{ + fields: map[string]any{ "f1": int64(1024), "f222": 1024.2048, "f3": cliutils.CreateRandomString(148), @@ -295,7 +295,7 @@ func TestEncode(t *testing.T) { pts []struct { measurement string tags map[string]string - fields map[string]interface{} + fields map[string]any time time.Time } opt *Option @@ -306,7 +306,7 @@ func TestEncode(t *testing.T) { pts: []struct { measurement string tags map[string]string - fields map[string]interface{} + fields map[string]any time time.Time }{ { @@ -316,7 +316,7 @@ func TestEncode(t *testing.T) { "t1": cliutils.CreateRandomString(100), "t2": cliutils.CreateRandomString(100), }, - fields: map[string]interface{}{ + fields: map[string]any{ "f1": int64(1024), "f2": 1024.2048, "f3": cliutils.CreateRandomString(128), @@ -331,7 +331,7 @@ func TestEncode(t *testing.T) { "t1": cliutils.CreateRandomString(100), "t2": cliutils.CreateRandomString(100), }, - fields: map[string]interface{}{ + fields: map[string]any{ "ffff": "", "f1": int64(1024), "f2": 1024.2048, @@ -375,7 +375,7 @@ func BenchmarkEncode(b *testing.B) { pts []struct { measurement string tags map[string]string - fields map[string]interface{} + fields map[string]any time time.Time } opt *Option @@ -386,7 +386,7 @@ func BenchmarkEncode(b *testing.B) { pts: []struct { measurement string tags map[string]string - fields map[string]interface{} + fields map[string]any time time.Time }{ { @@ -395,7 +395,7 @@ func BenchmarkEncode(b *testing.B) { "t1": cliutils.CreateRandomString(100), "t2": cliutils.CreateRandomString(100), }, - fields: map[string]interface{}{ + fields: map[string]any{ "f1": int64(1024), "f2": 1024.2048, "f3": cliutils.CreateRandomString(128), @@ -411,7 +411,7 @@ func BenchmarkEncode(b *testing.B) { pts: []struct { measurement string tags map[string]string - fields map[string]interface{} + fields map[string]any time time.Time }{ { @@ -420,7 +420,7 @@ func BenchmarkEncode(b *testing.B) { "t1": cliutils.CreateRandomString(100), "t2": cliutils.CreateRandomString(100), }, - fields: map[string]interface{}{ + fields: map[string]any{ "f1": int64(1024), "f2": 1024.2048, "f3": cliutils.CreateRandomString(1024), @@ -435,7 +435,7 @@ func BenchmarkEncode(b *testing.B) { "t1": cliutils.CreateRandomString(100), "t2": cliutils.CreateRandomString(100), }, - fields: map[string]interface{}{ + fields: map[string]any{ "f1": int64(1024), "f2": 1024.2048, "f3": cliutils.CreateRandomString(1024), @@ -450,7 +450,7 @@ func BenchmarkEncode(b *testing.B) { "t1": cliutils.CreateRandomString(100), "t2": cliutils.CreateRandomString(100), }, - fields: map[string]interface{}{ + fields: map[string]any{ "f1": int64(1024), "f2": 1024.2048, "f3": cliutils.CreateRandomString(1024), @@ -512,7 +512,7 @@ func TestLineEncoderBytesWithoutLn(t *testing.T) { pts []struct { measurement string tags map[string]string - fields map[string]interface{} + fields map[string]any time time.Time } opt *Option @@ -523,7 +523,7 @@ func TestLineEncoderBytesWithoutLn(t *testing.T) { pts: []struct { measurement string tags map[string]string - fields map[string]interface{} + fields map[string]any time time.Time }{ { @@ -532,7 +532,7 @@ func TestLineEncoderBytesWithoutLn(t *testing.T) { "t2": cliutils.CreateRandomString(100), "t1": cliutils.CreateRandomString(100), }, - fields: map[string]interface{}{ + fields: map[string]any{ "f3": int64(1024), "f1": 1024.2048, "f2": cliutils.CreateRandomString(128), @@ -545,7 +545,7 @@ func TestLineEncoderBytesWithoutLn(t *testing.T) { "aaa": cliutils.CreateRandomString(110), "AAA": cliutils.CreateRandomString(110), }, - fields: map[string]interface{}{ + fields: map[string]any{ "f1": int64(1024), "f5": 1024.2048, "f3": cliutils.CreateRandomString(138), @@ -559,7 +559,7 @@ func TestLineEncoderBytesWithoutLn(t *testing.T) { "t31": cliutils.CreateRandomString(100), "t22": cliutils.CreateRandomString(120), }, - fields: map[string]interface{}{ + fields: map[string]any{ "f1": int64(1024), "f222": 1024.2048, "f3": cliutils.CreateRandomString(148), diff --git a/logger/logger_ctx.go b/logger/logger_ctx.go index 5666cb7c..b10d350d 100644 --- a/logger/logger_ctx.go +++ b/logger/logger_ctx.go @@ -8,23 +8,23 @@ import ( ) type LoggerCtx interface { - Debugf(template string, args ...interface{}) - Infof(template string, args ...interface{}) - Warnf(template string, args ...interface{}) - Errorf(template string, args ...interface{}) - Debug(args ...interface{}) - Info(args ...interface{}) - Warn(args ...interface{}) - Error(args ...interface{}) - - DebugfCtx(ctx context.Context, template string, args ...interface{}) - InfofCtx(ctx context.Context, template string, args ...interface{}) - WarnfCtx(ctx context.Context, template string, args ...interface{}) - ErrorfCtx(ctx context.Context, template string, args ...interface{}) - DebugCtx(ctx context.Context, template string, args ...interface{}) - InfoCtx(ctx context.Context, template string, args ...interface{}) - WarnCtx(ctx context.Context, template string, args ...interface{}) - ErrorCtx(ctx context.Context, template string, args ...interface{}) + Debugf(template string, args ...any) + Infof(template string, args ...any) + Warnf(template string, args ...any) + Errorf(template string, args ...any) + Debug(args ...any) + Info(args ...any) + Warn(args ...any) + Error(args ...any) + + DebugfCtx(ctx context.Context, template string, args ...any) + InfofCtx(ctx context.Context, template string, args ...any) + WarnfCtx(ctx context.Context, template string, args ...any) + ErrorfCtx(ctx context.Context, template string, args ...any) + DebugCtx(ctx context.Context, template string, args ...any) + InfoCtx(ctx context.Context, template string, args ...any) + WarnCtx(ctx context.Context, template string, args ...any) + ErrorCtx(ctx context.Context, template string, args ...any) Named(name string) LoggerCtx With(fields ...zap.Field) LoggerCtx @@ -69,7 +69,7 @@ func (l *loggerCtx) getExtraFields(ctx context.Context) (fields []zap.Field) { return fields } -func (l *loggerCtx) getMessage(template string, args []interface{}) string { +func (l *loggerCtx) getMessage(template string, args []any) string { if len(args) == 0 { return template } @@ -93,67 +93,67 @@ func (l *loggerCtx) Named(name string) LoggerCtx { } } -func (l *loggerCtx) Debugf(template string, args ...interface{}) { +func (l *loggerCtx) Debugf(template string, args ...any) { l.logger.Debug(l.getMessage(template, args)) } -func (l *loggerCtx) DebugfCtx(ctx context.Context, template string, args ...interface{}) { +func (l *loggerCtx) DebugfCtx(ctx context.Context, template string, args ...any) { l.logger.Debug(l.getMessage(template, args), l.getExtraFields(ctx)...) } -func (l *loggerCtx) Infof(template string, args ...interface{}) { +func (l *loggerCtx) Infof(template string, args ...any) { l.logger.Info(l.getMessage(template, args)) } -func (l *loggerCtx) Warnf(template string, args ...interface{}) { +func (l *loggerCtx) Warnf(template string, args ...any) { l.logger.Warn(l.getMessage(template, args)) } -func (l *loggerCtx) Errorf(template string, args ...interface{}) { +func (l *loggerCtx) Errorf(template string, args ...any) { l.logger.Error(l.getMessage(template, args)) } -func (l *loggerCtx) InfofCtx(ctx context.Context, template string, args ...interface{}) { +func (l *loggerCtx) InfofCtx(ctx context.Context, template string, args ...any) { l.logger.Info(l.getMessage(template, args), l.getExtraFields(ctx)...) } -func (l *loggerCtx) WarnfCtx(ctx context.Context, template string, args ...interface{}) { +func (l *loggerCtx) WarnfCtx(ctx context.Context, template string, args ...any) { l.logger.Warn(l.getMessage(template, args), l.getExtraFields(ctx)...) } -func (l *loggerCtx) ErrorfCtx(ctx context.Context, template string, args ...interface{}) { +func (l *loggerCtx) ErrorfCtx(ctx context.Context, template string, args ...any) { l.logger.Error(l.getMessage(template, args), l.getExtraFields(ctx)...) } -func (l *loggerCtx) Debug(args ...interface{}) { +func (l *loggerCtx) Debug(args ...any) { l.logger.Debug(l.getMessage("", args)) } -func (l *loggerCtx) DebugCtx(ctx context.Context, template string, args ...interface{}) { +func (l *loggerCtx) DebugCtx(ctx context.Context, template string, args ...any) { l.logger.Debug(l.getMessage(template, args), l.getExtraFields(ctx)...) } -func (l *loggerCtx) Error(args ...interface{}) { +func (l *loggerCtx) Error(args ...any) { l.logger.Error(l.getMessage("", args)) } -func (l *loggerCtx) ErrorCtx(ctx context.Context, template string, args ...interface{}) { +func (l *loggerCtx) ErrorCtx(ctx context.Context, template string, args ...any) { l.logger.Error(l.getMessage(template, args), l.getExtraFields(ctx)...) } -func (l *loggerCtx) Info(args ...interface{}) { +func (l *loggerCtx) Info(args ...any) { l.logger.Info(l.getMessage("", args)) } -func (l *loggerCtx) InfoCtx(ctx context.Context, template string, args ...interface{}) { +func (l *loggerCtx) InfoCtx(ctx context.Context, template string, args ...any) { l.logger.Info(l.getMessage(template, args), l.getExtraFields(ctx)...) } -func (l *loggerCtx) Warn(args ...interface{}) { +func (l *loggerCtx) Warn(args ...any) { l.logger.Warn(l.getMessage("", args)) } -func (l *loggerCtx) WarnCtx(ctx context.Context, template string, args ...interface{}) { +func (l *loggerCtx) WarnCtx(ctx context.Context, template string, args ...any) { l.logger.Warn(l.getMessage("", args), l.getExtraFields(ctx)...) } diff --git a/logger/logger_test.go b/logger/logger_test.go index 74d5a1ba..af9b8dd3 100644 --- a/logger/logger_test.go +++ b/logger/logger_test.go @@ -335,12 +335,12 @@ func TestTotalSLoggers(t *testing.T) { n := int64(1000) - for i := int64(0); i < n; i++ { + for i := range n { _ = SLogger(fmt.Sprintf("slogger-%d", i)) } // should not create new SLogger any more - for i := int64(0); i < n; i++ { + for i := range n { _ = SLogger(fmt.Sprintf("slogger-%d", i)) } @@ -740,17 +740,17 @@ func TestSeparateErrorFile(t *testing.T) { assert.Len(t, validErrorLines, 1) // Parse each line as JSON and verify levels - var mainLogs []map[string]interface{} + var mainLogs []map[string]any for _, line := range validMainLines { - var logEntry map[string]interface{} + var logEntry map[string]any err = json.Unmarshal([]byte(line), &logEntry) assert.NoError(t, err) mainLogs = append(mainLogs, logEntry) } - var errorLogs []map[string]interface{} + var errorLogs []map[string]any for _, line := range validErrorLines { - var logEntry map[string]interface{} + var logEntry map[string]any err = json.Unmarshal([]byte(line), &logEntry) assert.NoError(t, err) errorLogs = append(errorLogs, logEntry) diff --git a/logger/tcpudp_test.go b/logger/tcpudp_test.go index fa5f317c..66c9afb1 100644 --- a/logger/tcpudp_test.go +++ b/logger/tcpudp_test.go @@ -26,7 +26,7 @@ func serve(t *testing.T, expectLogCnt int, proto string, listen string, - ch chan interface{}, + ch chan any, ) (listener, error) { t.Helper() switch proto { @@ -122,7 +122,7 @@ func TestRemoteLogger(t *testing.T) { options int logs [][2]string expectLogCnt int - ch chan interface{} + ch chan any fail bool }{ { @@ -135,7 +135,7 @@ func TestRemoteLogger(t *testing.T) { {INFO, "this is info msg"}, }, expectLogCnt: 2, - ch: make(chan interface{}), + ch: make(chan any), }, { @@ -148,7 +148,7 @@ func TestRemoteLogger(t *testing.T) { {INFO, "this is info msg"}, }, expectLogCnt: 2, - ch: make(chan interface{}), + ch: make(chan any), }, { @@ -161,7 +161,7 @@ func TestRemoteLogger(t *testing.T) { {INFO, "this is info msg"}, }, expectLogCnt: 2, - ch: make(chan interface{}), + ch: make(chan any), }, { diff --git a/metrics/http_test.go b/metrics/http_test.go index 34368fe5..8fa6c76a 100644 --- a/metrics/http_test.go +++ b/metrics/http_test.go @@ -37,7 +37,7 @@ func TestRouteForGin(t *T.T) { div := 10000.0 - for i := 0; i < 1000; i++ { + for i := range 1000 { switch i % 3 { case 0: vec.WithLabelValues("/v1/write/metric", "ok").Add(float64(i) / div) diff --git a/metrics/metrics_test.go b/metrics/metrics_test.go index 8b8bb036..76101653 100644 --- a/metrics/metrics_test.go +++ b/metrics/metrics_test.go @@ -30,7 +30,7 @@ func TestHistogram(t *T.T) { div := 10000.0 - for i := 0; i < 1000; i++ { + for i := range 1000 { switch i % 3 { case 0: vec.WithLabelValues("/v1/write/metric", "ok").Observe(float64(i) / div) @@ -76,7 +76,7 @@ func TestConcurrentAdd(t *T.T) { ) wg.Add(nwrk) - for i := 0; i < nwrk; i++ { + for range nwrk { go func() { defer wg.Done() @@ -113,7 +113,7 @@ func TestAdd(t *T.T) { reg := prometheus.NewRegistry() reg.MustRegister(vec) - for i := 0; i < 100; i++ { + for i := range 100 { vec.WithLabelValues("/v1/write/abc", "ok").Observe(float64(i)) vec.WithLabelValues("/v1/write/def", "fail").Observe(float64(100 - i)) } diff --git a/network/http/api_metric.go b/network/http/api_metric.go index e812974e..30187909 100644 --- a/network/http/api_metric.go +++ b/network/http/api_metric.go @@ -12,7 +12,7 @@ import ( var ( metricCh = make(chan *APIMetric, 32) qch = make(chan *qAPIStats) - exitch = make(chan interface{}) + exitch = make(chan any) ) type APIMetric struct { diff --git a/network/http/api_wrap.go b/network/http/api_wrap.go index e5f05e69..d43e5f9b 100644 --- a/network/http/api_wrap.go +++ b/network/http/api_wrap.go @@ -24,9 +24,9 @@ type WrapPlugins struct { Reporter APIMetricReporter } -type apiHandler func(http.ResponseWriter, *http.Request, ...interface{}) (interface{}, error) +type apiHandler func(http.ResponseWriter, *http.Request, ...any) (any, error) -func HTTPAPIWrapper(p *WrapPlugins, next apiHandler, args ...interface{}) func(*gin.Context) { +func HTTPAPIWrapper(p *WrapPlugins, next apiHandler, args ...any) func(*gin.Context) { return func(c *gin.Context) { var start time.Time var m *APIMetric diff --git a/network/http/api_wrap_test.go b/network/http/api_wrap_test.go index 1274c55f..c4721420 100644 --- a/network/http/api_wrap_test.go +++ b/network/http/api_wrap_test.go @@ -29,7 +29,7 @@ func TestHTTPWrapperWithMetricReporter(t *testing.T) { limitRate := 10 lmt := NewAPIRateLimiter(float64(limitRate), DefaultRequestKey) - testHandler := func(http.ResponseWriter, *http.Request, ...interface{}) (interface{}, error) { + testHandler := func(http.ResponseWriter, *http.Request, ...any) (any, error) { return nil, nil } @@ -39,11 +39,9 @@ func TestHTTPWrapperWithMetricReporter(t *testing.T) { } wg := sync.WaitGroup{} - wg.Add(1) - go func() { - defer wg.Done() + wg.Go(func() { StartReporter() - }() + }) r.GET("/test", HTTPAPIWrapper(plg, testHandler)) @@ -89,7 +87,7 @@ func TestHTTPWrapperWithRateLimit(t *testing.T) { limitRate := 10 lmt := NewAPIRateLimiter(float64(limitRate), DefaultRequestKey) - testHandler := func(http.ResponseWriter, *http.Request, ...interface{}) (interface{}, error) { + testHandler := func(http.ResponseWriter, *http.Request, ...any) (any, error) { return nil, nil } diff --git a/network/http/err.go b/network/http/err.go index 872de8da..55d3110d 100644 --- a/network/http/err.go +++ b/network/http/err.go @@ -29,8 +29,8 @@ type HttpError struct { type BodyResp struct { *HttpError - Message string `json:"message,omitempty"` - Content interface{} `json:"content,omitempty"` + Message string `json:"message,omitempty"` + Content any `json:"content,omitempty"` } func NewNamespaceErr(err error, httpCode int, namespace string) *HttpError { @@ -63,7 +63,7 @@ func (he *HttpError) Error() string { } } -func (he *HttpError) HttpBodyPretty(c *gin.Context, body interface{}) { +func (he *HttpError) HttpBodyPretty(c *gin.Context, body any) { if body == nil { c.Status(he.HttpCode) return @@ -84,7 +84,7 @@ func (he *HttpError) HttpBodyPretty(c *gin.Context, body interface{}) { c.Data(he.HttpCode, `application/json`, j) } -func (he *HttpError) WriteBody(c *gin.Context, obj interface{}) { +func (he *HttpError) WriteBody(c *gin.Context, obj any) { if obj == nil { c.Status(he.HttpCode) return @@ -114,7 +114,7 @@ func (he *HttpError) WriteBody(c *gin.Context, obj interface{}) { type RawJSONBody []byte // HttpBody Deprecated, use WriteBody. -func (he *HttpError) HttpBody(c *gin.Context, body interface{}) { +func (he *HttpError) HttpBody(c *gin.Context, body any) { if body == nil { c.Status(he.HttpCode) return @@ -164,7 +164,7 @@ func HttpErr(c *gin.Context, err error) { } } -func HttpErrf(c *gin.Context, err error, format string, args ...interface{}) { +func HttpErrf(c *gin.Context, err error, format string, args ...any) { var ( e1 *HttpError e2 *MsgError @@ -180,7 +180,7 @@ func HttpErrf(c *gin.Context, err error, format string, args ...interface{}) { } } -func (he *HttpError) httpRespf(c *gin.Context, format string, args ...interface{}) { +func (he *HttpError) httpRespf(c *gin.Context, format string, args ...any) { resp := &BodyResp{ HttpError: he, } @@ -225,10 +225,10 @@ func titleErr(namespace string, err error) string { type MsgError struct { *HttpError Fmt string - Args []interface{} + Args []any } -func Errorf(he *HttpError, format string, args ...interface{}) *MsgError { +func Errorf(he *HttpError, format string, args ...any) *MsgError { return &MsgError{ HttpError: he, Fmt: format, @@ -240,7 +240,7 @@ func Error(he *HttpError, msg string) *MsgError { return &MsgError{ HttpError: he, Fmt: "%s", - Args: []interface{}{msg}, + Args: []any{msg}, } } diff --git a/network/http/err_test.go b/network/http/err_test.go index bfdb94c9..105d143a 100644 --- a/network/http/err_test.go +++ b/network/http/err_test.go @@ -112,7 +112,7 @@ func TestHTTPErr(t *testing.T) { router := gin.New() g := router.Group("") - okbody := map[string]interface{}{ + okbody := map[string]any{ "data1": 1, "data2": "abc", } @@ -205,7 +205,7 @@ func TestHTTPErr(t *testing.T) { u: "http://localhost:8090/ok2", expect: func() string { x := struct { - Content interface{} `json:"content"` + Content any `json:"content"` }{ Content: okbody, } @@ -226,7 +226,7 @@ func TestHTTPErr(t *testing.T) { u: "http://localhost:8090/errfmsg-with-nil-args", expect: func() string { msg := `{"error_code":"reachMaxAPIRateLimit","message":"Errorf without args"}` - var x interface{} + var x any if err := json.Unmarshal([]byte(msg), &x); err != nil { t.Fatal(err) } @@ -268,7 +268,7 @@ func TestHTTPErr(t *testing.T) { } func TestErrorf(t *testing.T) { - var nilArgs []interface{} = nil + var nilArgs []any = nil format := "sprintf with nil args" str := fmt.Sprintf(format, nilArgs...) fmt.Println(str) diff --git a/network/http/gin.go b/network/http/gin.go index 8c312543..6a21c45b 100644 --- a/network/http/gin.go +++ b/network/http/gin.go @@ -15,6 +15,7 @@ import ( "net/http" "net/textproto" "os" + "slices" "sort" "strconv" "strings" @@ -132,7 +133,7 @@ func (c CORSHeaders) Add(requestHeaders string) string { return allowHeaders } headers := make([]string, 0) - for _, key := range strings.Split(requestHeaders, ",") { + for key := range strings.SplitSeq(requestHeaders, ",") { key = strings.TrimSpace(key) if key == "" { continue @@ -236,12 +237,7 @@ func originIsAllowed(origin string, allowedOrigins []string) bool { if len(allowedOrigins) == 0 { return true } - for _, allowedOrigin := range allowedOrigins { - if origin == allowedOrigin { - return true - } - } - return false + return slices.Contains(allowedOrigins, origin) } func TraceIDMiddleware(c *gin.Context) { diff --git a/network/ws/epoll_linux.go b/network/ws/epoll_linux.go index 121e3434..6d6a8f57 100644 --- a/network/ws/epoll_linux.go +++ b/network/ws/epoll_linux.go @@ -4,7 +4,6 @@ // Copyright 2021-present Guance, Inc. //go:build linux -// +build linux package ws @@ -75,7 +74,7 @@ func (e *epoll) Wait(cnt int) ([]net.Conn, error) { e.lock.RLock() defer e.lock.RUnlock() var connections []net.Conn - for i := 0; i < n; i++ { + for i := range n { conn := e.connections[int(events[i].Fd)] connections = append(connections, conn) } diff --git a/network/ws/ws.go b/network/ws/ws.go index 594161c1..65fb294d 100644 --- a/network/ws/ws.go +++ b/network/ws/ws.go @@ -103,11 +103,9 @@ func (s *Server) Start() { fmt.Printf("warn: Setrlimit %+#v failed: %s\n", rLimit, err.Error()) } - s.wg.Add(1) - go func() { - defer s.wg.Done() + s.wg.Go(func() { s.startEpoll() - }() + }) srv := &http.Server{ Addr: s.Bind, @@ -119,13 +117,11 @@ func (s *Server) Start() { http.HandleFunc(s.Path, s.AddCli) - s.wg.Add(1) - go func() { - defer s.wg.Done() + s.wg.Go(func() { if err := srv.ListenAndServe(); err != nil { l.Info(err) } - }() + }) <-s.exit.Wait() if err := srv.Shutdown(context.TODO()); err != nil { diff --git a/network/ws/ws_test.go b/network/ws/ws_test.go index f97d9c56..b909667a 100644 --- a/network/ws/ws_test.go +++ b/network/ws/ws_test.go @@ -44,7 +44,7 @@ func TestWSServer(t *testing.T) { // msg-handle callback df_srv.MsgHandler = func(s *Server, c net.Conn, data []byte, op ws.OpCode) error { - SendMsgToClient([]byte(fmt.Sprintf("your are %s", c.RemoteAddr().String())), c) + SendMsgToClient(fmt.Appendf(nil, "your are %s", c.RemoteAddr().String()), c) return nil } @@ -78,7 +78,7 @@ func TestWSServer(t *testing.T) { // datakit as ws proxy client dkclis := []*wscli{} - for i := 0; i < ncli; i++ { + for range ncli { cliid := cliutils.XID("id_") dw_wsurl := url.URL{ @@ -96,10 +96,10 @@ func TestWSServer(t *testing.T) { } __wg.Add(ncli) - ch := make(chan interface{}) + ch := make(chan any) // ws-cli send msg to ws-server - for i := 0; i < ncli; i++ { + for i := range ncli { go func(t *testing.T, i int) { t.Helper() diff --git a/oss.go b/oss.go index 7247a3eb..11e248f0 100644 --- a/oss.go +++ b/oss.go @@ -151,7 +151,7 @@ func (oc *OssCli) Move(from, to string) error { } func (oc *OssCli) mpworker(imur *oss.InitiateMultipartUploadResult, - c *oss.FileChunk, from string, exit chan interface{}, + c *oss.FileChunk, from string, exit chan any, ) (p oss.UploadPart, err error) { select { case <-exit: @@ -159,7 +159,7 @@ func (oc *OssCli) mpworker(imur *oss.InitiateMultipartUploadResult, default: - for i := 0; i < 3; i++ { + for range 3 { p, err = oc.bkt.UploadPartFromFile(*imur, from, c.Offset, c.Size, c.Number) if err == nil { return p, nil @@ -197,7 +197,7 @@ func (oc *OssCli) multipartUpload(from, to string) error { resCh := make(chan *oss.UploadPart, len(chunks)) // 接受返回结果 failedCh := make(chan error) - exit := make(chan interface{}) // 勒令退出 + exit := make(chan any) // 勒令退出 defer close(exit) diff --git a/otlp/string.go b/otlp/string.go index 5800c653..ea2cab8b 100644 --- a/otlp/string.go +++ b/otlp/string.go @@ -15,10 +15,7 @@ func ChunkStringByRuneLength(s string, size int) []string { chunks := make([]string, 0, (len(runes)+size-1)/size) for i := 0; i < len(runes); i += size { - end := i + size - if end > len(runes) { - end = len(runes) - } + end := min(i+size, len(runes)) chunks = append(chunks, string(runes[i:end])) } diff --git a/pkg/hash/fnv1a.go b/pkg/hash/fnv1a.go index 439afd43..cddce3fa 100644 --- a/pkg/hash/fnv1a.go +++ b/pkg/hash/fnv1a.go @@ -25,7 +25,7 @@ func Fnv1aHashAdd(h uint64, s string) uint64 { } func Fnv1aHashAddByte(h uint64, s []byte) uint64 { - for i := 0; i < len(s); i++ { + for i := range s { h ^= uint64(s[i]) h *= prime64 } @@ -43,7 +43,7 @@ func Fnv1aStrHash(s string) uint64 { func Fnv1aU8Hash(s []byte) uint64 { var h uint64 = offset64 - for i := 0; i < len(s); i++ { + for i := range s { h ^= uint64(s[i]) h *= prime64 } diff --git a/pkg/hash/fnv1a_test.go b/pkg/hash/fnv1a_test.go index 1000bce2..b7839d2a 100644 --- a/pkg/hash/fnv1a_test.go +++ b/pkg/hash/fnv1a_test.go @@ -7,6 +7,7 @@ package hash import ( "hash/fnv" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -29,11 +30,11 @@ func TestHash(t *testing.T) { h3 := hash4ts(d) - var s string + var s strings.Builder for _, v := range d { - s += v + s.WriteString(v) } - h4 := Fnv1aStrHash(s) + h4 := Fnv1aStrHash(s.String()) assert.NotEqual(t, Fnv1aNew(), h1) assert.Equal(t, h1, h2) diff --git a/point/category_test.go b/point/category_test.go index aee1fd5d..8995d826 100644 --- a/point/category_test.go +++ b/point/category_test.go @@ -6,6 +6,7 @@ package point import ( + "slices" "testing" "github.com/stretchr/testify/assert" @@ -106,11 +107,5 @@ func TestAllCategoriesIncludesNewCategories(t *testing.T) { } func containsCategory(all []Category, target Category) bool { - for _, c := range all { - if c == target { - return true - } - } - - return false + return slices.Contains(all, target) } diff --git a/point/check_test.go b/point/check_test.go index b5c906dd..f07055a5 100644 --- a/point/check_test.go +++ b/point/check_test.go @@ -264,26 +264,26 @@ func TestCheckTags(t *T.T) { func TestCheckFields(t *T.T) { cases := []struct { name string - f map[string]interface{} - expect map[string]interface{} + f map[string]any + expect map[string]any warns int opts []Option }{ { name: "exceed-max-field-len", - f: map[string]interface{}{ + f: map[string]any{ "f1": "123456", }, opts: []Option{WithMaxFieldValLen(1)}, warns: 1, - expect: map[string]interface{}{ + expect: map[string]any{ "f1": "1", }, }, { name: "exceed-max-field-count", - f: map[string]interface{}{ + f: map[string]any{ "f1": "aaaaaa1", "f2": "aaaaaa2", "f3": "aaaaaa3", @@ -297,20 +297,20 @@ func TestCheckFields(t *T.T) { }, opts: []Option{WithMaxFields(1), WithKeySorted(true)}, warns: 1, - expect: map[string]interface{}{ + expect: map[string]any{ "f0": "aaaaaa0", }, }, { name: "exceed-max-field-key-len", - f: map[string]interface{}{ + f: map[string]any{ "a1": "123456", "b": "abc123", }, opts: []Option{WithMaxFieldKeyLen(1)}, warns: 1, - expect: map[string]interface{}{ + expect: map[string]any{ "a": "123456", // key truncated "b": "abc123", }, @@ -318,20 +318,20 @@ func TestCheckFields(t *T.T) { { name: "drop-metric-string-field", - f: map[string]interface{}{ + f: map[string]any{ "a": 123456, "b": "abc123", // dropped }, opts: []Option{WithStrField(false)}, warns: 1, - expect: map[string]interface{}{ + expect: map[string]any{ "a": int64(123456), }, }, { name: "invalid-field-type", - f: map[string]interface{}{ + f: map[string]any{ "b": struct{}{}, }, warns: 1, @@ -339,13 +339,13 @@ func TestCheckFields(t *T.T) { { name: "nil-field", - f: map[string]interface{}{ + f: map[string]any{ "a": nil, // set value to nil "b": 123, "c": struct{}{}, // ignored }, warns: 2, - expect: map[string]interface{}{ + expect: map[string]any{ "b": int64(123), "a": nil, "c": nil, @@ -354,7 +354,7 @@ func TestCheckFields(t *T.T) { { name: "exceed-max-int64-under-influxdb1.x", - f: map[string]interface{}{ + f: map[string]any{ "b": uint64(math.MaxInt64) + 1, // exceed max-int64 }, opts: DefaultMetricOptionsForInflux1X(), @@ -363,12 +363,12 @@ func TestCheckFields(t *T.T) { { name: "exceed-max-int64", - f: map[string]interface{}{ + f: map[string]any{ "a": uint64(math.MaxInt64) + 1, // exceed max-int64, drop the key under non-strict mode "b": "abc", }, - expect: map[string]interface{}{ + expect: map[string]any{ "a": uint64(math.MaxInt64) + 1, "b": "abc", }, @@ -376,10 +376,10 @@ func TestCheckFields(t *T.T) { { name: "small-uint64", - f: map[string]interface{}{ + f: map[string]any{ "a": uint64(12345), }, - expect: map[string]interface{}{ + expect: map[string]any{ "a": uint64(12345), }, }, @@ -392,13 +392,13 @@ func TestCheckFields(t *T.T) { { name: "dot-in-key", - f: map[string]interface{}{ + f: map[string]any{ "a.b": 12345, "c": "12345", }, opts: []Option{WithDotInKey(false)}, warns: 1, - expect: map[string]interface{}{ + expect: map[string]any{ "a_b": int64(12345), "c": "12345", }, @@ -406,20 +406,20 @@ func TestCheckFields(t *T.T) { { name: "disabled-field", - f: map[string]interface{}{ + f: map[string]any{ "a": 12345, "b": "12345", }, warns: 1, opts: []Option{WithDisabledKeys(NewKey("a", I))}, - expect: map[string]interface{}{ + expect: map[string]any{ "b": "12345", }, }, { name: "valid-fields", - f: map[string]interface{}{ + f: map[string]any{ "small-uint64": uint64(12345), "int8": int8(1), "int": int(1), @@ -436,7 +436,7 @@ func TestCheckFields(t *T.T) { "str": "abc", }, - expect: map[string]interface{}{ + expect: map[string]any{ "small-uint64": uint64(12345), "int8": int64(1), "int": int64(1), @@ -530,7 +530,7 @@ func BenchmarkCheck(b *T.B) { name string m string t map[string]string - f map[string]interface{} + f map[string]any opts []Option }{ { @@ -539,7 +539,7 @@ func BenchmarkCheck(b *T.B) { t: map[string]string{ __shortKey: __shortVal, }, - f: map[string]interface{}{ + f: map[string]any{ "f1": 123, "f2": 123.0, "f3": __shortVal, @@ -553,7 +553,7 @@ func BenchmarkCheck(b *T.B) { t: map[string]string{ __shortKey: __shortVal, }, - f: map[string]interface{}{ + f: map[string]any{ "f1": 123, "f2": 123.0, "f3": __shortVal, @@ -569,7 +569,7 @@ func BenchmarkCheck(b *T.B) { __shortKey: __shortVal, "source": "should-be-dropped", }, - f: map[string]interface{}{ + f: map[string]any{ "f1": 123, "f2": 123.0, "f3": __shortVal, @@ -584,7 +584,7 @@ func BenchmarkCheck(b *T.B) { m: "not-set", t: func() map[string]string { x := map[string]string{} - for i := 0; i < 100; i++ { + for i := range 100 { switch i % 3 { case 0: // normal x[fmt.Sprintf("%s-%d", __shortKey, i)] = cliutils.CreateRandomString(32) @@ -596,9 +596,9 @@ func BenchmarkCheck(b *T.B) { } return x }(), - f: func() map[string]interface{} { - x := map[string]interface{}{} - for i := 0; i < 100; i++ { + f: func() map[string]any { + x := map[string]any{} + for i := range 100 { switch i % 3 { case 0: // exceed max int64 x[fmt.Sprintf("%s-%d", __shortKey, i)] = uint64(math.MaxInt64) + 1 diff --git a/point/decode_test.go b/point/decode_test.go index 7de3c401..6d34330c 100644 --- a/point/decode_test.go +++ b/point/decode_test.go @@ -286,7 +286,7 @@ func TestDecode(t *T.T) { data: func() []byte { pt, err := NewPointDeprecated("abc", map[string]string{"tag1": "v1", "tag2": "v2"}, - map[string]interface{}{"f1": 1, "f2": 2.0}, + map[string]any{"f1": 1, "f2": 2.0}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) @@ -309,7 +309,7 @@ func TestDecode(t *T.T) { data: func() []byte { pt, err := NewPointDeprecated("abc", map[string]string{"tag1": "v1", "tag2": "v2"}, - map[string]interface{}{"f1": 1, "f2": 2.0}, + map[string]any{"f1": 1, "f2": 2.0}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) @@ -329,7 +329,7 @@ func TestDecode(t *T.T) { data: func() []byte { pt, err := NewPointDeprecated("abc", map[string]string{"tag1": "v1", "tag2": "v2"}, - map[string]interface{}{"f1": 1, "f2": 2.0}, + map[string]any{"f1": 1, "f2": 2.0}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) @@ -358,7 +358,7 @@ func TestDecode(t *T.T) { data: func() []byte { pt, err := NewPointDeprecated("abc", map[string]string{"tag1": "v1", "tag2": "v2"}, - map[string]interface{}{"f1": 1, "f2": 2.0}, + map[string]any{"f1": 1, "f2": 2.0}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) diff --git a/point/encode_test.go b/point/encode_test.go index 8ac2d0ec..06c3db2e 100644 --- a/point/encode_test.go +++ b/point/encode_test.go @@ -100,17 +100,17 @@ func TestEncodeEqualty(t *T.T) { randPts = r.Rand(nrand) simplePts = []*Point{ - NewPoint(`abc`, NewKVs(map[string]interface{}{"f1": "fv1", "f2": "fv2", "f3": "fv3"}). + NewPoint(`abc`, NewKVs(map[string]any{"f1": "fv1", "f2": "fv2", "f3": "fv3"}). AddTag(`t1`, `tv1`). AddTag(`t2`, `tv2`). AddTag(`t3`, `tv3`), WithTime(time.Unix(0, 123))), - NewPoint(`def`, NewKVs(map[string]interface{}{"f1": "fv1", "f2": "fv2", "f3": "fv3"}). + NewPoint(`def`, NewKVs(map[string]any{"f1": "fv1", "f2": "fv2", "f3": "fv3"}). AddTag(`t1`, `tv1`). AddTag(`t2`, `tv2`). AddTag(`t3`, `tv3`), WithTime(time.Unix(0, 123))), - NewPoint(`xyz`, NewKVs(map[string]interface{}{"f1": "fv1", "f2": "fv2", "f3": "fv3"}). + NewPoint(`xyz`, NewKVs(map[string]any{"f1": "fv1", "f2": "fv2", "f3": "fv3"}). AddTag(`t1`, `tv1`). AddTag(`t2`, `tv2`). AddTag(`t3`, `tv3`), WithTime(time.Unix(0, 123))), @@ -142,7 +142,7 @@ func TestEncodeEqualty(t *T.T) { "t1": "tv1", "t2": "tv2", "t3": "tv3", - }, map[string]interface{}{ + }, map[string]any{ "f1": "fv1", "f2": "fv2", "f3": "fv3", @@ -405,7 +405,7 @@ func TestEncodeTags(t *T.T) { arr := func() []*Point { x, err := NewPointDeprecated("abc", map[string]string{ "service": "/sf-webproxy/api/online_status", - }, map[string]interface{}{ + }, map[string]any{ "f3": "fv3", }, WithTime(time.Unix(0, 123))) @@ -511,7 +511,7 @@ func TestEncBufUsage(t *T.T) { n := 0 usages := 0.0 - for i := 0; i < 100; i++ { + for range 100 { enc := GetEncoder(WithEncEncoding(Protobuf)) enc.EncodeV2(pts) @@ -1479,7 +1479,7 @@ func BenchmarkPointsSize(b *T.B) { func TestPointsSize(t *T.T) { r := NewRander(WithFixedTags(true), WithRandText(1)) - for i := 0; i < 10; i++ { + for range 10 { pt := r.Rand(1)[0] pt.MustAdd("s-arr", []string{"s1", "s2"}) diff --git a/point/equal.go b/point/equal.go index 1e0ca85c..6b90dd9e 100644 --- a/point/equal.go +++ b/point/equal.go @@ -9,6 +9,7 @@ import ( "crypto/md5" //nolint:gosec "crypto/sha256" "fmt" + "slices" "sort" ) @@ -20,13 +21,7 @@ type eqopt struct { } func (o *eqopt) keyExlcuded(key string) bool { - for _, k := range o.excludeKeys { - if k == key { - return true - } - } - - return false + return slices.Contains(o.excludeKeys, key) } func (o *eqopt) kvsEq(l, r KVs) (bool, string) { diff --git a/point/equal_test.go b/point/equal_test.go index 78890067..0e423c5c 100644 --- a/point/equal_test.go +++ b/point/equal_test.go @@ -25,14 +25,14 @@ func TestEqual(t *T.T) { expectEqual: true, l: func() *Point { x, err := NewPointDeprecated("abc", nil, - map[string]interface{}{"f1": 123.1}, + map[string]any{"f1": 123.1}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) return x }(), r: func() *Point { x, err := NewPointDeprecated("abc", nil, - map[string]interface{}{"f1": 123.1}, + map[string]any{"f1": 123.1}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) return x @@ -44,14 +44,14 @@ func TestEqual(t *T.T) { expectEqual: false, l: func() *Point { x, err := NewPointDeprecated("abc", nil, - map[string]interface{}{"f1": 123.1}, + map[string]any{"f1": 123.1}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) return x }(), r: func() *Point { x, err := NewPointDeprecated("abc", nil, - map[string]interface{}{"f1": 123.1}, + map[string]any{"f1": 123.1}, WithTime(time.Unix(1, 0))) assert.NoError(t, err) return x @@ -64,7 +64,7 @@ func TestEqual(t *T.T) { l: func() *Point { x, err := NewPointDeprecated("abc", map[string]string{"t1": "v1"}, - map[string]interface{}{"f1": 123.1}, + map[string]any{"f1": 123.1}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) return x @@ -72,7 +72,7 @@ func TestEqual(t *T.T) { r: func() *Point { x, err := NewPointDeprecated("abc", map[string]string{"t1": "v1"}, - map[string]interface{}{ + map[string]any{ "f1": 123.1, "t1": "duplicated-tag-key", // duplicated key }, @@ -88,7 +88,7 @@ func TestEqual(t *T.T) { l: func() *Point { x, err := NewPointDeprecated("abc", map[string]string{"t1": "v1"}, - map[string]interface{}{"f1": 123.1}, + map[string]any{"f1": 123.1}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) return x @@ -96,7 +96,7 @@ func TestEqual(t *T.T) { r: func() *Point { x, err := NewPointDeprecated("abc", map[string]string{"t1": "v1", "t2": "v2"}, - map[string]interface{}{"f1": 123.1}, + map[string]any{"f1": 123.1}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) return x @@ -109,7 +109,7 @@ func TestEqual(t *T.T) { l: func() *Point { x, err := NewPointDeprecated("abc", map[string]string{"t1": "v1"}, - map[string]interface{}{"f1": 123.1}, + map[string]any{"f1": 123.1}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) return x @@ -117,7 +117,7 @@ func TestEqual(t *T.T) { r: func() *Point { x, err := NewPointDeprecated("def", map[string]string{"t1": "v1"}, - map[string]interface{}{"f1": 123.1}, + map[string]any{"f1": 123.1}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) return x @@ -130,7 +130,7 @@ func TestEqual(t *T.T) { l: func() *Point { x, err := NewPointDeprecated("abc", map[string]string{"t1": "v1"}, - map[string]interface{}{"f1": 123.1}, + map[string]any{"f1": 123.1}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) return x @@ -138,7 +138,7 @@ func TestEqual(t *T.T) { r: func() *Point { x, err := NewPointDeprecated("abc", map[string]string{"t1": "v1"}, - map[string]interface{}{"f1": "foo"}, + map[string]any{"f1": "foo"}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) return x @@ -151,7 +151,7 @@ func TestEqual(t *T.T) { l: func() *Point { x, err := NewPointDeprecated("abc", map[string]string{"t1": "v1"}, - map[string]interface{}{"f1": 123.1}, + map[string]any{"f1": 123.1}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) return x @@ -159,7 +159,7 @@ func TestEqual(t *T.T) { r: func() *Point { x, err := NewPointDeprecated("abc", map[string]string{"t1": "v1"}, - map[string]interface{}{"f2": "foo"}, + map[string]any{"f2": "foo"}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) return x @@ -172,7 +172,7 @@ func TestEqual(t *T.T) { l: func() *Point { x, err := NewPointDeprecated("abc", map[string]string{"t1": "v1"}, - map[string]interface{}{"f1": 123.1, "f2": "haha"}, + map[string]any{"f1": 123.1, "f2": "haha"}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) return x @@ -180,7 +180,7 @@ func TestEqual(t *T.T) { r: func() *Point { x, err := NewPointDeprecated("abc", map[string]string{"t1": "v1"}, - map[string]interface{}{"f2": "foo"}, + map[string]any{"f2": "foo"}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) return x @@ -193,7 +193,7 @@ func TestEqual(t *T.T) { l: func() *Point { x, err := NewPointDeprecated("abc", map[string]string{"t1": "v1", "t2": "v2"}, - map[string]interface{}{"f1": 123.1}, + map[string]any{"f1": 123.1}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) return x @@ -201,7 +201,7 @@ func TestEqual(t *T.T) { r: func() *Point { x, err := NewPointDeprecated("abc", map[string]string{"t1": "v1"}, - map[string]interface{}{"f1": 123.1}, + map[string]any{"f1": 123.1}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) return x @@ -214,7 +214,7 @@ func TestEqual(t *T.T) { l: func() *Point { x, err := NewPointDeprecated("abc", map[string]string{"t1": "v1"}, - map[string]interface{}{"f1": 123.1}, + map[string]any{"f1": 123.1}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) return x @@ -222,7 +222,7 @@ func TestEqual(t *T.T) { r: func() *Point { x, err := NewPointDeprecated("abc", map[string]string{"t2": "v2"}, - map[string]interface{}{"f1": 123.1}, + map[string]any{"f1": 123.1}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) return x @@ -235,7 +235,7 @@ func TestEqual(t *T.T) { l: func() *Point { x, err := NewPointDeprecated("abc", map[string]string{"t1": "v1"}, - map[string]interface{}{"f1": 123.1}, + map[string]any{"f1": 123.1}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) return x @@ -243,7 +243,7 @@ func TestEqual(t *T.T) { r: func() *Point { x, err := NewPointDeprecated("abc", map[string]string{"t1": "vx"}, - map[string]interface{}{"f1": 123.1}, + map[string]any{"f1": 123.1}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) return x @@ -255,7 +255,7 @@ func TestEqual(t *T.T) { expectEqual: true, l: func() *Point { x, err := NewPointDeprecated("abc", nil, - map[string]interface{}{ + map[string]any{ "f1": int64(123), "f2_f64": 123.01234567890123456789, "f2_large_f64": 1234567890123456789.01234567890123456789, // -> 1234567890123456800 @@ -272,7 +272,7 @@ func TestEqual(t *T.T) { }(), r: func() *Point { x, err := NewPointDeprecated("abc", nil, - map[string]interface{}{ + map[string]any{ "f1": int64(123), "f2_f64": 123.01234567890123456789, "f2_large_f64": 1234567890123456789.01234567890123456789, @@ -321,7 +321,7 @@ func TestHash(t *T.T) { l: func() *Point { x, err := NewPointDeprecated("abc", map[string]string{"t1": "v1"}, - map[string]interface{}{"f1": 123.1}, + map[string]any{"f1": 123.1}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) return x @@ -329,7 +329,7 @@ func TestHash(t *T.T) { r: func() *Point { x, err := NewPointDeprecated("abc", map[string]string{"t1": "v1"}, - map[string]interface{}{"f2": 123}, + map[string]any{"f2": 123}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) return x @@ -342,7 +342,7 @@ func TestHash(t *T.T) { l: func() *Point { x, err := NewPointDeprecated("abc", map[string]string{"t1": "v1"}, - map[string]interface{}{"f1": 123.1}, + map[string]any{"f1": 123.1}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) return x @@ -350,7 +350,7 @@ func TestHash(t *T.T) { r: func() *Point { x, err := NewPointDeprecated("abc", map[string]string{"t1": "v1"}, - map[string]interface{}{"f2": 123}, + map[string]any{"f2": 123}, WithTime(time.Unix(0, 456))) assert.NoError(t, err) return x @@ -363,7 +363,7 @@ func TestHash(t *T.T) { l: func() *Point { x, err := NewPointDeprecated("def", map[string]string{"t1": "v1"}, - map[string]interface{}{"f1": 123.1}, + map[string]any{"f1": 123.1}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) return x @@ -371,7 +371,7 @@ func TestHash(t *T.T) { r: func() *Point { x, err := NewPointDeprecated("abc", map[string]string{"t1": "v1"}, - map[string]interface{}{"f2": 123}, + map[string]any{"f2": 123}, WithTime(time.Unix(0, 456))) assert.NoError(t, err) return x @@ -384,7 +384,7 @@ func TestHash(t *T.T) { l: func() *Point { x, err := NewPointDeprecated("def", map[string]string{"t2": "v1"}, - map[string]interface{}{"f1": 123.1}, + map[string]any{"f1": 123.1}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) return x @@ -392,7 +392,7 @@ func TestHash(t *T.T) { r: func() *Point { x, err := NewPointDeprecated("abc", map[string]string{"t1": "v1"}, - map[string]interface{}{"f2": 123}, + map[string]any{"f2": 123}, WithTime(time.Unix(0, 456))) assert.NoError(t, err) return x @@ -428,7 +428,7 @@ func TestTimeSeriesHash(t *T.T) { p: func() *Point { x, err := NewPointDeprecated("abc", map[string]string{}, - map[string]interface{}{"f1": 123.1}, + map[string]any{"f1": 123.1}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) return x @@ -440,7 +440,7 @@ func TestTimeSeriesHash(t *T.T) { p: func() *Point { x, err := NewPointDeprecated("abc", map[string]string{"t1": "v1"}, - map[string]interface{}{"f1": 123.1, "f2": 123.2}, + map[string]any{"f1": 123.1, "f2": 123.2}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) return x diff --git a/point/json.go b/point/json.go index 6d5a256c..3a24e429 100644 --- a/point/json.go +++ b/point/json.go @@ -21,7 +21,7 @@ type JSONPoint struct { Tags map[string]string `json:"tags,omitempty"` // Requride - Fields map[string]interface{} `json:"fields"` + Fields map[string]any `json:"fields"` // Unix nanosecond Time int64 `json:"time,omitempty"` diff --git a/point/json_test.go b/point/json_test.go index b8e73530..db6d9cea 100644 --- a/point/json_test.go +++ b/point/json_test.go @@ -28,7 +28,7 @@ func TestJSONPointMarshal(t *T.T) { p: func() *Point { pt, err := NewPointDeprecated("abc", map[string]string{"t1": "tv1", "t2": "tv2"}, - map[string]interface{}{"f1": 123, "f2": false}, + map[string]any{"f1": 123, "f2": false}, WithTime(time.Unix(0, 123))) assert.NoError(t, err) @@ -136,7 +136,7 @@ func TestJSONPoint2Point(t *T.T) { p: &JSONPoint{ Measurement: "abc", Tags: nil, - Fields: map[string]interface{}{"f1": 123, "f2": false}, + Fields: map[string]any{"f1": 123, "f2": false}, }, opts: []Option{WithTime(time.Unix(0, 123))}, expect: "abc f1=123i,f2=false 123", @@ -157,7 +157,7 @@ func TestJSONPoint2Point(t *T.T) { p: &JSONPoint{ Measurement: "", // not set Tags: nil, - Fields: map[string]interface{}{"f1": 123, "f2": false}, + Fields: map[string]any{"f1": 123, "f2": false}, }, opts: []Option{WithTime(time.Unix(0, 123))}, expect: fmt.Sprintf("%s f1=123i,f2=false 123", DefaultMeasurementName), diff --git a/point/kvs.go b/point/kvs.go index 6c1bc386..aad0063e 100644 --- a/point/kvs.go +++ b/point/kvs.go @@ -465,7 +465,7 @@ func (x KVs) Keys() *Keys { func (x KVs) shuffle() KVs { rand.Seed(time.Now().UnixNano()) // nolint: staticcheck n := len(x) - for i := 0; i < n; i++ { + for i := range n { j := rand.Intn(n) // nolint:gosec x[i], x[j] = x[j], x[i] } @@ -538,7 +538,7 @@ func NewKV(k string, v any, opts ...KVOption) *Field { } // NewKVs create kvs slice from map structure. -func NewKVs(kvs map[string]interface{}) (res KVs) { +func NewKVs(kvs map[string]any) (res KVs) { for k, v := range kvs { res = append(res, NewKV(k, v)) } diff --git a/point/kvs_test.go b/point/kvs_test.go index 604eaaa8..eabeffe5 100644 --- a/point/kvs_test.go +++ b/point/kvs_test.go @@ -119,7 +119,7 @@ func TestTrim(t *T.T) { SetPointPool(pp) defer ClearPointPool() - for loop := 0; loop < 2; loop++ { + for range 2 { var kvs KVs kvs = kvs.AddTag("t1", "v1") kvs = kvs.Add("f0", 1.23) @@ -167,7 +167,7 @@ func TestTrim(t *T.T) { SetPointPool(pp) defer ClearPointPool() - for loop := 0; loop < 2; loop++ { + for range 2 { var kvs KVs kvs = kvs.Add("f0", 1.23) @@ -613,7 +613,7 @@ func Test_shuffle(t *T.T) { func TestShuffleGzip(t *T.T) { var pts []*Point - for i := 0; i < 1000; i++ { + for range 1000 { var kvs KVs kvs = kvs.Add(`f1`, false) kvs = kvs.Add(`f2`, 123) diff --git a/point/lp_test.go b/point/lp_test.go index eeffea73..874a5870 100644 --- a/point/lp_test.go +++ b/point/lp_test.go @@ -147,7 +147,7 @@ func TestNewLPPoint(t *testing.T) { tname string // test name name string tags map[string]string - fields map[string]interface{} + fields map[string]any opts []Option expect string warns int @@ -156,7 +156,7 @@ func TestNewLPPoint(t *testing.T) { { tname: `field-key-with-dot`, name: "abc", - fields: map[string]interface{}{"f1.a": 1}, + fields: map[string]any{"f1.a": 1}, tags: map[string]string{"t1": "def"}, opts: []Option{WithTime(time.Unix(0, 123)), WithDotInKey(false)}, warns: 1, @@ -166,7 +166,7 @@ func TestNewLPPoint(t *testing.T) { { tname: `tag-key-with-dot`, name: "abc", - fields: map[string]interface{}{"f1": 1, "f2": nil}, + fields: map[string]any{"f1": 1, "f2": nil}, tags: map[string]string{"t1.a": "def"}, opts: []Option{WithTime(time.Unix(0, 123)), WithDotInKey(false)}, warns: 2, @@ -176,7 +176,7 @@ func TestNewLPPoint(t *testing.T) { { tname: `influx1.x-small-uint`, name: "abc", - fields: map[string]interface{}{"f.1": 1, "f2": uint64(32)}, + fields: map[string]any{"f.1": 1, "f2": uint64(32)}, tags: map[string]string{"t1": "abc", "t2": "32"}, opts: append(DefaultMetricOptionsForInflux1X(), WithTime(time.Unix(0, 123))), expect: "abc,t1=abc,t2=32 f.1=1i,f2=32i 123", @@ -185,7 +185,7 @@ func TestNewLPPoint(t *testing.T) { { tname: `large-field-value-length`, name: "some", - fields: map[string]interface{}{ + fields: map[string]any{ "key1": __largeVal, }, opts: []Option{WithMaxFieldValLen(len(__largeVal) - 2), WithTime(time.Unix(0, 123))}, @@ -197,7 +197,7 @@ func TestNewLPPoint(t *testing.T) { { tname: `max-field-value-length`, name: "some", - fields: map[string]interface{}{"key": "too-long-field-value-123"}, + fields: map[string]any{"key": "too-long-field-value-123"}, opts: []Option{ WithMaxFieldValLen(2), WithTime(time.Unix(0, 123)), @@ -209,7 +209,7 @@ func TestNewLPPoint(t *testing.T) { { tname: `max-field-key-length`, name: "some", - fields: map[string]interface{}{ + fields: map[string]any{ __largeVal: "123", }, opts: []Option{ @@ -223,7 +223,7 @@ func TestNewLPPoint(t *testing.T) { { tname: `max-tag-value-length`, name: "some", - fields: map[string]interface{}{"f1": 1}, + fields: map[string]any{"f1": 1}, tags: map[string]string{"key": "too-long-tag-value-123"}, opts: []Option{ WithMaxTagValLen(2), @@ -235,7 +235,7 @@ func TestNewLPPoint(t *testing.T) { { tname: `disable-string-field`, name: "some", - fields: map[string]interface{}{"f1": 1, "f2": "this is a string"}, + fields: map[string]any{"f1": 1, "f2": "this is a string"}, tags: map[string]string{"key": "string"}, opts: []Option{ WithStrField(false), @@ -248,7 +248,7 @@ func TestNewLPPoint(t *testing.T) { { tname: `max-tag-key-length`, name: "some", - fields: map[string]interface{}{"f1": 1}, + fields: map[string]any{"f1": 1}, tags: map[string]string{"too-long-tag-key": "123"}, opts: []Option{ WithMaxTagKeyLen(2), @@ -261,7 +261,7 @@ func TestNewLPPoint(t *testing.T) { { tname: `empty-measurement-name`, name: "", // empty - fields: map[string]interface{}{"f1": 1, "f2": uint64(32)}, + fields: map[string]any{"f1": 1, "f2": uint64(32)}, tags: map[string]string{"t1": "abc", "t2": "32"}, opts: []Option{ WithTime(time.Unix(0, 123)), @@ -274,7 +274,7 @@ func TestNewLPPoint(t *testing.T) { { tname: `enable-dot-in-metric-point`, name: "abc", - fields: map[string]interface{}{"f.1": 1, "f2": uint64(32)}, + fields: map[string]any{"f.1": 1, "f2": uint64(32)}, tags: map[string]string{"t.1": "abc", "t2": "32"}, opts: []Option{ WithDotInKey(true), @@ -287,7 +287,7 @@ func TestNewLPPoint(t *testing.T) { { tname: `with-disabled-field-keys`, name: "abc", - fields: map[string]interface{}{"f1": 1, "f2": uint64(32), "f3": 32}, + fields: map[string]any{"f1": 1, "f2": uint64(32), "f3": 32}, tags: map[string]string{"t1": "abc", "t2": "32"}, warns: 1, opts: []Option{ @@ -300,7 +300,7 @@ func TestNewLPPoint(t *testing.T) { { tname: `with-disabled-tag-keys`, name: "abc", - fields: map[string]interface{}{"f1": 1, "f2": uint64(32)}, + fields: map[string]any{"f1": 1, "f2": uint64(32)}, tags: map[string]string{"t1": "abc", "t2": "32"}, opts: []Option{ WithDisabledKeys(NewTagKey(`t2`, "")), @@ -314,7 +314,7 @@ func TestNewLPPoint(t *testing.T) { { tname: `int-exceed-influx1.x-int64-max`, name: "abc", - fields: map[string]interface{}{"f1": 1, "f2": uint64(math.MaxInt64) + 1}, + fields: map[string]any{"f1": 1, "f2": uint64(math.MaxInt64) + 1}, opts: append(DefaultMetricOptionsForInflux1X(), WithTime(time.Unix(0, 123))), warns: 1, @@ -324,7 +324,7 @@ func TestNewLPPoint(t *testing.T) { { tname: `extra-tags-and-field-exceed-max-tags`, name: "abc", - fields: map[string]interface{}{"f1": 1, "f2": "3"}, + fields: map[string]any{"f1": 1, "f2": "3"}, tags: map[string]string{"t1": "def", "t2": "abc"}, opts: []Option{ WithTime(time.Unix(0, 123)), @@ -344,7 +344,7 @@ func TestNewLPPoint(t *testing.T) { { tname: `extra-tags-exceed-max-tags`, name: "abc", - fields: map[string]interface{}{"f1": 1}, + fields: map[string]any{"f1": 1}, tags: map[string]string{"t1": "def", "t2": "abc"}, warns: 1, opts: []Option{ @@ -363,7 +363,7 @@ func TestNewLPPoint(t *testing.T) { { tname: `extra-tags-not-exceed-max-tags`, name: "abc", - fields: map[string]interface{}{"f1": 1}, + fields: map[string]any{"f1": 1}, tags: map[string]string{"t1": "def", "t2": "abc"}, expect: "abc,etag1=1,etag2=2,t1=def,t2=abc f1=1i 123", opts: []Option{ @@ -379,7 +379,7 @@ func TestNewLPPoint(t *testing.T) { { tname: `only-extra-tags`, name: "abc", - fields: map[string]interface{}{"f1": 1}, + fields: map[string]any{"f1": 1}, expect: "abc,etag1=1,etag2=2 f1=1i 123", opts: []Option{ @@ -395,7 +395,7 @@ func TestNewLPPoint(t *testing.T) { { tname: `exceed-max-tags`, name: "abc", - fields: map[string]interface{}{"f1": 1, "f2": nil}, + fields: map[string]any{"f1": 1, "f2": nil}, tags: map[string]string{"t1": "def", "t2": "abc"}, opts: []Option{WithTime(time.Unix(0, 123)), WithMaxTags(1), WithKeySorted(true)}, expect: `abc,t1=def f1=1i 123`, @@ -405,7 +405,7 @@ func TestNewLPPoint(t *testing.T) { { tname: `exceed-max-field`, name: "abc", - fields: map[string]interface{}{"f1": 1, "f2": 2}, + fields: map[string]any{"f1": 1, "f2": 2}, tags: map[string]string{"t1": "def"}, opts: []Option{WithTime(time.Unix(0, 123)), WithMaxFields(1)}, warns: 1, @@ -415,7 +415,7 @@ func TestNewLPPoint(t *testing.T) { { tname: `nil-field-not-allowed`, name: "abc", - fields: map[string]interface{}{"f1": 1, "f2": nil}, + fields: map[string]any{"f1": 1, "f2": nil}, tags: map[string]string{"t1": "def"}, opts: []Option{WithTime(time.Unix(0, 123))}, warns: 1, @@ -425,7 +425,7 @@ func TestNewLPPoint(t *testing.T) { { tname: `same-key-in-field-and-tag`, name: "abc", - fields: map[string]interface{}{"f1": 1, "f2": 2, "x": 123}, + fields: map[string]any{"f1": 1, "f2": 2, "x": 123}, tags: map[string]string{"t1": "def", "x": "42"}, opts: []Option{WithTime(time.Unix(0, 123))}, warns: 0, // tag `x` override field `x` before checking point(kvs.Add()), so no warning here. @@ -435,7 +435,7 @@ func TestNewLPPoint(t *testing.T) { { tname: `no-tag`, name: "abc", - fields: map[string]interface{}{"f1": 1}, + fields: map[string]any{"f1": 1}, tags: nil, opts: []Option{WithTime(time.Unix(0, 123))}, expect: "abc f1=1i 123", @@ -452,7 +452,7 @@ func TestNewLPPoint(t *testing.T) { { tname: `field-val-with-new-line`, name: "abc", - fields: map[string]interface{}{"f1": `abc + fields: map[string]any{"f1": `abc 123`}, opts: []Option{WithTime(time.Unix(0, 123))}, expect: `abc f1="abc @@ -470,7 +470,7 @@ func TestNewLPPoint(t *testing.T) { 2`: `def 456\`, }, - fields: map[string]interface{}{"f1": 123}, + fields: map[string]any{"f1": 123}, opts: []Option{WithTime(time.Unix(0, 123))}, warns: 3, expect: "abc,tag\\ 2=def\\ 456,tag1=abc\\ 123 f1=123i 123", @@ -481,7 +481,7 @@ func TestNewLPPoint(t *testing.T) { tname: `ok-case`, name: "abc", tags: nil, - fields: map[string]interface{}{"f1": 123}, + fields: map[string]any{"f1": 123}, opts: []Option{WithTime(time.Unix(0, 123))}, expect: "abc f1=123i 123", fail: false, @@ -491,7 +491,7 @@ func TestNewLPPoint(t *testing.T) { tname: `tag-key-with-backslash`, name: "abc", tags: map[string]string{"tag1": "val1", `tag2\`: `val2\`}, - fields: map[string]interface{}{"f1": 123}, + fields: map[string]any{"f1": 123}, opts: []Option{WithTime(time.Unix(0, 123))}, warns: 2, expect: `abc,tag1=val1,tag2=val2 f1=123i 123`, @@ -501,7 +501,7 @@ func TestNewLPPoint(t *testing.T) { tname: `field-is-nil`, name: "abc", tags: map[string]string{"tag1": "val1", `tag2`: `val2`}, - fields: map[string]interface{}{"f1": 123, "f2": nil}, + fields: map[string]any{"f1": 123, "f2": nil}, opts: []Option{WithTime(time.Unix(0, 123))}, warns: 1, @@ -513,7 +513,7 @@ func TestNewLPPoint(t *testing.T) { tname: `field-is-map`, name: "abc", tags: map[string]string{"tag1": "val1", `tag2`: `val2`}, - fields: map[string]interface{}{"f1": 123, "f2": map[string]interface{}{"a": "b"}}, + fields: map[string]any{"f1": 123, "f2": map[string]any{"a": "b"}}, opts: []Option{WithTime(time.Unix(0, 123))}, warns: 1, @@ -525,7 +525,7 @@ func TestNewLPPoint(t *testing.T) { tname: `field-is-object`, name: "abc", tags: map[string]string{"tag1": "val1", `tag2`: `val2`}, - fields: map[string]interface{}{"f1": 123, "f2": struct{ a string }{a: "abc"}}, + fields: map[string]any{"f1": 123, "f2": struct{ a string }{a: "abc"}}, opts: []Option{WithTime(time.Unix(0, 123))}, @@ -537,7 +537,7 @@ func TestNewLPPoint(t *testing.T) { tname: `ignore-nil-field`, name: "abc", tags: map[string]string{"tag1": "val1", `tag2\`: `val2\`}, - fields: map[string]interface{}{"f1": 123, "f2": nil}, + fields: map[string]any{"f1": 123, "f2": nil}, opts: []Option{WithTime(time.Unix(0, 123))}, warns: 3, @@ -548,7 +548,7 @@ func TestNewLPPoint(t *testing.T) { tname: `utf8-characters-in-metric-name`, name: "abc≈≈≈≈øøππ†®", tags: map[string]string{"tag1": "val1", `tag2`: `val2`}, - fields: map[string]interface{}{"f1": 123}, + fields: map[string]any{"f1": 123}, opts: []Option{WithTime(time.Unix(0, 123))}, warns: 0, @@ -560,7 +560,7 @@ func TestNewLPPoint(t *testing.T) { tname: `utf8-characters-in-metric-name-fields-tags`, name: "abc≈≈≈≈øøππ†®", tags: map[string]string{"tag1": "val1", `tag2`: `val2`, "tag3": `ºª•¶§∞¢£`, `tag-中文`: "foobar"}, - fields: map[string]interface{}{"f1": 123, "f2": "¡™£¢∞§¶•ªº", `field-中文`: "barfoo"}, + fields: map[string]any{"f1": 123, "f2": "¡™£¢∞§¶•ªº", `field-中文`: "barfoo"}, opts: []Option{WithTime(time.Unix(0, 123))}, warns: 0, @@ -583,7 +583,7 @@ func TestNewLPPoint(t *testing.T) { tname: `new-line-in-field`, name: "abc", tags: map[string]string{"tag1": "val1"}, - fields: map[string]interface{}{ + fields: map[string]any{ "f1": `aaa bbb ccc`, @@ -631,7 +631,7 @@ func TestNewLPPoint(t *testing.T) { func TestParsePoint(t *testing.T) { newPoint := func(m string, tags map[string]string, - fields map[string]interface{}, + fields map[string]any, ts ...time.Time, ) *influxdb.Point { pt, err := influxdb.NewPoint(m, tags, fields, ts...) @@ -655,7 +655,7 @@ func TestParsePoint(t *testing.T) { }{ { name: `32mb-field`, - data: []byte(fmt.Sprintf(`abc f1="%s" 123`, __32mbString)), + data: fmt.Appendf(nil, `abc f1="%s" 123`, __32mbString), opts: []Option{ WithTime(time.Unix(0, 123)), WithMaxFieldValLen(32 * 1024 * 1024), @@ -664,13 +664,13 @@ func TestParsePoint(t *testing.T) { expect: []*influxdb.Point{ newPoint("abc", nil, - map[string]interface{}{"f1": __32mbString}, + map[string]any{"f1": __32mbString}, time.Unix(0, 123)), }, }, { name: `65k-field`, - data: []byte(fmt.Sprintf(`abc f1="%s" 123`, __65kbString)), + data: fmt.Appendf(nil, `abc f1="%s" 123`, __65kbString), opts: []Option{ WithTime(time.Unix(0, 123)), WithMaxFieldValLen(0), @@ -679,7 +679,7 @@ func TestParsePoint(t *testing.T) { expect: []*influxdb.Point{ newPoint("abc", nil, - map[string]interface{}{"f1": __65kbString}, + map[string]any{"f1": __65kbString}, time.Unix(0, 123)), }, }, @@ -695,7 +695,7 @@ func TestParsePoint(t *testing.T) { expect: []*influxdb.Point{ newPoint("abc", map[string]string{"t1": "1", "t2": "2"}, - map[string]interface{}{"f2": 2.0, "f3": "abc"}, + map[string]any{"f2": 2.0, "f3": "abc"}, time.Unix(0, 123)), }, }, @@ -711,7 +711,7 @@ func TestParsePoint(t *testing.T) { expect: []*influxdb.Point{ newPoint("abc", map[string]string{"t2": "2"}, - map[string]interface{}{"f1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 123)), }, }, @@ -724,7 +724,7 @@ func TestParsePoint(t *testing.T) { expect: []*influxdb.Point{ newPoint("abc", map[string]string{"t1": "1"}, - map[string]interface{}{"f1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 123)), }, }, @@ -737,7 +737,7 @@ func TestParsePoint(t *testing.T) { expect: []*influxdb.Point{ newPoint("abc", nil, - map[string]interface{}{"f1": 1, "f2": 2.0}, + map[string]any{"f1": 1, "f2": 2.0}, time.Unix(0, 123)), }, }, @@ -749,7 +749,7 @@ func TestParsePoint(t *testing.T) { expect: []*influxdb.Point{ newPoint("abc", map[string]string{"tag_1": "xxx"}, - map[string]interface{}{"f1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 123)), }, }, @@ -761,7 +761,7 @@ func TestParsePoint(t *testing.T) { expect: []*influxdb.Point{ newPoint("abc", nil, - map[string]interface{}{"f_1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f_1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 123)), }, }, @@ -779,17 +779,17 @@ abc f1=1i,f2=2,f3="abc" 789 expect: []*influxdb.Point{ newPoint("abc", nil, - map[string]interface{}{"f1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 123)), newPoint("abc", nil, - map[string]interface{}{"f1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 456)), newPoint("abc", nil, - map[string]interface{}{"f1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 789)), }, }, @@ -802,7 +802,7 @@ abc f1=1i,f2=2,f3="abc" 789 expect: []*influxdb.Point{ newPoint("abc", map[string]string{"x": "456"}, - map[string]interface{}{"f1": "abc", "f3": 2.0}, // field x dropped + map[string]any{"f1": "abc", "f3": 2.0}, // field x dropped time.Unix(0, 123)), }, }, @@ -815,7 +815,7 @@ abc f1=1i,f2=2,f3="abc" 789 expect: []*influxdb.Point{ newPoint("abc", map[string]string{"b": "abc"}, - map[string]interface{}{"a": 1}, // field b dropped + map[string]any{"a": 1}, // field b dropped time.Unix(0, 123)), }, }, @@ -827,7 +827,7 @@ abc f1=1i,f2=2,f3="abc" 789 expect: []*influxdb.Point{ newPoint("abc", map[string]string{"b": "abc"}, - map[string]interface{}{"a": 1, "c": "xyz"}, + map[string]any{"a": 1, "c": "xyz"}, time.Unix(0, 123)), }, }, @@ -851,7 +851,7 @@ abc f1=1i,f2=2,f3="abc" 789 expect: []*influxdb.Point{ newPoint("abc", map[string]string{"tag1": "1", "tag2": "2"}, - map[string]interface{}{"f1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 123)), }, }, @@ -863,7 +863,7 @@ abc f1=1i,f2=2,f3="abc" 789 expect: []*influxdb.Point{ newPoint("abc", nil, - map[string]interface{}{"f1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 123)), }, }, @@ -881,17 +881,17 @@ abc f1=1i,f2=2,f3="abc" 789 expect: []*influxdb.Point{ newPoint("abc", nil, - map[string]interface{}{"f1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 123)), newPoint("abc", nil, - map[string]interface{}{"f1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 456)), newPoint("abc", nil, - map[string]interface{}{"f1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 789)), }, }, @@ -912,7 +912,7 @@ abc f1=1i,f2=2,f3="abc" 789 expect: []*influxdb.Point{ newPoint("abc", map[string]string{"tag1": "1", "tag2": "2"}, - map[string]interface{}{"f1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 123)), }, }, @@ -928,7 +928,7 @@ abc f1=1i,f2=2,f3="abc" 789 expect: []*influxdb.Point{ newPoint("abc", map[string]string{`tag1`: "1", "tag2": `2`}, - map[string]interface{}{"f1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 123)), }, }, @@ -944,7 +944,7 @@ abc f1=1i,f2=2,f3="abc" 789 expect: []*influxdb.Point{ newPoint("abc", map[string]string{`tag1`: "1,", "tag2": `2`, "tag3": `3`}, - map[string]interface{}{"f1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 123)), }, }, @@ -960,7 +960,7 @@ abc f1=1i,f2=2,f3="abc" 789 expect: []*influxdb.Point{ newPoint("abc", map[string]string{`tag\1`: "1", "tag2": `2\34`}, - map[string]interface{}{"f1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 123)), }, }, @@ -988,7 +988,7 @@ abc f1=1i,f2=2,f3="abc" 789 expect: []*influxdb.Point{ newPoint("abc", nil, - map[string]interface{}{"f1": 1, "f2": 2.0, "f3": "abc"}, + map[string]any{"f1": 1, "f2": 2.0, "f3": "abc"}, time.Unix(0, 123)), }, }, @@ -1114,9 +1114,9 @@ func TestParseLineProto(t *testing.T) { { name: `65kb-field-key`, - data: []byte(fmt.Sprintf(`abc,tag1=1,tag2=2 "%s"="hello" 123`, func() string { + data: fmt.Appendf(nil, `abc,tag1=1,tag2=2 "%s"="hello" 123`, func() string { return __65kbString - }())), + }()), fail: true, prec: "n", @@ -1124,9 +1124,9 @@ func TestParseLineProto(t *testing.T) { { name: `65kb-tag-key`, - data: []byte(fmt.Sprintf(`abc,tag1=1,%s=2 f1="hello" 123`, func() string { + data: fmt.Appendf(nil, `abc,tag1=1,%s=2 f1="hello" 123`, func() string { return __65kbString - }())), + }()), fail: true, prec: "n", @@ -1134,9 +1134,9 @@ func TestParseLineProto(t *testing.T) { { name: `65kb-measurement-name`, - data: []byte(fmt.Sprintf(`%s,tag1=1,t2=2 f1="hello" 123`, func() string { + data: fmt.Appendf(nil, `%s,tag1=1,t2=2 f1="hello" 123`, func() string { return __65kbString - }())), + }()), fail: true, prec: "n", @@ -1144,9 +1144,9 @@ func TestParseLineProto(t *testing.T) { { name: `32mb-field`, - data: []byte(fmt.Sprintf(`abc,tag1=1,tag2=2 f3="%s" 123`, func() string { + data: fmt.Appendf(nil, `abc,tag1=1,tag2=2 f3="%s" 123`, func() string { return __32mbString - }())), + }()), check: func(pts models.Points) error { if len(pts) != 1 { diff --git a/point/new_point_test.go b/point/new_point_test.go index 5bae5f92..edd02094 100644 --- a/point/new_point_test.go +++ b/point/new_point_test.go @@ -17,11 +17,11 @@ import ( "github.com/stretchr/testify/assert" ) -func getNFields(n int) map[string]interface{} { +func getNFields(n int) map[string]any { i := 0 - fields := map[string]interface{}{} + fields := map[string]any{} for { - var v interface{} + var v any v = i // int if i%2 == 0 { // string @@ -52,7 +52,7 @@ func getNFields(n int) map[string]interface{} { func TestV2NewPoint(t *T.T) { t.Run("valid-fields", func(t *T.T) { pt := NewPoint("abc", NewKVs( - map[string]interface{}{ + map[string]any{ "[]byte": []byte("abc"), "[]uint8": []uint8("abc"), @@ -80,7 +80,7 @@ func TestV2NewPoint(t *T.T) { }) t.Run("valid-fields-under-pb", func(t *T.T) { - kvs := map[string]interface{}{ + kvs := map[string]any{ "[]byte": []byte("abc"), "[]uint8": []uint8("abc"), "b-false": false, @@ -107,7 +107,7 @@ func TestV2NewPoint(t *T.T) { }) t.Run("basic", func(t *T.T) { - kvs := NewKVs(map[string]interface{}{"f1": 12}).SetTag(`t1`, `tval1`) + kvs := NewKVs(map[string]any{"f1": 12}).SetTag(`t1`, `tval1`) pt := NewPoint(`abc`, kvs, WithTime(time.Unix(0, 123))) assert.Equal(t, "abc,t1=tval1 f1=12i 123", pt.LineProto()) @@ -121,7 +121,7 @@ func TestNewPoint(t *T.T) { tname, name, expect string t map[string]string - f map[string]interface{} + f map[string]any warns int withPool bool @@ -131,7 +131,7 @@ func TestNewPoint(t *T.T) { opts: []Option{WithTime(time.Unix(0, 123))}, name: "valid-fields", withPool: true, - f: map[string]interface{}{ + f: map[string]any{ "[]byte": []byte("abc"), "[]uint8": []uint8("abc"), @@ -160,7 +160,7 @@ func TestNewPoint(t *T.T) { tname: "valid-fields", opts: []Option{WithTime(time.Unix(0, 123))}, name: "valid-fields", - f: map[string]interface{}{ + f: map[string]any{ "[]byte": []byte("abc"), "[]uint8": []uint8("abc"), @@ -189,7 +189,7 @@ func TestNewPoint(t *T.T) { tname: "valid-fields-under-pb", opts: []Option{WithTime(time.Unix(0, 123)), WithEncoding(Protobuf)}, name: "valid-fields", - f: map[string]interface{}{ + f: map[string]any{ "[]byte": []byte("abc"), "[]uint8": []uint8("abc"), "b-false": false, @@ -218,7 +218,7 @@ func TestNewPoint(t *T.T) { opts: []Option{WithTime(time.Unix(0, 123)), WithMaxMeasurementLen(10)}, name: "name-exceed-len", - f: map[string]interface{}{"f1": 123}, + f: map[string]any{"f1": 123}, expect: `name-excee f1=123i 123`, warns: 1, }, @@ -227,7 +227,7 @@ func TestNewPoint(t *T.T) { tname: "empty-measurement", opts: []Option{WithTime(time.Unix(0, 123))}, name: "", - f: map[string]interface{}{"f1": 123}, + f: map[string]any{"f1": 123}, expect: fmt.Sprintf(`%s f1=123i 123`, DefaultMeasurementName), warns: 1, }, @@ -247,7 +247,7 @@ func TestNewPoint(t *T.T) { opts: []Option{WithTime(time.Unix(0, 123))}, name: "abc", t: map[string]string{"t1": "tval1"}, - f: map[string]interface{}{"f1": 12}, + f: map[string]any{"f1": 12}, expect: "abc,t1=tval1 f1=12i 123", }, { @@ -255,7 +255,7 @@ func TestNewPoint(t *T.T) { name: "abc", opts: append(DefaultMetricOptions(), WithTime(time.Unix(0, 123))), t: map[string]string{"t1": "tval1"}, - f: map[string]interface{}{"f.1": 12}, + f: map[string]any{"f.1": 12}, expect: "abc,t1=tval1 f.1=12i 123", }, { @@ -263,7 +263,7 @@ func TestNewPoint(t *T.T) { name: "abc", opts: append(DefaultMetricOptions(), WithTime(time.Unix(0, 123))), t: map[string]string{"t.1": "tval1"}, - f: map[string]interface{}{"f1": 12}, + f: map[string]any{"f1": 12}, expect: "abc,t.1=tval1 f1=12i 123", }, { @@ -272,7 +272,7 @@ func TestNewPoint(t *T.T) { opts: append(DefaultObjectOptions(), WithTime(time.Unix(0, 123))), t: map[string]string{"t1": "tval1"}, - f: map[string]interface{}{"f.1": 12}, + f: map[string]any{"f.1": 12}, expect: fmt.Sprintf(`abc,t1=tval1 f_1=12i,name="%s" 123`, defaultObjectName), warns: 2, }, @@ -282,7 +282,7 @@ func TestNewPoint(t *T.T) { name: "abc", opts: append(DefaultObjectOptions(), WithTime(time.Unix(0, 123))), t: map[string]string{"t1": "abc", "t.2": "xyz"}, - f: map[string]interface{}{"f1": 123, "f.2": "def"}, + f: map[string]any{"f1": 123, "f.2": "def"}, expect: fmt.Sprintf(`abc,t1=abc,t_2=xyz f1=123i,f_2="def",name="%s" 123`, defaultObjectName), warns: 3, }, @@ -301,7 +301,7 @@ func TestNewPoint(t *T.T) { "t8": "abc", "t9": "abc", }, - f: map[string]interface{}{ + f: map[string]any{ "f1": 123, "f2": "def", "f3": "def", @@ -330,7 +330,7 @@ func TestNewPoint(t *T.T) { "t1": "abc", "t2": "xyz", }, - f: map[string]interface{}{ + f: map[string]any{ "f1": 123, "f2": "def", "f3": "def", @@ -360,7 +360,7 @@ func TestNewPoint(t *T.T) { "t8": "abc", "t9": "abc", }, - f: map[string]interface{}{ + f: map[string]any{ "f1": 123, }, expect: `abc,t1=abc f1=123i 123`, @@ -372,7 +372,7 @@ func TestNewPoint(t *T.T) { opts: []Option{WithTime(time.Unix(0, 123)), WithMaxTagKeyLen(1)}, name: "abc", t: map[string]string{"t1": "x"}, - f: map[string]interface{}{"f1": 123}, + f: map[string]any{"f1": 123}, expect: `abc,t=x f1=123i 123`, warns: 1, }, @@ -382,7 +382,7 @@ func TestNewPoint(t *T.T) { opts: []Option{WithTime(time.Unix(0, 123)), WithMaxTagValLen(3)}, name: "abc", t: map[string]string{"t": "1234"}, - f: map[string]interface{}{"f1": 123}, + f: map[string]any{"f1": 123}, expect: `abc,t=123 f1=123i 123`, warns: 1, }, @@ -391,7 +391,7 @@ func TestNewPoint(t *T.T) { tname: "exceed-max-field-key-len", name: "abc", opts: []Option{WithTime(time.Unix(0, 123)), WithMaxFieldKeyLen(3)}, - f: map[string]interface{}{"f123": 123}, + f: map[string]any{"f123": 123}, expect: `abc f12=123i 123`, warns: 1, }, @@ -400,7 +400,7 @@ func TestNewPoint(t *T.T) { tname: "exceed-max-field-val-len", name: "abc", opts: []Option{WithTime(time.Unix(0, 123)), WithMaxFieldValLen(3)}, - f: map[string]interface{}{"f1": "hello"}, + f: map[string]any{"f1": "hello"}, expect: `abc f1="hel" 123`, warns: 1, }, @@ -411,7 +411,7 @@ func TestNewPoint(t *T.T) { opts: append(DefaultLoggingOptions(), WithTime(time.Unix(0, 123))), t: map[string]string{"source": "s1"}, - f: map[string]interface{}{"f1": 123}, + f: map[string]any{"f1": 123}, expect: fmt.Sprintf(`abc f1=123i,status="%s" 123`, DefaultLoggingStatus), warns: 2, }, @@ -420,7 +420,7 @@ func TestNewPoint(t *T.T) { name: "abc", opts: append(DefaultObjectOptions(), WithTime(time.Unix(0, 123))), t: map[string]string{"class": "xyz"}, - f: map[string]interface{}{"class": 123, "f1": 1}, + f: map[string]any{"class": 123, "f1": 1}, expect: fmt.Sprintf(`abc f1=1i,name="%s" 123`, defaultObjectName), // NOTE: tag key `class` override field `class`, then the tag disabled @@ -432,7 +432,7 @@ func TestNewPoint(t *T.T) { opts: []Option{WithTime(time.Unix(0, 123))}, name: "abc", t: map[string]string{}, - f: map[string]interface{}{"f1": 123}, + f: map[string]any{"f1": 123}, expect: "abc f1=123i 123", }, @@ -440,7 +440,7 @@ func TestNewPoint(t *T.T) { tname: "invalid-category", opts: []Option{WithTime(time.Unix(0, 123))}, name: "abc", - f: map[string]interface{}{"f1": 123}, + f: map[string]any{"f1": 123}, expect: `abc f1=123i 123`, }, @@ -448,7 +448,7 @@ func TestNewPoint(t *T.T) { tname: "nil-opiton", name: "abc", t: map[string]string{}, - f: map[string]interface{}{"f1": 123}, + f: map[string]any{"f1": 123}, }, } @@ -571,7 +571,7 @@ func BenchmarkV2NewPoint(b *T.B) { "t9": "val9", "t0": "val0", } - fields := map[string]interface{}{ + fields := map[string]any{ "f1": 123, "f2": "abc", "f3": 45.6, @@ -799,7 +799,7 @@ func FuzzPLPBEquality(f *T.F) { lppt, err := NewPointDeprecated(measurement, map[string]string{tagk: tagv}, - map[string]interface{}{ + map[string]any{ "i64": i64, "u64": u64, "str": str, @@ -814,7 +814,7 @@ func FuzzPLPBEquality(f *T.F) { pbpt, err := NewPointDeprecated(measurement, map[string]string{tagk: tagv}, - map[string]interface{}{ + map[string]any{ "i64": i64, "u64": u64, "str": str, diff --git a/point/pbpoint_test.go b/point/pbpoint_test.go index b87f25b8..7abd07cd 100644 --- a/point/pbpoint_test.go +++ b/point/pbpoint_test.go @@ -50,7 +50,7 @@ func BenchmarkMarshal(b *T.B) { pts := RandPoints(tc.repeat) for i := 0; i < b.N; i++ { arr := []string{} - for i := 0; i < len(pts); i++ { + for i := range pts { arr = append(arr, pts[i].LineProto()) } x := strings.Join(arr, "\n") @@ -77,7 +77,7 @@ func BenchmarkMarshal(b *T.B) { b.Run(tc.name+"_lp-unmarshal", func(b *T.B) { pts := RandPoints(tc.repeat) arr := []string{} - for i := 0; i < len(pts); i++ { + for i := range pts { arr = append(arr, pts[i].LineProto()) } ptbytes := []byte(strings.Join(arr, "\n")) @@ -95,7 +95,7 @@ func TestPBPointJSON(t *T.T) { cases := []struct { name string tags map[string]string - fields map[string]interface{} + fields map[string]any time time.Time warns []*Warn debugs []*Debug @@ -103,7 +103,7 @@ func TestPBPointJSON(t *T.T) { { name: "simple", tags: nil, - fields: map[string]interface{}{ + fields: map[string]any{ "f1": int64(123), "f2": 123.4, "f3": false, @@ -120,7 +120,7 @@ func TestPBPointJSON(t *T.T) { "t1": "123", "t2": "xyz", }, - fields: map[string]interface{}{ + fields: map[string]any{ "f1": int64(123), "f2": 123.4, "f3": false, @@ -136,7 +136,7 @@ func TestPBPointJSON(t *T.T) { tags: map[string]string{ "t1": "123", }, - fields: map[string]interface{}{ + fields: map[string]any{ "t1": "dulicated key in tags", // triger warnning "f1": int64(123), }, @@ -148,7 +148,7 @@ func TestPBPointJSON(t *T.T) { tags: map[string]string{ "t1": "123", }, - fields: map[string]interface{}{ + fields: map[string]any{ "t1": "dulicated key in tags", "f1": int64(123), }, diff --git a/point/point_test.go b/point/point_test.go index ce4396d5..82d17bf2 100644 --- a/point/point_test.go +++ b/point/point_test.go @@ -39,14 +39,14 @@ func TestSizeofPoint(t *T.T) { kvs = kvs.SetTag("t2", "v2") pbpt := NewPoint("some", kvs) - t.Logf("type size(pbpt): %d", reflect.TypeOf(*pbpt).Size()) + t.Logf("type size(pbpt): %d", reflect.TypeFor[Point]().Size()) t.Logf("value size(pbpt): %d", pbpt.Size()) }) t.Run("rand-large-pt", func(t *T.T) { r := NewRander(WithFixedTags(true), WithRandText(3)) pts := r.Rand(1) - t.Logf("type size(pbpt): %d", reflect.TypeOf(*pts[0]).Size()) + t.Logf("type size(pbpt): %d", reflect.TypeFor[Point]().Size()) t.Logf("value size(pbpt): %d", pts[0].Size()) }) } @@ -337,7 +337,7 @@ func TestPointString(t *T.T) { pt: func() *Point { pt, err := NewPointDeprecated("abc", map[string]string{"tag1": "v1"}, - map[string]interface{}{ + map[string]any{ "f1": 123, "f2": true, }, WithTime(time.Unix(0, 123))) @@ -355,7 +355,7 @@ func TestPointString(t *T.T) { "tag1": "v1", "tag2": "v2", "xtag": "vx", - }, map[string]interface{}{ + }, map[string]any{ "f1": 123, "f2": true, "f3": uint64(123), @@ -434,7 +434,7 @@ func TestPointLineProtocol(t *T.T) { name: "lp-point-ns-prec", prec: PrecNS, pt: func() *Point { - pt, err := NewPointDeprecated("abc", nil, map[string]interface{}{"f1": 1}, + pt, err := NewPointDeprecated("abc", nil, map[string]any{"f1": 1}, append(DefaultLoggingOptions(), WithTime(time.Unix(0, 123)))...) assert.NoError(t, err) @@ -449,7 +449,7 @@ func TestPointLineProtocol(t *T.T) { name: "lp-point-ms-prec", prec: PrecMS, pt: func() *Point { - pt, err := NewPointDeprecated("abc", nil, map[string]interface{}{"f1": 1}, + pt, err := NewPointDeprecated("abc", nil, map[string]any{"f1": 1}, append(DefaultLoggingOptions(), WithTime(time.Unix(0, 12345678)))...) assert.NoError(t, err) @@ -462,7 +462,7 @@ func TestPointLineProtocol(t *T.T) { name: "lp-point-us-prec", prec: PrecUS, // only accept u pt: func() *Point { - pt, err := NewPointDeprecated("abc", nil, map[string]interface{}{"f1": 1}, + pt, err := NewPointDeprecated("abc", nil, map[string]any{"f1": 1}, append(DefaultLoggingOptions(), WithTime(time.Unix(0, 12345678)))...) assert.NoError(t, err) @@ -475,7 +475,7 @@ func TestPointLineProtocol(t *T.T) { name: "lp-point-ns-prec", prec: PrecNS, // only accept u pt: func() *Point { - pt, err := NewPointDeprecated("abc", nil, map[string]interface{}{"f1": 1}, + pt, err := NewPointDeprecated("abc", nil, map[string]any{"f1": 1}, append(DefaultLoggingOptions(), WithTime(time.Unix(0, 12345678)))...) assert.NoError(t, err) return pt @@ -487,7 +487,7 @@ func TestPointLineProtocol(t *T.T) { name: "lp-point-invalid-prec", prec: -1, pt: func() *Point { - pt, err := NewPointDeprecated("abc", nil, map[string]interface{}{"f1": 1}, + pt, err := NewPointDeprecated("abc", nil, map[string]any{"f1": 1}, append(DefaultLoggingOptions(), WithTime(time.Unix(0, 12345678)))...) assert.NoError(t, err) return pt @@ -499,7 +499,7 @@ func TestPointLineProtocol(t *T.T) { name: "lp-point-second-prec", prec: PrecS, pt: func() *Point { - pt, err := NewPointDeprecated("abc", nil, map[string]interface{}{"f1": 1}, + pt, err := NewPointDeprecated("abc", nil, map[string]any{"f1": 1}, append(DefaultLoggingOptions(), WithTime(time.Unix(1, 123456789)))...) assert.NoError(t, err) return pt @@ -511,7 +511,7 @@ func TestPointLineProtocol(t *T.T) { name: "lp-point-minute-prec", prec: PrecM, pt: func() *Point { - pt, err := NewPointDeprecated("abc", nil, map[string]interface{}{"f1": 1}, + pt, err := NewPointDeprecated("abc", nil, map[string]any{"f1": 1}, append(DefaultLoggingOptions(), WithTime(time.Unix(120, 123456789)))...) assert.NoError(t, err) return pt @@ -523,7 +523,7 @@ func TestPointLineProtocol(t *T.T) { name: "lp-point-hour-prec", prec: PrecH, pt: func() *Point { - pt, err := NewPointDeprecated("abc", nil, map[string]interface{}{"f1": 1}, + pt, err := NewPointDeprecated("abc", nil, map[string]any{"f1": 1}, append(DefaultLoggingOptions(), WithTime(time.Unix(7199, 123456789)))...) assert.NoError(t, err) return pt @@ -538,7 +538,7 @@ func TestPointLineProtocol(t *T.T) { pt: func() *Point { pt, err := NewPointDeprecated("abc", nil, - map[string]interface{}{"f1": int64(1)}, + map[string]any{"f1": int64(1)}, WithTime(time.Unix(0, 123)), WithEncoding(Protobuf)) assert.NoError(t, err) @@ -555,7 +555,7 @@ func TestPointLineProtocol(t *T.T) { pt: func() *Point { pt, err := NewPointDeprecated("abc", map[string]string{"t1": "v1"}, - map[string]interface{}{"f1": []byte("abc123")}, + map[string]any{"f1": []byte("abc123")}, WithTime(time.Unix(0, 1)), WithEncoding(Protobuf)) assert.NoError(t, err) @@ -749,14 +749,14 @@ func TestFields(t *T.T) { cases := []struct { name string pt *Point - expect map[string]interface{} + expect map[string]any }{ { name: "basic-lp-point", pt: func() *Point { x, err := NewPointDeprecated("abc", nil, - map[string]interface{}{ + map[string]any{ "i8": int8(1), "u8": uint8(1), "i16": int16(1), @@ -778,7 +778,7 @@ func TestFields(t *T.T) { return x }(), - expect: map[string]interface{}{ + expect: map[string]any{ "i8": int64(1), "u8": uint64(1), "i16": int64(1), @@ -803,7 +803,7 @@ func TestFields(t *T.T) { pt: func() *Point { x, err := NewPointDeprecated("abc", nil, - map[string]interface{}{ + map[string]any{ "bool_1": false, "bool_2": true, "data": []byte("abc123"), @@ -825,7 +825,7 @@ func TestFields(t *T.T) { return x }(), - expect: map[string]interface{}{ + expect: map[string]any{ // "any": someAny, "bool_1": false, "bool_2": true, @@ -918,7 +918,7 @@ func FuzzPBPointString(f *T.F) { ) { pt, err := NewPointDeprecated(measurement, map[string]string{tagk: tagv}, - map[string]interface{}{ + map[string]any{ "i64": i64, "u64": u64, "str": str, diff --git a/point/ptpool_test.go b/point/ptpool_test.go index 13e835c7..fd4a4e03 100644 --- a/point/ptpool_test.go +++ b/point/ptpool_test.go @@ -253,7 +253,7 @@ func TestPointPool(t *T.T) { defer ClearPointPool() // total add 100 * 4 Field - for i := 0; i < 100; i++ { + for i := range 100 { var kvs KVs kvs = kvs.Add(fmt.Sprintf("f%d", i), 123) kvs = kvs.Add(fmt.Sprintf("f%d", i+1), 123) @@ -283,7 +283,7 @@ func TestPointPool(t *T.T) { f := func() { defer wg.Done() - for i := 0; i < 100; i++ { + for i := range 100 { var kvs KVs kvs = kvs.Add(fmt.Sprintf("f-%d", i*100), 123) kvs = kvs.Add(fmt.Sprintf("f-%d", i*100+1), 123) @@ -298,7 +298,7 @@ func TestPointPool(t *T.T) { n := 100 wg.Add(n) - for i := 0; i < n; i++ { + for range n { go f() } @@ -393,7 +393,7 @@ func TestPointPoolMetrics(t *T.T) { metrics.MustRegister(pp) // total add 100 * 4 Field - for i := 0; i < 100; i++ { + for i := range 100 { func() { var kvs KVs kvs = kvs.Add(fmt.Sprintf("f%d", i), 123) @@ -603,7 +603,7 @@ func TestPoolKVResuable(t *T.T) { var f Foo maxPT := (1 << 20) - for i := 0; i < maxPT; i++ { + for i := range maxPT { assert.NoError(t, gofakeit.Struct(&f)) var kvs KVs kvs = kvs.AddTag("T_"+f.T1Key, f.T1) diff --git a/point/rand.go b/point/rand.go index a841a646..a73d09b9 100644 --- a/point/rand.go +++ b/point/rand.go @@ -126,7 +126,7 @@ func (r *ptRander) Rand(count int) []*Point { pts := make([]*Point, 0, count) - for i := 0; i < count; i++ { + for range count { pts = append(pts, r.doRand()) } diff --git a/point/rand_deprecated.go b/point/rand_deprecated.go index 70451197..784f330f 100644 --- a/point/rand_deprecated.go +++ b/point/rand_deprecated.go @@ -35,7 +35,7 @@ func doRandomPoints(count int) ([]*Point, error) { } var pts []*Point - for i := 0; i < count; i++ { + for range count { if pt, err := NewPointDeprecated(cliutils.CreateRandomString(30), map[string]string{ // 4 tags cliutils.CreateRandomString(10): sampleLogs[mrand.Int63()%int64(len(sampleLogs))], @@ -44,7 +44,7 @@ func doRandomPoints(count int) ([]*Point, error) { cliutils.CreateRandomString(13): sampleLogs[mrand.Int63()%int64(len(sampleLogs))], }, - map[string]interface{}{ + map[string]any{ "message": sampleLogs[mrand.Int63()%int64(len(sampleLogs))], cliutils.CreateRandomString(10): mrand.Int63(), cliutils.CreateRandomString(10): mrand.Int63(), @@ -97,7 +97,7 @@ func doRandomPBPoints(count int) (*PBPoints, error) { pts := &PBPoints{} - for i := 0; i < count; i++ { + for range count { pts.Arr = append(pts.Arr, &PBPoint{ Name: cliutils.CreateRandomString(30), Fields: []*Field{ diff --git a/prom2metrics.go b/prom2metrics.go index 93061a7f..4776b8b0 100644 --- a/prom2metrics.go +++ b/prom2metrics.go @@ -128,7 +128,7 @@ func (p *prom) getValue(m *dto.Metric, v value) { } tags := labelToTags(m.GetLabel()) - fields := map[string]interface{}{p.metricName: v.GetValue()} + fields := map[string]any{p.metricName: v.GetValue()} pt, err := p.newPoint(tags, fields, m.GetTimestampMs()) if err != nil { @@ -148,7 +148,7 @@ func (p *prom) getCountAndSum(m *dto.Metric, c countAndSum) { } tags := labelToTags(m.GetLabel()) - fields := map[string]interface{}{ + fields := map[string]any{ p.metricName + "_count": float64(c.GetSampleCount()), p.metricName + "_sum": c.GetSampleSum(), } @@ -160,7 +160,7 @@ func (p *prom) getCountAndSum(m *dto.Metric, c countAndSum) { p.pts = append(p.pts, pt) } -func (p *prom) newPoint(tags map[string]string, fields map[string]interface{}, ts int64) (*ifxcli.Point, error) { +func (p *prom) newPoint(tags map[string]string, fields map[string]any, ts int64) (*ifxcli.Point, error) { if ts > 0 { p.t = time.Unix(0, ts*int64(time.Millisecond)) } diff --git a/testutil/http.go b/testutil/http.go index 8a29b9c4..5182b404 100644 --- a/testutil/http.go +++ b/testutil/http.go @@ -15,7 +15,7 @@ import ( type HTTPServerOptions struct { Bind string - Exit chan interface{} + Exit chan any SSL bool CrtFile string diff --git a/testutil/http_test.go b/testutil/http_test.go index c6e9eb02..f96b2548 100644 --- a/testutil/http_test.go +++ b/testutil/http_test.go @@ -18,7 +18,7 @@ import ( func TestHTTPServer(t *testing.T) { opt := &HTTPServerOptions{ Bind: ":12345", - Exit: make(chan interface{}), + Exit: make(chan any), Routes: map[string]func(*gin.Context){ "/route1": func(*gin.Context) { fmt.Printf("on route1") }, "/route2": func(*gin.Context) { fmt.Printf("on route2") }, @@ -27,12 +27,9 @@ func TestHTTPServer(t *testing.T) { wg := sync.WaitGroup{} - wg.Add(1) - - go func() { - defer wg.Done() + wg.Go(func() { NewHTTPServer(t, opt) - }() + }) time.Sleep(time.Second) diff --git a/testutil/t.go b/testutil/t.go index 8ce481c9..73b9f914 100644 --- a/testutil/t.go +++ b/testutil/t.go @@ -12,10 +12,10 @@ import ( type TB interface { Helper() - Fatalf(string, ...interface{}) + Fatalf(string, ...any) } -func Assert(tb TB, condition bool, fmt string, a ...interface{}) { +func Assert(tb TB, condition bool, fmt string, a ...any) { tb.Helper() if !condition { tb.Fatalf("\033[31m"+fmt+"\033[39m\n", a...) @@ -29,7 +29,7 @@ func Ok(tb TB, err error) { } } -func NotOk(tb TB, err error, fmt string, a ...interface{}) { +func NotOk(tb TB, err error, fmt string, a ...any) { tb.Helper() if err == nil { if len(a) != 0 { @@ -39,7 +39,7 @@ func NotOk(tb TB, err error, fmt string, a ...interface{}) { } } -func Equals(tb TB, exp, act interface{}) { +func Equals(tb TB, exp, act any) { tb.Helper() if !reflect.DeepEqual(exp, act) { tb.Fatalf("\033[31m\nexp: %#v\n\ngot: %#v\033[39m\n", exp, act) diff --git a/tracer/option.go b/tracer/option.go index 6f03a2b1..24c47038 100644 --- a/tracer/option.go +++ b/tracer/option.go @@ -49,7 +49,7 @@ func WithDebug(debug bool) StartOption { } } -func WithGlobalTag(key string, value interface{}) StartOption { +func WithGlobalTag(key string, value any) StartOption { return func(t *Tracer) { t.Tags[key] = value } diff --git a/tracer/tracer.go b/tracer/tracer.go index bb030d92..0a597706 100644 --- a/tracer/tracer.go +++ b/tracer/tracer.go @@ -34,15 +34,15 @@ type Tracer struct { agentAddr string - LogsStartup bool `toml:"logs_startup" yaml:"logs_startup"` // env: DD_TRACE_STARTUP_LOGS - Debug bool `toml:"debug" yaml:"debug"` // env: DD_TRACE_DEBUG - TraceEnabled bool `toml:"trace_enabled" yaml:"trace_enabled"` // env: DD_TRACE_ENABLED - Tags map[string]interface{} `toml:"tags" yaml:"tags"` // env: DD_TAGS + LogsStartup bool `toml:"logs_startup" yaml:"logs_startup"` // env: DD_TRACE_STARTUP_LOGS + Debug bool `toml:"debug" yaml:"debug"` // env: DD_TRACE_DEBUG + TraceEnabled bool `toml:"trace_enabled" yaml:"trace_enabled"` // env: DD_TRACE_ENABLED + Tags map[string]any `toml:"tags" yaml:"tags"` // env: DD_TAGS } func (t *Tracer) GetStartOptions(opts ...StartOption) []tracer.StartOption { if t.Tags == nil { - t.Tags = make(map[string]interface{}) + t.Tags = make(map[string]any) } for i := range opts { diff --git a/utils.go b/utils.go index d2a64ba2..e8f03526 100644 --- a/utils.go +++ b/utils.go @@ -28,11 +28,11 @@ import ( ) type Sem struct { - sem chan interface{} + sem chan any } func NewSem() *Sem { - return &Sem{sem: make(chan interface{})} + return &Sem{sem: make(chan any)} } func (s *Sem) Close() { @@ -44,12 +44,12 @@ func (s *Sem) Close() { } } -func (s *Sem) Wait() <-chan interface{} { +func (s *Sem) Wait() <-chan any { return s.sem } func WgWait(wg *sync.WaitGroup, timeout int) { - c := make(chan interface{}) + c := make(chan any) go func() { defer close(c) diff --git a/whitelist.go b/whitelist.go index b5941abb..f952a541 100644 --- a/whitelist.go +++ b/whitelist.go @@ -16,8 +16,8 @@ func NewWhiteListItem(pattern string) *WhiteListItem { pattern = strings.TrimSpace(pattern) // 处理正则表达式模式 - if strings.HasPrefix(pattern, "reg:") { - regexPattern := strings.TrimPrefix(pattern, "reg:") + if after, ok := strings.CutPrefix(pattern, "reg:"); ok { + regexPattern := after return &WhiteListItem{ s: regexPattern, re: regexp.MustCompile(regexPattern),