diff --git a/encoding/jsonschema/pointer.go b/encoding/jsonschema/pointer.go index 1dbe4f0ce..bff9b690f 100644 --- a/encoding/jsonschema/pointer.go +++ b/encoding/jsonschema/pointer.go @@ -1,6 +1,9 @@ package jsonschema -import "strings" +import ( + "iter" + "strings" +) // TODO this file contains functionality that mimics the JSON Pointer functionality // in https://pkg.go.dev/github.com/go-json-experiment/json/jsontext#Pointer; @@ -11,20 +14,16 @@ var ( jsonPtrUnesc = strings.NewReplacer("~0", "~", "~1", "/") ) -// TODO(go1.23) func jsonPointerFromTokens(tokens iter.Seq[string]) string -func jsonPointerFromTokens(tokens func(func(string) bool)) string { +func jsonPointerFromTokens(tokens iter.Seq[string]) string { var buf strings.Builder - // TODO for tok := range tokens { - tokens(func(tok string) bool { + for tok := range tokens { buf.WriteByte('/') buf.WriteString(jsonPtrEsc.Replace(tok)) - return true - }) + } return buf.String() } -// TODO(go1.23) func jsonPointerTokens(p string) iter.Seq[string] -func jsonPointerTokens(p string) func(func(string) bool) { +func jsonPointerTokens(p string) iter.Seq[string] { return func(yield func(string) bool) { needUnesc := strings.IndexByte(p, '~') >= 0 for len(p) > 0 { diff --git a/encoding/jsonschema/ref.go b/encoding/jsonschema/ref.go index c802d6ddb..ca92b480e 100644 --- a/encoding/jsonschema/ref.go +++ b/encoding/jsonschema/ref.go @@ -62,9 +62,8 @@ func parseRootRef(str string) (cue.Path, error) { var errRefNotFound = errors.New("JSON Pointer reference not found") -func lookupJSONPointer(v cue.Value, p string) (_ cue.Value, _err error) { - // TODO(go1.23) for part := range jsonPointerTokens(p) - jsonPointerTokens(p)(func(part string) bool { +func lookupJSONPointer(v cue.Value, p string) (cue.Value, error) { + for part := range jsonPointerTokens(p) { // Note: a JSON Pointer doesn't distinguish between indexing // and struct lookup. We have to use the value itself to decide // which operation is appropriate. @@ -76,23 +75,19 @@ func lookupJSONPointer(v cue.Value, p string) (_ cue.Value, _err error) { idx := int64(0) if len(part) > 1 && part[0] == '0' { // Leading zeros are not allowed - _err = errRefNotFound - return false + return cue.Value{}, errRefNotFound } idx, err := strconv.ParseInt(part, 10, 64) if err != nil { - _err = errRefNotFound - return false + return cue.Value{}, errRefNotFound } v = v.LookupPath(cue.MakePath(cue.Index(idx))) } if !v.Exists() { - _err = errRefNotFound - return false + return cue.Value{}, errRefNotFound } - return true - }) - return v, _err + } + return v, nil } func sameSchemaRoot(u1, u2 *url.URL) bool { diff --git a/internal/mod/modimports/modimports.go b/internal/mod/modimports/modimports.go index 81c10a3d4..4499b6a7f 100644 --- a/internal/mod/modimports/modimports.go +++ b/internal/mod/modimports/modimports.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "io/fs" + "iter" "maps" "path" "slices" @@ -32,7 +33,7 @@ type ModuleFile struct { // AllImports returns a sorted list of all the package paths // imported by the module files produced by modFilesIter // in canonical form. -func AllImports(modFilesIter func(func(ModuleFile, error) bool)) (_ []string, retErr error) { +func AllImports(modFilesIter iter.Seq2[ModuleFile, error]) (_ []string, retErr error) { pkgPaths := make(map[string]bool) modFilesIter(func(mf ModuleFile, err error) bool { if err != nil { @@ -65,7 +66,7 @@ func AllImports(modFilesIter func(func(ModuleFile, error) bool)) (_ []string, re // // TODO(mvdan): this should now be called InstanceFiles, to follow the naming from // https://cuelang.org/docs/concept/modules-packages-instances/#instances. -func PackageFiles(fsys fs.FS, dir string, pkgQualifier string) func(func(ModuleFile, error) bool) { +func PackageFiles(fsys fs.FS, dir string, pkgQualifier string) iter.Seq2[ModuleFile, error] { return func(yield func(ModuleFile, error) bool) { // Start at the target directory, but also include package files // from packages with the same name(s) in parent directories. @@ -146,7 +147,7 @@ func PackageFiles(fsys fs.FS, dir string, pkgQualifier string) func(func(ModuleF // module at the given root. // // The caller may assume that files from the same package are always adjacent. -func AllModuleFiles(fsys fs.FS, root string) func(func(ModuleFile, error) bool) { +func AllModuleFiles(fsys fs.FS, root string) iter.Seq2[ModuleFile, error] { return func(yield func(ModuleFile, error) bool) { yieldAllModFiles(fsys, root, true, yield) } diff --git a/internal/mod/modload/tidy.go b/internal/mod/modload/tidy.go index 7913e1097..ddd4b5a63 100644 --- a/internal/mod/modload/tidy.go +++ b/internal/mod/modload/tidy.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io/fs" + "iter" "log" "maps" "path" @@ -713,15 +714,16 @@ func (ld *loader) spotCheckRoots(ctx context.Context, rs *modrequirements.Requir return true } -func withoutIgnoredFiles(iter func(func(modimports.ModuleFile, error) bool)) func(func(modimports.ModuleFile, error) bool) { +func withoutIgnoredFiles(modFiles iter.Seq2[modimports.ModuleFile, error]) iter.Seq2[modimports.ModuleFile, error] { return func(yield func(modimports.ModuleFile, error) bool) { - // TODO for mf, err := range iter { - iter(func(mf modimports.ModuleFile, err error) bool { + for mf, err := range modFiles { if err == nil && buildattr.ShouldIgnoreFile(mf.Syntax) { - return true + continue } - return yield(mf, err) - }) + if !yield(mf, err) { + break + } + } } } diff --git a/mod/modregistry/client.go b/mod/modregistry/client.go index efcf6a929..d003c7b86 100644 --- a/mod/modregistry/client.go +++ b/mod/modregistry/client.go @@ -27,6 +27,7 @@ import ( "errors" "fmt" "io" + "iter" "net/http" "strings" @@ -124,13 +125,11 @@ func (src *Client) Mirror(ctx context.Context, dst *Client, mv module.Version) e // on whatever is calling the function too rather than just doing // all uploads in parallel. var g errgroup.Group - // TODO for desc := range manifestRefs(m.manifest) - manifestRefs(m.manifest)(func(desc ociregistry.Descriptor) bool { + for desc := range manifestRefs(m.manifest) { g.Go(func() error { return mirrorBlob(ctx, m.loc, dstLoc, desc) }) - return true - }) + } if err := g.Wait(); err != nil { return err } @@ -570,7 +569,7 @@ func (r singleResolver) ResolveToRegistry(mpath, vers string) (RegistryLocation, // manifestRefs returns an iterator that produces all the references // contained in m. -func manifestRefs(m ocispec.Manifest) func(func(ociregistry.Descriptor) bool) { +func manifestRefs(m ocispec.Manifest) iter.Seq[ociregistry.Descriptor] { return func(yield func(ociregistry.Descriptor) bool) { if !yield(m.Config) { return