Skip to content

Commit

Permalink
buildflags: make work on go 1.22 by reverting rangefunc usage
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
jsternberg committed Feb 10, 2025
1 parent 0a4eb7e commit d9f8168
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 32 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) (err 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 err = entry.FromCtyValue(value, p); err != nil {
return false
}
*e = append(*e, entry)
return true
}
return nil
eachElement(in)(yield)
return err
}

func (e Attests) ToCtyValue() cty.Value {
Expand Down
20 changes: 12 additions & 8 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) (err 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()})
var entries CacheOptions
entries, err = ParseCacheEntry([]string{value.AsString()})
if err != nil {
return err
return false
}
*o = append(*o, entries...)
continue
return true
}

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

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) (err 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 err = entry.FromCtyValue(value, p); err != nil {
return false
}
*e = append(*e, entry)
return true
}
return nil
eachElement(in)(yield)
return err
}

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) (err 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 err = entry.FromCtyValue(value, p); err != nil {
return false
}
*s = append(*s, entry)
return true
}
return nil
eachElement(in)(yield)
return err
}

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) (err 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 err = entry.FromCtyValue(value, p); err != nil {
return false
}
*s = append(*s, entry)
return true
}
return nil
eachElement(in)(yield)
return err
}

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 d9f8168

Please sign in to comment.