-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.go
69 lines (51 loc) · 1.45 KB
/
convert.go
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
package mongosteps
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/cucumber/godog"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
)
func stringToDocs(data *godog.DocString) ([]bsoncore.Document, error) {
if data == nil {
return nil, errors.New("data is nil") // nolint: goerr113
}
return bytesToDocs([]byte(data.Content))
}
func bytesToDocs(data []byte) ([]bsoncore.Document, error) {
var docs []bsoncore.Document
if err := bson.UnmarshalExtJSON(data, true, &docs); err != nil {
return nil, fmt.Errorf("error unmarshaling extjson: %w", err)
}
return docs, nil
}
func docsToExtJSON(docs []bsoncore.Document) ([]byte, error) {
if len(docs) == 0 {
return []byte("[]"), nil
}
result := make([]json.RawMessage, len(docs))
for i, doc := range docs {
result[i] = json.RawMessage(doc.String())
}
data, err := json.Marshal(result)
if err != nil {
return nil, fmt.Errorf("error marshaling documents: %w", err)
}
data = bytes.ReplaceAll(data, []byte(`\u003cignored-diff\u003e`), []byte("<ignore-diff>"))
return data, nil
}
func stringToBSOND(data *godog.DocString) (bson.D, error) {
if data == nil {
return bson.D{}, nil
}
return bytesToBSOND([]byte(data.Content))
}
func bytesToBSOND(data []byte) (bson.D, error) {
var result bson.D
if err := bson.UnmarshalExtJSON(data, true, &result); err != nil {
return nil, fmt.Errorf("error unmarshaling extjson: %w", err)
}
return result, nil
}