Skip to content

Interface unification, both of AggregateI and QueryI returns CursorI by same method. #278

@podhmo

Description

@podhmo

Is your feature request related to a problem? Please describe.
I think it is better that both of AggregateI and QueryI returns CursorI by same method.

Describe the solution you'd like

change AggregateI's interface to have Cursor() method.

// AggregateI define the interface of aggregate
type AggregateI interface {
	All(results interface{}) error
	One(result interface{}) error
	Cursor() CursorI // not Iter()
}

Describe alternatives you've considered

QueryI has Iter() method.

Additional context

We have the interface in our application code

type Cursor = qmgo.CursorI

type Iterable interface { // the name Iterable is for historical reason (from mgo)
	Cursor() Cursor
}

and, Iterate function is defined such as flowlings.

func Iterate(query Iterable, body func(Cursor) (next bool, _ error)) error {
	cursor := query.Cursor()
	defer cursor.Close() // nolint

	if err := cursor.Err(); err != nil {
		return err
	}

	for {
		next, err := body(cursor)
		if err != nil {
			return err
		}
		if err := cursor.Err(); err != nil {
			return err
		}
		if !next {
			return nil
		}
	}
}

using Iterate(collection.Find(ctx, ...), func(...){...}) is OK but Iterate(collection.Aggregat(ctx, ...), func(...){...}) is NG.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions