Skip to content

Commit

Permalink
Merge pull request #2978 from jsternberg/rangefunc-go1.22-revert
Browse files Browse the repository at this point in the history
buildflags: make work on go 1.22 by reverting rangefunc usage
  • Loading branch information
crazy-max authored Feb 11, 2025
2 parents 40b9ac1 + 3ae9970 commit f580211
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 33 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/docker/buildx

go 1.23.0
go 1.22.0

require (
github.com/Masterminds/semver/v3 v3.2.1
Expand Down
13 changes: 8 additions & 5 deletions util/buildflags/attests_cty.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@ func (e *Attests) FromCtyValue(in cty.Value, p cty.Path) error {
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())
for value := range eachElement(in) {

yield := func(value cty.Value) bool {
entry := &Attest{}
if err := entry.FromCtyValue(value, p); err != nil {
return err
if retErr = entry.FromCtyValue(value, p); retErr != nil {
return false
}
*e = append(*e, entry)
return true
}
return nil
eachElement(in)(yield)
return retErr
}

func (e Attests) ToCtyValue() cty.Value {
Expand Down
22 changes: 13 additions & 9 deletions util/buildflags/cache_cty.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,30 @@ func (o *CacheOptions) FromCtyValue(in cty.Value, p cty.Path) error {
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())
for value := range eachElement(in) {

yield := func(value cty.Value) bool {
// Special handling for a string type to handle ref only format.
if value.Type() == cty.String {
entries, err := ParseCacheEntry([]string{value.AsString()})
if err != nil {
return err
var entries CacheOptions
entries, retErr = ParseCacheEntry([]string{value.AsString()})
if retErr != nil {
return false
}
*o = append(*o, entries...)
continue
return true
}

entry := &CacheOptionsEntry{}
if err := entry.FromCtyValue(value, p); err != nil {
return err
if retErr = entry.FromCtyValue(value, p); retErr != nil {
return false
}
*o = append(*o, entry)
return true
}
return nil
eachElement(in)(yield)
return retErr
}

func (o CacheOptions) ToCtyValue() cty.Value {
Expand Down
13 changes: 8 additions & 5 deletions util/buildflags/export_cty.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@ func (e *Exports) FromCtyValue(in cty.Value, p cty.Path) error {
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())
for value := range eachElement(in) {

yield := func(value cty.Value) bool {
entry := &ExportEntry{}
if err := entry.FromCtyValue(value, p); err != nil {
return err
if retErr = entry.FromCtyValue(value, p); retErr != nil {
return false
}
*e = append(*e, entry)
return true
}
return nil
eachElement(in)(yield)
return retErr
}

func (e Exports) ToCtyValue() cty.Value {
Expand Down
13 changes: 8 additions & 5 deletions util/buildflags/secrets_cty.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,19 @@ func (s *Secrets) FromCtyValue(in cty.Value, p cty.Path) error {
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())
for value := range eachElement(in) {

yield := func(value cty.Value) bool {
entry := &Secret{}
if err := entry.FromCtyValue(value, p); err != nil {
return err
if retErr = entry.FromCtyValue(value, p); retErr != nil {
return false
}
*s = append(*s, entry)
return true
}
return nil
eachElement(in)(yield)
return retErr
}

func (s Secrets) ToCtyValue() cty.Value {
Expand Down
13 changes: 8 additions & 5 deletions util/buildflags/ssh_cty.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,19 @@ func (s *SSHKeys) FromCtyValue(in cty.Value, p cty.Path) error {
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())
for value := range eachElement(in) {

yield := func(value cty.Value) bool {
entry := &SSH{}
if err := entry.FromCtyValue(value, p); err != nil {
return err
if retErr = entry.FromCtyValue(value, p); retErr != nil {
return false
}
*s = append(*s, entry)
return true
}
return nil
eachElement(in)(yield)
return retErr
}

func (s SSHKeys) ToCtyValue() cty.Value {
Expand Down
9 changes: 6 additions & 3 deletions util/buildflags/utils.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package buildflags

import (
"iter"

"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/gocty"
)
Expand Down Expand Up @@ -57,7 +55,12 @@ func isEmptyOrUnknown(v cty.Value) bool {
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) {
for elem := in.ElementIterator(); elem.Next(); {
_, value := elem.Element()
Expand Down

0 comments on commit f580211

Please sign in to comment.