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 command |
| 8 | |
| 9 | import ( |
| 10 | "context" |
| 11 | |
| 12 | "github.com/mongodb/mongo-go-driver/bson" |
| 13 | "github.com/mongodb/mongo-go-driver/mongo/readpref" |
| 14 | "github.com/mongodb/mongo-go-driver/x/bsonx" |
| 15 | "github.com/mongodb/mongo-go-driver/x/mongo/driver/session" |
| 16 | "github.com/mongodb/mongo-go-driver/x/network/description" |
| 17 | "github.com/mongodb/mongo-go-driver/x/network/wiremessage" |
| 18 | ) |
| 19 | |
| 20 | // ListCollections represents the listCollections command. |
| 21 | // |
| 22 | // The listCollections command lists the collections in a database. |
| 23 | type ListCollections struct { |
| 24 | Clock *session.ClusterClock |
| 25 | DB string |
| 26 | Filter bsonx.Doc |
| 27 | CursorOpts []bsonx.Elem |
| 28 | Opts []bsonx.Elem |
| 29 | ReadPref *readpref.ReadPref |
| 30 | Session *session.Client |
| 31 | |
| 32 | result bson.Raw |
| 33 | err error |
| 34 | } |
| 35 | |
| 36 | // Encode will encode this command into a wire message for the given server description. |
| 37 | func (lc *ListCollections) Encode(desc description.SelectedServer) (wiremessage.WireMessage, error) { |
| 38 | encoded, err := lc.encode(desc) |
| 39 | if err != nil { |
| 40 | return nil, err |
| 41 | } |
| 42 | return encoded.Encode(desc) |
| 43 | } |
| 44 | |
| 45 | func (lc *ListCollections) encode(desc description.SelectedServer) (*Read, error) { |
| 46 | cmd := bsonx.Doc{{"listCollections", bsonx.Int32(1)}} |
| 47 | |
| 48 | if lc.Filter != nil { |
| 49 | cmd = append(cmd, bsonx.Elem{"filter", bsonx.Document(lc.Filter)}) |
| 50 | } |
| 51 | cmd = append(cmd, lc.Opts...) |
| 52 | |
| 53 | return &Read{ |
| 54 | Clock: lc.Clock, |
| 55 | DB: lc.DB, |
| 56 | Command: cmd, |
| 57 | ReadPref: lc.ReadPref, |
| 58 | Session: lc.Session, |
| 59 | }, nil |
| 60 | } |
| 61 | |
| 62 | // Decode will decode the wire message using the provided server description. Errors during decolcng |
| 63 | // are deferred until either the Result or Err methods are called. |
| 64 | func (lc *ListCollections) Decode(desc description.SelectedServer, wm wiremessage.WireMessage) *ListCollections { |
| 65 | rdr, err := (&Read{}).Decode(desc, wm).Result() |
| 66 | if err != nil { |
| 67 | lc.err = err |
| 68 | return lc |
| 69 | } |
| 70 | return lc.decode(desc, rdr) |
| 71 | } |
| 72 | |
| 73 | func (lc *ListCollections) decode(desc description.SelectedServer, rdr bson.Raw) *ListCollections { |
| 74 | lc.result = rdr |
| 75 | return lc |
| 76 | } |
| 77 | |
| 78 | // Result returns the result of a decoded wire message and server description. |
| 79 | func (lc *ListCollections) Result() (bson.Raw, error) { |
| 80 | if lc.err != nil { |
| 81 | return nil, lc.err |
| 82 | } |
| 83 | return lc.result, nil |
| 84 | } |
| 85 | |
| 86 | // Err returns the error set on this command. |
| 87 | func (lc *ListCollections) Err() error { return lc.err } |
| 88 | |
| 89 | // RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter. |
| 90 | func (lc *ListCollections) RoundTrip(ctx context.Context, desc description.SelectedServer, rw wiremessage.ReadWriter) (bson.Raw, error) { |
| 91 | cmd, err := lc.encode(desc) |
| 92 | if err != nil { |
| 93 | return nil, err |
| 94 | } |
| 95 | |
| 96 | rdr, err := cmd.RoundTrip(ctx, desc, rw) |
| 97 | if err != nil { |
| 98 | return nil, err |
| 99 | } |
| 100 | |
| 101 | return lc.decode(desc, rdr).Result() |
| 102 | } |