From cac9d9ff86aaba66cc9ec4910a907144108a1a80 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 25 Mar 2011 18:45:10 -0700 Subject: [PATCH] schema: camli non-utf8 JSON string decoding --- lib/go/camli/schema/schema.go | 5 ++--- lib/go/camli/schema/schema_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/go/camli/schema/schema.go b/lib/go/camli/schema/schema.go index 35d7ccc5f..f5717cd47 100644 --- a/lib/go/camli/schema/schema.go +++ b/lib/go/camli/schema/schema.go @@ -107,11 +107,10 @@ func stringFromMixedArray(parts []interface{}) string { buf.WriteString(s) continue } - if b, ok := part.(byte); ok { - buf.WriteByte(b) + if num, ok := part.(float64); ok { + buf.WriteByte(byte(num)) continue } - // TODO: finish / test (see what types actually come from JSON unmarshalling) } return buf.String() } diff --git a/lib/go/camli/schema/schema_test.go b/lib/go/camli/schema/schema_test.go index 4623064df..041e82769 100644 --- a/lib/go/camli/schema/schema_test.go +++ b/lib/go/camli/schema/schema_test.go @@ -18,6 +18,7 @@ package schema import ( . "camli/test/asserts" + "json" "os" "strings" "testing" @@ -103,4 +104,27 @@ func TestSymlink(t *testing.T) { t.Fatalf("Unexpected error: %v", err) } t.Logf("Got json for symlink file: [%s]\n", json) +} + +type mixPartsTest struct { + json, expected string +} + +func TestStringFromMixedArray(t *testing.T) { + tests := []mixPartsTest{ + {`["brad"]`, "brad"}, + {`["brad", 32, 70]`, "brad F"}, + {`["brad", "fitz"]`, "bradfitz"}, + {`["../foo/Am", 233, "lie.jpg"]`, "../foo/Am\xe9lie.jpg"}, + } + for idx, test := range tests { + var v []interface{} + if err := json.Unmarshal([]byte(test.json), &v); err != nil { + t.Fatalf("invalid JSON in test %d", idx) + } + got := stringFromMixedArray(v) + if got != test.expected { + t.Errorf("test %d got %q; expected %q", idx, got, test.expected) + } + } } \ No newline at end of file