Remove the enc package

Move the remaining files to types/
This commit is contained in:
Erik Arvidsson
2015-10-28 12:38:30 -04:00
parent d3d04d5e05
commit d7ac77571c
9 changed files with 29 additions and 41 deletions

View File

@@ -1,20 +0,0 @@
package enc
import (
"encoding/json"
"io"
"github.com/attic-labs/noms/d"
)
var (
typedTag = []byte("t ")
)
func typedEncode(dst io.Writer, v interface{}) {
_, err := dst.Write(typedTag)
d.Exp.NoError(err)
err = json.NewEncoder(dst).Encode(v)
d.Exp.NoError(err)
return
}

View File

@@ -1,4 +1,4 @@
package enc
package types
import (
"bytes"

View File

@@ -1,4 +1,4 @@
package enc
package types
import (
"bytes"

View File

@@ -1,5 +1,4 @@
// Package enc contains a very low-level encoder/decoder. Serializes from interface{} to an io.Writer and deserializes from an io.Reader into an interface{}.
package enc
package types
import (
"bufio"
@@ -11,7 +10,7 @@ import (
)
// Encode serializes v into dst, and panics on unsupported types.
func Encode(dst io.Writer, v interface{}) {
func encode(dst io.Writer, v interface{}) {
d.Chk.NotNil(dst)
switch v := v.(type) {
case io.Reader:
@@ -22,7 +21,7 @@ func Encode(dst io.Writer, v interface{}) {
}
// Decode deserializes data from r into an interface{}, and panics on unsupported encoded types.
func Decode(r io.Reader) interface{} {
func decode(r io.Reader) interface{} {
d.Chk.NotNil(r)
// assumes all tags are same size, which they are for now.

View File

@@ -1,4 +1,4 @@
package enc
package types
import (
"bytes"
@@ -15,11 +15,11 @@ func TestEncode(t *testing.T) {
// Encoding details for each codec are tested elsewhere.
// Here we just want to make sure codecs are selected correctly.
dst := &bytes.Buffer{}
Encode(dst, bytes.NewReader([]byte{0x00, 0x01, 0x02}))
encode(dst, bytes.NewReader([]byte{0x00, 0x01, 0x02}))
assert.Equal([]byte{'b', ' ', 0x00, 0x01, 0x02}, dst.Bytes())
dst.Reset()
Encode(dst, []interface{}{42})
encode(dst, []interface{}{42})
assert.Equal("t [42]\n", string(dst.Bytes()))
}
@@ -27,18 +27,18 @@ func TestInvalidDecode(t *testing.T) {
assert := assert.New(t)
d.IsUsageError(assert, func() {
Decode(bytes.NewReader([]byte{}))
decode(bytes.NewReader([]byte{}))
})
d.IsUsageError(assert, func() {
Decode(bytes.NewReader([]byte{0xff}))
decode(bytes.NewReader([]byte{0xff}))
})
}
func TestSelectBlobDecoder(t *testing.T) {
assert := assert.New(t)
decoded := Decode(bytes.NewReader([]byte{'b', ' ', 0x2B}))
decoded := decode(bytes.NewReader([]byte{'b', ' ', 0x2B}))
out := &bytes.Buffer{}
_, err := io.Copy(out, decoded.(io.Reader))
assert.NoError(err)
@@ -46,6 +46,6 @@ func TestSelectBlobDecoder(t *testing.T) {
}
func TestSelectTypedDecoder(t *testing.T) {
v := Decode(bytes.NewBufferString(`t [42]`))
v := decode(bytes.NewBufferString(`t [42]`))
assert.Equal(t, []interface{}{float64(42)}, v)
}

View File

@@ -7,7 +7,6 @@ import (
"github.com/attic-labs/noms/chunks"
"github.com/attic-labs/noms/d"
"github.com/attic-labs/noms/enc"
"github.com/attic-labs/noms/ref"
)
@@ -19,7 +18,7 @@ func ReadValue(r ref.Ref, cs chunks.ChunkSource) Value {
return nil
}
v := enc.Decode(bytes.NewReader(c.Data()))
v := decode(bytes.NewReader(c.Data()))
switch v := v.(type) {
case io.Reader:

View File

@@ -7,7 +7,6 @@ import (
"github.com/attic-labs/noms/Godeps/_workspace/src/github.com/stretchr/testify/assert"
"github.com/attic-labs/noms/chunks"
"github.com/attic-labs/noms/enc"
"github.com/attic-labs/noms/ref"
)
@@ -17,11 +16,11 @@ func TestTolerateUngettableRefs(t *testing.T) {
assert.Nil(v)
}
func TestBlobLeafDecode(t *testing.T) {
func TestReadValueBlobLeafDecode(t *testing.T) {
assert := assert.New(t)
blobLeafDecode := func(r io.Reader) Value {
i := enc.Decode(r)
i := decode(r)
b, err := NewBlob(i.(io.Reader))
assert.NoError(err)
return b

View File

@@ -1,4 +1,4 @@
package enc
package types
import (
"encoding/json"
@@ -7,6 +7,18 @@ import (
"github.com/attic-labs/noms/d"
)
var (
typedTag = []byte("t ")
)
func typedEncode(dst io.Writer, v interface{}) {
_, err := dst.Write(typedTag)
d.Exp.NoError(err)
err = json.NewEncoder(dst).Encode(v)
d.Exp.NoError(err)
return
}
func typedDecode(reader io.Reader) []interface{} {
prefix := make([]byte, len(typedTag))
_, err := io.ReadFull(reader, prefix)

View File

@@ -3,7 +3,6 @@ package types
import (
"github.com/attic-labs/noms/chunks"
"github.com/attic-labs/noms/d"
"github.com/attic-labs/noms/enc"
"github.com/attic-labs/noms/ref"
)
@@ -27,7 +26,7 @@ func writeChildValueInternal(v Value, cs chunks.ChunkSink) ref.Ref {
func writeValueInternal(v Value, cs chunks.ChunkSink) ref.Ref {
e := toEncodeable(v, cs)
w := chunks.NewChunkWriter()
enc.Encode(w, e)
encode(w, e)
c := w.Chunk()
if cs != nil {
cs.Put(c)