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

Add documentation on how to set a custom bsoncodec.Registry

    • Type: Icon: Improvement Improvement
    • Resolution: Gone away
    • Priority: Icon: Trivial - P5 Trivial - P5
    • None
    • Affects Version/s: None
    • Component/s: Documentation

      At the moment it is not obvious how to set a custom registry, such that it is used both when writing to mongo and reading with a cursor.

       

      Bellow an example of what is I had to do to get this t work:

       
      rb := bson.NewRegistryBuilder()

          cod := &UUIDCodec{}
          tUUID := reflect.TypeOf(uuid.UUID{})

          rb.RegisterDecoder(reflect.PtrTo(tUUID), bsoncodec.ValueDecoderFunc(cod.DecodeValue))
          rb.RegisterEncoder(reflect.PtrTo(tUUID), cod)

          reg := rb.Build()

          opt := &options.ClientOptions{
              Registry: reg,
              TopologyOptions: []topology.Option{
                  topology.WithServerOptions(func(opts ...topology.ServerOption) []topology.ServerOption {
                      return []topology.ServerOption{
                          topology.WithRegistry(func(r *bsoncodec.Registry) *bsoncodec.Registry

      {                         return reg                     }

      ),
                      }
                  }),
              },
          }
       
      Adding this or something similar as an example, or even pushing this code into the SetRegistry method of the mongo.Options would be helpful.

          client, err := mongo.NewClientWithOptions(url, opt)

            Assignee:
            Unassigned Unassigned
            Reporter:
            tomzierbock Tom Zierbock
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: