|
7 | 7 | package bsonrw
|
8 | 8 |
|
9 | 9 | import (
|
| 10 | + "fmt" |
| 11 | + "io" |
| 12 | + "strings" |
10 | 13 | "testing"
|
11 | 14 |
|
12 | 15 | "github.com/google/go-cmp/cmp"
|
@@ -45,3 +48,118 @@ func TestExtJSONReader(t *testing.T) {
|
45 | 48 | })
|
46 | 49 | })
|
47 | 50 | }
|
| 51 | + |
| 52 | +func TestReadMultipleTopLevelDocuments(t *testing.T) { |
| 53 | + testCases := []struct { |
| 54 | + name string |
| 55 | + input string |
| 56 | + expected [][]byte |
| 57 | + }{ |
| 58 | + { |
| 59 | + "single top-level document", |
| 60 | + "{\"foo\":1}", |
| 61 | + [][]byte{ |
| 62 | + {0x0E, 0x00, 0x00, 0x00, 0x10, 'f', 'o', 'o', 0x00, 0x01, 0x00, 0x00, 0x00, 0x00}, |
| 63 | + }, |
| 64 | + }, |
| 65 | + { |
| 66 | + "single top-level document with leading and trailing whitespace", |
| 67 | + "\n\n {\"foo\":1} \n", |
| 68 | + [][]byte{ |
| 69 | + {0x0E, 0x00, 0x00, 0x00, 0x10, 'f', 'o', 'o', 0x00, 0x01, 0x00, 0x00, 0x00, 0x00}, |
| 70 | + }, |
| 71 | + }, |
| 72 | + { |
| 73 | + "two top-level documents", |
| 74 | + "{\"foo\":1}{\"foo\":2}", |
| 75 | + [][]byte{ |
| 76 | + {0x0E, 0x00, 0x00, 0x00, 0x10, 'f', 'o', 'o', 0x00, 0x01, 0x00, 0x00, 0x00, 0x00}, |
| 77 | + {0x0E, 0x00, 0x00, 0x00, 0x10, 'f', 'o', 'o', 0x00, 0x02, 0x00, 0x00, 0x00, 0x00}, |
| 78 | + }, |
| 79 | + }, |
| 80 | + { |
| 81 | + "two top-level documents with leading and trailing whitespace and whitespace separation ", |
| 82 | + "\n\n {\"foo\":1}\n{\"foo\":2}\n ", |
| 83 | + [][]byte{ |
| 84 | + {0x0E, 0x00, 0x00, 0x00, 0x10, 'f', 'o', 'o', 0x00, 0x01, 0x00, 0x00, 0x00, 0x00}, |
| 85 | + {0x0E, 0x00, 0x00, 0x00, 0x10, 'f', 'o', 'o', 0x00, 0x02, 0x00, 0x00, 0x00, 0x00}, |
| 86 | + }, |
| 87 | + }, |
| 88 | + { |
| 89 | + "top-level array with single document", |
| 90 | + "[{\"foo\":1}]", |
| 91 | + [][]byte{ |
| 92 | + {0x0E, 0x00, 0x00, 0x00, 0x10, 'f', 'o', 'o', 0x00, 0x01, 0x00, 0x00, 0x00, 0x00}, |
| 93 | + }, |
| 94 | + }, |
| 95 | + { |
| 96 | + "top-level array with 2 documents", |
| 97 | + "[{\"foo\":1},{\"foo\":2}]", |
| 98 | + [][]byte{ |
| 99 | + {0x0E, 0x00, 0x00, 0x00, 0x10, 'f', 'o', 'o', 0x00, 0x01, 0x00, 0x00, 0x00, 0x00}, |
| 100 | + {0x0E, 0x00, 0x00, 0x00, 0x10, 'f', 'o', 'o', 0x00, 0x02, 0x00, 0x00, 0x00, 0x00}, |
| 101 | + }, |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + for _, tc := range testCases { |
| 106 | + t.Run(tc.name, func(t *testing.T) { |
| 107 | + r := strings.NewReader(tc.input) |
| 108 | + vr := NewExtJSONValueReader(r, false) |
| 109 | + |
| 110 | + actual, err := readAllDocuments(vr) |
| 111 | + if err != nil { |
| 112 | + t.Fatalf("expected no error, but got %v", err) |
| 113 | + } |
| 114 | + |
| 115 | + if diff := cmp.Diff(tc.expected, actual); diff != "" { |
| 116 | + t.Fatalf("expected does not match actual: %v", diff) |
| 117 | + } |
| 118 | + }) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +func readAllDocuments(vr ValueReader) ([][]byte, error) { |
| 123 | + c := NewCopier() |
| 124 | + var actual [][]byte |
| 125 | + |
| 126 | + switch vr.Type() { |
| 127 | + case bsontype.EmbeddedDocument: |
| 128 | + for { |
| 129 | + result, err := c.CopyDocumentToBytes(vr) |
| 130 | + if err != nil { |
| 131 | + if err == io.EOF { |
| 132 | + break |
| 133 | + } |
| 134 | + return nil, err |
| 135 | + } |
| 136 | + |
| 137 | + actual = append(actual, result) |
| 138 | + } |
| 139 | + case bsontype.Array: |
| 140 | + ar, err := vr.ReadArray() |
| 141 | + if err != nil { |
| 142 | + return nil, err |
| 143 | + } |
| 144 | + for { |
| 145 | + evr, err := ar.ReadValue() |
| 146 | + if err != nil { |
| 147 | + if err == ErrEOA { |
| 148 | + break |
| 149 | + } |
| 150 | + return nil, err |
| 151 | + } |
| 152 | + |
| 153 | + result, err := c.CopyDocumentToBytes(evr) |
| 154 | + if err != nil { |
| 155 | + return nil, err |
| 156 | + } |
| 157 | + |
| 158 | + actual = append(actual, result) |
| 159 | + } |
| 160 | + default: |
| 161 | + return nil, fmt.Errorf("expected an array or a document, but got %s", vr.Type()) |
| 162 | + } |
| 163 | + |
| 164 | + return actual, nil |
| 165 | +} |
0 commit comments