Merge pull request #3053 from tonistiigi/modernize-fixes

lint: apply x/tools/modernize fixes and validation
This commit is contained in:
Tõnis Tiigi
2025-03-10 18:37:51 -07:00
committed by GitHub
50 changed files with 238 additions and 266 deletions

View File

@@ -90,7 +90,7 @@ func (a *Attest) ToPB() *controllerapi.Attest {
}
func (a *Attest) MarshalJSON() ([]byte, error) {
m := make(map[string]interface{}, len(a.Attrs)+2)
m := make(map[string]any, len(a.Attrs)+2)
for k, v := range a.Attrs {
m[k] = v
}
@@ -102,7 +102,7 @@ func (a *Attest) MarshalJSON() ([]byte, error) {
}
func (a *Attest) UnmarshalJSON(data []byte) error {
var m map[string]interface{}
var m map[string]any
if err := json.Unmarshal(data, &m); err != nil {
return err
}

View File

@@ -150,7 +150,7 @@ func (e *CacheOptionsEntry) UnmarshalText(text []byte) error {
return e.validate(text)
}
func (e *CacheOptionsEntry) validate(gv interface{}) error {
func (e *CacheOptionsEntry) validate(gv any) error {
if e.Type == "" {
var text []byte
switch gv := gv.(type) {

View File

@@ -33,7 +33,7 @@ func removeDupes[E comparable[E]](s []E) []E {
return s
}
func getAndDelete(m map[string]cty.Value, attr string, gv interface{}) error {
func getAndDelete(m map[string]cty.Value, attr string, gv any) error {
if v, ok := m[attr]; ok && v.IsKnown() {
delete(m, attr)
return gocty.FromCtyValue(v, gv)

View File

@@ -156,7 +156,7 @@ func (r *Resolver) Combine(ctx context.Context, srcs []*Source, ann map[exptypes
case exptypes.AnnotationIndex:
indexAnnotation[k.Key] = v
case exptypes.AnnotationManifestDescriptor:
for i := 0; i < len(newDescs); i++ {
for i := range newDescs {
if newDescs[i].Annotations == nil {
newDescs[i].Annotations = map[string]string{}
}

View File

@@ -278,8 +278,8 @@ func (l *loader) scanConfig(ctx context.Context, fetcher remotes.Fetcher, desc o
}
type sbomStub struct {
SPDX interface{} `json:",omitempty"`
AdditionalSPDXs []interface{} `json:",omitempty"`
SPDX any `json:",omitempty"`
AdditionalSPDXs []any `json:",omitempty"`
}
func (l *loader) scanSBOM(ctx context.Context, fetcher remotes.Fetcher, r *result, refs []digest.Digest, as *asset) error {
@@ -309,7 +309,7 @@ func (l *loader) scanSBOM(ctx context.Context, fetcher remotes.Fetcher, r *resul
}
var spdx struct {
Predicate interface{} `json:"predicate"`
Predicate any `json:"predicate"`
}
if err := json.Unmarshal(dt, &spdx); err != nil {
return nil, err
@@ -330,7 +330,7 @@ func (l *loader) scanSBOM(ctx context.Context, fetcher remotes.Fetcher, r *resul
}
type provenanceStub struct {
SLSA interface{} `json:",omitempty"`
SLSA any `json:",omitempty"`
}
func (l *loader) scanProvenance(ctx context.Context, fetcher remotes.Fetcher, r *result, refs []digest.Digest, as *asset) error {
@@ -360,7 +360,7 @@ func (l *loader) scanProvenance(ctx context.Context, fetcher remotes.Fetcher, r
}
var slsa struct {
Predicate interface{} `json:"predicate"`
Predicate any `json:"predicate"`
}
if err := json.Unmarshal(dt, &slsa); err != nil {
return nil, err

View File

@@ -89,7 +89,7 @@ func (p *Printer) Print(raw bool, out io.Writer) error {
}
tpl, err := template.New("").Funcs(template.FuncMap{
"json": func(v interface{}) string {
"json": func(v any) string {
b, _ := json.MarshalIndent(v, "", " ")
return string(b)
},
@@ -101,7 +101,7 @@ func (p *Printer) Print(raw bool, out io.Writer) error {
imageconfigs := res.Configs()
format := tpl.Root.String()
var mfst interface{}
var mfst any
switch p.manifest.MediaType {
case images.MediaTypeDockerSchema2Manifest, ocispecs.MediaTypeImageManifest:
mfst = p.manifest
@@ -206,7 +206,7 @@ func (p *Printer) printManifestList(out io.Writer) error {
type tplInput struct {
Name string `json:"name,omitempty"`
Manifest interface{} `json:"manifest,omitempty"`
Manifest any `json:"manifest,omitempty"`
Image *ocispecs.Image `json:"image,omitempty"`
result *result
@@ -236,7 +236,7 @@ func (inp tplInput) Provenance() (provenanceStub, error) {
type tplInputs struct {
Name string `json:"name,omitempty"`
Manifest interface{} `json:"manifest,omitempty"`
Manifest any `json:"manifest,omitempty"`
Image map[string]*ocispecs.Image `json:"image,omitempty"`
result *result

View File

@@ -126,7 +126,7 @@ func TestMuxIO(t *testing.T) {
if tt.outputsNum != len(tt.wants) {
t.Fatalf("wants != outputsNum")
}
for i := 0; i < tt.outputsNum; i++ {
for i := range tt.outputsNum {
outBuf, out := newTestOut(i)
outBufs = append(outBufs, outBuf)
outs = append(outs, MuxOut{out, nil, nil})
@@ -304,7 +304,7 @@ func writeMasked(w io.Writer, s string) io.Writer {
return
}
var masked string
for i := 0; i < n; i++ {
for range n {
masked += s
}
if _, err := w.Write([]byte(masked)); err != nil {

View File

@@ -83,9 +83,9 @@ type Log struct {
// KeyValue is a key-value pair with typed value.
type KeyValue struct {
Key string `json:"key"`
Type ValueType `json:"type,omitempty"`
Value interface{} `json:"value"`
Key string `json:"key"`
Type ValueType `json:"type,omitempty"`
Value any `json:"value"`
}
// DependencyLink shows dependencies between services

View File

@@ -149,7 +149,7 @@ type keyValue struct {
// value is a custom type used to unmarshal otel Value correctly.
type value struct {
Type string
Value interface{}
Value any
}
// UnmarshalJSON implements json.Unmarshaler for Span which allows correctly
@@ -318,7 +318,7 @@ func (kv *keyValue) asAttributeKeyValue() (attribute.KeyValue, error) {
switch sli := kv.Value.Value.(type) {
case []string:
strSli = sli
case []interface{}:
case []any:
for i := range sli {
var v string
// best case we have a string, otherwise, cast it using

View File

@@ -131,7 +131,7 @@ func TestAsAttributeKeyValue(t *testing.T) {
name: "stringslice (interface of string)",
args: args{
Type: attribute.STRINGSLICE.String(),
value: []interface{}{"value1", "value2"},
value: []any{"value1", "value2"},
},
want: attribute.StringSlice("key", []string{"value1", "value2"}),
},
@@ -139,7 +139,7 @@ func TestAsAttributeKeyValue(t *testing.T) {
name: "stringslice (interface mixed)",
args: args{
Type: attribute.STRINGSLICE.String(),
value: []interface{}{"value1", 2},
value: []any{"value1", 2},
},
want: attribute.StringSlice("key", []string{"value1", "2"}),
},

View File

@@ -27,7 +27,7 @@ type Printer struct {
err error
warnings []client.VertexWarning
logMu sync.Mutex
logSourceMap map[digest.Digest]interface{}
logSourceMap map[digest.Digest]any
metrics *metricWriter
// TODO: remove once we can use result context to pass build ref
@@ -74,7 +74,7 @@ func (p *Printer) Warnings() []client.VertexWarning {
return dedupWarnings(p.warnings)
}
func (p *Printer) ValidateLogSource(dgst digest.Digest, v interface{}) bool {
func (p *Printer) ValidateLogSource(dgst digest.Digest, v any) bool {
p.logMu.Lock()
defer p.logMu.Unlock()
src, ok := p.logSourceMap[dgst]
@@ -89,7 +89,7 @@ func (p *Printer) ValidateLogSource(dgst digest.Digest, v interface{}) bool {
return false
}
func (p *Printer) ClearLogSource(v interface{}) {
func (p *Printer) ClearLogSource(v any) {
p.logMu.Lock()
defer p.logMu.Unlock()
for d := range p.logSourceMap {
@@ -125,7 +125,7 @@ func NewPrinter(ctx context.Context, out console.File, mode progressui.DisplayMo
pw.closeOnce = sync.Once{}
pw.logMu.Lock()
pw.logSourceMap = map[digest.Digest]interface{}{}
pw.logSourceMap = map[digest.Digest]any{}
pw.logMu.Unlock()
resumeLogs := logutil.Pause(logrus.StandardLogger())

View File

@@ -11,8 +11,8 @@ import (
type Writer interface {
Write(*client.SolveStatus)
WriteBuildRef(string, string)
ValidateLogSource(digest.Digest, interface{}) bool
ClearLogSource(interface{})
ValidateLogSource(digest.Digest, any) bool
ClearLogSource(any)
}
func Write(w Writer, name string, f func() error) error {

View File

@@ -7,18 +7,18 @@ import (
type Map struct {
mu sync.RWMutex
m map[string]interface{}
m map[string]any
ch map[string]chan struct{}
}
func New() *Map {
return &Map{
m: make(map[string]interface{}),
m: make(map[string]any),
ch: make(map[string]chan struct{}),
}
}
func (m *Map) Set(key string, value interface{}) {
func (m *Map) Set(key string, value any) {
m.mu.Lock()
defer m.mu.Unlock()
@@ -32,13 +32,13 @@ func (m *Map) Set(key string, value interface{}) {
m.ch[key] = nil
}
func (m *Map) Get(ctx context.Context, keys ...string) (map[string]interface{}, error) {
func (m *Map) Get(ctx context.Context, keys ...string) (map[string]any, error) {
if len(keys) == 0 {
return map[string]interface{}{}, nil
return map[string]any{}, nil
}
if len(keys) > 1 {
out := make(map[string]interface{})
out := make(map[string]any)
for _, key := range keys {
mm, err := m.Get(ctx, key)
if err != nil {
@@ -70,5 +70,5 @@ func (m *Map) Get(ctx context.Context, keys ...string) (map[string]interface{},
res := m.m[key]
m.mu.Unlock()
return map[string]interface{}{key: res}, nil
return map[string]any{key: res}, nil
}