-
Type: Improvement
-
Resolution: Done
-
Priority: Minor - P4
-
None
-
Affects Version/s: 1.9.1
-
Component/s: None
Summary
Consider the difference between these interfaces:
func (c *Cursor) All(ctx context.Context, results interface{}) error
and
func (coll *Collection) Distinct(ctx context.Context, fieldName string, filter interface{}, opts ...*options.DistinctOptions) ([]interface{}, error)
In the former case, result is unmarshal-ed into user-provided slice, and in the latter, provided as []interface which is populated via a code like this:
for i, val := range values { raw := bson.RawValue{Type: val.Type, Value: val.Data} err = *raw.Unmarshal(&retArray[i]) if err != nil { return nil, err } } }
This proves problematic for a caller if you want to provide the same api with user-provided slices.
Why would you want this?
For example, if the field in question is a custom bson-marshalled type, and you wish to have the result as a slice of this type. Or if you simply do not wish to spoil your business logic with extra type checks and conversions.
A list of workarounds appears to be:
- Write a conversion code with heavy use of reflection (needs PoC)
- Write a conversion code that marshals the result back into Raw and then marshals into user-provided slice (poor performance, needs PoC)
- Use public operation.NewDistinct() and implement the desired functionality outside of the driver (needs PoC)
- Change the code of the driver to allow for return of the original DistinctResult or to provide the desired api
We currently consider option 4 and will be happy to send a patch upstream, but it is best to first discuss if such change would be welcome.
Motivation
Who is the affected end user?
Anyone who would use Distinct() in a general purpose code.
How does this affect the end user?
They need to implement a workaround.
How likely is it that this problem or use case will occur?
Any usage, possibly depending on the usage pattern.
If the problem does occur, what are the consequences and how severe are they?
A need to implement a workaround.
Is this issue urgent?
No.
Is this ticket required by a downstream team?
No.
Is this ticket only for tests?
No.
- related to
-
GODRIVER-2444 Consider a DistinctCursor collection method
- Backlog
-
GODRIVER-2443 Make Distinct return a decodable struct "[]any"
- Closed