Pārlūkot izejas kodu

Allow unicode keys

Kenny Shen 9 gadi atpakaļ
vecāks
revīzija
797efe68fa
2 mainītis faili ar 28 papildinājumiem un 2 dzēšanām
  1. 2 2
      memcache/memcache.go
  2. 26 0
      memcache/memcache_test.go

+ 2 - 2
memcache/memcache.go

@@ -56,7 +56,7 @@ var (
 	ErrNoStats = errors.New("memcache: no statistics available")
 
 	// ErrMalformedKey is returned when an invalid key is used.
-	// Keys must be at maximum 250 bytes long, ASCII, and not
+	// Keys must be at maximum 250 bytes long and not
 	// contain whitespace or control characters.
 	ErrMalformedKey = errors.New("malformed: key is too long or contains invalid characters")
 
@@ -89,7 +89,7 @@ func legalKey(key string) bool {
 		return false
 	}
 	for i := 0; i < len(key); i++ {
-		if key[i] <= ' ' || key[i] > 0x7e {
+		if key[i] <= ' ' || key[i] == 0x7f {
 			return false
 		}
 	}

+ 26 - 0
memcache/memcache_test.go

@@ -104,6 +104,32 @@ func testWithClient(t *testing.T, c *Client) {
 		t.Errorf("get(foo) Flags = %v, want 123", it.Flags)
 	}
 
+	// Get and set a unicode key
+	quxKey := "Hello_世界"
+	qux := &Item{Key: quxKey, Value: []byte("hello world")}
+	err = c.Set(qux)
+	checkErr(err, "first set(Hello_世界): %v", err)
+	it, err = c.Get(quxKey)
+	checkErr(err, "get(Hello_世界): %v", err)
+	if it.Key != quxKey {
+		t.Errorf("get(Hello_世界) Key = %q, want Hello_世界", it.Key)
+	}
+	if string(it.Value) != "hello world" {
+		t.Errorf("get(Hello_世界) Value = %q, want hello world", string(it.Value))
+	}
+
+	// Set malformed keys
+	malFormed := &Item{Key: "foo bar", Value: []byte("foobarval")}
+	err = c.Set(malFormed)
+	if err != ErrMalformedKey {
+		t.Errorf("set(foo bar) should return ErrMalformedKey instead of %v", err)
+	}
+	malFormed = &Item{Key: "foo" + string(0x7f), Value: []byte("foobarval")}
+	err = c.Set(malFormed)
+	if err != ErrMalformedKey {
+		t.Errorf("set(foo<0x7f>) should return ErrMalformedKey instead of %v", err)
+	}
+
 	// Add
 	bar := &Item{Key: "bar", Value: []byte("barval")}
 	err = c.Add(bar)