featurebase/serializer.go
reesporte 4c53f86e82 removed license from each go file
i used this script, a little clunky but it got the job done

```bash
for file in `find . -type f -print | grep '\.go'`; do
    sed '1,/^\/\/ limitations under the License.$/d' $file > $file.tmp;
    result=`cat $file.tmp`
    if [[ result != "" ]]; then
        gofmt $file.tmp &> /dev/null;
        if [[ $? == 0 ]]; then
            mv $file.tmp $file && gofmt -w $file;
        else
            rm $file.tmp;
        fi
    else
        rm $file.tmp;
    fi
done
```
2021-12-10 09:17:17 -06:00

46 lines
1.5 KiB
Go

package pilosa
import (
"bytes"
"encoding/gob"
"fmt"
"github.com/pkg/errors"
)
// GobSerializer represents a Serializer that uses gob encoding. This is only
// used in tests; there's really no reason to use this instead of the proto
// serializer except that, as it's currently implemented, the proto serializer
// can't be used in internal tests (i.e test in the pilosa package) because the
// proto package imports the pilosa package, so it would result in circular
// imports. We really need all the pilosa types to be in a sub-package of
// pilosa, so that both proto and pilosa can import them without resulting in
// circular imports.
var GobSerializer Serializer = &gobSerializer{}
type gobSerializer struct{}
// Marshal is a gob-encoded implementation of the Serializer Marshal method.
func (s *gobSerializer) Marshal(msg Message) ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
if err := enc.Encode(msg); err != nil {
return nil, errors.Wrap(err, "gob encoding message")
}
return buf.Bytes(), nil
}
// Unmarshal is a gob-encoded implementation of the Serializer Unmarshal method.
func (s *gobSerializer) Unmarshal(b []byte, m Message) error {
switch mt := m.(type) {
case *CreateIndexMessage, *CreateFieldMessage:
dec := gob.NewDecoder(bytes.NewReader(b))
err := dec.Decode(mt)
if err != nil {
return errors.Wrapf(err, "decoding %T", mt)
}
return nil
default:
panic(fmt.Sprintf("unhandled Message of type %T: %#v", mt, m))
}
}