buildflags: make work on go 1.22 by reverting rangefunc usage

Reverts the usage of rangefunc and attempts to keep the foundation of it
in for when we move to go 1.23. We have downstream dependencies that
aren't ready to move to go 1.23. We can likely move after go 1.24 is
released.

Signed-off-by: Jonathan A. Sternberg <jonathan.sternberg@docker.com>
This commit is contained in:
Jonathan A. Sternberg 2025-02-10 11:03:46 -06:00
parent 0a4eb7ec76
commit 3ae9970da5
No known key found for this signature in database
GPG Key ID: 6603D4B96394F6B1
7 changed files with 52 additions and 33 deletions

2
go.mod
View File

@ -1,6 +1,6 @@
module github.com/docker/buildx module github.com/docker/buildx
go 1.23.0 go 1.22.0
require ( require (
github.com/Masterminds/semver/v3 v3.2.1 github.com/Masterminds/semver/v3 v3.2.1

View File

@ -22,16 +22,19 @@ func (e *Attests) FromCtyValue(in cty.Value, p cty.Path) error {
return p.NewErrorf("%s", convert.MismatchMessage(got, want)) return p.NewErrorf("%s", convert.MismatchMessage(got, want))
} }
func (e *Attests) fromCtyValue(in cty.Value, p cty.Path) error { func (e *Attests) fromCtyValue(in cty.Value, p cty.Path) (retErr error) {
*e = make([]*Attest, 0, in.LengthInt()) *e = make([]*Attest, 0, in.LengthInt())
for value := range eachElement(in) {
yield := func(value cty.Value) bool {
entry := &Attest{} entry := &Attest{}
if err := entry.FromCtyValue(value, p); err != nil { if retErr = entry.FromCtyValue(value, p); retErr != nil {
return err return false
} }
*e = append(*e, entry) *e = append(*e, entry)
return true
} }
return nil eachElement(in)(yield)
return retErr
} }
func (e Attests) ToCtyValue() cty.Value { func (e Attests) ToCtyValue() cty.Value {

View File

@ -21,26 +21,30 @@ func (o *CacheOptions) FromCtyValue(in cty.Value, p cty.Path) error {
return p.NewErrorf("%s", convert.MismatchMessage(got, want)) return p.NewErrorf("%s", convert.MismatchMessage(got, want))
} }
func (o *CacheOptions) fromCtyValue(in cty.Value, p cty.Path) error { func (o *CacheOptions) fromCtyValue(in cty.Value, p cty.Path) (retErr error) {
*o = make([]*CacheOptionsEntry, 0, in.LengthInt()) *o = make([]*CacheOptionsEntry, 0, in.LengthInt())
for value := range eachElement(in) {
yield := func(value cty.Value) bool {
// Special handling for a string type to handle ref only format. // Special handling for a string type to handle ref only format.
if value.Type() == cty.String { if value.Type() == cty.String {
entries, err := ParseCacheEntry([]string{value.AsString()}) var entries CacheOptions
if err != nil { entries, retErr = ParseCacheEntry([]string{value.AsString()})
return err if retErr != nil {
return false
} }
*o = append(*o, entries...) *o = append(*o, entries...)
continue return true
} }
entry := &CacheOptionsEntry{} entry := &CacheOptionsEntry{}
if err := entry.FromCtyValue(value, p); err != nil { if retErr = entry.FromCtyValue(value, p); retErr != nil {
return err return false
} }
*o = append(*o, entry) *o = append(*o, entry)
return true
} }
return nil eachElement(in)(yield)
return retErr
} }
func (o CacheOptions) ToCtyValue() cty.Value { func (o CacheOptions) ToCtyValue() cty.Value {

View File

@ -21,16 +21,19 @@ func (e *Exports) FromCtyValue(in cty.Value, p cty.Path) error {
return p.NewErrorf("%s", convert.MismatchMessage(got, want)) return p.NewErrorf("%s", convert.MismatchMessage(got, want))
} }
func (e *Exports) fromCtyValue(in cty.Value, p cty.Path) error { func (e *Exports) fromCtyValue(in cty.Value, p cty.Path) (retErr error) {
*e = make([]*ExportEntry, 0, in.LengthInt()) *e = make([]*ExportEntry, 0, in.LengthInt())
for value := range eachElement(in) {
yield := func(value cty.Value) bool {
entry := &ExportEntry{} entry := &ExportEntry{}
if err := entry.FromCtyValue(value, p); err != nil { if retErr = entry.FromCtyValue(value, p); retErr != nil {
return err return false
} }
*e = append(*e, entry) *e = append(*e, entry)
return true
} }
return nil eachElement(in)(yield)
return retErr
} }
func (e Exports) ToCtyValue() cty.Value { func (e Exports) ToCtyValue() cty.Value {

View File

@ -28,16 +28,19 @@ func (s *Secrets) FromCtyValue(in cty.Value, p cty.Path) error {
return p.NewErrorf("%s", convert.MismatchMessage(got, want)) return p.NewErrorf("%s", convert.MismatchMessage(got, want))
} }
func (s *Secrets) fromCtyValue(in cty.Value, p cty.Path) error { func (s *Secrets) fromCtyValue(in cty.Value, p cty.Path) (retErr error) {
*s = make([]*Secret, 0, in.LengthInt()) *s = make([]*Secret, 0, in.LengthInt())
for value := range eachElement(in) {
yield := func(value cty.Value) bool {
entry := &Secret{} entry := &Secret{}
if err := entry.FromCtyValue(value, p); err != nil { if retErr = entry.FromCtyValue(value, p); retErr != nil {
return err return false
} }
*s = append(*s, entry) *s = append(*s, entry)
return true
} }
return nil eachElement(in)(yield)
return retErr
} }
func (s Secrets) ToCtyValue() cty.Value { func (s Secrets) ToCtyValue() cty.Value {

View File

@ -28,16 +28,19 @@ func (s *SSHKeys) FromCtyValue(in cty.Value, p cty.Path) error {
return p.NewErrorf("%s", convert.MismatchMessage(got, want)) return p.NewErrorf("%s", convert.MismatchMessage(got, want))
} }
func (s *SSHKeys) fromCtyValue(in cty.Value, p cty.Path) error { func (s *SSHKeys) fromCtyValue(in cty.Value, p cty.Path) (retErr error) {
*s = make([]*SSH, 0, in.LengthInt()) *s = make([]*SSH, 0, in.LengthInt())
for value := range eachElement(in) {
yield := func(value cty.Value) bool {
entry := &SSH{} entry := &SSH{}
if err := entry.FromCtyValue(value, p); err != nil { if retErr = entry.FromCtyValue(value, p); retErr != nil {
return err return false
} }
*s = append(*s, entry) *s = append(*s, entry)
return true
} }
return nil eachElement(in)(yield)
return retErr
} }
func (s SSHKeys) ToCtyValue() cty.Value { func (s SSHKeys) ToCtyValue() cty.Value {

View File

@ -1,8 +1,6 @@
package buildflags package buildflags
import ( import (
"iter"
"github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/gocty" "github.com/zclconf/go-cty/cty/gocty"
) )
@ -57,7 +55,12 @@ func isEmptyOrUnknown(v cty.Value) bool {
return !v.IsKnown() || (v.Type() == cty.String && v.AsString() == "") return !v.IsKnown() || (v.Type() == cty.String && v.AsString() == "")
} }
func eachElement(in cty.Value) iter.Seq[cty.Value] { // Seq is a temporary definition of iter.Seq until we are able to migrate
// to using go1.23 as our minimum version. This can be removed when go1.24
// is released and usages can be changed to use rangefunc.
type Seq[V any] func(yield func(V) bool)
func eachElement(in cty.Value) Seq[cty.Value] {
return func(yield func(v cty.Value) bool) { return func(yield func(v cty.Value) bool) {
for elem := in.ElementIterator(); elem.Next(); { for elem := in.ElementIterator(); elem.Next(); {
_, value := elem.Element() _, value := elem.Element()