Skip to content

Commit 98f2965

Browse files
authored
Merge pull request moby#3707 from jedevc/comparable-interfaces
Use comparables instead of reflection in result struct
2 parents baaf67b + 8f603db commit 98f2965

File tree

6 files changed

+32
-25
lines changed

6 files changed

+32
-25
lines changed

frontend/attestations/sbom/sbom.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const (
3131
// build-contexts or multi-stage builds. Handling these separately allows the
3232
// scanner to optionally ignore these or to mark them as such in the
3333
// attestation.
34-
type Scanner func(ctx context.Context, name string, ref llb.State, extras map[string]llb.State, opts ...llb.ConstraintsOpt) (result.Attestation[llb.State], error)
34+
type Scanner func(ctx context.Context, name string, ref llb.State, extras map[string]llb.State, opts ...llb.ConstraintsOpt) (result.Attestation[*llb.State], error)
3535

3636
func CreateSBOMScanner(ctx context.Context, resolver llb.ImageMetaResolver, scanner string) (Scanner, error) {
3737
if scanner == "" {
@@ -55,7 +55,7 @@ func CreateSBOMScanner(ctx context.Context, resolver llb.ImageMetaResolver, scan
5555
return nil, errors.Errorf("scanner %s does not have cmd", scanner)
5656
}
5757

58-
return func(ctx context.Context, name string, ref llb.State, extras map[string]llb.State, opts ...llb.ConstraintsOpt) (result.Attestation[llb.State], error) {
58+
return func(ctx context.Context, name string, ref llb.State, extras map[string]llb.State, opts ...llb.ConstraintsOpt) (result.Attestation[*llb.State], error) {
5959
var env []string
6060
env = append(env, cfg.Config.Env...)
6161
env = append(env, "BUILDKIT_SCAN_DESTINATION="+outDir)
@@ -86,9 +86,9 @@ func CreateSBOMScanner(ctx context.Context, resolver llb.ImageMetaResolver, scan
8686
}
8787

8888
stsbom := runscan.AddMount(outDir, llb.Scratch())
89-
return result.Attestation[llb.State]{
89+
return result.Attestation[*llb.State]{
9090
Kind: gatewaypb.AttestationKindBundle,
91-
Ref: stsbom,
91+
Ref: &stsbom,
9292
Metadata: map[string][]byte{
9393
result.AttestationReasonKey: []byte(result.AttestationReasonSBOM),
9494
result.AttestationSBOMCore: []byte(CoreSBOMName),
@@ -100,7 +100,7 @@ func CreateSBOMScanner(ctx context.Context, resolver llb.ImageMetaResolver, scan
100100
}, nil
101101
}
102102

103-
func HasSBOM[T any](res *result.Result[T]) bool {
103+
func HasSBOM[T comparable](res *result.Result[T]) bool {
104104
for _, as := range res.Attestations {
105105
for _, a := range as {
106106
if a.InToto.PredicateType == intoto.PredicateSPDX {

frontend/dockerfile/builder/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func Build(ctx context.Context, c client.Client) (_ *client.Result, err error) {
169169
return err
170170
}
171171

172-
attSolve, err := result.ConvertAttestation(&att, func(st llb.State) (client.Reference, error) {
172+
attSolve, err := result.ConvertAttestation(&att, func(st *llb.State) (client.Reference, error) {
173173
def, err := st.Marshal(ctx)
174174
if err != nil {
175175
return nil, err

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/moby/buildkit
22

3-
go 1.19
3+
go 1.20
44

55
require (
66
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.1.0

solver/llbsolver/proc/sbom.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func SBOMProcessor(scannerRef string, useCache bool) llbsolver.Processor {
5252
if err != nil {
5353
return nil, err
5454
}
55-
attSolve, err := result.ConvertAttestation(&att, func(st llb.State) (solver.ResultProxy, error) {
55+
attSolve, err := result.ConvertAttestation(&att, func(st *llb.State) (solver.ResultProxy, error) {
5656
def, err := st.Marshal(ctx)
5757
if err != nil {
5858
return nil, err

solver/result/attestation.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package result
22

33
import (
4-
"reflect"
5-
64
pb "github.com/moby/buildkit/frontend/gateway/pb"
75
digest "github.com/opencontainers/go-digest"
86
)
@@ -58,9 +56,11 @@ func FromDigestMap(m map[string]string) []digest.Digest {
5856
return ds
5957
}
6058

61-
func ConvertAttestation[U any, V any](a *Attestation[U], fn func(U) (V, error)) (*Attestation[V], error) {
59+
func ConvertAttestation[U comparable, V comparable](a *Attestation[U], fn func(U) (V, error)) (*Attestation[V], error) {
60+
var zero U
61+
6262
var ref V
63-
if reflect.ValueOf(a.Ref).IsValid() {
63+
if a.Ref != zero {
6464
var err error
6565
ref, err = fn(a.Ref)
6666
if err != nil {

solver/result/result.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package result
22

33
import (
4-
"reflect"
54
"sync"
65

76
"github.com/pkg/errors"
87
)
98

10-
type Result[T any] struct {
9+
type Result[T comparable] struct {
1110
mu sync.Mutex
1211
Ref T
1312
Refs map[string]T
@@ -50,7 +49,8 @@ func (r *Result[T]) SingleRef() (T, error) {
5049
r.mu.Lock()
5150
defer r.mu.Unlock()
5251

53-
if r.Refs != nil && !reflect.ValueOf(r.Ref).IsValid() {
52+
var zero T
53+
if r.Refs != nil && r.Ref == zero {
5454
var t T
5555
return t, errors.Errorf("invalid map result")
5656
}
@@ -77,19 +77,20 @@ func (r *Result[T]) FindRef(key string) (T, bool) {
7777
}
7878

7979
func (r *Result[T]) EachRef(fn func(T) error) (err error) {
80-
if reflect.ValueOf(r.Ref).IsValid() {
80+
var zero T
81+
if r.Ref != zero {
8182
err = fn(r.Ref)
8283
}
8384
for _, r := range r.Refs {
84-
if reflect.ValueOf(r).IsValid() {
85+
if r != zero {
8586
if err1 := fn(r); err1 != nil && err == nil {
8687
err = err1
8788
}
8889
}
8990
}
9091
for _, as := range r.Attestations {
9192
for _, a := range as {
92-
if reflect.ValueOf(a.Ref).IsValid() {
93+
if a.Ref != zero {
9394
if err1 := fn(a.Ref); err1 != nil && err == nil {
9495
err = err1
9596
}
@@ -102,16 +103,20 @@ func (r *Result[T]) EachRef(fn func(T) error) (err error) {
102103
// EachRef iterates over references in both a and b.
103104
// a and b are assumed to be of the same size and map their references
104105
// to the same set of keys
105-
func EachRef[U any, V any](a *Result[U], b *Result[V], fn func(U, V) error) (err error) {
106-
if reflect.ValueOf(a.Ref).IsValid() && reflect.ValueOf(b.Ref).IsValid() {
106+
func EachRef[U comparable, V comparable](a *Result[U], b *Result[V], fn func(U, V) error) (err error) {
107+
var (
108+
zeroU U
109+
zeroV V
110+
)
111+
if a.Ref != zeroU && b.Ref != zeroV {
107112
err = fn(a.Ref, b.Ref)
108113
}
109114
for k, r := range a.Refs {
110115
r2, ok := b.Refs[k]
111116
if !ok {
112117
continue
113118
}
114-
if reflect.ValueOf(r).IsValid() && reflect.ValueOf(r2).IsValid() {
119+
if r != zeroU && r2 != zeroV {
115120
if err1 := fn(r, r2); err1 != nil && err == nil {
116121
err = err1
117122
}
@@ -127,7 +132,7 @@ func EachRef[U any, V any](a *Result[U], b *Result[V], fn func(U, V) error) (err
127132
break
128133
}
129134
att2 := atts2[i]
130-
if reflect.ValueOf(att.Ref).IsValid() && reflect.ValueOf(att2.Ref).IsValid() {
135+
if att.Ref != zeroU && att2.Ref != zeroV {
131136
if err1 := fn(att.Ref, att2.Ref); err1 != nil && err == nil {
132137
err = err1
133138
}
@@ -137,11 +142,13 @@ func EachRef[U any, V any](a *Result[U], b *Result[V], fn func(U, V) error) (err
137142
return err
138143
}
139144

140-
func ConvertResult[U any, V any](r *Result[U], fn func(U) (V, error)) (*Result[V], error) {
145+
func ConvertResult[U comparable, V comparable](r *Result[U], fn func(U) (V, error)) (*Result[V], error) {
146+
var zero U
147+
141148
r2 := &Result[V]{}
142149
var err error
143150

144-
if reflect.ValueOf(r.Ref).IsValid() {
151+
if r.Ref != zero {
145152
r2.Ref, err = fn(r.Ref)
146153
if err != nil {
147154
return nil, err
@@ -152,7 +159,7 @@ func ConvertResult[U any, V any](r *Result[U], fn func(U) (V, error)) (*Result[V
152159
r2.Refs = map[string]V{}
153160
}
154161
for k, r := range r.Refs {
155-
if !reflect.ValueOf(r).IsValid() {
162+
if r == zero {
156163
continue
157164
}
158165
r2.Refs[k], err = fn(r)

0 commit comments

Comments
 (0)