Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-2271

Update "bson.D" BSON document literal syntax to resolve Go vet linter warnings

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None

      Use a different syntax for declaring BSON document literals to avoid the "unkeyed fields" "go vet" linter warnings.

      Reference BSON document definitions

      JSON:

      {
          "key1": {
              "nested": "value"
          },
          "key2": "value2",
          "key3": {
              "nested1": {
                  "nested2": 14
              }
          }
      }
      

      Javascript:

      {
          key1: {
              nested: "value",
          },
          key2: "value2",
          key3: {
              nested1: {
                  nested2: 14,
              },
          },
      }
      

      Existing Go syntax

      bson.M:

      fmt.Println(bson.M{
      	"key1": bson.M{
      		"nested": "value",
      	},
      	"key2": "value2",
      	"key3": bson.M{
      		"nested1": bson.M{
      			"nested2": 14,
      		},
      	},
      })
      

      bson.D:

      fmt.Println(bson.D{
      	{"key1", bson.D{
      		{"nested", "value"},
      	}},
      	{"key2", "value2"},
      	{"key3", bson.D{
      		{"nested1", bson.D{
      			{"nested2", 14},
      		}},
      	}},
      })
      

      bson.D with keyed fields:

      fmt.Println(bson.D{
      	{Key: "key1", Value: bson.D{
      		{Key: "nested", Value: "value"},
      	}},
      	{Key: "key2", Value: "value2"},
      	{Key: "key3", Value: bson.D{
      		{Key: "nested1", Value: bson.D{
      			{Key: "nested2", Value: 14},
      		}},
      	}},
      })
      

      bson.D syntax proposals

      1. bson.D with keyed parameters and short keys:
        fmt.Println(bson.D{
        	{K: "key1", V: bson.D{
        		{K: "nested", V: "value"},
        	}},
        	{K: "key2", V: "value2"},
        	{K: "key3", V: bson.D{
        		{K: "nested1", V: bson.D{
        			{K: "nested2", V: 14},
        		}},
        	}},
        })
        
      2. bson.D with bson.E as a function:
        fmt.Println(bson.D{
        	bson.E("key1", bson.D{
        		bson.E("nested", "value"),
        	}),
        	bson.E("key2", "value2"),
        	bson.E("key3", bson.D{
        		bson.E("nested1", bson.D{
        			bson.E("nested2", 14),
        		}),
        	}),
        })
        
      3. bson.D and bson.E as functions:
        fmt.Println(bson.D(
        	bson.E("key1", bson.D(
        		bson.E("nested", "value"),
        	)),
        	bson.E("key2", "value2"),
        	bson.E("key3", bson.D(
        		bson.E("nested1", bson.D(
        			bson.E("nested2", 14),
        		)),
        	)),
        ))
        
      4. bson.D as function that takes an even number of k/v arguments:
        fmt.Println(bson.D(
        	"key1", bson.D(
        		"nested", "value",
        	),
        	"key2", "value2",
        	"key3", bson.D(
        		"nested1", bson.D(
        			"nested2", 14,
        		),
        	),
        ))
        
      5. bson.D as the builder pattern:
        fmt.Println(bson.D().
        	E("key1", bson.D().
        		E("nested", "value"),
        	).
        	E("key2", "value2").
        	E("key3", bson.D().
        		E("nested1", bson.D().
        			E("nested2", 14),
        		),
        	),
        )
        
      6. JSON-to-BSON converter function (from GODRIVER-2459).
        fmt.Println(mongo.MustCompileQuery(`{
        	"key1": {
        		"nested": $1
        	},
        	"key2": $2,
        	"key3": {
        		"nested1": {
        			"nested2": $3
        		}
        	}`,
        	"$1", "value",
        	"$2", "value2",
        	"$3", 14,
        ))
        

            Assignee:
            Unassigned Unassigned
            Reporter:
            matt.dale@mongodb.com Matt Dale
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: