File: example_test.go

package info (click to toggle)
golang-mongodb-mongo-driver 1.17.1%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 25,988 kB
  • sloc: perl: 533; ansic: 491; python: 432; sh: 327; makefile: 174
file content (143 lines) | stat: -rw-r--r-- 3,129 bytes parent folder | download
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright (C) MongoDB, Inc. 2023-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 bson_test

import (
	"fmt"
	"time"

	"go.mongodb.org/mongo-driver/bson"
)

// This example uses Raw to skip parsing a nested document in a BSON message.
func ExampleRaw_unmarshal() {
	b, err := bson.Marshal(bson.M{
		"Word":     "beach",
		"Synonyms": bson.A{"coast", "shore", "waterfront"},
	})
	if err != nil {
		panic(err)
	}

	var res struct {
		Word     string
		Synonyms bson.Raw // Don't parse the whole list, we just want to count the elements.
	}

	err = bson.Unmarshal(b, &res)
	if err != nil {
		panic(err)
	}
	elems, err := res.Synonyms.Elements()
	if err != nil {
		panic(err)
	}
	fmt.Printf("%s, synonyms count: %d\n", res.Word, len(elems))

	// Output: beach, synonyms count: 3
}

// This example uses Raw to add a precomputed BSON document during marshal.
func ExampleRaw_marshal() {
	precomputed, err := bson.Marshal(bson.M{"Precomputed": true})
	if err != nil {
		panic(err)
	}

	msg := struct {
		Message  string
		Metadata bson.Raw
	}{
		Message:  "Hello World!",
		Metadata: precomputed,
	}

	b, err := bson.Marshal(msg)
	if err != nil {
		panic(err)
	}
	// Print the Extended JSON by converting BSON to bson.Raw.
	fmt.Println(bson.Raw(b).String())

	// Output: {"message": "Hello World!","metadata": {"Precomputed": true}}
}

// This example uses RawValue to delay parsing a value in a BSON message.
func ExampleRawValue_unmarshal() {
	b1, err := bson.Marshal(bson.M{
		"Format":    "UNIX",
		"Timestamp": 1675282389,
	})
	if err != nil {
		panic(err)
	}

	b2, err := bson.Marshal(bson.M{
		"Format":    "RFC3339",
		"Timestamp": time.Unix(1675282389, 0).Format(time.RFC3339),
	})
	if err != nil {
		panic(err)
	}

	for _, b := range [][]byte{b1, b2} {
		var res struct {
			Format    string
			Timestamp bson.RawValue // Delay parsing until we know the timestamp format.
		}

		err = bson.Unmarshal(b, &res)
		if err != nil {
			panic(err)
		}

		var t time.Time
		switch res.Format {
		case "UNIX":
			t = time.Unix(res.Timestamp.AsInt64(), 0)
		case "RFC3339":
			t, err = time.Parse(time.RFC3339, res.Timestamp.StringValue())
			if err != nil {
				panic(err)
			}
		}
		fmt.Println(res.Format, t.Unix())
	}

	// Output:
	// UNIX 1675282389
	// RFC3339 1675282389
}

// This example uses RawValue to add a precomputed BSON string value during marshal.
func ExampleRawValue_marshal() {
	t, val, err := bson.MarshalValue("Precomputed message!")
	if err != nil {
		panic(err)
	}
	precomputed := bson.RawValue{
		Type:  t,
		Value: val,
	}

	msg := struct {
		Message bson.RawValue
		Time    time.Time
	}{
		Message: precomputed,
		Time:    time.Unix(1675282389, 0),
	}

	b, err := bson.Marshal(msg)
	if err != nil {
		panic(err)
	}
	// Print the Extended JSON by converting BSON to bson.Raw.
	fmt.Println(bson.Raw(b).String())

	// Output: {"message": "Precomputed message!","time": {"$date":{"$numberLong":"1675282389000"}}}
}