go-cloud icon indicating copy to clipboard operation
go-cloud copied to clipboard

docstore cannot use custom decoder

Open eqinox76 opened this issue 4 years ago • 1 comments

Describe the bug

I am trying to use a custom decoder. Reading the docs and code i see that the encoding.BinaryUnmarshaler interface is supported. https://github.com/google/go-cloud/blob/b052f1888ac6bd661c693890de1b44a388794ee1/docstore/driver/codec.go#L407-L412 But the gocloud docstore seems to ignore it.

To Reproduce

package main

import (
	"context"
	"encoding"
	"io"
	"log"

	"github.com/google/uuid"
	"gocloud.dev/docstore"
	_ "gocloud.dev/docstore/memdocstore"
)

type Myuuid uuid.UUID

var _ encoding.BinaryUnmarshaler = &Myuuid{}

func (m *Myuuid) UnmarshalBinary(data []byte) error {
	log.Pritnln("unmarshaller called")
	return nil
}

type Doc struct {
	Id               Myuuid
	Name             string
	DocstoreRevision interface{}
}

func main() {
	ctx := context.Background()
	coll, err := docstore.OpenCollection(ctx, "mem://Test/Id")
	if err != nil {
		log.Panic(err)
	}

	err = coll.Put(ctx, &Doc{
		Id:   Myuuid(uuid.New()),
		Name: "john",
	})
	if err != nil {
		log.Panic(err)
	}

	iter := coll.Query().Get(ctx)
	for {
		doc := Doc{}
		err := iter.Next(ctx, &doc)
		if err != nil {
			if err == io.EOF {
				return
			}
			log.Panic(err)
		}
		log.Printf("%+v", doc)
	}
}

Expected behavior

Myuuid.UnmarshalBinary is called when reading from the docstore.

Version

gocloud.dev v0.24.0

Additional context

Also tested with gocloud.dev/docstore/mongodocstore

Thanks for your work in maintaining this library!

eqinox76 avatar Sep 02 '21 14:09 eqinox76

@jba is this supposed to work?

vangent avatar Sep 02 '21 19:09 vangent

This works if your actual type implements the interface (not a pointer to your type).

E.g.:

func (m Myuuid) UnmarshalBinary(data []byte) error {

not

func (m *Myuuid) UnmarshalBinary(data []byte) error {

vangent avatar Dec 28 '22 17:12 vangent