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

Document decoding to empty interface

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Major - P3
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Documentation
    • Labels:
      None

      Description

      A Reddit question about decoding SingleResult suggests that the driver will successfully decode into an empty interface variable.

      func GetMonthStatusByID(ctx context.Context, id string) (interface{}, error) {
          var monthStatus interface{}
          filter := bson.M{"_id": id}
          err := db.Collection("Months").FindOne(ctx, filter).Decode(&monthStatus)
          return monthStatus, err
      }
      

      It looks like the result might have been a primative.D.

      On one hand, this sort of makes sense because any type will satisfy the empty interface. OTOH, it's probably a user being misled by the type signature, as there's no real use for decoding to empty interface versus a concrete type or an Unmarshaler interface.

      I think it should either error, on the grounds that users probably made a mistake, or else we should document the behavior to indicate that decoding to empty interface defaults to primative.D.

        Attachments

          Activity

            People

            Assignee:
            Unassigned
            Reporter:
            david.golden David Golden
            Participants:
            Votes:
            3 Vote for this issue
            Watchers:
            8 Start watching this issue

              Dates

              Created:
              Updated: