[GODRIVER-713] Remove string key restriction for Encoder and Decoder lookups Created: 17/Dec/18  Updated: 28/Oct/23  Resolved: 30/Jan/19

Status: Closed
Project: Go Driver
Component/s: BSON
Affects Version/s: None
Fix Version/s: 0.3.0

Type: Improvement Priority: Major - P3
Reporter: zhengyang zhu Assignee: Divjot Arora (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

The LookupEncoder and LookupDecoder methods on bsoncodec.Registry prevent looking up a default codec for maps with non-string keys. While the default map encoder and decoder does not support non-string keys, users who wish to do so should be able to register their own default encoder and decoder for maps with non-string keys. Users can already register a default map encoder or decoder that handles non-string keys.

Remove the check from LookupEncoder and LookupDecoder that prevents returning the default map encoder or decoder if the map's key is not a string.

 

(original description below)


Why bson package cannot resolve map[int]int, while encoding/json and mgo can.

https://github.com/globalsign/mgo/pull/140

 

I wrote a MapEncoder for myself, then call RegisterDefaultEncoder(). It is stuck in

 (r *Registry) LookupEncoder(t reflect.Type) (ValueEncoder, error) {

...

if t != nil && t.Kind() == reflect.Map && t.Key().Kind() != reflect.String

{ ... }

I cannot implement a mapEncoder/Decoder without modifying this driver.

Please support numeric map keys.



 Comments   
Comment by Githook User [ 30/Jan/19 ]

Author:

{'username': 'divjotarora', 'email': 'divjot.arora@10gen.com', 'name': 'Divjot Arora'}

Message: Remove string key restriction for maps in LookupEncoder/LookupDecoder.

GODRIVER-713

Change-Id: Id30100a8bdb64f9cea15b42449632583a41e9f07
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/769f6da9b23d5c239b3550df016a03abde60ec0b

Comment by Kristofer Brandow (Inactive) [ 04/Jan/19 ]

Hi 825335693@qq.com,

While we don't support non-string keys by default, there we shouldn't prevent users from doing so. For now, you should be able to register a codec for map[int]int via the RegisterEncoder method.

--Kris

Generated at Thu Feb 08 08:34:48 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.