Always wrap list, map and set in an array

This commit is contained in:
Erik Arvidsson
2015-10-09 14:07:06 -07:00
parent 37336f41be
commit 9795d49074
4 changed files with 47 additions and 60 deletions
+12 -19
View File
@@ -105,7 +105,7 @@ func (r *jsonArrayReader) readList(t TypeRef, pkg *Package) NomsValue {
ll := []Value{}
elemType := desc.ElemTypes[0]
for !r.atEnd() {
v := r.readTopLevelValueWithoutTag(elemType, pkg)
v := r.readValueWithoutTag(elemType, pkg)
ll = append(ll, v.NomsValue())
}
@@ -117,7 +117,7 @@ func (r *jsonArrayReader) readSet(t TypeRef, pkg *Package) NomsValue {
ll := []Value{}
elemType := desc.ElemTypes[0]
for !r.atEnd() {
v := r.readTopLevelValueWithoutTag(elemType, pkg)
v := r.readValueWithoutTag(elemType, pkg)
ll = append(ll, v.NomsValue())
}
return ToNomsValueFromTypeRef(t, NewSet(ll...))
@@ -129,8 +129,8 @@ func (r *jsonArrayReader) readMap(t TypeRef, pkg *Package) NomsValue {
keyType := desc.ElemTypes[0]
valueType := desc.ElemTypes[1]
for !r.atEnd() {
k := r.readTopLevelValueWithoutTag(keyType, pkg)
v := r.readTopLevelValueWithoutTag(valueType, pkg)
k := r.readValueWithoutTag(keyType, pkg)
v := r.readValueWithoutTag(valueType, pkg)
ll = append(ll, k.NomsValue(), v.NomsValue())
}
return ToNomsValueFromTypeRef(t, NewMap(ll...))
@@ -148,10 +148,10 @@ func (r *jsonArrayReader) readRefValue(t TypeRef) NomsValue {
func (r *jsonArrayReader) readTopLevelValue() NomsValue {
t := r.readTypeRefAsTag()
return r.readTopLevelValueWithoutTag(t, nil)
return r.readValueWithoutTag(t, nil)
}
func (r *jsonArrayReader) readTopLevelValueWithoutTag(t TypeRef, pkg *Package) NomsValue {
func (r *jsonArrayReader) readValueWithoutTag(t TypeRef, pkg *Package) NomsValue {
switch t.Kind() {
case BoolKind:
return valueAsNomsValue{Bool(r.read().(bool)), t}
@@ -184,13 +184,16 @@ func (r *jsonArrayReader) readTopLevelValueWithoutTag(t TypeRef, pkg *Package) N
t := r.readTypeRefAsTag()
return r.readValueWithoutTag(t, pkg)
case ListKind:
return r.readList(t, pkg)
r2 := newJsonArrayReader(r.readArray(), r.cs)
return r2.readList(t, pkg)
case MapKind:
return r.readMap(t, pkg)
r2 := newJsonArrayReader(r.readArray(), r.cs)
return r2.readMap(t, pkg)
case RefKind:
return r.readRefValue(t)
case SetKind:
return r.readSet(t, pkg)
r2 := newJsonArrayReader(r.readArray(), r.cs)
return r2.readSet(t, pkg)
case EnumKind, StructKind:
panic("not allowed")
case TypeRefKind:
@@ -199,16 +202,6 @@ func (r *jsonArrayReader) readTopLevelValueWithoutTag(t TypeRef, pkg *Package) N
panic("not reachable")
}
func (r *jsonArrayReader) readValueWithoutTag(t TypeRef, pkg *Package) NomsValue {
switch t.Kind() {
case ListKind, MapKind, SetKind:
r2 := newJsonArrayReader(r.readArray(), r.cs)
return r2.readTopLevelValueWithoutTag(t, pkg)
}
return r.readTopLevelValueWithoutTag(t, pkg)
}
func (r *jsonArrayReader) readTypeRefKindToValue(t TypeRef, pkg *Package) NomsValue {
if _, ok := t.Desc.(PrimitiveDesc); ok {
return valueAsNomsValue{r.readTypeRefAsValue(pkg), t}
+4 -4
View File
@@ -58,7 +58,7 @@ func TestReadListOfInt32(t *testing.T) {
assert := assert.New(t)
cs := chunks.NewMemoryStore()
a := parseJson("[%d, %d, 0, 1, 2, 3]", ListKind, Int32Kind)
a := parseJson("[%d, %d, [0, 1, 2, 3]]", ListKind, Int32Kind)
r := newJsonArrayReader(a, cs)
tr := MakeCompoundTypeRef("", ListKind, MakePrimitiveTypeRef(Int32Kind))
@@ -74,7 +74,7 @@ func TestReadListOfValue(t *testing.T) {
assert := assert.New(t)
cs := chunks.NewMemoryStore()
a := parseJson(`[%d, %d, %d, 1, %d, "hi", %d, true]`, ListKind, ValueKind, Int32Kind, StringKind, BoolKind)
a := parseJson(`[%d, %d, [%d, 1, %d, "hi", %d, true]]`, ListKind, ValueKind, Int32Kind, StringKind, BoolKind)
r := newJsonArrayReader(a, cs)
listTr := MakeCompoundTypeRef("", ListKind, MakePrimitiveTypeRef(ValueKind))
@@ -106,7 +106,7 @@ func TestReadMapOfInt64ToFloat64(t *testing.T) {
assert := assert.New(t)
cs := chunks.NewMemoryStore()
a := parseJson("[%d, %d, %d, 0, 1, 2, 3]", MapKind, Int64Kind, Float64Kind)
a := parseJson("[%d, %d, %d, [0, 1, 2, 3]]", MapKind, Int64Kind, Float64Kind)
r := newJsonArrayReader(a, cs)
tr := MakeCompoundTypeRef("", MapKind, MakePrimitiveTypeRef(Int64Kind), MakePrimitiveTypeRef(Float64Kind))
@@ -138,7 +138,7 @@ func TestReadSetOfUInt8(t *testing.T) {
assert := assert.New(t)
cs := chunks.NewMemoryStore()
a := parseJson("[%d, %d, 0, 1, 2, 3]", SetKind, UInt8Kind)
a := parseJson("[%d, %d, [0, 1, 2, 3]]", SetKind, UInt8Kind)
r := newJsonArrayReader(a, cs)
tr := MakeCompoundTypeRef("", SetKind, MakePrimitiveTypeRef(UInt8Kind))
+17 -24
View File
@@ -67,59 +67,52 @@ func (w *jsonArrayWriter) writeTypeRefAsTag(t TypeRef) {
func (w *jsonArrayWriter) writeTopLevelValue(v NomsValue) {
tr := v.TypeRef()
w.writeTypeRefAsTag(tr)
w.writeValueWithoutTag(v.NomsValue(), tr, nil)
w.writeValue(v.NomsValue(), tr, nil)
}
func (w *jsonArrayWriter) writeValueWithoutTag(v Value, tr TypeRef, pkg *Package) {
func (w *jsonArrayWriter) writeValue(v Value, tr TypeRef, pkg *Package) {
switch tr.Kind() {
case BlobKind:
panic("not yet implemented")
case BoolKind, Float32Kind, Float64Kind, Int16Kind, Int32Kind, Int64Kind, Int8Kind, UInt16Kind, UInt32Kind, UInt64Kind, UInt8Kind:
w.write(v.(primitive).ToPrimitive())
case ListKind:
w2 := newJsonArrayWriter()
elemType := tr.Desc.(CompoundDesc).ElemTypes[0]
v.(List).IterAll(func(v Value, i uint64) {
w.writeValue(v, elemType, pkg)
w2.writeValue(v, elemType, pkg)
})
w.write(w2.toArray())
case MapKind:
w2 := newJsonArrayWriter()
elemTypes := tr.Desc.(CompoundDesc).ElemTypes
v.(Map).IterAll(func(k, v Value) {
w.writeValue(k, elemTypes[0], pkg)
w.writeValue(v, elemTypes[1], pkg)
w2.writeValue(k, elemTypes[0], pkg)
w2.writeValue(v, elemTypes[1], pkg)
})
w.write(w2.toArray())
case RefKind:
w.writeRef(v.Ref())
case SetKind:
w2 := newJsonArrayWriter()
elemType := tr.Desc.(CompoundDesc).ElemTypes[0]
v.(Set).IterAll(func(v Value) {
w.writeValue(v, elemType, pkg)
w2.writeValue(v, elemType, pkg)
})
w.write(w2.toArray())
case StringKind:
w.write(v.(String).String())
case TypeRefKind:
pkg := LookupPackage(tr.PackageRef())
pkgRef := tr.PackageRef()
if pkgRef != (ref.Ref{}) {
pkg = LookupPackage(tr.PackageRef())
}
w.writeTypeRefKindValue(v, tr, pkg)
case ValueKind:
d.Chk.Fail("Should be handled by callers")
default:
d.Chk.Fail("Unknown NomsKind")
}
}
func (w *jsonArrayWriter) writeValue(v Value, t TypeRef, pkg *Package) {
k := t.Kind()
switch k {
case ListKind, MapKind, SetKind:
w2 := newJsonArrayWriter()
w2.writeValueWithoutTag(v, t, pkg)
w.write(w2.toArray())
case TypeRefKind:
w.writeTypeRefKindValue(v, t, pkg)
case ValueKind:
w.writeTypeRefAsTag(v.TypeRef())
w.writeValue(v, v.TypeRef(), pkg)
default:
w.writeValueWithoutTag(v, t, pkg)
d.Chk.Fail("Unknown NomsKind")
}
}
+14 -13
View File
@@ -42,7 +42,7 @@ func TestWriteList(t *testing.T) {
w := newJsonArrayWriter()
w.writeTopLevelValue(valueAsNomsValue{Value: v, t: tref})
assert.EqualValues([]interface{}{ListKind, Int32Kind, int32(0), int32(1), int32(2), int32(3)}, *w)
assert.EqualValues([]interface{}{ListKind, Int32Kind, []interface{}{int32(0), int32(1), int32(2), int32(3)}}, *w)
}
func TestWriteListOfList(t *testing.T) {
@@ -54,7 +54,8 @@ func TestWriteListOfList(t *testing.T) {
w := newJsonArrayWriter()
w.writeTopLevelValue(valueAsNomsValue{Value: v, t: tref})
assert.EqualValues([]interface{}{ListKind, ListKind, Int16Kind, []interface{}{int16(0)}, []interface{}{int16(1), int16(2), int16(3)}}, *w)
assert.EqualValues([]interface{}{ListKind, ListKind, Int16Kind,
[]interface{}{[]interface{}{int16(0)}, []interface{}{int16(1), int16(2), int16(3)}}}, *w)
}
func TestWriteSet(t *testing.T) {
@@ -66,7 +67,7 @@ func TestWriteSet(t *testing.T) {
w := newJsonArrayWriter()
w.writeTopLevelValue(valueAsNomsValue{Value: v, t: tref})
// the order of the elements is based on the ref of the value.
assert.EqualValues([]interface{}{SetKind, UInt32Kind, uint32(3), uint32(1), uint32(0), uint32(2)}, *w)
assert.EqualValues([]interface{}{SetKind, UInt32Kind, []interface{}{uint32(3), uint32(1), uint32(0), uint32(2)}}, *w)
}
func TestWriteSetOfSet(t *testing.T) {
@@ -79,7 +80,7 @@ func TestWriteSetOfSet(t *testing.T) {
w := newJsonArrayWriter()
w.writeTopLevelValue(valueAsNomsValue{Value: v, t: tref})
// the order of the elements is based on the ref of the value.
assert.EqualValues([]interface{}{SetKind, SetKind, Int32Kind, []interface{}{int32(0)}, []interface{}{int32(1), int32(3), int32(2)}}, *w)
assert.EqualValues([]interface{}{SetKind, SetKind, Int32Kind, []interface{}{[]interface{}{int32(0)}, []interface{}{int32(1), int32(3), int32(2)}}}, *w)
}
func TestWriteMap(t *testing.T) {
@@ -91,7 +92,7 @@ func TestWriteMap(t *testing.T) {
w := newJsonArrayWriter()
w.writeTopLevelValue(valueAsNomsValue{Value: v, t: tref})
// the order of the elements is based on the ref of the value.
assert.EqualValues([]interface{}{MapKind, StringKind, BoolKind, "a", false, "b", true}, *w)
assert.EqualValues([]interface{}{MapKind, StringKind, BoolKind, []interface{}{"a", false, "b", true}}, *w)
}
func TestWriteMapOfMap(t *testing.T) {
@@ -105,7 +106,7 @@ func TestWriteMapOfMap(t *testing.T) {
w := newJsonArrayWriter()
w.writeTopLevelValue(valueAsNomsValue{Value: 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{}{"a", int64(0)}, []interface{}{true}}, *w)
assert.EqualValues([]interface{}{MapKind, MapKind, StringKind, Int64Kind, SetKind, BoolKind, []interface{}{[]interface{}{"a", int64(0)}, []interface{}{true}}}, *w)
}
func TestWriteEmptyStruct(t *testing.T) {
@@ -253,7 +254,7 @@ func TestWriteListOfEnum(t *testing.T) {
w := newJsonArrayWriter()
w.writeTopLevelValue(valueAsNomsValue{Value: v, t: tref})
assert.EqualValues([]interface{}{ListKind, TypeRefKind, pkgRef.String(), "E", uint32(0), uint32(1), uint32(2)}, *w)
assert.EqualValues([]interface{}{ListKind, TypeRefKind, pkgRef.String(), "E", []interface{}{uint32(0), uint32(1), uint32(2)}}, *w)
}
func TestWriteListOfValue(t *testing.T) {
@@ -278,7 +279,7 @@ func TestWriteListOfValue(t *testing.T) {
w := newJsonArrayWriter()
w.writeTopLevelValue(valueAsNomsValue{Value: v, t: tref})
assert.EqualValues([]interface{}{ListKind, ValueKind,
assert.EqualValues([]interface{}{ListKind, ValueKind, []interface{}{
BoolKind, true,
UInt8Kind, uint8(1),
UInt16Kind, uint16(1),
@@ -291,7 +292,7 @@ func TestWriteListOfValue(t *testing.T) {
Float32Kind, float32(1),
Float64Kind, float64(1),
StringKind, "hi",
}, *w)
}}, *w)
}
func TestWriteListOfValueWithStruct(t *testing.T) {
@@ -309,7 +310,7 @@ func TestWriteListOfValueWithStruct(t *testing.T) {
w := newJsonArrayWriter()
w.writeTopLevelValue(valueAsNomsValue{Value: v, t: tref})
assert.EqualValues([]interface{}{ListKind, ValueKind, TypeRefKind, pkgRef.String(), "S", int32(42)}, *w)
assert.EqualValues([]interface{}{ListKind, ValueKind, []interface{}{TypeRefKind, pkgRef.String(), "S", int32(42)}}, *w)
}
func TestWriteListOfValueWithTypeRefs(t *testing.T) {
@@ -331,12 +332,12 @@ func TestWriteListOfValueWithTypeRefs(t *testing.T) {
w := newJsonArrayWriter()
w.writeTopLevelValue(valueAsNomsValue{Value: v, t: tref})
assert.EqualValues([]interface{}{ListKind, ValueKind,
assert.EqualValues([]interface{}{ListKind, ValueKind, []interface{}{
BoolKind, true,
TypeRefKind, Int32Kind,
TypeRefKind, TypeRefKind,
TypeRefKind, TypeRefKind, pkgRef.String(), "S",
}, *w)
}}, *w)
}
func TestWriteRef(t *testing.T) {
@@ -400,7 +401,7 @@ func TestWriteListOfTypeRefs(t *testing.T) {
w := newJsonArrayWriter()
w.writeTopLevelValue(valueAsNomsValue{Value: v, t: tref})
assert.EqualValues([]interface{}{ListKind, TypeRefKind, BoolKind, EnumKind, "E", []interface{}{"a", "b", "c"}, StringKind}, *w)
assert.EqualValues([]interface{}{ListKind, TypeRefKind, []interface{}{BoolKind, EnumKind, "E", []interface{}{"a", "b", "c"}, StringKind}}, *w)
}
func TestWritePackage(t *testing.T) {