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/x/bsonx" |
| 14 | "github.com/mongodb/mongo-go-driver/x/mongo/driver/session" |
| 15 | "github.com/mongodb/mongo-go-driver/x/network/description" |
| 16 | "github.com/mongodb/mongo-go-driver/x/network/result" |
| 17 | "github.com/mongodb/mongo-go-driver/x/network/wiremessage" |
| 18 | ) |
| 19 | |
| 20 | // StartSession represents a startSession command |
| 21 | type StartSession struct { |
| 22 | Clock *session.ClusterClock |
| 23 | result result.StartSession |
| 24 | err error |
| 25 | } |
| 26 | |
| 27 | // Encode will encode this command into a wiremessage for the given server description. |
| 28 | func (ss *StartSession) Encode(desc description.SelectedServer) (wiremessage.WireMessage, error) { |
| 29 | cmd := ss.encode(desc) |
| 30 | return cmd.Encode(desc) |
| 31 | } |
| 32 | |
| 33 | func (ss *StartSession) encode(desc description.SelectedServer) *Write { |
| 34 | cmd := bsonx.Doc{{"startSession", bsonx.Int32(1)}} |
| 35 | return &Write{ |
| 36 | Clock: ss.Clock, |
| 37 | DB: "admin", |
| 38 | Command: cmd, |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // Decode will decode the wire message using the provided server description. Errors during decoding are deferred until |
| 43 | // either the Result or Err methods are called. |
| 44 | func (ss *StartSession) Decode(desc description.SelectedServer, wm wiremessage.WireMessage) *StartSession { |
| 45 | rdr, err := (&Write{}).Decode(desc, wm).Result() |
| 46 | if err != nil { |
| 47 | ss.err = err |
| 48 | return ss |
| 49 | } |
| 50 | |
| 51 | return ss.decode(desc, rdr) |
| 52 | } |
| 53 | |
| 54 | func (ss *StartSession) decode(desc description.SelectedServer, rdr bson.Raw) *StartSession { |
| 55 | ss.err = bson.Unmarshal(rdr, &ss.result) |
| 56 | return ss |
| 57 | } |
| 58 | |
| 59 | // Result returns the result of a decoded wire message and server description. |
| 60 | func (ss *StartSession) Result() (result.StartSession, error) { |
| 61 | if ss.err != nil { |
| 62 | return result.StartSession{}, ss.err |
| 63 | } |
| 64 | |
| 65 | return ss.result, nil |
| 66 | } |
| 67 | |
| 68 | // Err returns the error set on this command |
| 69 | func (ss *StartSession) Err() error { |
| 70 | return ss.err |
| 71 | } |
| 72 | |
| 73 | // RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter |
| 74 | func (ss *StartSession) RoundTrip(ctx context.Context, desc description.SelectedServer, rw wiremessage.ReadWriter) (result.StartSession, error) { |
| 75 | cmd := ss.encode(desc) |
| 76 | rdr, err := cmd.RoundTrip(ctx, desc, rw) |
| 77 | if err != nil { |
| 78 | return result.StartSession{}, err |
| 79 | } |
| 80 | |
| 81 | return ss.decode(desc, rdr).Result() |
| 82 | } |