Factor Blob back out into a method of String, rather than embedded. Embedding makes branching on nom type hard.

This commit is contained in:
Aaron Boodman
2015-06-09 22:56:17 -07:00
parent 5640511f7c
commit 266a96a084
6 changed files with 22 additions and 26 deletions

View File

@@ -3,7 +3,6 @@ package store
import (
"bytes"
"crypto/sha1"
"fmt"
"io"
"io/ioutil"
@@ -45,7 +44,6 @@ func (w *memoryChunkWriter) Ref() (ref.Ref, error) {
if w.ms.data == nil {
w.ms.data = map[ref.Ref][]byte{}
}
fmt.Println(string(w.buf.Bytes()))
w.ms.data[r] = w.buf.Bytes()
w.Close()
return r, nil

View File

@@ -4,6 +4,7 @@ import "io"
type Blob interface {
Value
// TODO: Rename just Len()
ByteLen() uint64
Read() io.Reader
}

View File

@@ -85,7 +85,8 @@ func TestFlatMapIter(t *testing.T) {
stop = true
m.Iter(cb)
assert.Equal(1, len(got))
assert.True(Int32(0).Equals(got["a"]))
// Iteration order not guaranteed, but it has to be one of these.
assert.True(Int32(0).Equals(got["a"]) || Int32(1).Equals(got["b"]))
}
func TestFlatMapEquals(t *testing.T) {

View File

@@ -1,9 +1,22 @@
package types
// Stupid inefficient temporary implementation of the String interface.
type flatString struct {
flatBlob
s string
}
func (fs flatString) Blob() Blob {
return NewBlob([]byte(fs.s))
}
func (fs flatString) String() string {
return string(fs.data)
return fs.s
}
func (fs flatString) Equals(other Value) bool {
if other, ok := other.(String); ok {
return fs.String() == other.String()
} else {
return false
}
}

View File

@@ -12,21 +12,6 @@ func TestNewStringIsFlatString(t *testing.T) {
assert.IsType(s, flatString{})
}
func TestStringFromBytes(t *testing.T) {
assert := assert.New(t)
s := StringFromBytes([]byte("foo"))
assert.IsType(s, flatString{})
assert.Equal("foo", s.String())
}
func TestFlatStringLen(t *testing.T) {
assert := assert.New(t)
s1 := NewString("foo")
s2 := NewString("⌘")
assert.Equal(uint64(3), uint64(s1.ByteLen()))
assert.Equal(uint64(3), uint64(s2.ByteLen()))
}
func TestFlatStringEquals(t *testing.T) {
assert := assert.New(t)
s1 := NewString("foo")

View File

@@ -1,16 +1,14 @@
package types
type String interface {
Blob
Value
Blob() Blob
// Slurps the entire string into memory. You obviously don't want to do this if the string might be large.
String() string
}
func NewString(s string) String {
return flatString{flatBlob{[]byte(s)}}
}
func StringFromBytes(b []byte) String {
return flatString{flatBlob{b}}
return flatString{s}
}