From c3ff7f58df675dfa5c4c6db85125e70a7fc7627e Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Fri, 28 Mar 2025 19:53:47 -0700 Subject: [PATCH] Use any instead of interface{} It's available since Go 1.18 (see https://pkg.go.dev/builtin#any). Signed-off-by: Kir Kolyshkin --- cmd/containers-storage/main.go | 2 +- containers.go | 8 ++++---- deprecated.go | 6 +++--- drivers/driver.go | 4 ++-- drivers/overlay/composefs.go | 2 +- images.go | 6 +++--- layers.go | 12 ++++++------ pkg/archive/archive.go | 6 +++--- pkg/archive/archive_linux.go | 2 +- pkg/archive/archive_unix.go | 6 +++--- pkg/chunked/dump/dump.go | 2 +- pkg/chunked/storage_linux.go | 4 ++-- pkg/ioutils/bytespipe.go | 2 +- pkg/mflag/flag.go | 22 +++++++++++----------- pkg/pools/pools.go | 4 ++-- pkg/unshare/unshare_linux.go | 2 +- store.go | 12 ++++++------ 17 files changed, 51 insertions(+), 51 deletions(-) diff --git a/cmd/containers-storage/main.go b/cmd/containers-storage/main.go index 9b1547cd7..1d6b821f4 100644 --- a/cmd/containers-storage/main.go +++ b/cmd/containers-storage/main.go @@ -148,7 +148,7 @@ func main() { // outputJSON formats its input as JSON to stdout, and returns values suitable // for directly returning from command.action -func outputJSON(data interface{}) (int, error) { +func outputJSON(data any) (int, error) { if err := json.NewEncoder(os.Stdout).Encode(data); err != nil { return 1, err } diff --git a/containers.go b/containers.go index 143fde297..6e973dcab 100644 --- a/containers.go +++ b/containers.go @@ -82,7 +82,7 @@ type Container struct { UIDMap []idtools.IDMap `json:"uidmap,omitempty"` GIDMap []idtools.IDMap `json:"gidmap,omitempty"` - Flags map[string]interface{} `json:"flags,omitempty"` + Flags map[string]any `json:"flags,omitempty"` // volatileStore is true if the container is from the volatile json file volatileStore bool `json:"-"` @@ -196,7 +196,7 @@ func (c *Container) MountOpts() []string { switch value := c.Flags[mountOptsFlag].(type) { case []string: return value - case []interface{}: + case []any: var mountOpts []string for _, v := range value { if flag, ok := v.(string); ok { @@ -641,13 +641,13 @@ func (r *containerStore) ClearFlag(id string, flag string) error { } // Requires startWriting. -func (r *containerStore) SetFlag(id string, flag string, value interface{}) error { +func (r *containerStore) SetFlag(id string, flag string, value any) error { container, ok := r.lookup(id) if !ok { return ErrContainerUnknown } if container.Flags == nil { - container.Flags = make(map[string]interface{}) + container.Flags = make(map[string]any) } container.Flags[flag] = value return r.saveFor(container) diff --git a/deprecated.go b/deprecated.go index 76ae6328b..5fadc27df 100644 --- a/deprecated.go +++ b/deprecated.go @@ -111,7 +111,7 @@ type LayerBigDataStore interface { // Deprecated: There is no way to use this from any external user of c/storage to invoke c/storage functionality. type FlaggableStore interface { ClearFlag(id string, flag string) error - SetFlag(id string, flag string, value interface{}) error + SetFlag(id string, flag string, value any) error } // ContainerStore is a deprecated interface with no documented way to use it from callers outside of c/storage. @@ -195,8 +195,8 @@ type LayerStore interface { FlaggableStore RWLayerBigDataStore Create(id string, parent *Layer, names []string, mountLabel string, options map[string]string, moreOptions *LayerOptions, writeable bool) (*Layer, error) - CreateWithFlags(id string, parent *Layer, names []string, mountLabel string, options map[string]string, moreOptions *LayerOptions, writeable bool, flags map[string]interface{}) (layer *Layer, err error) - Put(id string, parent *Layer, names []string, mountLabel string, options map[string]string, moreOptions *LayerOptions, writeable bool, flags map[string]interface{}, diff io.Reader) (*Layer, int64, error) + CreateWithFlags(id string, parent *Layer, names []string, mountLabel string, options map[string]string, moreOptions *LayerOptions, writeable bool, flags map[string]any) (layer *Layer, err error) + Put(id string, parent *Layer, names []string, mountLabel string, options map[string]string, moreOptions *LayerOptions, writeable bool, flags map[string]any, diff io.Reader) (*Layer, int64, error) SetNames(id string, names []string) error AddNames(id string, names []string) error RemoveNames(id string, names []string) error diff --git a/drivers/driver.go b/drivers/driver.go index 529cd66c0..06263e750 100644 --- a/drivers/driver.go +++ b/drivers/driver.go @@ -79,7 +79,7 @@ type ApplyDiffOpts struct { type ApplyDiffWithDifferOpts struct { ApplyDiffOpts - Flags map[string]interface{} + Flags map[string]any } // DedupArgs contains the information to perform storage deduplication. @@ -222,7 +222,7 @@ type DriverWithDifferOutput struct { RootDirMode *os.FileMode // Artifacts is a collection of additional artifacts // generated by the differ that the storage driver can use. - Artifacts map[string]interface{} + Artifacts map[string]any } type DifferOutputFormat int diff --git a/drivers/overlay/composefs.go b/drivers/overlay/composefs.go index 016f353f0..270425a51 100644 --- a/drivers/overlay/composefs.go +++ b/drivers/overlay/composefs.go @@ -42,7 +42,7 @@ func getComposefsBlob(dataDir string) string { return filepath.Join(dataDir, "composefs.blob") } -func generateComposeFsBlob(verityDigests map[string]string, toc interface{}, composefsDir string) error { +func generateComposeFsBlob(verityDigests map[string]string, toc any, composefsDir string) error { if err := os.MkdirAll(composefsDir, 0o700); err != nil { return err } diff --git a/images.go b/images.go index 5c9127ede..74f738cd7 100644 --- a/images.go +++ b/images.go @@ -93,7 +93,7 @@ type Image struct { // ReadOnly is true if this image resides in a read-only layer store. ReadOnly bool `json:"-"` - Flags map[string]interface{} `json:"flags,omitempty"` + Flags map[string]any `json:"flags,omitempty"` } // roImageStore provides bookkeeping for information about Images. @@ -675,7 +675,7 @@ func (r *imageStore) ClearFlag(id string, flag string) error { } // Requires startWriting. -func (r *imageStore) SetFlag(id string, flag string, value interface{}) error { +func (r *imageStore) SetFlag(id string, flag string, value any) error { if !r.lockfile.IsReadWrite() { return fmt.Errorf("not allowed to set flags on images at %q: %w", r.imagespath(), ErrStoreIsReadOnly) } @@ -684,7 +684,7 @@ func (r *imageStore) SetFlag(id string, flag string, value interface{}) error { return fmt.Errorf("locating image with ID %q: %w", id, ErrImageUnknown) } if image.Flags == nil { - image.Flags = make(map[string]interface{}) + image.Flags = make(map[string]any) } image.Flags[flag] = value return r.Save() diff --git a/layers.go b/layers.go index 8fbd842fb..9ab139c63 100644 --- a/layers.go +++ b/layers.go @@ -161,7 +161,7 @@ type Layer struct { GIDs []uint32 `json:"gidset,omitempty"` // Flags is arbitrary data about the layer. - Flags map[string]interface{} `json:"flags,omitempty"` + Flags map[string]any `json:"flags,omitempty"` // UIDMap and GIDMap are used for setting up a layer's contents // for use inside of a user namespace where UID mapping is being used. @@ -922,7 +922,7 @@ func (r *layerStore) load(lockedForWriting bool) (bool, error) { var layersToDelete []*Layer for _, layer := range r.layers { if layer.Flags == nil { - layer.Flags = make(map[string]interface{}) + layer.Flags = make(map[string]any) } if layerHasIncompleteFlag(layer) { // Important: Do not call r.deleteInternal() here. It modifies r.layers @@ -1261,7 +1261,7 @@ func (r *layerStore) ClearFlag(id string, flag string) error { } // Requires startWriting. -func (r *layerStore) SetFlag(id string, flag string, value interface{}) error { +func (r *layerStore) SetFlag(id string, flag string, value any) error { if !r.lockfile.IsReadWrite() { return fmt.Errorf("not allowed to set flags on layers at %q: %w", r.layerdir, ErrStoreIsReadOnly) } @@ -1270,7 +1270,7 @@ func (r *layerStore) SetFlag(id string, flag string, value interface{}) error { return ErrLayerUnknown } if layer.Flags == nil { - layer.Flags = make(map[string]interface{}) + layer.Flags = make(map[string]any) } layer.Flags[flag] = value return r.saveFor(layer) @@ -1931,7 +1931,7 @@ func (r *layerStore) deleteInternal(id string) error { // Ensure that if we are interrupted, the layer will be cleaned up. if !layerHasIncompleteFlag(layer) { if layer.Flags == nil { - layer.Flags = make(map[string]interface{}) + layer.Flags = make(map[string]any) } layer.Flags[incompleteFlag] = true if err := r.saveFor(layer); err != nil { @@ -2540,7 +2540,7 @@ func (r *layerStore) applyDiffFromStagingDirectory(id string, diffOutput *driver layer.Metadata = diffOutput.Metadata if options != nil && options.Flags != nil { if layer.Flags == nil { - layer.Flags = make(map[string]interface{}) + layer.Flags = make(map[string]any) } maps.Copy(layer.Flags, options.Flags) } diff --git a/pkg/archive/archive.go b/pkg/archive/archive.go index 6ce681283..d63fe24cc 100644 --- a/pkg/archive/archive.go +++ b/pkg/archive/archive.go @@ -53,7 +53,7 @@ type ( // This is additional data to be used by the converter. It will // not survive a round trip through JSON, so it's primarily // intended for generating archives (i.e., converting writes). - WhiteoutData interface{} + WhiteoutData any // When unpacking, specifies whether overwriting a directory with a // non-directory is allowed and vice versa. NoOverwriteDirNonDir bool @@ -83,7 +83,7 @@ const ( freebsd = "freebsd" ) -var xattrsToIgnore = map[string]interface{}{ +var xattrsToIgnore = map[string]any{ "security.selinux": true, } @@ -378,7 +378,7 @@ type nosysFileInfo struct { os.FileInfo } -func (fi nosysFileInfo) Sys() interface{} { +func (fi nosysFileInfo) Sys() any { // A Sys value of type *tar.Header is safe as it is system-independent. // The tar.FileInfoHeader function copies the fields into the returned // header without performing any OS lookups. diff --git a/pkg/archive/archive_linux.go b/pkg/archive/archive_linux.go index b9d718b60..011668627 100644 --- a/pkg/archive/archive_linux.go +++ b/pkg/archive/archive_linux.go @@ -16,7 +16,7 @@ func getOverlayOpaqueXattrName() string { return GetOverlayXattrName("opaque") } -func GetWhiteoutConverter(format WhiteoutFormat, data interface{}) TarWhiteoutConverter { +func GetWhiteoutConverter(format WhiteoutFormat, data any) TarWhiteoutConverter { if format == OverlayWhiteoutFormat { if rolayers, ok := data.([]string); ok && len(rolayers) > 0 { return overlayWhiteoutConverter{rolayers: rolayers} diff --git a/pkg/archive/archive_unix.go b/pkg/archive/archive_unix.go index 56f2086bc..3a02a88c1 100644 --- a/pkg/archive/archive_unix.go +++ b/pkg/archive/archive_unix.go @@ -67,7 +67,7 @@ func chmodTarEntry(perm os.FileMode) os.FileMode { return perm // noop for unix as golang APIs provide perm bits correctly } -func setHeaderForSpecialDevice(hdr *tar.Header, name string, stat interface{}) (err error) { +func setHeaderForSpecialDevice(hdr *tar.Header, name string, stat any) (err error) { s, ok := stat.(*syscall.Stat_t) if ok { @@ -82,7 +82,7 @@ func setHeaderForSpecialDevice(hdr *tar.Header, name string, stat interface{}) ( return } -func getInodeFromStat(stat interface{}) (inode uint64, err error) { +func getInodeFromStat(stat any) (inode uint64, err error) { s, ok := stat.(*syscall.Stat_t) if ok { @@ -92,7 +92,7 @@ func getInodeFromStat(stat interface{}) (inode uint64, err error) { return } -func getFileUIDGID(stat interface{}) (idtools.IDPair, error) { +func getFileUIDGID(stat any) (idtools.IDPair, error) { s, ok := stat.(*syscall.Stat_t) if !ok { diff --git a/pkg/chunked/dump/dump.go b/pkg/chunked/dump/dump.go index 0e673f3f2..3a5f61917 100644 --- a/pkg/chunked/dump/dump.go +++ b/pkg/chunked/dump/dump.go @@ -214,7 +214,7 @@ func dumpNode(out io.Writer, added map[string]*minimal.FileMetadata, links map[s } // GenerateDump generates a dump of the TOC in the same format as `composefs-info dump` -func GenerateDump(tocI interface{}, verityDigests map[string]string) (io.Reader, error) { +func GenerateDump(tocI any, verityDigests map[string]string) (io.Reader, error) { toc, ok := tocI.(*minimal.TOC) if !ok { return nil, fmt.Errorf("invalid TOC type") diff --git a/pkg/chunked/storage_linux.go b/pkg/chunked/storage_linux.go index d0d8b3a77..1f3e229f9 100644 --- a/pkg/chunked/storage_linux.go +++ b/pkg/chunked/storage_linux.go @@ -111,7 +111,7 @@ type chunkedDiffer struct { useFsVerity graphdriver.DifferFsVerity } -var xattrsToIgnore = map[string]interface{}{ +var xattrsToIgnore = map[string]any{ "security.selinux": true, } @@ -1483,7 +1483,7 @@ func (c *chunkedDiffer) ApplyDiff(dest string, options *archive.TarOptions, diff bigDataKey: c.manifest, chunkedLayerDataKey: lcdBigData, }, - Artifacts: map[string]interface{}{ + Artifacts: map[string]any{ tocKey: toc, }, TOCDigest: c.tocDigest, diff --git a/pkg/ioutils/bytespipe.go b/pkg/ioutils/bytespipe.go index e6cff03a5..cf6058035 100644 --- a/pkg/ioutils/bytespipe.go +++ b/pkg/ioutils/bytespipe.go @@ -175,7 +175,7 @@ func getBuffer(size int) *fixedBuffer { bufPoolsLock.Lock() pool, ok := bufPools[size] if !ok { - pool = &sync.Pool{New: func() interface{} { return &fixedBuffer{buf: make([]byte, 0, size)} }} + pool = &sync.Pool{New: func() any { return &fixedBuffer{buf: make([]byte, 0, size)} }} bufPools[size] = pool } bufPoolsLock.Unlock() diff --git a/pkg/mflag/flag.go b/pkg/mflag/flag.go index a4ac13e00..85a2bf342 100644 --- a/pkg/mflag/flag.go +++ b/pkg/mflag/flag.go @@ -116,7 +116,7 @@ func (b *boolValue) Set(s string) error { return err } -func (b *boolValue) Get() interface{} { return bool(*b) } +func (b *boolValue) Get() any { return bool(*b) } func (b *boolValue) String() string { return fmt.Sprintf("%v", *b) } @@ -143,7 +143,7 @@ func (i *intValue) Set(s string) error { return err } -func (i *intValue) Get() interface{} { return int(*i) } +func (i *intValue) Get() any { return int(*i) } func (i *intValue) String() string { return fmt.Sprintf("%v", *i) } @@ -161,7 +161,7 @@ func (i *int64Value) Set(s string) error { return err } -func (i *int64Value) Get() interface{} { return int64(*i) } +func (i *int64Value) Get() any { return int64(*i) } func (i *int64Value) String() string { return fmt.Sprintf("%v", *i) } @@ -179,7 +179,7 @@ func (i *uintValue) Set(s string) error { return err } -func (i *uintValue) Get() interface{} { return uint(*i) } +func (i *uintValue) Get() any { return uint(*i) } func (i *uintValue) String() string { return fmt.Sprintf("%v", *i) } @@ -197,7 +197,7 @@ func (i *uint64Value) Set(s string) error { return err } -func (i *uint64Value) Get() interface{} { return uint64(*i) } +func (i *uint64Value) Get() any { return uint64(*i) } func (i *uint64Value) String() string { return fmt.Sprintf("%v", *i) } @@ -215,7 +215,7 @@ func (i *uint16Value) Set(s string) error { return err } -func (i *uint16Value) Get() interface{} { return uint16(*i) } +func (i *uint16Value) Get() any { return uint16(*i) } func (i *uint16Value) String() string { return fmt.Sprintf("%v", *i) } @@ -232,7 +232,7 @@ func (s *stringValue) Set(val string) error { return nil } -func (s *stringValue) Get() interface{} { return string(*s) } +func (s *stringValue) Get() any { return string(*s) } func (s *stringValue) String() string { return string(*s) } @@ -250,7 +250,7 @@ func (f *float64Value) Set(s string) error { return err } -func (f *float64Value) Get() interface{} { return float64(*f) } +func (f *float64Value) Get() any { return float64(*f) } func (f *float64Value) String() string { return fmt.Sprintf("%v", *f) } @@ -268,7 +268,7 @@ func (d *durationValue) Set(s string) error { return err } -func (d *durationValue) Get() interface{} { return time.Duration(*d) } +func (d *durationValue) Get() any { return time.Duration(*d) } func (d *durationValue) String() string { return (*time.Duration)(d).String() } @@ -289,7 +289,7 @@ type Value interface { // by this package satisfy the Getter interface. type Getter interface { Value - Get() interface{} + Get() any } // ErrorHandling defines how to handle flag parsing errors. @@ -932,7 +932,7 @@ func Var(value Value, names []string, usage string) { // failf prints to standard error a formatted error and usage message and // returns the error. -func (fs *FlagSet) failf(format string, a ...interface{}) error { +func (fs *FlagSet) failf(format string, a ...any) error { err := fmt.Errorf(format, a...) fmt.Fprintln(fs.Out(), err) if os.Args[0] == fs.name { diff --git a/pkg/pools/pools.go b/pkg/pools/pools.go index a15e3688b..e68648d1d 100644 --- a/pkg/pools/pools.go +++ b/pkg/pools/pools.go @@ -40,7 +40,7 @@ func init() { // added here to be shared where required. func newBufioReaderPoolWithSize(size int) *BufioReaderPool { pool := &sync.Pool{ - New: func() interface{} { return bufio.NewReaderSize(nil, size) }, + New: func() any { return bufio.NewReaderSize(nil, size) }, } return &BufioReaderPool{pool: pool} } @@ -87,7 +87,7 @@ type BufioWriterPool struct { // added here to be shared where required. func newBufioWriterPoolWithSize(size int) *BufioWriterPool { pool := &sync.Pool{ - New: func() interface{} { return bufio.NewWriterSize(nil, size) }, + New: func() any { return bufio.NewWriterSize(nil, size) }, } return &BufioWriterPool{pool: pool} } diff --git a/pkg/unshare/unshare_linux.go b/pkg/unshare/unshare_linux.go index 29f0c8ac9..3f63d3191 100644 --- a/pkg/unshare/unshare_linux.go +++ b/pkg/unshare/unshare_linux.go @@ -468,7 +468,7 @@ type Runnable interface { Run() error } -func bailOnError(err error, format string, a ...interface{}) { // nolint: revive,goprintffuncname +func bailOnError(err error, format string, a ...any) { // nolint: revive,goprintffuncname if err != nil { if format != "" { logrus.Errorf("%s: %v", fmt.Sprintf(format, a...), err) diff --git a/store.go b/store.go index c7ada8881..2221cd501 100644 --- a/store.go +++ b/store.go @@ -162,7 +162,7 @@ type flaggableStore interface { ClearFlag(id string, flag string) error // SetFlag sets a named flag and its value on an item in the store. - SetFlag(id string, flag string, value interface{}) error + SetFlag(id string, flag string, value any) error } type StoreOptions = types.StoreOptions @@ -672,7 +672,7 @@ type LayerOptions struct { // Flags is a set of named flags and their values to store with the layer. // Currently these can only be set when the layer record is created, but that // could change in the future. - Flags map[string]interface{} + Flags map[string]any } type LayerBigDataOption struct { @@ -700,7 +700,7 @@ type ImageOptions struct { NamesHistory []string // Flags is a set of named flags and their values to store with the image. Currently these can only // be set when the image record is created, but that could change in the future. - Flags map[string]interface{} + Flags map[string]any } type ImageBigDataOption struct { @@ -720,7 +720,7 @@ type ContainerOptions struct { // Flags is a set of named flags and their values to store with the container. // Currently these can only be set when the container record is created, but that // could change in the future. - Flags map[string]interface{} + Flags map[string]any MountOpts []string Volatile bool StorageOpt map[string]string @@ -1649,7 +1649,7 @@ func (s *store) CreateImage(id string, names []string, layer, metadata string, i options.BigData = append(options.BigData, copyImageBigDataOptionSlice(iOptions.BigData)...) options.NamesHistory = append(options.NamesHistory, iOptions.NamesHistory...) if options.Flags == nil { - options.Flags = make(map[string]interface{}) + options.Flags = make(map[string]any) } maps.Copy(options.Flags, iOptions.Flags) } @@ -1918,7 +1918,7 @@ func (s *store) CreateContainer(id string, names []string, image, layer, metadat } } if options.Flags == nil { - options.Flags = make(map[string]interface{}) + options.Flags = make(map[string]any) } plabel, _ := options.Flags[processLabelFlag].(string) mlabel, _ := options.Flags[mountLabelFlag].(string)