From 1a88f25514f6796680d35ac63a728756111ada51 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Tue, 21 Apr 2015 09:03:35 -0400 Subject: [PATCH] naive slice-encoding functionality --- lib/emit.go | 13 +++++++++++++ lib/emit_test.go | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/lib/emit.go b/lib/emit.go index 6176b75..e9191c8 100644 --- a/lib/emit.go +++ b/lib/emit.go @@ -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(']') +} diff --git a/lib/emit_test.go b/lib/emit_test.go index b006913..261d020 100644 --- a/lib/emit_test.go +++ b/lib/emit_test.go @@ -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) {