89

I am working with new official mongodb driver for golang. I have created one complex query to insert the data into mongo db and then sort it according to an element value. I am using a filter in which I have created the bson type using :-

filter := bson.D{{"autorefid", "100"}}

But It is showing a warning saying:

primitive.E composite literal uses unkeyed fields

The warnings are creating a mess in my code.

Himanshu
  • 12,071
  • 7
  • 46
  • 61

6 Answers6

97

The warnings can be stopped by setting the check flag to false.

$ go doc cmd/vet

By default all checks are performed. If any flags are explicitly set to true, only those tests are run. Conversely, if any flag is explicitly set to false, only those tests are disabled. Thus -printf=true runs the printf check, -printf=false runs all checks except the printf check.

Unkeyed composite literals

Flag: -composites

Composite struct literals that do not use the field-keyed syntax.

But the warning is due to not providing the keys name when setting the value in primitive.E struct.

Setting keys for primitive.E struct will remove the warning messages. For example

filter := bson.D{primitive.E{Key: "autorefid", Value: "100"}}

Package primitive contains types similar to Go primitives for BSON types can do not have direct Go primitive representations.

type E struct {
    Key   string
    Value interface{}
}

E represents a BSON element for a D. It is usually used inside a D.

For more information have a look at primitive.E

Scott Stensland
  • 26,870
  • 12
  • 93
  • 104
Himanshu
  • 12,071
  • 7
  • 46
  • 61
  • I see its cleaner when we provide the keys – Karl Anthony Baluyot Dec 31 '19 at 04:18
  • 7
    filter := bson.D{{Key: "autorefid", Value: "100"}} // skipping `primitive.E` also works. – Chris W Feb 06 '20 at 20:20
  • 1
    So if we have it in a variable `e := functionWhichReturnsE(...)` and we need a `bson.D` we need to do `bd := bson.D{Key: e.Key, Value: e.Value}` ? looks ok, until you do it for `template.Template`. Better to disable it smh – Phani Rithvij Nov 02 '20 at 00:59
  • I think I'll add something like `func E(s string, i interface{}) bson.E { return bson.E{ Key: s, Value: i, } }` to my `utils` package :S – Cirelli94 Mar 19 '21 at 10:06
39

Since no one else has pointed this out — the warning is there to safeguard you from changes in the third-party API breaking your code silently.

Ignoring this vet warning has the potential to lead to really nasty and hard to track down runtime bugs, so you'd be better off if you were to always specify the keys of 3rd party structs explicitly.

Such would be the case if the maintainers of the library you are using decided to change the order of elements in their struct for whatever reason. For example, switching Key with Value, in the OP case. Your code would still appear to compile just fine, but what you intended to be the key would now be passed as the value and vice versa, and things would just start breaking in unexpected ways.

antichris
  • 2,827
  • 1
  • 22
  • 18
  • 5
    This explains why it only warns about struct from other package. This won't warn about struct from the same package of the code. – fefe Dec 08 '20 at 02:30
  • Most of the code uses same models package for struct. This should not be a warning, as using the struct literal initialization catches the errors while copying the structs. The newly added fields in the struct will fail at compile time. If we use key value type of initialization, we will not able to catch the missing new fields during copy operations – user640554 Mar 23 '22 at 16:20
  • strange there's this warning because if you look at mongo's documentation for golang + export code feature of Compass, they do not use Key-Value pair for bson.D. – Someone Special Jun 29 '22 at 18:13
37

You can simply change:

filter := bson.D{{"autorefid", "100"}}

to:

filter := bson.D{{Key: "autorefid", Value: "100"}}

//Skip primitive.E as said by Chris W

25

You can change:

filter := bson.D{{"autorefid", "100"}}

to:

filter := bson.D{primitive.E{Key: "autorefid", Value: "100"}}
dbam
  • 1,088
  • 1
  • 13
  • 16
13

Actually, if ordered representation of a BSON document doesn't necessary you can change your BSON document to unordered representation by simply change

filter := bson.D{{"autorefid", "100"}}

to

filter := bson.M{"autorefid": "100"}

See this stackoverflow answer for more details about bson.D vs bson.M. The MongoDB server is smart enough to find matching indices regardless of the used order.

Mekel Ilyasa
  • 355
  • 2
  • 6
  • 16
2

If you are using vscode try disable the following in settings:

enter image description here