Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blob: Bucket.List() with Go 1.23 iterators #3488

Open
myaaaaaaaaa opened this issue Oct 1, 2024 · 1 comment
Open

blob: Bucket.List() with Go 1.23 iterators #3488

myaaaaaaaaa opened this issue Oct 1, 2024 · 1 comment

Comments

@myaaaaaaaaa
Copy link

Is your feature request related to a problem? Please describe.

Bucket.List() is somewhat cumbersome to use since it was designed long before Go 1.23. Now that Go 1.23 standardized what iterators should look like, there's an opportunity to design simpler iteration APIs.

For details, see https://go.dev/blog/range-functions

Describe the solution you'd like

Option 1: ListIterator.All()

func (i *ListIterator) All(ctx context.Context, err *error) iter.Seq2[*ListObject, func(io.Writer)]
// Note: The iter.Seq2 type cannot be used until Go CDK phases out support for Go versions older than 1.23.
// For the time being, this function will need to return func(func(*ListObject, func(io.Writer)) bool)

Consumes the remaining objects in the ListIterator. The second value is a convenient way to call Bucket.Download() on the current object without the typical overhead of error handling, as this is already handled by All()

	var err error
	iter := bucket.List(options)
	for obj, download := range iter.All(ctx, &err) {
		fmt.Println(obj.Key)
		if obj.Size < 1024 {
			var buf bytes.Buffer
			download(&buf)
			fmt.Println(buf.String())
		}
	}
	// loop terminates early on error
	if err != nil {
		// also happens if download() fails or ctx is cancelled
		log.Fatal(err)
	}

Option 2: Bucket.All()

Avoids the need for the ListIterator object entirely.

	var err error
	for obj, download := range bucket.All(ctx, options, &err) {
		fmt.Println(obj.Key)
		if obj.Size < 1024 {
			var buf bytes.Buffer
			download(&buf)
			fmt.Println(buf.String())
		}
	}
	if err != nil {
		log.Fatal(err)
	}

One potential downside to this is that if a standard io.FS.All() method is ever introduced, it will almost certainly have an incompatible signature with whatever is decided here. It seems safer to avoid adding iterator methods directly to Bucket at this time.

@vangent
Copy link
Contributor

vangent commented Dec 7, 2024

We commit to n-1 release support, so holding off on considering this until 1.24 is out (IIUC, expected Feb 2025).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants