mirror of
https://github.com/dolthub/dolt.git
synced 2026-05-24 02:43:42 -05:00
types.WriteValue -> types.EncodeValue
This commit is contained in:
@@ -59,7 +59,9 @@ func (ds *dataStoreCommon) ReadValue(r ref.Ref) types.Value {
|
||||
}
|
||||
|
||||
func (ds *dataStoreCommon) WriteValue(v types.Value) ref.Ref {
|
||||
return types.WriteValue(v, ds.cs)
|
||||
chunk := types.EncodeValue(v, ds)
|
||||
ds.cs.Put(chunk)
|
||||
return chunk.Ref()
|
||||
}
|
||||
|
||||
// Has should really not be exposed on DataStore :-/
|
||||
|
||||
+3
-1
@@ -82,7 +82,9 @@ func (lds *localDataSink) transitionalChunkSink() chunks.ChunkSink {
|
||||
}
|
||||
|
||||
func (lds *localDataSink) WriteValue(v types.Value) ref.Ref {
|
||||
return types.WriteValue(v, lds.cs)
|
||||
chunk := types.EncodeValue(v, lds)
|
||||
lds.cs.Put(chunk)
|
||||
return chunk.Ref()
|
||||
}
|
||||
|
||||
func (lds *localDataSink) Close() error {
|
||||
|
||||
+21
-22
@@ -7,24 +7,23 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/attic-labs/noms/chunks"
|
||||
"github.com/attic-labs/noms/d"
|
||||
"github.com/attic-labs/noms/ref"
|
||||
)
|
||||
|
||||
func encNomsValue(v Value, cs chunks.ChunkSink) []interface{} {
|
||||
w := newJsonArrayWriter(cs)
|
||||
func encNomsValue(v Value, vw ValueWriter) []interface{} {
|
||||
w := newJsonArrayWriter(vw)
|
||||
w.writeTopLevelValue(v)
|
||||
return w.toArray()
|
||||
}
|
||||
|
||||
type jsonArrayWriter struct {
|
||||
a []interface{}
|
||||
cs chunks.ChunkSink
|
||||
vw ValueWriter
|
||||
}
|
||||
|
||||
func newJsonArrayWriter(cs chunks.ChunkSink) *jsonArrayWriter {
|
||||
return &jsonArrayWriter{cs: cs, a: []interface{}{}}
|
||||
func newJsonArrayWriter(vw ValueWriter) *jsonArrayWriter {
|
||||
return &jsonArrayWriter{vw: vw, a: []interface{}{}}
|
||||
}
|
||||
|
||||
func (w *jsonArrayWriter) write(v interface{}) {
|
||||
@@ -80,8 +79,8 @@ func (w *jsonArrayWriter) writeTypeAsTag(t Type) {
|
||||
w.writeInt(int64(t.Ordinal()))
|
||||
|
||||
pkg := LookupPackage(pkgRef)
|
||||
if pkg != nil {
|
||||
writeChildValueInternal(*pkg, w.cs)
|
||||
if pkg != nil && w.vw != nil {
|
||||
w.vw.WriteValue(*pkg)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -100,12 +99,12 @@ func (w *jsonArrayWriter) maybeWriteMetaSequence(v Value, tr Type, pkg *Package)
|
||||
}
|
||||
|
||||
w.write(true) // a meta sequence
|
||||
w2 := newJsonArrayWriter(w.cs)
|
||||
w2 := newJsonArrayWriter(w.vw)
|
||||
indexType := indexTypeForMetaSequence(tr)
|
||||
for _, tuple := range ms.(metaSequence).data() {
|
||||
if tuple.child != nil && w.cs != nil {
|
||||
if tuple.child != nil && w.vw != nil {
|
||||
// Write unwritten chunked sequences. Chunks are lazily written so that intermediate chunked structures like NewList().Append(x).Append(y) don't cause unnecessary churn.
|
||||
writeValueInternal(tuple.child, w.cs)
|
||||
w.vw.WriteValue(tuple.child)
|
||||
}
|
||||
w2.writeRef(tuple.ChildRef())
|
||||
w2.writeValue(tuple.value, indexType, pkg)
|
||||
@@ -151,7 +150,7 @@ func (w *jsonArrayWriter) writeValue(v Value, tr Type, pkg *Package) {
|
||||
return
|
||||
}
|
||||
|
||||
w2 := newJsonArrayWriter(w.cs)
|
||||
w2 := newJsonArrayWriter(w.vw)
|
||||
elemType := tr.Desc.(CompoundDesc).ElemTypes[0]
|
||||
v.(List).IterAll(func(v Value, i uint64) {
|
||||
w2.writeValue(v, elemType, pkg)
|
||||
@@ -164,7 +163,7 @@ func (w *jsonArrayWriter) writeValue(v Value, tr Type, pkg *Package) {
|
||||
return
|
||||
}
|
||||
|
||||
w2 := newJsonArrayWriter(w.cs)
|
||||
w2 := newJsonArrayWriter(w.vw)
|
||||
elemTypes := tr.Desc.(CompoundDesc).ElemTypes
|
||||
v.(Map).IterAll(func(k, v Value) {
|
||||
w2.writeValue(k, elemTypes[0], pkg)
|
||||
@@ -173,12 +172,12 @@ func (w *jsonArrayWriter) writeValue(v Value, tr Type, pkg *Package) {
|
||||
w.write(w2.toArray())
|
||||
case PackageKind:
|
||||
ptr := MakePrimitiveType(TypeKind)
|
||||
w2 := newJsonArrayWriter(w.cs)
|
||||
w2 := newJsonArrayWriter(w.vw)
|
||||
for _, v := range v.(Package).types {
|
||||
w2.writeValue(v, ptr, pkg)
|
||||
}
|
||||
w.write(w2.toArray())
|
||||
w3 := newJsonArrayWriter(w.cs)
|
||||
w3 := newJsonArrayWriter(w.vw)
|
||||
for _, r := range v.(Package).dependencies {
|
||||
w3.writeRef(r)
|
||||
}
|
||||
@@ -192,7 +191,7 @@ func (w *jsonArrayWriter) writeValue(v Value, tr Type, pkg *Package) {
|
||||
return
|
||||
}
|
||||
|
||||
w2 := newJsonArrayWriter(w.cs)
|
||||
w2 := newJsonArrayWriter(w.vw)
|
||||
elemType := tr.Desc.(CompoundDesc).ElemTypes[0]
|
||||
v.(Set).IterAll(func(v Value) {
|
||||
w2.writeValue(v, elemType, pkg)
|
||||
@@ -221,27 +220,27 @@ func (w *jsonArrayWriter) writeTypeAsValue(v Type) {
|
||||
switch k {
|
||||
case EnumKind:
|
||||
w.write(v.Name())
|
||||
w2 := newJsonArrayWriter(w.cs)
|
||||
w2 := newJsonArrayWriter(w.vw)
|
||||
for _, id := range v.Desc.(EnumDesc).IDs {
|
||||
w2.write(id)
|
||||
}
|
||||
w.write(w2.toArray())
|
||||
case ListKind, MapKind, RefKind, SetKind:
|
||||
w2 := newJsonArrayWriter(w.cs)
|
||||
w2 := newJsonArrayWriter(w.vw)
|
||||
for _, elemType := range v.Desc.(CompoundDesc).ElemTypes {
|
||||
w2.writeTypeAsValue(elemType)
|
||||
}
|
||||
w.write(w2.toArray())
|
||||
case StructKind:
|
||||
w.write(v.Name())
|
||||
fieldWriter := newJsonArrayWriter(w.cs)
|
||||
fieldWriter := newJsonArrayWriter(w.vw)
|
||||
for _, field := range v.Desc.(StructDesc).Fields {
|
||||
fieldWriter.write(field.Name)
|
||||
fieldWriter.writeTypeAsValue(field.T)
|
||||
fieldWriter.write(field.Optional)
|
||||
}
|
||||
w.write(fieldWriter.toArray())
|
||||
choiceWriter := newJsonArrayWriter(w.cs)
|
||||
choiceWriter := newJsonArrayWriter(w.vw)
|
||||
for _, choice := range v.Desc.(StructDesc).Union {
|
||||
choiceWriter.write(choice.Name)
|
||||
choiceWriter.writeTypeAsValue(choice.T)
|
||||
@@ -260,8 +259,8 @@ func (w *jsonArrayWriter) writeTypeAsValue(v Type) {
|
||||
}
|
||||
|
||||
pkg := LookupPackage(pkgRef)
|
||||
if pkg != nil {
|
||||
writeChildValueInternal(*pkg, w.cs)
|
||||
if pkg != nil && w.vw != nil {
|
||||
w.vw.WriteValue(*pkg)
|
||||
}
|
||||
|
||||
default:
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/attic-labs/noms/chunks"
|
||||
"github.com/attic-labs/noms/ref"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -13,8 +12,8 @@ func TestWritePrimitives(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
f := func(k NomsKind, v Value, ex interface{}) {
|
||||
cs := chunks.NewMemoryStore()
|
||||
w := newJsonArrayWriter(cs)
|
||||
|
||||
w := newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(v)
|
||||
assert.EqualValues([]interface{}{k, ex}, w.toArray())
|
||||
}
|
||||
@@ -43,27 +42,24 @@ func TestWritePrimitives(t *testing.T) {
|
||||
|
||||
func TestWriteSimpleBlob(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
w := newJsonArrayWriter(cs)
|
||||
w := newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(NewBlob(bytes.NewBuffer([]byte{0x00, 0x01})))
|
||||
assert.EqualValues([]interface{}{BlobKind, false, "AAE="}, w.toArray())
|
||||
}
|
||||
|
||||
func TestWriteList(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
|
||||
typ := MakeCompoundType(ListKind, MakePrimitiveType(Int32Kind))
|
||||
v := NewTypedList(typ, Int32(0), Int32(1), Int32(2), Int32(3))
|
||||
|
||||
w := newJsonArrayWriter(cs)
|
||||
w := newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(v)
|
||||
assert.EqualValues([]interface{}{ListKind, Int32Kind, false, []interface{}{"0", "1", "2", "3"}}, w.toArray())
|
||||
}
|
||||
|
||||
func TestWriteListOfList(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
|
||||
it := MakeCompoundType(ListKind, MakePrimitiveType(Int16Kind))
|
||||
typ := MakeCompoundType(ListKind, it)
|
||||
@@ -71,19 +67,18 @@ func TestWriteListOfList(t *testing.T) {
|
||||
l2 := NewTypedList(it, Int16(1), Int16(2), Int16(3))
|
||||
v := NewTypedList(typ, l1, l2)
|
||||
|
||||
w := newJsonArrayWriter(cs)
|
||||
w := newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(v)
|
||||
assert.EqualValues([]interface{}{ListKind, ListKind, Int16Kind, false, []interface{}{false, []interface{}{"0"}, false, []interface{}{"1", "2", "3"}}}, w.toArray())
|
||||
}
|
||||
|
||||
func TestWriteSet(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
|
||||
typ := MakeCompoundType(SetKind, MakePrimitiveType(Uint32Kind))
|
||||
v := NewTypedSet(typ, Uint32(3), Uint32(1), Uint32(2), Uint32(0))
|
||||
|
||||
w := newJsonArrayWriter(cs)
|
||||
w := newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(v)
|
||||
// The order of the elements is based on the order defined by OrderedValue.
|
||||
assert.EqualValues([]interface{}{SetKind, Uint32Kind, false, []interface{}{"0", "1", "2", "3"}}, w.toArray())
|
||||
@@ -91,13 +86,12 @@ func TestWriteSet(t *testing.T) {
|
||||
|
||||
func TestWriteSetOfSet(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
|
||||
st := MakeCompoundType(SetKind, MakePrimitiveType(Int32Kind))
|
||||
typ := MakeCompoundType(SetKind, st)
|
||||
v := NewTypedSet(typ, NewTypedSet(st, Int32(0)), NewTypedSet(st, Int32(1), Int32(2), Int32(3)))
|
||||
|
||||
w := newJsonArrayWriter(cs)
|
||||
w := newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(v)
|
||||
// The order of the elements is based on the order defined by OrderedValue.
|
||||
assert.EqualValues([]interface{}{SetKind, SetKind, Int32Kind, false, []interface{}{false, []interface{}{"1", "2", "3"}, false, []interface{}{"0"}}}, w.toArray())
|
||||
@@ -105,12 +99,11 @@ func TestWriteSetOfSet(t *testing.T) {
|
||||
|
||||
func TestWriteMap(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
|
||||
typ := MakeCompoundType(MapKind, MakePrimitiveType(StringKind), MakePrimitiveType(BoolKind))
|
||||
v := newMapLeaf(typ, mapEntry{NewString("a"), Bool(false)}, mapEntry{NewString("b"), Bool(true)})
|
||||
|
||||
w := newJsonArrayWriter(cs)
|
||||
w := newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(v)
|
||||
// The order of the elements is based on the order defined by OrderedValue.
|
||||
assert.EqualValues([]interface{}{MapKind, StringKind, BoolKind, false, []interface{}{"a", false, "b", true}}, w.toArray())
|
||||
@@ -118,14 +111,13 @@ func TestWriteMap(t *testing.T) {
|
||||
|
||||
func TestWriteMapOfMap(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
|
||||
kt := MakeCompoundType(MapKind, MakePrimitiveType(StringKind), MakePrimitiveType(Int64Kind))
|
||||
vt := MakeCompoundType(SetKind, MakePrimitiveType(BoolKind))
|
||||
typ := MakeCompoundType(MapKind, kt, vt)
|
||||
v := NewTypedMap(typ, NewTypedMap(kt, NewString("a"), Int64(0)), NewTypedSet(vt, Bool(true)))
|
||||
|
||||
w := newJsonArrayWriter(cs)
|
||||
w := newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(v)
|
||||
// the order of the elements is based on the ref of the value.
|
||||
assert.EqualValues([]interface{}{MapKind, MapKind, StringKind, Int64Kind, SetKind, BoolKind, false, []interface{}{false, []interface{}{"a", "0"}, false, []interface{}{true}}}, w.toArray())
|
||||
@@ -133,7 +125,6 @@ func TestWriteMapOfMap(t *testing.T) {
|
||||
|
||||
func TestWriteCompoundBlob(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
|
||||
r1 := ref.Parse("sha1-0000000000000000000000000000000000000001")
|
||||
r2 := ref.Parse("sha1-0000000000000000000000000000000000000002")
|
||||
@@ -143,8 +134,8 @@ func TestWriteCompoundBlob(t *testing.T) {
|
||||
newMetaTuple(Uint64(20), nil, r1),
|
||||
newMetaTuple(Uint64(40), nil, r2),
|
||||
newMetaTuple(Uint64(60), nil, r3),
|
||||
}, newValueStore(cs))
|
||||
w := newJsonArrayWriter(cs)
|
||||
}, NewTestValueStore())
|
||||
w := newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(v)
|
||||
|
||||
// the order of the elements is based on the ref of the value.
|
||||
@@ -153,7 +144,6 @@ func TestWriteCompoundBlob(t *testing.T) {
|
||||
|
||||
func TestWriteEmptyStruct(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
|
||||
typeDef := MakeStructType("S", []Field{}, Choices{})
|
||||
pkg := NewPackage([]Type{typeDef}, []ref.Ref{})
|
||||
@@ -161,14 +151,13 @@ func TestWriteEmptyStruct(t *testing.T) {
|
||||
typ := MakeType(pkgRef, 0)
|
||||
v := NewStruct(typ, typeDef, nil)
|
||||
|
||||
w := newJsonArrayWriter(cs)
|
||||
w := newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(v)
|
||||
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), "0"}, w.toArray())
|
||||
}
|
||||
|
||||
func TestWriteStruct(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
|
||||
typeDef := MakeStructType("S", []Field{
|
||||
Field{"x", MakePrimitiveType(Int8Kind), false},
|
||||
@@ -179,14 +168,13 @@ func TestWriteStruct(t *testing.T) {
|
||||
typ := MakeType(pkgRef, 0)
|
||||
v := NewStruct(typ, typeDef, structData{"x": Int8(42), "b": Bool(true)})
|
||||
|
||||
w := newJsonArrayWriter(cs)
|
||||
w := newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(v)
|
||||
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), "0", "42", true}, w.toArray())
|
||||
}
|
||||
|
||||
func TestWriteStructOptionalField(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
|
||||
typeDef := MakeStructType("S", []Field{
|
||||
Field{"x", MakePrimitiveType(Int8Kind), true},
|
||||
@@ -197,20 +185,19 @@ func TestWriteStructOptionalField(t *testing.T) {
|
||||
typ := MakeType(pkgRef, 0)
|
||||
v := NewStruct(typ, typeDef, structData{"x": Int8(42), "b": Bool(true)})
|
||||
|
||||
w := newJsonArrayWriter(cs)
|
||||
w := newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(v)
|
||||
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), "0", true, "42", true}, w.toArray())
|
||||
|
||||
v = NewStruct(typ, typeDef, structData{"b": Bool(true)})
|
||||
|
||||
w = newJsonArrayWriter(cs)
|
||||
w = newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(v)
|
||||
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), "0", false, true}, w.toArray())
|
||||
}
|
||||
|
||||
func TestWriteStructWithUnion(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
|
||||
typeDef := MakeStructType("S", []Field{
|
||||
Field{"x", MakePrimitiveType(Int8Kind), false},
|
||||
@@ -223,20 +210,19 @@ func TestWriteStructWithUnion(t *testing.T) {
|
||||
typ := MakeType(pkgRef, 0)
|
||||
v := NewStruct(typ, typeDef, structData{"x": Int8(42), "s": NewString("hi")})
|
||||
|
||||
w := newJsonArrayWriter(cs)
|
||||
w := newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(v)
|
||||
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), "0", "42", "1", "hi"}, w.toArray())
|
||||
|
||||
v = NewStruct(typ, typeDef, structData{"x": Int8(42), "b": Bool(true)})
|
||||
|
||||
w = newJsonArrayWriter(cs)
|
||||
w = newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(v)
|
||||
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), "0", "42", "0", true}, w.toArray())
|
||||
}
|
||||
|
||||
func TestWriteStructWithList(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
|
||||
typeDef := MakeStructType("S", []Field{
|
||||
Field{"l", MakeCompoundType(ListKind, MakePrimitiveType(StringKind)), false},
|
||||
@@ -246,19 +232,18 @@ func TestWriteStructWithList(t *testing.T) {
|
||||
typ := MakeType(pkgRef, 0)
|
||||
|
||||
v := NewStruct(typ, typeDef, structData{"l": NewList(NewString("a"), NewString("b"))})
|
||||
w := newJsonArrayWriter(cs)
|
||||
w := newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(v)
|
||||
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), "0", false, []interface{}{"a", "b"}}, w.toArray())
|
||||
|
||||
v = NewStruct(typ, typeDef, structData{"l": NewList()})
|
||||
w = newJsonArrayWriter(cs)
|
||||
w = newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(v)
|
||||
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), "0", false, []interface{}{}}, w.toArray())
|
||||
}
|
||||
|
||||
func TestWriteStructWithStruct(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
|
||||
s2TypeDef := MakeStructType("S2", []Field{
|
||||
Field{"x", MakePrimitiveType(Int32Kind), false},
|
||||
@@ -272,14 +257,13 @@ func TestWriteStructWithStruct(t *testing.T) {
|
||||
sType := MakeType(pkgRef, 1)
|
||||
|
||||
v := NewStruct(sType, sTypeDef, structData{"s": NewStruct(s2Type, s2TypeDef, structData{"x": Int32(42)})})
|
||||
w := newJsonArrayWriter(cs)
|
||||
w := newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(v)
|
||||
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), "1", "42"}, w.toArray())
|
||||
}
|
||||
|
||||
func TestWriteStructWithBlob(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
|
||||
typeDef := MakeStructType("S", []Field{
|
||||
Field{"b", MakePrimitiveType(BlobKind), false},
|
||||
@@ -290,28 +274,26 @@ func TestWriteStructWithBlob(t *testing.T) {
|
||||
b := NewBlob(bytes.NewBuffer([]byte{0x00, 0x01}))
|
||||
v := NewStruct(typ, typeDef, structData{"b": b})
|
||||
|
||||
w := newJsonArrayWriter(cs)
|
||||
w := newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(v)
|
||||
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), "0", false, "AAE="}, w.toArray())
|
||||
}
|
||||
|
||||
func TestWriteEnum(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
|
||||
pkg := NewPackage([]Type{
|
||||
MakeEnumType("E", "a", "b", "c")}, []ref.Ref{})
|
||||
pkgRef := RegisterPackage(&pkg)
|
||||
typ := MakeType(pkgRef, 0)
|
||||
|
||||
w := newJsonArrayWriter(cs)
|
||||
w := newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(Enum{1, typ})
|
||||
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), "0", "1"}, w.toArray())
|
||||
}
|
||||
|
||||
func TestWriteListOfEnum(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
|
||||
pkg := NewPackage([]Type{
|
||||
MakeEnumType("E", "a", "b", "c")}, []ref.Ref{})
|
||||
@@ -320,14 +302,13 @@ func TestWriteListOfEnum(t *testing.T) {
|
||||
typ := MakeCompoundType(ListKind, et)
|
||||
v := NewTypedList(typ, Enum{0, et}, Enum{1, et}, Enum{2, et})
|
||||
|
||||
w := newJsonArrayWriter(cs)
|
||||
w := newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(v)
|
||||
assert.EqualValues([]interface{}{ListKind, UnresolvedKind, pkgRef.String(), "0", false, []interface{}{"0", "1", "2"}}, w.toArray())
|
||||
}
|
||||
|
||||
func TestWriteCompoundList(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
|
||||
ltr := MakeCompoundType(ListKind, MakePrimitiveType(Int32Kind))
|
||||
leaf1 := newListLeaf(ltr, Int32(0))
|
||||
@@ -335,16 +316,15 @@ func TestWriteCompoundList(t *testing.T) {
|
||||
cl := buildCompoundList([]metaTuple{
|
||||
newMetaTuple(Uint64(1), leaf1, ref.Ref{}),
|
||||
newMetaTuple(Uint64(4), leaf2, ref.Ref{}),
|
||||
}, ltr, newValueStore(cs))
|
||||
}, ltr, NewTestValueStore())
|
||||
|
||||
w := newJsonArrayWriter(cs)
|
||||
w := newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(cl)
|
||||
assert.EqualValues([]interface{}{ListKind, Int32Kind, true, []interface{}{leaf1.Ref().String(), "1", leaf2.Ref().String(), "4"}}, w.toArray())
|
||||
}
|
||||
|
||||
func TestWriteListOfValue(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
|
||||
typ := MakeCompoundType(ListKind, MakePrimitiveType(ValueKind))
|
||||
blob := NewBlob(bytes.NewBuffer([]byte{0x01}))
|
||||
@@ -364,7 +344,7 @@ func TestWriteListOfValue(t *testing.T) {
|
||||
blob,
|
||||
)
|
||||
|
||||
w := newJsonArrayWriter(cs)
|
||||
w := newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(v)
|
||||
|
||||
assert.EqualValues([]interface{}{ListKind, ValueKind, false, []interface{}{
|
||||
@@ -386,7 +366,6 @@ func TestWriteListOfValue(t *testing.T) {
|
||||
|
||||
func TestWriteListOfValueWithStruct(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
|
||||
typeDef := MakeStructType("S", []Field{
|
||||
Field{"x", MakePrimitiveType(Int32Kind), false},
|
||||
@@ -397,14 +376,13 @@ func TestWriteListOfValueWithStruct(t *testing.T) {
|
||||
structType := MakeType(pkgRef, 0)
|
||||
v := NewTypedList(listType, NewStruct(structType, typeDef, structData{"x": Int32(42)}))
|
||||
|
||||
w := newJsonArrayWriter(cs)
|
||||
w := newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(v)
|
||||
assert.EqualValues([]interface{}{ListKind, ValueKind, false, []interface{}{UnresolvedKind, pkgRef.String(), "0", "42"}}, w.toArray())
|
||||
}
|
||||
|
||||
func TestWriteListOfValueWithType(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
|
||||
pkg := NewPackage([]Type{
|
||||
MakeStructType("S", []Field{
|
||||
@@ -420,7 +398,7 @@ func TestWriteListOfValueWithType(t *testing.T) {
|
||||
MakeType(pkgRef, 0),
|
||||
)
|
||||
|
||||
w := newJsonArrayWriter(cs)
|
||||
w := newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(v)
|
||||
assert.EqualValues([]interface{}{ListKind, ValueKind, false, []interface{}{
|
||||
BoolKind, true,
|
||||
@@ -445,23 +423,21 @@ func (r testRef) TargetRef() ref.Ref {
|
||||
|
||||
func TestWriteRef(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
|
||||
typ := MakeCompoundType(RefKind, MakePrimitiveType(Uint32Kind))
|
||||
r := ref.Parse("sha1-0123456789abcdef0123456789abcdef01234567")
|
||||
v := NewRef(r)
|
||||
|
||||
w := newJsonArrayWriter(cs)
|
||||
w := newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(testRef{Value: v, t: typ})
|
||||
assert.EqualValues([]interface{}{RefKind, Uint32Kind, r.String()}, w.toArray())
|
||||
}
|
||||
|
||||
func TestWriteTypeValue(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
|
||||
test := func(expected []interface{}, v Type) {
|
||||
w := newJsonArrayWriter(cs)
|
||||
w := newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(v)
|
||||
assert.EqualValues(expected, w.toArray())
|
||||
}
|
||||
@@ -502,18 +478,16 @@ func TestWriteTypeValue(t *testing.T) {
|
||||
|
||||
func TestWriteListOfTypes(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
|
||||
typ := MakeCompoundType(ListKind, MakePrimitiveType(TypeKind))
|
||||
v := NewTypedList(typ, MakePrimitiveType(BoolKind), MakeEnumType("E", "a", "b", "c"), MakePrimitiveType(StringKind))
|
||||
|
||||
w := newJsonArrayWriter(cs)
|
||||
w := newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(v)
|
||||
assert.EqualValues([]interface{}{ListKind, TypeKind, false, []interface{}{BoolKind, EnumKind, "E", []interface{}{"a", "b", "c"}, StringKind}}, w.toArray())
|
||||
}
|
||||
|
||||
func TestWritePackage(t *testing.T) {
|
||||
cs := chunks.NewMemoryStore()
|
||||
pkg := NewPackage([]Type{
|
||||
MakeStructType("EnumStruct",
|
||||
[]Field{
|
||||
@@ -524,7 +498,7 @@ func TestWritePackage(t *testing.T) {
|
||||
MakeEnumType("Handedness", "right", "left", "switch"),
|
||||
}, []ref.Ref{})
|
||||
|
||||
w := newJsonArrayWriter(cs)
|
||||
w := newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(pkg)
|
||||
|
||||
// struct Package {
|
||||
@@ -548,13 +522,12 @@ func TestWritePackage(t *testing.T) {
|
||||
|
||||
func TestWritePackage2(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
|
||||
setTref := MakeCompoundType(SetKind, MakePrimitiveType(Uint32Kind))
|
||||
r := ref.Parse("sha1-0123456789abcdef0123456789abcdef01234567")
|
||||
v := Package{[]Type{setTref}, []ref.Ref{r}, &ref.Ref{}}
|
||||
|
||||
w := newJsonArrayWriter(cs)
|
||||
w := newJsonArrayWriter(NewTestValueStore())
|
||||
w.writeTopLevelValue(v)
|
||||
assert.EqualValues([]interface{}{PackageKind, []interface{}{SetKind, []interface{}{Uint32Kind}}, []interface{}{r.String()}}, w.toArray())
|
||||
}
|
||||
|
||||
+3
-3
@@ -10,7 +10,7 @@ import (
|
||||
|
||||
func TestGenericEnumWriteRead(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
vs := newValueStore(chunks.NewMemoryStore())
|
||||
|
||||
typeDefA := MakeEnumType("EA", "aA", "bA")
|
||||
typeDefB := MakeEnumType("EB", "aB", "bB")
|
||||
@@ -24,8 +24,8 @@ func TestGenericEnumWriteRead(t *testing.T) {
|
||||
|
||||
assert.False(vA.Equals(vB))
|
||||
|
||||
rA := WriteValue(vA, cs)
|
||||
vA2 := newValueStore(cs).ReadValue(rA)
|
||||
rA := vs.WriteValue(vA)
|
||||
vA2 := vs.ReadValue(rA)
|
||||
|
||||
assert.True(vA.Equals(vA2))
|
||||
assert.True(vA2.Equals(vA))
|
||||
|
||||
+1
-1
@@ -12,7 +12,7 @@ func getRef(v Value) ref.Ref {
|
||||
}
|
||||
|
||||
func getRefNoOverride(v Value) ref.Ref {
|
||||
return writeValueInternal(v, nil)
|
||||
return EncodeValue(v, nil).Ref()
|
||||
}
|
||||
|
||||
func EnsureRef(r *ref.Ref, v Value) ref.Ref {
|
||||
|
||||
@@ -20,10 +20,11 @@ func NewTestValueStore() *ValueStore {
|
||||
|
||||
// ReadValue reads and decodes a value from vrw. It is not considered an error for the requested chunk to be empty; in this case, the function simply returns nil.
|
||||
func (vrw *ValueStore) ReadValue(r ref.Ref) Value {
|
||||
c := vrw.cs.Get(r)
|
||||
return DecodeChunk(c, vrw)
|
||||
return DecodeChunk(vrw.cs.Get(r), vrw)
|
||||
}
|
||||
|
||||
func (vrw *ValueStore) WriteValue(v Value) ref.Ref {
|
||||
return WriteValue(v, vrw.cs)
|
||||
chunk := EncodeValue(v, vrw)
|
||||
vrw.cs.Put(chunk)
|
||||
return chunk.Ref()
|
||||
}
|
||||
|
||||
+14
-28
@@ -2,7 +2,6 @@ package types
|
||||
|
||||
import (
|
||||
"github.com/attic-labs/noms/chunks"
|
||||
"github.com/attic-labs/noms/d"
|
||||
"github.com/attic-labs/noms/ref"
|
||||
)
|
||||
|
||||
@@ -15,46 +14,33 @@ type primitive interface {
|
||||
ToPrimitive() interface{}
|
||||
}
|
||||
|
||||
// WriteValue takes a Value, encodes it into Chunks, and puts them into cs. As a part of BUG 654, we're trying to get rid of the need to provide a ChunkSink here.
|
||||
func WriteValue(v Value, cs chunks.ChunkSink) ref.Ref {
|
||||
d.Chk.NotNil(cs)
|
||||
return writeValueInternal(v, cs)
|
||||
}
|
||||
|
||||
func writeChildValueInternal(v Value, cs chunks.ChunkSink) ref.Ref {
|
||||
if cs == nil {
|
||||
return v.Ref()
|
||||
}
|
||||
|
||||
return writeValueInternal(v, cs)
|
||||
}
|
||||
|
||||
func writeValueInternal(v Value, cs chunks.ChunkSink) ref.Ref {
|
||||
e := toEncodeable(v, cs)
|
||||
// WriteValue takes a Value and encodes it to a Chunk, if |vw| is non-nill, it will vw.WriteValue reachable unwritten sub-chunks.
|
||||
func EncodeValue(v Value, vw ValueWriter) chunks.Chunk {
|
||||
e := toEncodeable(v, vw)
|
||||
w := chunks.NewChunkWriter()
|
||||
encode(w, e)
|
||||
c := w.Chunk()
|
||||
if cs != nil {
|
||||
cs.Put(c)
|
||||
}
|
||||
return c.Ref()
|
||||
return w.Chunk()
|
||||
}
|
||||
|
||||
func toEncodeable(v Value, cs chunks.ChunkSink) interface{} {
|
||||
func toEncodeable(v Value, vw ValueWriter) interface{} {
|
||||
switch v := v.(type) {
|
||||
case blobLeaf:
|
||||
return v.Reader()
|
||||
case Package:
|
||||
processPackageChildren(v, cs)
|
||||
processPackageChildren(v, vw)
|
||||
}
|
||||
return encNomsValue(v, cs)
|
||||
return encNomsValue(v, vw)
|
||||
}
|
||||
|
||||
func processPackageChildren(p Package, cs chunks.ChunkSink) {
|
||||
func processPackageChildren(p Package, vw ValueWriter) {
|
||||
if vw == nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, r := range p.dependencies {
|
||||
p := LookupPackage(r)
|
||||
if p != nil {
|
||||
writeChildValueInternal(*p, cs)
|
||||
if p != nil && vw != nil {
|
||||
vw.WriteValue(*p)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user