Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-2426

Distinct does not provide unmarshalling api

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 1.9.1
    • Component/s: None
    • Labels:

      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:

      1. Write a conversion code with heavy use of reflection (needs PoC)
      2. Write a conversion code that marshals the result back into Raw and then marshals into user-provided slice (poor performance, needs PoC)
      3. Use public operation.NewDistinct() and implement the desired functionality outside of the driver (needs PoC)
      4. 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.

            Assignee:
            benji.rewis@mongodb.com Benji Rewis (Inactive)
            Reporter:
            petr.ivanov.s@gmail.com Peter Ivanov
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: