|
| 1 | +package bson_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "time" |
| 6 | + |
| 7 | + "go.mongodb.org/mongo-driver/bson" |
| 8 | +) |
| 9 | + |
| 10 | +// This example uses Raw to skip parsing a nested document in a BSON message. |
| 11 | +func ExampleRaw_unmarshal() { |
| 12 | + b, err := bson.Marshal(bson.M{ |
| 13 | + "Word": "beach", |
| 14 | + "Synonyms": bson.A{"coast", "shore", "waterfront"}, |
| 15 | + }) |
| 16 | + if err != nil { |
| 17 | + panic(err) |
| 18 | + } |
| 19 | + |
| 20 | + var res struct { |
| 21 | + Word string |
| 22 | + Synonyms bson.Raw // Don't parse the whole list, we just want to count the elements. |
| 23 | + } |
| 24 | + |
| 25 | + err = bson.Unmarshal(b, &res) |
| 26 | + if err != nil { |
| 27 | + panic(err) |
| 28 | + } |
| 29 | + elems, err := res.Synonyms.Elements() |
| 30 | + if err != nil { |
| 31 | + panic(err) |
| 32 | + } |
| 33 | + fmt.Printf("%s, synonyms count: %d\n", res.Word, len(elems)) |
| 34 | + |
| 35 | + // Output: beach, synonyms count: 3 |
| 36 | +} |
| 37 | + |
| 38 | +// This example uses Raw to add a precomputed BSON document during marshal. |
| 39 | +func ExampleRaw_marshal() { |
| 40 | + precomputed, err := bson.Marshal(bson.M{"Precomputed": true}) |
| 41 | + if err != nil { |
| 42 | + panic(err) |
| 43 | + } |
| 44 | + |
| 45 | + msg := struct { |
| 46 | + Message string |
| 47 | + Metadata bson.Raw |
| 48 | + }{ |
| 49 | + Message: "Hello World!", |
| 50 | + Metadata: precomputed, |
| 51 | + } |
| 52 | + |
| 53 | + b, err := bson.Marshal(msg) |
| 54 | + if err != nil { |
| 55 | + panic(err) |
| 56 | + } |
| 57 | + // Print the Extended JSON by converting BSON to bson.Raw. |
| 58 | + fmt.Println(bson.Raw(b).String()) |
| 59 | + |
| 60 | + // Output: {"message": "Hello World!","metadata": {"Precomputed": true}} |
| 61 | +} |
| 62 | + |
| 63 | +// This example uses RawValue to delay parsing a value in a BSON message. |
| 64 | +func ExampleRawValue_unmarshal() { |
| 65 | + b1, err := bson.Marshal(bson.M{ |
| 66 | + "Format": "UNIX", |
| 67 | + "Timestamp": 1675282389, |
| 68 | + }) |
| 69 | + if err != nil { |
| 70 | + panic(err) |
| 71 | + } |
| 72 | + |
| 73 | + b2, err := bson.Marshal(bson.M{ |
| 74 | + "Format": "RFC3339", |
| 75 | + "Timestamp": time.Unix(1675282389, 0).Format(time.RFC3339), |
| 76 | + }) |
| 77 | + if err != nil { |
| 78 | + panic(err) |
| 79 | + } |
| 80 | + |
| 81 | + for _, b := range [][]byte{b1, b2} { |
| 82 | + var res struct { |
| 83 | + Format string |
| 84 | + Timestamp bson.RawValue // Delay parsing until we know the timestamp format. |
| 85 | + } |
| 86 | + |
| 87 | + err = bson.Unmarshal(b, &res) |
| 88 | + if err != nil { |
| 89 | + panic(err) |
| 90 | + } |
| 91 | + |
| 92 | + var t time.Time |
| 93 | + switch res.Format { |
| 94 | + case "UNIX": |
| 95 | + t = time.Unix(res.Timestamp.AsInt64(), 0) |
| 96 | + case "RFC3339": |
| 97 | + t, err = time.Parse(time.RFC3339, res.Timestamp.StringValue()) |
| 98 | + if err != nil { |
| 99 | + panic(err) |
| 100 | + } |
| 101 | + } |
| 102 | + fmt.Println(res.Format, t.Unix()) |
| 103 | + } |
| 104 | + |
| 105 | + // Output: |
| 106 | + // UNIX 1675282389 |
| 107 | + // RFC3339 1675282389 |
| 108 | +} |
| 109 | + |
| 110 | +// This example uses RawValue to add a precomputed BSON string value during marshal. |
| 111 | +func ExampleRawValue_marshal() { |
| 112 | + t, val, err := bson.MarshalValue("Precomputed message!") |
| 113 | + if err != nil { |
| 114 | + panic(err) |
| 115 | + } |
| 116 | + precomputed := bson.RawValue{ |
| 117 | + Type: t, |
| 118 | + Value: val, |
| 119 | + } |
| 120 | + |
| 121 | + msg := struct { |
| 122 | + Message bson.RawValue |
| 123 | + Time time.Time |
| 124 | + }{ |
| 125 | + Message: precomputed, |
| 126 | + Time: time.Unix(1675282389, 0), |
| 127 | + } |
| 128 | + |
| 129 | + b, err := bson.Marshal(msg) |
| 130 | + if err != nil { |
| 131 | + panic(err) |
| 132 | + } |
| 133 | + // Print the Extended JSON by converting BSON to bson.Raw. |
| 134 | + fmt.Println(bson.Raw(b).String()) |
| 135 | + |
| 136 | + // Output: {"message": "Precomputed message!","time": {"$date":{"$numberLong":"1675282389000"}}} |
| 137 | +} |
0 commit comments