1
0
mirror of https://github.com/golang/go synced 2024-11-24 19:50:18 -07:00

go/token: remove dependency on encoding/gob

R=dsymonds
CC=golang-dev
https://golang.org/cl/5636053
This commit is contained in:
Robert Griesemer 2012-02-06 17:41:19 -08:00
parent 1f133e2b8e
commit 668418d122
3 changed files with 23 additions and 24 deletions

View File

@ -867,7 +867,10 @@ func (x *Index) Write(w io.Writer) error {
return err return err
} }
if fulltext { if fulltext {
if err := x.fset.Write(w); err != nil { encode := func(x interface{}) error {
return gob.NewEncoder(w).Encode(x)
}
if err := x.fset.Write(encode); err != nil {
return err return err
} }
if err := x.suffixes.Write(w); err != nil { if err := x.suffixes.Write(w); err != nil {
@ -897,7 +900,10 @@ func (x *Index) Read(r io.Reader) error {
return err return err
} }
x.suffixes = new(suffixarray.Index) x.suffixes = new(suffixarray.Index)
if err := x.suffixes.Read(r); err != nil { decode := func(x interface{}) error {
return gob.NewDecoder(r).Decode(x)
}
if err := x.suffixes.Read(decode); err != nil {
return err return err
} }
} }

View File

@ -4,11 +4,6 @@
package token package token
import (
"encoding/gob"
"io"
)
type serializedFile struct { type serializedFile struct {
// fields correspond 1:1 to fields with same (lower-case) name in File // fields correspond 1:1 to fields with same (lower-case) name in File
Name string Name string
@ -23,19 +18,10 @@ type serializedFileSet struct {
Files []serializedFile Files []serializedFile
} }
func (s *serializedFileSet) Read(r io.Reader) error { // Read calls decode to deserialize a file set into s; s must not be nil.
return gob.NewDecoder(r).Decode(s) func (s *FileSet) Read(decode func(interface{}) error) error {
}
func (s *serializedFileSet) Write(w io.Writer) error {
return gob.NewEncoder(w).Encode(s)
}
// Read reads the fileset from r into s; s must not be nil.
// If r does not also implement io.ByteReader, it will be wrapped in a bufio.Reader.
func (s *FileSet) Read(r io.Reader) error {
var ss serializedFileSet var ss serializedFileSet
if err := ss.Read(r); err != nil { if err := decode(&ss); err != nil {
return err return err
} }
@ -53,8 +39,8 @@ func (s *FileSet) Read(r io.Reader) error {
return nil return nil
} }
// Write writes the fileset s to w. // Write calls encode to serialize the file set s.
func (s *FileSet) Write(w io.Writer) error { func (s *FileSet) Write(encode func(interface{}) error) error {
var ss serializedFileSet var ss serializedFileSet
s.mutex.Lock() s.mutex.Lock()
@ -66,5 +52,5 @@ func (s *FileSet) Write(w io.Writer) error {
ss.Files = files ss.Files = files
s.mutex.Unlock() s.mutex.Unlock()
return ss.Write(w) return encode(ss)
} }

View File

@ -6,6 +6,7 @@ package token
import ( import (
"bytes" "bytes"
"encoding/gob"
"fmt" "fmt"
"testing" "testing"
) )
@ -69,12 +70,18 @@ func equal(p, q *FileSet) error {
func checkSerialize(t *testing.T, p *FileSet) { func checkSerialize(t *testing.T, p *FileSet) {
var buf bytes.Buffer var buf bytes.Buffer
if err := p.Write(&buf); err != nil { encode := func(x interface{}) error {
return gob.NewEncoder(&buf).Encode(x)
}
if err := p.Write(encode); err != nil {
t.Errorf("writing fileset failed: %s", err) t.Errorf("writing fileset failed: %s", err)
return return
} }
q := NewFileSet() q := NewFileSet()
if err := q.Read(&buf); err != nil { decode := func(x interface{}) error {
return gob.NewDecoder(&buf).Decode(x)
}
if err := q.Read(decode); err != nil {
t.Errorf("reading fileset failed: %s", err) t.Errorf("reading fileset failed: %s", err)
return return
} }