1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package mtest
import (
"go.mongodb.org/mongo-driver/bson"
)
// BatchIdentifier specifies the keyword to identify the batch in a cursor response.
type BatchIdentifier string
// These constants specify valid values for BatchIdentifier.
const (
FirstBatch BatchIdentifier = "firstBatch"
NextBatch BatchIdentifier = "nextBatch"
)
// CommandError is a representation of a command error from the server.
type CommandError struct {
Code int32
Message string
Name string
Labels []string
}
// WriteError is a representation of a write error from the server.
type WriteError struct {
Index int
Code int
Message string
}
// WriteConcernError is a representation of a write concern error from the server.
type WriteConcernError struct {
Name string `bson:"codeName"`
Code int `bson:"code"`
Message string `bson:"errmsg"`
Details bson.Raw `bson:"errInfo"`
}
// CreateCursorResponse creates a response for a cursor command.
func CreateCursorResponse(cursorID int64, ns string, identifier BatchIdentifier, batch ...bson.D) bson.D {
batchArr := bson.A{}
for _, doc := range batch {
batchArr = append(batchArr, doc)
}
return bson.D{
{"ok", 1},
{"cursor", bson.D{
{"id", cursorID},
{"ns", ns},
{string(identifier), batchArr},
}},
}
}
// CreateCommandErrorResponse creates a response with a command error.
func CreateCommandErrorResponse(ce CommandError) bson.D {
res := bson.D{
{"ok", 0},
{"code", ce.Code},
{"errmsg", ce.Message},
{"codeName", ce.Name},
}
if len(ce.Labels) > 0 {
var labelsArr bson.A
for _, label := range ce.Labels {
labelsArr = append(labelsArr, label)
}
res = append(res, bson.E{Key: "errorLabels", Value: labelsArr})
}
return res
}
// CreateWriteErrorsResponse creates a response with one or more write errors.
func CreateWriteErrorsResponse(writeErrorrs ...WriteError) bson.D {
arr := make(bson.A, len(writeErrorrs))
for idx, we := range writeErrorrs {
arr[idx] = bson.D{
{"index", we.Index},
{"code", we.Code},
{"errmsg", we.Message},
}
}
return bson.D{
{"ok", 1},
{"writeErrors", arr},
}
}
// CreateWriteConcernErrorResponse creates a response with a write concern error.
func CreateWriteConcernErrorResponse(wce WriteConcernError) bson.D {
wceDoc := bson.D{
{"code", wce.Code},
{"codeName", wce.Name},
{"errmsg", wce.Message},
}
if len(wce.Details) > 0 {
wceDoc = append(wceDoc, bson.E{Key: "errInfo", Value: wce.Details})
}
return bson.D{
{"ok", 1},
{"writeConcernError", wceDoc},
}
}
// CreateSuccessResponse creates a response for a successful operation with the given elements.
func CreateSuccessResponse(elems ...bson.E) bson.D {
res := bson.D{
{"ok", 1},
}
return append(res, elems...)
}
|