Make sure we write packages to the store

When we write a value that has a TypeRef with a package we need to
write that package.

When we write a package that has dependencies we need to write the
dependent packages too.
This commit is contained in:
Erik Arvidsson
2015-10-26 18:40:14 -04:00
parent 11799dc957
commit cb233b0f68
5 changed files with 146 additions and 111 deletions
+2
View File
@@ -239,6 +239,8 @@ func (r *jsonArrayReader) readUnresolvedKindToValue(t TypeRef, pkg *Package) Val
pkg = pkg2
}
d.Chk.NotNil(pkg, "Woah, got a nil pkg. pkgRef: %s, ordinal: %d\n", pkgRef, ordinal)
typeDef := pkg.types[ordinal]
if typeDef.Kind() == EnumKind {
+31 -16
View File
@@ -24,23 +24,26 @@ func (tv typedValue) TypedValue() []interface{} {
}
func encNomsValue(v Value, cs chunks.ChunkSink) typedValue {
w := newJsonArrayWriter()
w := newJsonArrayWriter(cs)
w.writeTopLevelValue(v)
return typedValue{w.toArray()}
}
type jsonArrayWriter []interface{}
type jsonArrayWriter struct {
a []interface{}
cs chunks.ChunkSink
}
func newJsonArrayWriter() *jsonArrayWriter {
return &jsonArrayWriter{}
func newJsonArrayWriter(cs chunks.ChunkSink) *jsonArrayWriter {
return &jsonArrayWriter{cs: cs, a: []interface{}{}}
}
func (w *jsonArrayWriter) write(v interface{}) {
*w = append(*w, v)
w.a = append(w.a, v)
}
func (w *jsonArrayWriter) toArray() []interface{} {
return *w
return w.a
}
func (w *jsonArrayWriter) writeRef(r ref.Ref) {
@@ -62,6 +65,11 @@ func (w *jsonArrayWriter) writeTypeRefAsTag(t TypeRef) {
d.Chk.NotEqual(ref.Ref{}, pkgRef)
w.writeRef(pkgRef)
w.write(t.Ordinal())
pkg := LookupPackage(pkgRef)
if pkg != nil {
writeChildValueInternal(*pkg, w.cs)
}
}
}
@@ -78,14 +86,14 @@ func (w *jsonArrayWriter) writeValue(v Value, tr TypeRef, pkg *Package) {
case BoolKind, Float32Kind, Float64Kind, Int16Kind, Int32Kind, Int64Kind, Int8Kind, UInt16Kind, UInt32Kind, UInt64Kind, UInt8Kind:
w.write(v.(primitive).ToPrimitive())
case ListKind:
w2 := newJsonArrayWriter()
w2 := newJsonArrayWriter(w.cs)
elemType := tr.Desc.(CompoundDesc).ElemTypes[0]
getListFromListKind(v).IterAll(func(v Value, i uint64) {
w2.writeValue(v, elemType, pkg)
})
w.write(w2.toArray())
case MapKind:
w2 := newJsonArrayWriter()
w2 := newJsonArrayWriter(w.cs)
elemTypes := tr.Desc.(CompoundDesc).ElemTypes
getMapFromMapKind(v).IterAll(func(k, v Value) {
w2.writeValue(k, elemTypes[0], pkg)
@@ -94,12 +102,12 @@ func (w *jsonArrayWriter) writeValue(v Value, tr TypeRef, pkg *Package) {
w.write(w2.toArray())
case PackageKind:
ptr := MakePrimitiveTypeRef(TypeRefKind)
w2 := newJsonArrayWriter()
w2 := newJsonArrayWriter(w.cs)
for _, v := range v.(Package).types {
w2.writeValue(v, ptr, pkg)
}
w.write(w2.toArray())
w3 := newJsonArrayWriter()
w3 := newJsonArrayWriter(w.cs)
for _, r := range v.(Package).dependencies {
w3.writeRef(r)
}
@@ -107,7 +115,7 @@ func (w *jsonArrayWriter) writeValue(v Value, tr TypeRef, pkg *Package) {
case RefKind:
w.writeRef(getRefFromRefKind(v))
case SetKind:
w2 := newJsonArrayWriter()
w2 := newJsonArrayWriter(w.cs)
elemType := tr.Desc.(CompoundDesc).ElemTypes[0]
getSetFromSetKind(v).IterAll(func(v Value) {
w2.writeValue(v, elemType, pkg)
@@ -190,27 +198,27 @@ func (w *jsonArrayWriter) writeTypeRefAsValue(v TypeRef) {
switch k {
case EnumKind:
w.write(v.Name())
w2 := newJsonArrayWriter()
w2 := newJsonArrayWriter(w.cs)
for _, id := range v.Desc.(EnumDesc).IDs {
w2.write(id)
}
w.write(w2.toArray())
case ListKind, MapKind, RefKind, SetKind:
w2 := newJsonArrayWriter()
w2 := newJsonArrayWriter(w.cs)
for _, elemType := range v.Desc.(CompoundDesc).ElemTypes {
w2.writeTypeRefAsValue(elemType)
}
w.write(w2.toArray())
case StructKind:
w.write(v.Name())
fieldWriter := newJsonArrayWriter()
fieldWriter := newJsonArrayWriter(w.cs)
for _, field := range v.Desc.(StructDesc).Fields {
fieldWriter.write(field.Name)
fieldWriter.writeTypeRefAsValue(field.T)
fieldWriter.write(field.Optional)
}
w.write(fieldWriter.toArray())
choiceWriter := newJsonArrayWriter()
choiceWriter := newJsonArrayWriter(w.cs)
for _, choice := range v.Desc.(StructDesc).Union {
choiceWriter.write(choice.Name)
choiceWriter.writeTypeRefAsValue(choice.T)
@@ -218,7 +226,8 @@ func (w *jsonArrayWriter) writeTypeRefAsValue(v TypeRef) {
}
w.write(choiceWriter.toArray())
case UnresolvedKind:
w.writeRef(v.PackageRef())
pkgRef := v.PackageRef()
w.writeRef(pkgRef)
// Don't use Ordinal() here since we might need to serialize a TypeRef that hasn't gotten a valid ordinal yet.
ordinal := v.Desc.(UnresolvedDesc).ordinal
w.write(ordinal)
@@ -226,6 +235,12 @@ func (w *jsonArrayWriter) writeTypeRefAsValue(v TypeRef) {
w.write(v.Namespace())
w.write(v.Name())
}
pkg := LookupPackage(pkgRef)
if pkg != nil {
writeChildValueInternal(*pkg, w.cs)
}
default:
d.Chk.True(IsPrimitiveKind(k), v.Describe())
}
+78 -53
View File
@@ -5,6 +5,7 @@ import (
"testing"
"github.com/attic-labs/noms/Godeps/_workspace/src/github.com/stretchr/testify/assert"
"github.com/attic-labs/noms/chunks"
"github.com/attic-labs/noms/ref"
)
@@ -12,9 +13,10 @@ func TestWritePrimitives(t *testing.T) {
assert := assert.New(t)
f := func(k NomsKind, v Value, ex interface{}) {
w := newJsonArrayWriter()
cs := chunks.NewMemoryStore()
w := newJsonArrayWriter(cs)
w.writeTopLevelValue(v)
assert.EqualValues([]interface{}{k, ex}, *w)
assert.EqualValues([]interface{}{k, ex}, w.toArray())
}
f(BoolKind, Bool(true), true)
@@ -53,26 +55,28 @@ func (l testList) InternalImplementation() List {
func TestWriteList(t *testing.T) {
assert := assert.New(t)
cs := chunks.NewMemoryStore()
tref := MakeCompoundTypeRef(ListKind, MakePrimitiveTypeRef(Int32Kind))
v := NewList(Int32(0), Int32(1), Int32(2), Int32(3))
w := newJsonArrayWriter()
w := newJsonArrayWriter(cs)
w.writeTopLevelValue(testList{List: v, t: tref})
assert.EqualValues([]interface{}{ListKind, Int32Kind, []interface{}{int32(0), int32(1), int32(2), int32(3)}}, *w)
assert.EqualValues([]interface{}{ListKind, Int32Kind, []interface{}{int32(0), int32(1), int32(2), int32(3)}}, w.toArray())
}
func TestWriteListOfList(t *testing.T) {
assert := assert.New(t)
cs := chunks.NewMemoryStore()
it := MakeCompoundTypeRef(ListKind, MakePrimitiveTypeRef(Int16Kind))
tref := MakeCompoundTypeRef(ListKind, it)
v := NewList(NewList(Int16(0)), NewList(Int16(1), Int16(2), Int16(3)))
w := newJsonArrayWriter()
w := newJsonArrayWriter(cs)
w.writeTopLevelValue(testList{List: v, t: tref})
assert.EqualValues([]interface{}{ListKind, ListKind, Int16Kind,
[]interface{}{[]interface{}{int16(0)}, []interface{}{int16(1), int16(2), int16(3)}}}, *w)
[]interface{}{[]interface{}{int16(0)}, []interface{}{int16(1), int16(2), int16(3)}}}, w.toArray())
}
type testSet struct {
@@ -90,27 +94,29 @@ func (s testSet) InternalImplementation() Set {
func TestWriteSet(t *testing.T) {
assert := assert.New(t)
cs := chunks.NewMemoryStore()
tref := MakeCompoundTypeRef(SetKind, MakePrimitiveTypeRef(UInt32Kind))
v := NewSet(UInt32(3), UInt32(1), UInt32(2), UInt32(0))
w := newJsonArrayWriter()
w := newJsonArrayWriter(cs)
w.writeTopLevelValue(testSet{Set: v, t: tref})
// the order of the elements is based on the ref of the value.
assert.EqualValues([]interface{}{SetKind, UInt32Kind, []interface{}{uint32(3), uint32(1), uint32(2), uint32(0)}}, *w)
assert.EqualValues([]interface{}{SetKind, UInt32Kind, []interface{}{uint32(3), uint32(1), uint32(2), uint32(0)}}, w.toArray())
}
func TestWriteSetOfSet(t *testing.T) {
assert := assert.New(t)
cs := chunks.NewMemoryStore()
st := MakeCompoundTypeRef(SetKind, MakePrimitiveTypeRef(Int32Kind))
tref := MakeCompoundTypeRef(SetKind, st)
v := NewSet(NewSet(Int32(0)), NewSet(Int32(1), Int32(2), Int32(3)))
w := newJsonArrayWriter()
w := newJsonArrayWriter(cs)
w.writeTopLevelValue(testSet{Set: v, t: tref})
// the order of the elements is based on the ref of the value.
assert.EqualValues([]interface{}{SetKind, SetKind, Int32Kind, []interface{}{[]interface{}{int32(1), int32(2), int32(3)}, []interface{}{int32(0)}}}, *w)
assert.EqualValues([]interface{}{SetKind, SetKind, Int32Kind, []interface{}{[]interface{}{int32(1), int32(2), int32(3)}, []interface{}{int32(0)}}}, w.toArray())
}
type testMap struct {
@@ -128,32 +134,35 @@ func (m testMap) InternalImplementation() Map {
func TestWriteMap(t *testing.T) {
assert := assert.New(t)
cs := chunks.NewMemoryStore()
tref := MakeCompoundTypeRef(MapKind, MakePrimitiveTypeRef(StringKind), MakePrimitiveTypeRef(BoolKind))
v := NewMap(NewString("a"), Bool(false), NewString("b"), Bool(true))
w := newJsonArrayWriter()
w := newJsonArrayWriter(cs)
w.writeTopLevelValue(testMap{Map: v, t: tref})
// the order of the elements is based on the ref of the value.
assert.EqualValues([]interface{}{MapKind, StringKind, BoolKind, []interface{}{"a", false, "b", true}}, *w)
assert.EqualValues([]interface{}{MapKind, StringKind, BoolKind, []interface{}{"a", false, "b", true}}, w.toArray())
}
func TestWriteMapOfMap(t *testing.T) {
assert := assert.New(t)
cs := chunks.NewMemoryStore()
kt := MakeCompoundTypeRef(MapKind, MakePrimitiveTypeRef(StringKind), MakePrimitiveTypeRef(Int64Kind))
vt := MakeCompoundTypeRef(SetKind, MakePrimitiveTypeRef(BoolKind))
tref := MakeCompoundTypeRef(MapKind, kt, vt)
v := NewMap(NewMap(NewString("a"), Int64(0)), NewSet(Bool(true)))
w := newJsonArrayWriter()
w := newJsonArrayWriter(cs)
w.writeTopLevelValue(testMap{Map: v, t: tref})
// the order of the elements is based on the ref of the value.
assert.EqualValues([]interface{}{MapKind, MapKind, StringKind, Int64Kind, SetKind, BoolKind, []interface{}{[]interface{}{"a", int64(0)}, []interface{}{true}}}, *w)
assert.EqualValues([]interface{}{MapKind, MapKind, StringKind, Int64Kind, SetKind, BoolKind, []interface{}{[]interface{}{"a", int64(0)}, []interface{}{true}}}, w.toArray())
}
func TestWriteEmptyStruct(t *testing.T) {
assert := assert.New(t)
cs := chunks.NewMemoryStore()
pkg := NewPackage([]TypeRef{
MakeStructTypeRef("S", []Field{}, Choices{})}, []ref.Ref{})
@@ -161,13 +170,14 @@ func TestWriteEmptyStruct(t *testing.T) {
tref := MakeTypeRef(pkgRef, 0)
v := NewMap()
w := newJsonArrayWriter()
w := newJsonArrayWriter(cs)
w.writeTopLevelValue(testMap{Map: v, t: tref})
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), int16(0)}, *w)
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), int16(0)}, w.toArray())
}
func TestWriteStruct(t *testing.T) {
assert := assert.New(t)
cs := chunks.NewMemoryStore()
pkg := NewPackage([]TypeRef{
MakeStructTypeRef("S", []Field{
@@ -178,13 +188,14 @@ func TestWriteStruct(t *testing.T) {
tref := MakeTypeRef(pkgRef, 0)
v := NewMap(NewString("x"), Int8(42), NewString("b"), Bool(true))
w := newJsonArrayWriter()
w := newJsonArrayWriter(cs)
w.writeTopLevelValue(testMap{Map: v, t: tref})
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), int16(0), int8(42), true}, *w)
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), int16(0), int8(42), true}, w.toArray())
}
func TestWriteStructOptionalField(t *testing.T) {
assert := assert.New(t)
cs := chunks.NewMemoryStore()
pkg := NewPackage([]TypeRef{
MakeStructTypeRef("S", []Field{
@@ -195,19 +206,20 @@ func TestWriteStructOptionalField(t *testing.T) {
tref := MakeTypeRef(pkgRef, 0)
v := NewMap(NewString("x"), Int8(42), NewString("b"), Bool(true))
w := newJsonArrayWriter()
w := newJsonArrayWriter(cs)
w.writeTopLevelValue(testMap{Map: v, t: tref})
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), int16(0), true, int8(42), true}, *w)
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), int16(0), true, int8(42), true}, w.toArray())
v = NewMap(NewString("b"), Bool(true))
w = newJsonArrayWriter()
w = newJsonArrayWriter(cs)
w.writeTopLevelValue(testMap{Map: v, t: tref})
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), int16(0), false, true}, *w)
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), int16(0), false, true}, w.toArray())
}
func TestWriteStructWithUnion(t *testing.T) {
assert := assert.New(t)
cs := chunks.NewMemoryStore()
pkg := NewPackage([]TypeRef{
MakeStructTypeRef("S", []Field{
@@ -220,19 +232,20 @@ func TestWriteStructWithUnion(t *testing.T) {
tref := MakeTypeRef(pkgRef, 0)
v := NewMap(NewString("x"), Int8(42), NewString("$unionIndex"), UInt32(1), NewString("$unionValue"), NewString("hi"))
w := newJsonArrayWriter()
w := newJsonArrayWriter(cs)
w.writeTopLevelValue(testMap{Map: v, t: tref})
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), int16(0), int8(42), uint32(1), "hi"}, *w)
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), int16(0), int8(42), uint32(1), "hi"}, w.toArray())
v = NewMap(NewString("x"), Int8(42), NewString("$unionIndex"), UInt32(0), NewString("$unionValue"), Bool(true))
w = newJsonArrayWriter()
w = newJsonArrayWriter(cs)
w.writeTopLevelValue(testMap{Map: v, t: tref})
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), int16(0), int8(42), uint32(0), true}, *w)
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), int16(0), int8(42), uint32(0), true}, w.toArray())
}
func TestWriteStructWithList(t *testing.T) {
assert := assert.New(t)
cs := chunks.NewMemoryStore()
pkg := NewPackage([]TypeRef{
MakeStructTypeRef("S", []Field{
@@ -242,18 +255,19 @@ func TestWriteStructWithList(t *testing.T) {
tref := MakeTypeRef(pkgRef, 0)
v := NewMap(NewString("l"), NewList(NewString("a"), NewString("b")))
w := newJsonArrayWriter()
w := newJsonArrayWriter(cs)
w.writeTopLevelValue(testMap{Map: v, t: tref})
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), int16(0), []interface{}{"a", "b"}}, *w)
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), int16(0), []interface{}{"a", "b"}}, w.toArray())
v = NewMap(NewString("l"), NewList())
w = newJsonArrayWriter()
w = newJsonArrayWriter(cs)
w.writeTopLevelValue(testMap{Map: v, t: tref})
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), int16(0), []interface{}{}}, *w)
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), int16(0), []interface{}{}}, w.toArray())
}
func TestWriteStructWithStruct(t *testing.T) {
assert := assert.New(t)
cs := chunks.NewMemoryStore()
pkg := NewPackage([]TypeRef{
MakeStructTypeRef("S2", []Field{
@@ -266,13 +280,14 @@ func TestWriteStructWithStruct(t *testing.T) {
tref := MakeTypeRef(pkgRef, 1)
v := NewMap(NewString("s"), NewMap(NewString("x"), Int32(42)))
w := newJsonArrayWriter()
w := newJsonArrayWriter(cs)
w.writeTopLevelValue(testMap{Map: v, t: tref})
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), int16(1), int32(42)}, *w)
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), int16(1), int32(42)}, w.toArray())
}
func TestWriteStructWithBlob(t *testing.T) {
assert := assert.New(t)
cs := chunks.NewMemoryStore()
pkg := NewPackage([]TypeRef{
MakeStructTypeRef("S", []Field{
@@ -283,9 +298,9 @@ func TestWriteStructWithBlob(t *testing.T) {
b, _ := NewBlob(bytes.NewBuffer([]byte{0x00, 0x01}))
v := NewMap(NewString("b"), b)
w := newJsonArrayWriter()
w := newJsonArrayWriter(cs)
w.writeTopLevelValue(testMap{Map: v, t: tref})
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), int16(0), "AAE="}, *w)
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), int16(0), "AAE="}, w.toArray())
}
type testEnum struct {
@@ -303,19 +318,21 @@ func (e testEnum) InternalImplementation() uint32 {
func TestWriteEnum(t *testing.T) {
assert := assert.New(t)
cs := chunks.NewMemoryStore()
pkg := NewPackage([]TypeRef{
MakeEnumTypeRef("E", "a", "b", "c")}, []ref.Ref{})
pkgRef := RegisterPackage(&pkg)
tref := MakeTypeRef(pkgRef, 0)
w := newJsonArrayWriter()
w := newJsonArrayWriter(cs)
w.writeTopLevelValue(testEnum{UInt32: UInt32(1), t: tref})
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), int16(0), uint32(1)}, *w)
assert.EqualValues([]interface{}{UnresolvedKind, pkgRef.String(), int16(0), uint32(1)}, w.toArray())
}
func TestWriteListOfEnum(t *testing.T) {
assert := assert.New(t)
cs := chunks.NewMemoryStore()
pkg := NewPackage([]TypeRef{
MakeEnumTypeRef("E", "a", "b", "c")}, []ref.Ref{})
@@ -324,13 +341,14 @@ func TestWriteListOfEnum(t *testing.T) {
tref := MakeCompoundTypeRef(ListKind, et)
v := NewList(testEnum{UInt32(0), et}, testEnum{UInt32(1), et}, testEnum{UInt32(2), et})
w := newJsonArrayWriter()
w := newJsonArrayWriter(cs)
w.writeTopLevelValue(testList{List: v, t: tref})
assert.EqualValues([]interface{}{ListKind, UnresolvedKind, pkgRef.String(), int16(0), []interface{}{uint32(0), uint32(1), uint32(2)}}, *w)
assert.EqualValues([]interface{}{ListKind, UnresolvedKind, pkgRef.String(), int16(0), []interface{}{uint32(0), uint32(1), uint32(2)}}, w.toArray())
}
func TestWriteListOfValue(t *testing.T) {
assert := assert.New(t)
cs := chunks.NewMemoryStore()
tref := MakeCompoundTypeRef(ListKind, MakePrimitiveTypeRef(ValueKind))
blob, _ := NewBlob(bytes.NewBuffer([]byte{0x01}))
@@ -350,7 +368,7 @@ func TestWriteListOfValue(t *testing.T) {
blob,
)
w := newJsonArrayWriter()
w := newJsonArrayWriter(cs)
w.writeTopLevelValue(testList{List: v, t: tref})
assert.EqualValues([]interface{}{ListKind, ValueKind, []interface{}{
@@ -367,11 +385,12 @@ func TestWriteListOfValue(t *testing.T) {
Float64Kind, float64(1),
StringKind, "hi",
BlobKind, "AQ==",
}}, *w)
}}, w.toArray())
}
func TestWriteListOfValueWithStruct(t *testing.T) {
assert := assert.New(t)
cs := chunks.NewMemoryStore()
pkg := NewPackage([]TypeRef{
MakeStructTypeRef("S", []Field{
@@ -383,13 +402,14 @@ func TestWriteListOfValueWithStruct(t *testing.T) {
st := MakeTypeRef(pkgRef, 0)
v := NewList(testMap{Map: NewMap(NewString("x"), Int32(42)), t: st})
w := newJsonArrayWriter()
w := newJsonArrayWriter(cs)
w.writeTopLevelValue(testList{List: v, t: tref})
assert.EqualValues([]interface{}{ListKind, ValueKind, []interface{}{UnresolvedKind, pkgRef.String(), int16(0), int32(42)}}, *w)
assert.EqualValues([]interface{}{ListKind, ValueKind, []interface{}{UnresolvedKind, pkgRef.String(), int16(0), int32(42)}}, w.toArray())
}
func TestWriteListOfValueWithTypeRefs(t *testing.T) {
assert := assert.New(t)
cs := chunks.NewMemoryStore()
pkg := NewPackage([]TypeRef{
MakeStructTypeRef("S", []Field{
@@ -405,14 +425,14 @@ func TestWriteListOfValueWithTypeRefs(t *testing.T) {
MakeTypeRef(pkgRef, 0),
)
w := newJsonArrayWriter()
w := newJsonArrayWriter(cs)
w.writeTopLevelValue(testList{List: v, t: tref})
assert.EqualValues([]interface{}{ListKind, ValueKind, []interface{}{
BoolKind, true,
TypeRefKind, Int32Kind,
TypeRefKind, TypeRefKind,
TypeRefKind, UnresolvedKind, pkgRef.String(), int16(0),
}}, *w)
}}, w.toArray())
}
type testRef struct {
@@ -430,23 +450,25 @@ func (r testRef) TargetRef() ref.Ref {
func TestWriteRef(t *testing.T) {
assert := assert.New(t)
cs := chunks.NewMemoryStore()
tref := MakeCompoundTypeRef(RefKind, MakePrimitiveTypeRef(UInt32Kind))
r := ref.Parse("sha1-0123456789abcdef0123456789abcdef01234567")
v := NewRef(r)
w := newJsonArrayWriter()
w := newJsonArrayWriter(cs)
w.writeTopLevelValue(testRef{Value: v, t: tref})
assert.EqualValues([]interface{}{RefKind, UInt32Kind, r.String()}, *w)
assert.EqualValues([]interface{}{RefKind, UInt32Kind, r.String()}, w.toArray())
}
func TestWriteTypeRefValue(t *testing.T) {
assert := assert.New(t)
cs := chunks.NewMemoryStore()
test := func(expected []interface{}, v TypeRef) {
w := newJsonArrayWriter()
w := newJsonArrayWriter(cs)
w.writeTopLevelValue(v)
assert.EqualValues(expected, *w)
assert.EqualValues(expected, w.toArray())
}
test([]interface{}{TypeRefKind, Int32Kind}, MakePrimitiveTypeRef(Int32Kind))
@@ -485,16 +507,18 @@ func TestWriteTypeRefValue(t *testing.T) {
func TestWriteListOfTypeRefs(t *testing.T) {
assert := assert.New(t)
cs := chunks.NewMemoryStore()
tref := MakeCompoundTypeRef(ListKind, MakePrimitiveTypeRef(TypeRefKind))
v := NewList(MakePrimitiveTypeRef(BoolKind), MakeEnumTypeRef("E", "a", "b", "c"), MakePrimitiveTypeRef(StringKind))
w := newJsonArrayWriter()
w := newJsonArrayWriter(cs)
w.writeTopLevelValue(testList{List: v, t: tref})
assert.EqualValues([]interface{}{ListKind, TypeRefKind, []interface{}{BoolKind, EnumKind, "E", []interface{}{"a", "b", "c"}, StringKind}}, *w)
assert.EqualValues([]interface{}{ListKind, TypeRefKind, []interface{}{BoolKind, EnumKind, "E", []interface{}{"a", "b", "c"}, StringKind}}, w.toArray())
}
func TestWritePackage(t *testing.T) {
cs := chunks.NewMemoryStore()
pkg := NewPackage([]TypeRef{
MakeStructTypeRef("EnumStruct",
[]Field{
@@ -505,7 +529,7 @@ func TestWritePackage(t *testing.T) {
MakeEnumTypeRef("Handedness", "right", "left", "switch"),
}, []ref.Ref{})
w := newJsonArrayWriter()
w := newJsonArrayWriter(cs)
w.writeTopLevelValue(pkg)
// struct Package {
@@ -529,12 +553,13 @@ func TestWritePackage(t *testing.T) {
func TestWritePackage2(t *testing.T) {
assert := assert.New(t)
cs := chunks.NewMemoryStore()
setTref := MakeCompoundTypeRef(SetKind, MakePrimitiveTypeRef(UInt32Kind))
r := ref.Parse("sha1-0123456789abcdef0123456789abcdef01234567")
v := Package{[]TypeRef{setTref}, []ref.Ref{r}, &ref.Ref{}}
w := newJsonArrayWriter()
w := newJsonArrayWriter(cs)
w.writeTopLevelValue(v)
assert.EqualValues([]interface{}{PackageKind, []interface{}{SetKind, []interface{}{UInt32Kind}}, []interface{}{r.String()}}, *w)
assert.EqualValues([]interface{}{PackageKind, []interface{}{SetKind, []interface{}{UInt32Kind}}, []interface{}{r.String()}}, w.toArray())
}
-42
View File
@@ -41,16 +41,8 @@ func toEncodeable(v Value, cs chunks.ChunkSink) interface{} {
return v.Reader()
case compoundBlob:
return encCompoundBlobFromCompoundBlob(v, cs)
case List:
processListChildren(v, cs)
case Map:
processMapChildren(v, cs)
case Package:
processPackageChildren(v, cs)
case Set:
processSetChildren(v, cs)
case TypeRef:
processTypeRefChildren(v, cs)
}
return encNomsValue(v, cs)
}
@@ -65,40 +57,6 @@ func encCompoundBlobFromCompoundBlob(cb compoundBlob, cs chunks.ChunkSink) inter
return enc.CompoundBlob{Offsets: cb.offsets, Blobs: refs}
}
func processListChildren(l List, cs chunks.ChunkSink) {
l.IterAll(func(v Value, i uint64) {
writeChildValueInternal(v, cs)
})
}
func processMapChildren(m Map, cs chunks.ChunkSink) {
for _, r := range m.m {
processChild(r.key, cs)
processChild(r.value, cs)
}
}
func processSetChildren(s Set, cs chunks.ChunkSink) {
for _, f := range s.m {
processChild(f, cs)
}
}
func processTypeRefChildren(t TypeRef, cs chunks.ChunkSink) {
if t.HasPackageRef() {
pkgRef := t.PackageRef()
p := LookupPackage(pkgRef)
if p != nil {
writeChildValueInternal(*p, cs)
}
}
if desc, ok := t.Desc.(CompoundDesc); ok {
for _, t := range desc.ElemTypes {
writeChildValueInternal(t, cs)
}
}
}
func processPackageChildren(p Package, cs chunks.ChunkSink) {
for _, t := range p.types {
writeChildValueInternal(t, cs)
+35
View File
@@ -60,3 +60,38 @@ func TestWriteBlobLeaf(t *testing.T) {
// echo -n 'b Hello, World!' | sha1sum
assert.Equal("sha1-135fe1453330547994b2ce8a1b238adfbd7df87e", r2.String())
}
func TestWritePackageWhenValueIsWritten(t *testing.T) {
assert := assert.New(t)
cs := chunks.NewMemoryStore()
pkg1 := NewPackage([]TypeRef{
MakeStructTypeRef("S", []Field{
Field{"X", MakePrimitiveTypeRef(Int32Kind), false},
}, Choices{}),
}, []ref.Ref{})
// Don't write package
pkgRef1 := RegisterPackage(&pkg1)
m := NewMap(NewString("X"), Int32(42))
tref := MakeTypeRef(pkgRef1, 0)
WriteValue(testMap{Map: m, t: tref}, cs)
pkg2 := ReadValue(pkgRef1, cs)
assert.True(pkg1.Equals(pkg2))
}
func TestWritePackageDepWhenPackageIsWritten(t *testing.T) {
assert := assert.New(t)
cs := chunks.NewMemoryStore()
pkg1 := NewPackage([]TypeRef{}, []ref.Ref{})
// Don't write package
pkgRef1 := RegisterPackage(&pkg1)
pkg2 := NewPackage([]TypeRef{}, []ref.Ref{pkgRef1})
WriteValue(pkg2, cs)
pkg3 := ReadValue(pkgRef1, cs)
assert.True(pkg1.Equals(pkg3))
}