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

Map keys always use stringer function

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: 1.3.0
    • Fix Version/s: 1.4.0
    • Component/s: BSON
    • Security Level: Public
    • Labels:
      None
    • Backwards Compatibility:
      Minor Change

      Description

      When you insert a map into a collection the keys are always `fmt.Sprint`ed, and therefore always use the `String()` function if one exists on that type - this is not always ideal.

       

      Please see the below example for a valid use case and further explanation of the current versus desired behaviour:

      package mainimport (
      "context"
      "go.mongodb.org/mongo-driver/mongo"
      "go.mongodb.org/mongo-driver/mongo/options"
      )
       
      type MyConstantyType stringconst (
      	TheFirst  MyConstantyType = "TheFirst"
      	TheSecond MyConstantyType = "TheSecond"
      )
       
      var myConstantyTypeDisplayNames = map[MyConstantyType]string{
      	TheFirst:  "The first one",
      	TheSecond: "The second one",
      }
       
      func (m MyConstantyType) String() string {
      	return myConstantyTypeDisplayNames[m]
      }
       
      type OuterType struct {
      	Things map[MyConstantyType]int
      }
       
      func main() {
      	opts := options.Client().ApplyURI("mongodb://localhost")
      	client, err := mongo.Connect(context.TODO(), opts)
      	if err != nil {
      		panic(err)
      	}
      	defer client.Disconnect(context.TODO())	o := &OuterType{
      		Things: map[MyConstantyType]int{
      			TheFirst:  12312,
      			TheSecond: 9876,
      		},
      	}	/*
      		We would expect the above to be stored in the database as:
      		{
      			"things": {
      				"TheFirst": 12312,
      				"TheSecond": 9876,
      			}
      		}		But it is actually stored as:
      		{
      			"things": {
      				"The first one": 12312,
      				"The second one": 9876,
      			}
      		}		This is because of the stringer method `String()` here, which is called from the `fmt.Sprint(key)`
      		at https://github.com/mongodb/mongo-go-driver/blob/master/bson/bsoncodec/map_codec.go#L82
      	*/	
       
      _, err = client.Database("test").Collection("test").InsertOne(context.TODO(), o)	if err != nil {
      		panic(err)
      	}
      }
      
      

       

       
       

        Attachments

          Activity

            People

            Assignee:
            isabella.siu Isabella Siu
            Reporter:
            anonymous.user Anonymous User
            Participants:
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: