Skip to content

Commit 2feca6f

Browse files
lindseymoorerachel-mack
authored andcommitted
DOCSP-51825 Standardize Struct Tag Usage ex (#546)
1 parent 4621b3c commit 2feca6f

File tree

5 files changed

+117
-119
lines changed

5 files changed

+117
-119
lines changed

source/archive-reference-files/usage-examples/struct-tagging.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ contains a struct tag that maps the ``WordCount`` field to the BSON
1919
field name ``word_count``. By default, the driver marshals the other
2020
fields as the lowercase of the struct field name:
2121

22-
.. literalinclude:: /includes/usage-examples/code-snippets/struct-tag.go
22+
.. literalinclude:: /includes/usage-examples/code-snippets/structTag.go
2323
:start-after: begin struct
2424
:end-before: end struct
2525
:language: go
@@ -32,13 +32,13 @@ struct field as ``word_count``:
3232

3333
.. include:: /includes/usage-examples/run-example-tip.rst
3434

35-
.. literalinclude:: /includes/usage-examples/code-snippets/struct-tag.go
35+
.. literalinclude:: /includes/usage-examples/code-snippets/structTag.go
3636
:start-after: begin create and insert
3737
:end-before: end create and insert
3838
:language: go
3939
:dedent:
4040

41-
View a `fully runnable example. <{+example+}/struct-tag.go>`__
41+
View a `fully runnable example. <{+example+}/structTag.go>`__
4242

4343
Expected Result
4444
---------------

source/data-formats/struct-tagging.txt

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,50 +10,59 @@ Use Struct Tags
1010
You can specify the way that the Go Driver converts Go
1111
structs to :manual:`BSON </reference/bson-types/>` by using struct tags.
1212

13-
Example
14-
-------
15-
.. TODO: Add the Usage Examples intro include once it is merged
16-
17-
The following code declares a struct of type ``BlogPost``. This struct
18-
contains a struct tag that maps the ``WordCount`` field to the BSON
19-
field name ``word_count``. By default, the driver marshals the other
20-
fields as the lowercase of the struct field name:
21-
22-
.. literalinclude:: /includes/usage-examples/code-snippets/struct-tag.go
23-
:start-after: begin struct
24-
:end-before: end struct
25-
:language: go
26-
:dedent:
13+
Example: Full File
14+
------------------
15+
16+
.. include:: /includes/usage-examples/example-intro.rst
17+
18+
This example declares a struct of type ``Restaurant`` with the
19+
following struct tags:
20+
21+
- A struct tag that maps the ``RestaurantId`` field to the BSON
22+
field name ``restaurant_id``. By default, the driver marshals the other
23+
fields as the lowercase of the struct field name.
24+
25+
- The ``omitempty`` struct tag omits the corresponding field from the
26+
inserted document when left empty.
2727

28-
The following example creates a ``BlogPost`` instance and inserts it
29-
into the ``posts`` collection. During the insert operation, the driver
30-
interprets the struct tag to marshal the ``WordCount``
31-
struct field as ``word_count``:
28+
The following code shows the ``Restaurant`` struct used in the example:
3229

33-
.. literalinclude:: /includes/usage-examples/code-snippets/struct-tag.go
34-
:start-after: begin create and insert
35-
:end-before: end create and insert
30+
.. literalinclude:: /includes/usage-examples/code-snippets/structTagStruct.go
3631
:language: go
37-
:dedent:
3832

39-
View a `fully runnable example. <{+example+}/struct-tag.go>`__
33+
The following example creates a ``Restaurant`` instance and inserts it
34+
into the ``restaurants`` collection. During the insert operation, the driver
35+
interprets the struct tags to marshal the ``RestaurantId``
36+
struct field as ``restaurant_id`` and omits fields that are left empty in the
37+
sample document:
38+
39+
.. io-code-block::
40+
:copyable: true
41+
42+
.. input:: /includes/usage-examples/code-snippets/structTag.go
43+
:language: go
44+
:dedent:
45+
46+
.. output::
47+
:language: none
48+
:visible: false
49+
50+
Document inserted with ID: ObjectID("...")
4051

4152
Expected Result
4253
---------------
4354

4455
After you run the full example, you can find the following document
45-
in the ``posts`` collection:
56+
in the ``restaurants`` collection:
4657

4758
.. code-block:: json
4859
:copyable: false
4960

5061
{
5162
"_id" : ObjectId("..."),
52-
"title" : "Annuals vs. Perennials?",
53-
"author" : "Sam Lee",
54-
"word_count" : 682,
55-
"lastupdated": ...,
56-
"tags" : ["seasons", "gardening", "flower"]
63+
"name" : "Amazing Pizza",
64+
"restaurant_id" : "123456789",
65+
"cuisine" : "American
5766
}
5867

5968
For an example on how to find a document, see the :ref:`golang-find-one` guide.

source/includes/usage-examples/code-snippets/struct-tag.go

Lines changed: 0 additions & 87 deletions
This file was deleted.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Specifies struct tags on a struct by using the Go driver
2+
package main
3+
4+
import (
5+
"context"
6+
"fmt"
7+
"log"
8+
"os"
9+
10+
"github.com/joho/godotenv"
11+
"go.mongodb.org/mongo-driver/v2/mongo"
12+
"go.mongodb.org/mongo-driver/v2/mongo/options"
13+
)
14+
15+
// Specifies a different name for RestaurantID
16+
// and marks certain fields as omitempty
17+
type Restaurant struct {
18+
Name string
19+
RestaurantId string `bson:"restaurant_id,omitempty"`
20+
Cuisine string `bson:"cuisine,omitempty"`
21+
Address interface{} `bson:"address,omitempty"`
22+
Borough string `bson:"borough,omitempty"`
23+
Grades []interface{} `bson:"grades,omitempty"`
24+
}
25+
26+
func main() {
27+
if err := godotenv.Load(); err != nil {
28+
log.Println("No .env file found")
29+
}
30+
31+
var uri string
32+
if uri = os.Getenv("MONGODB_URI"); uri == "" {
33+
log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable")
34+
}
35+
36+
client, err := mongo.Connect(options.Client().ApplyURI(uri))
37+
if err != nil {
38+
panic(err)
39+
}
40+
defer func() {
41+
if err = client.Disconnect(context.TODO()); err != nil {
42+
panic(err)
43+
}
44+
}()
45+
46+
coll := client.Database("sample_restaurants").Collection("restaurants")
47+
48+
// Creates a Restaurant document
49+
50+
newRestaurant := Restaurant{
51+
Name: "Amazing Pizza",
52+
RestaurantId: "123456789",
53+
Cuisine: "American",
54+
}
55+
56+
// Inserts the sample document describing a restaurant into the collection
57+
58+
result, err := coll.InsertOne(context.TODO(), newRestaurant)
59+
if err != nil {
60+
panic(err)
61+
}
62+
63+
// Prints the ID of the inserted document
64+
fmt.Printf("Document inserted with ID: %s\n", result.InsertedID)
65+
66+
// When you run this file, it should print:
67+
// Document inserted with ID: ObjectID("...")
68+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
type Restaurant struct {
2+
Name string
3+
RestaurantId string `bson:"restaurant_id,omitempty"`
4+
Cuisine string `bson:"cuisine,omitempty"`
5+
Address interface{} `bson:"address,omitempty"`
6+
Borough string `bson:"borough,omitempty"`
7+
Grades []interface{} `bson:"grades,omitempty"`
8+
}

0 commit comments

Comments
 (0)