Don Newton | 379ae25 | 2019-04-01 12:17:06 -0400 | [diff] [blame^] | 1 | // Copyright (C) MongoDB, Inc. 2017-present. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | // not use this file except in compliance with the License. You may obtain |
| 5 | // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | |
| 7 | package mongo |
| 8 | |
| 9 | import ( |
| 10 | "context" |
| 11 | "errors" |
| 12 | |
| 13 | "github.com/mongodb/mongo-go-driver/bson" |
| 14 | "github.com/mongodb/mongo-go-driver/bson/bsoncodec" |
| 15 | ) |
| 16 | |
| 17 | // ErrNoDocuments is returned by Decode when an operation that returns a |
| 18 | // SingleResult doesn't return any documents. |
| 19 | var ErrNoDocuments = errors.New("mongo: no documents in result") |
| 20 | |
| 21 | // SingleResult represents a single document returned from an operation. If |
| 22 | // the operation returned an error, the Err method of SingleResult will |
| 23 | // return that error. |
| 24 | type SingleResult struct { |
| 25 | err error |
| 26 | cur *Cursor |
| 27 | rdr bson.Raw |
| 28 | reg *bsoncodec.Registry |
| 29 | } |
| 30 | |
| 31 | // Decode will attempt to decode the first document into v. If there was an |
| 32 | // error from the operation that created this SingleResult then the error |
| 33 | // will be returned. If there were no returned documents, ErrNoDocuments is |
| 34 | // returned. |
| 35 | func (sr *SingleResult) Decode(v interface{}) error { |
| 36 | if sr.err != nil { |
| 37 | return sr.err |
| 38 | } |
| 39 | if sr.reg == nil { |
| 40 | return bson.ErrNilRegistry |
| 41 | } |
| 42 | switch { |
| 43 | case sr.rdr != nil: |
| 44 | if v == nil { |
| 45 | return nil |
| 46 | } |
| 47 | return bson.UnmarshalWithRegistry(sr.reg, sr.rdr, v) |
| 48 | case sr.cur != nil: |
| 49 | defer sr.cur.Close(context.TODO()) |
| 50 | if !sr.cur.Next(context.TODO()) { |
| 51 | if err := sr.cur.Err(); err != nil { |
| 52 | return err |
| 53 | } |
| 54 | return ErrNoDocuments |
| 55 | } |
| 56 | if v == nil { |
| 57 | return nil |
| 58 | } |
| 59 | return sr.cur.Decode(v) |
| 60 | } |
| 61 | |
| 62 | return ErrNoDocuments |
| 63 | } |
| 64 | |
| 65 | // DecodeBytes will return a copy of the document as a bson.Raw. If there was an |
| 66 | // error from the operation that created this SingleResult then the error |
| 67 | // will be returned. If there were no returned documents, ErrNoDocuments is |
| 68 | // returned. |
| 69 | func (sr *SingleResult) DecodeBytes() (bson.Raw, error) { |
| 70 | switch { |
| 71 | case sr.err != nil: |
| 72 | return nil, sr.err |
| 73 | case sr.rdr != nil: |
| 74 | return sr.rdr, nil |
| 75 | case sr.cur != nil: |
| 76 | defer sr.cur.Close(context.TODO()) |
| 77 | if !sr.cur.Next(context.TODO()) { |
| 78 | if err := sr.cur.Err(); err != nil { |
| 79 | return nil, err |
| 80 | } |
| 81 | return nil, ErrNoDocuments |
| 82 | } |
| 83 | return sr.cur.Current, nil |
| 84 | } |
| 85 | |
| 86 | return nil, ErrNoDocuments |
| 87 | } |
| 88 | |
| 89 | // Err will return the error from the operation that created this SingleResult. |
| 90 | // If there was no error, nil is returned. |
| 91 | func (sr *SingleResult) Err() error { |
| 92 | return sr.err |
| 93 | } |