naive slice-encoding functionality

master
Jordan Orelli 10 years ago
parent fdc088b3bc
commit 1a88f25514

@ -66,6 +66,8 @@ func typeEncoder(t reflect.Type) encodeFn {
return encodeString
case reflect.Struct:
return encodeStruct
case reflect.Slice:
return encodeSlice
default:
panic(fmt.Errorf("unhandled type: %v kind: %v", t, t.Kind()))
}
@ -142,3 +144,14 @@ func encodeString(e *encoder, v reflect.Value) {
}
e.WriteByte('"')
}
func encodeSlice(e *encoder, v reflect.Value) {
e.WriteByte('[')
for i := 0; i < v.Len(); i++ {
if i > 0 {
e.WriteByte(' ')
}
e.encodeValue(v.Index(i))
}
e.WriteByte(']')
}

@ -29,6 +29,10 @@ var valueTests = []struct {
{"a string", `"a string"`},
{`it's got "quotes"`, `"it's got \"quotes\""`},
{person{"jordan", 28}, `{Name: "jordan" Age: 28}`},
{[]int{1, 2, 3}, `[1 2 3]`},
{[]float32{1.0, 2.2, 3.3}, `[1.0 2.2 3.3]`},
{[]float64{1.0, 2.2, 3.3}, `[1.0 2.2 3.3]`},
{[]string{"one", "two", "three"}, `["one" "two" "three"]`},
}
func TestWriteValues(t *testing.T) {

Loading…
Cancel
Save