| 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 bsonrwscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/ |
| 8 | |
| 9 | import ( |
| 10 | "errorsscip-go gomod github.com/golang/go/src go1.18 errors/" |
| 11 | "fmtscip-go gomod github.com/golang/go/src go1.18 fmt/" |
| 12 | "ioscip-go gomod github.com/golang/go/src go1.18 io/" |
| 13 | "stringsscip-go gomod github.com/golang/go/src go1.18 strings/" |
| 14 | "testingscip-go gomod github.com/golang/go/src go1.18 testing/" |
| 15 | |
| 16 | "github.com/google/go-cmp/cmpscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp`/" |
| 17 | "go.mongodb.org/mongo-driver/bson/bsontypescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsontype`/" |
| 18 | ) |
| 19 | |
| 20 | func TestExtJSONReaderscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/TestExtJSONReader().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) { |
| 21 | t.Runscip-go gomod github.com/golang/go/src go1.18 testing/T#Run().("ReadDocument", func(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) { |
| 22 | t.Runscip-go gomod github.com/golang/go/src go1.18 testing/T#Run().("EmbeddedDocument", func(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) { |
| 23 | ejvr := &extJSONValueReaderscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/extJSONValueReader#{ |
| 24 | stackscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/extJSONValueReader#stack.: []ejvrStatescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/ejvrState#{ |
| 25 | {modescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/ejvrState#mode.: mTopLevelscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/mTopLevel.}, |
| 26 | {modescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/ejvrState#mode.: mElementscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/mElement., vTypescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/ejvrState#vType.: bsontypescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsontype`/.Booleanscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsontype`/Boolean.}, |
| 27 | }, |
| 28 | framescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/extJSONValueReader#frame.: 1, |
| 29 | } |
| 30 | |
| 31 | ejvr.stackscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/extJSONValueReader#stack.[1].modescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/ejvrState#mode. = mArrayscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/mArray. |
| 32 | wanterr := ejvr.invalidTransitionErrscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/extJSONValueReader#invalidTransitionErr().(mDocumentscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/mDocument., "ReadDocument", []modescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/mode#{mTopLevelscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/mTopLevel., mElementscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/mElement., mValuescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/mValue.}) |
| 33 | _, err := ejvr.ReadDocumentscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/extJSONValueReader#ReadDocument().() |
| 34 | if err == nil || err.Error() != wanterr.Error() { |
| 35 | t.Errorfscip-go gomod github.com/golang/go/src go1.18 testing/common#Errorf().("Incorrect returned error. got %v; want %v", err, wanterr) |
| 36 | } |
| 37 | |
| 38 | }) |
| 39 | }) |
| 40 | |
| 41 | t.Runscip-go gomod github.com/golang/go/src go1.18 testing/T#Run().("invalid transition", func(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) { |
| 42 | t.Runscip-go gomod github.com/golang/go/src go1.18 testing/T#Run().("Skip", func(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) { |
| 43 | ejvr := &extJSONValueReaderscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/extJSONValueReader#{stackscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/extJSONValueReader#stack.: []ejvrStatescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/ejvrState#{{modescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/ejvrState#mode.: mTopLevelscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/mTopLevel.}}} |
| 44 | wanterr := (&extJSONValueReaderscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/extJSONValueReader#{stackscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/extJSONValueReader#stack.: []ejvrStatescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/ejvrState#{{modescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/ejvrState#mode.: mTopLevelscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/mTopLevel.}}}).invalidTransitionErrscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/extJSONValueReader#invalidTransitionErr().(0, "Skip", []modescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/mode#{mElementscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/mElement., mValuescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/mValue.}) |
| 45 | goterr := ejvr.Skipscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/extJSONValueReader#Skip().() |
| 46 | if !cmpscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp`/.Equalscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp`/Equal().(goterr, wanterr, cmpscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp`/.Comparerscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp`/Comparer().(compareErrorsscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/compareErrors().)) { |
| 47 | t.Errorfscip-go gomod github.com/golang/go/src go1.18 testing/common#Errorf().("Expected correct invalid transition error. got %v; want %v", goterr, wanterr) |
| 48 | } |
| 49 | }) |
| 50 | }) |
| 51 | } |
| 52 | |
| 53 | func TestReadMultipleTopLevelDocumentsscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/TestReadMultipleTopLevelDocuments().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) { |
| 54 | testCases := []struct { |
| 55 | name string |
| 56 | input string |
| 57 | expected [][]byte |
| 58 | }{ |
| 59 | { |
| 60 | "single top-level document", |
| 61 | "{\"foo\":1}", |
| 62 | [][]byte{ |
| 63 | {0x0E, 0x00, 0x00, 0x00, 0x10, 'f', 'o', 'o', 0x00, 0x01, 0x00, 0x00, 0x00, 0x00}, |
| 64 | }, |
| 65 | }, |
| 66 | { |
| 67 | "single top-level document with leading and trailing whitespace", |
| 68 | "\n\n {\"foo\":1} \n", |
| 69 | [][]byte{ |
| 70 | {0x0E, 0x00, 0x00, 0x00, 0x10, 'f', 'o', 'o', 0x00, 0x01, 0x00, 0x00, 0x00, 0x00}, |
| 71 | }, |
| 72 | }, |
| 73 | { |
| 74 | "two top-level documents", |
| 75 | "{\"foo\":1}{\"foo\":2}", |
| 76 | [][]byte{ |
| 77 | {0x0E, 0x00, 0x00, 0x00, 0x10, 'f', 'o', 'o', 0x00, 0x01, 0x00, 0x00, 0x00, 0x00}, |
| 78 | {0x0E, 0x00, 0x00, 0x00, 0x10, 'f', 'o', 'o', 0x00, 0x02, 0x00, 0x00, 0x00, 0x00}, |
| 79 | }, |
| 80 | }, |
| 81 | { |
| 82 | "two top-level documents with leading and trailing whitespace and whitespace separation ", |
| 83 | "\n\n {\"foo\":1}\n{\"foo\":2}\n ", |
| 84 | [][]byte{ |
| 85 | {0x0E, 0x00, 0x00, 0x00, 0x10, 'f', 'o', 'o', 0x00, 0x01, 0x00, 0x00, 0x00, 0x00}, |
| 86 | {0x0E, 0x00, 0x00, 0x00, 0x10, 'f', 'o', 'o', 0x00, 0x02, 0x00, 0x00, 0x00, 0x00}, |
| 87 | }, |
| 88 | }, |
| 89 | { |
| 90 | "top-level array with single document", |
| 91 | "[{\"foo\":1}]", |
| 92 | [][]byte{ |
| 93 | {0x0E, 0x00, 0x00, 0x00, 0x10, 'f', 'o', 'o', 0x00, 0x01, 0x00, 0x00, 0x00, 0x00}, |
| 94 | }, |
| 95 | }, |
| 96 | { |
| 97 | "top-level array with 2 documents", |
| 98 | "[{\"foo\":1},{\"foo\":2}]", |
| 99 | [][]byte{ |
| 100 | {0x0E, 0x00, 0x00, 0x00, 0x10, 'f', 'o', 'o', 0x00, 0x01, 0x00, 0x00, 0x00, 0x00}, |
| 101 | {0x0E, 0x00, 0x00, 0x00, 0x10, 'f', 'o', 'o', 0x00, 0x02, 0x00, 0x00, 0x00, 0x00}, |
| 102 | }, |
| 103 | }, |
| 104 | } |
| 105 | |
| 106 | for _, tc := range testCases { |
| 107 | t.Runscip-go gomod github.com/golang/go/src go1.18 testing/T#Run().(tc.name, func(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) { |
| 108 | r := stringsscip-go gomod github.com/golang/go/src go1.18 strings/.NewReaderscip-go gomod github.com/golang/go/src go1.18 strings/NewReader().(tc.input) |
| 109 | vr, err := NewExtJSONValueReaderscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/NewExtJSONValueReader().(r, false) |
| 110 | if err != nil { |
| 111 | t.Fatalfscip-go gomod github.com/golang/go/src go1.18 testing/common#Fatalf().("expected no error, but got %v", err) |
| 112 | } |
| 113 | |
| 114 | actual, err := readAllDocumentsscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/readAllDocuments().(vr) |
| 115 | if err != nil { |
| 116 | t.Fatalfscip-go gomod github.com/golang/go/src go1.18 testing/common#Fatalf().("expected no error, but got %v", err) |
| 117 | } |
| 118 | |
| 119 | if diff := cmpscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp`/.Diffscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp`/Diff().(tc.expected, actual); diff != "" { |
| 120 | t.Fatalfscip-go gomod github.com/golang/go/src go1.18 testing/common#Fatalf().("expected does not match actual: %v", diff) |
| 121 | } |
| 122 | }) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | func readAllDocumentsscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/readAllDocuments().(vr ValueReaderscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/ValueReader#) ([][]byte, error) { |
| 127 | c := NewCopierscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/NewCopier().() |
| 128 | var actual [][]byte |
| 129 | |
| 130 | switch vr.Typescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/ValueReader#Type.() { |
| 131 | case bsontypescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsontype`/.EmbeddedDocumentscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsontype`/EmbeddedDocument.: |
| 132 | for { |
| 133 | result, err := c.CopyDocumentToBytesscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/Copier#CopyDocumentToBytes().(vr) |
| 134 | if err != nil { |
| 135 | if errorsscip-go gomod github.com/golang/go/src go1.18 errors/.Isscip-go gomod github.com/golang/go/src go1.18 errors/Is().(err, ioscip-go gomod github.com/golang/go/src go1.18 io/.EOFscip-go gomod github.com/golang/go/src go1.18 io/EOF.) { |
| 136 | break |
| 137 | } |
| 138 | return nil, err |
| 139 | } |
| 140 | |
| 141 | actual = append(actual, result) |
| 142 | } |
| 143 | case bsontypescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsontype`/.Arrayscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsontype`/Array.: |
| 144 | ar, err := vr.ReadArrayscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/ValueReader#ReadArray.() |
| 145 | if err != nil { |
| 146 | return nil, err |
| 147 | } |
| 148 | for { |
| 149 | evr, err := ar.ReadValuescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/ArrayReader#ReadValue.() |
| 150 | if err != nil { |
| 151 | if errorsscip-go gomod github.com/golang/go/src go1.18 errors/.Isscip-go gomod github.com/golang/go/src go1.18 errors/Is().(err, ErrEOAscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/ErrEOA.) { |
| 152 | break |
| 153 | } |
| 154 | return nil, err |
| 155 | } |
| 156 | |
| 157 | result, err := c.CopyDocumentToBytesscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/Copier#CopyDocumentToBytes().(evr) |
| 158 | if err != nil { |
| 159 | return nil, err |
| 160 | } |
| 161 | |
| 162 | actual = append(actual, result) |
| 163 | } |
| 164 | default: |
| 165 | return nil, fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("expected an array or a document, but got %s", vr.Typescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/bson/bsonrw`/ValueReader#Type.()) |
| 166 | } |
| 167 | |
| 168 | return actual, nil |
| 169 | } |